joao-r-reis commented on code in PR #1926:
URL: 
https://github.com/apache/cassandra-gocql-driver/pull/1926#discussion_r2828300966


##########
metadata.go:
##########
@@ -327,11 +344,99 @@ func (s *schemaDescriber) refreshSchema(keyspaceName 
string) error {
                materializedViews)
 
        // update the cache
-       s.cache[keyspaceName] = keyspace
+       s.mu.Lock()
+       defer s.mu.Unlock()
+       meta := s.getSchemaMetaForUpdate()
+       newKeyspaceMeta := make(map[string]*KeyspaceMetadata)
+       for ks, meta := range meta.keyspaceMeta {
+               newKeyspaceMeta[ks] = meta
+       }
+       newKeyspaceMeta[keyspaceName] = keyspace
+
+       meta.keyspaceMeta = newKeyspaceMeta
+       s.schemaMeta.Store(meta)
+
+       return nil
+}
+
+// forcibly updates the current KeyspaceMetadata held by the schema describer
+// for all the keyspaces.
+// This function is called via schemaRefresher refreshDebouncer to batch and
+// debounce schema refresh requests.
+func refreshSchemas(session *Session) error {
+       var err error
+
+       // query the system keyspace for schema data
+       keyspaces, err := getAllKeyspaceMetadata(session)
+       if err != nil {
+               return err
+       }
+       tables, err := getAllTablesMetadata(session)
+       if err != nil {
+               return err
+       }
+       columns, err := getAllColumnMetadata(session)
+       if err != nil {
+               return err
+       }
+       functions, err := getAllFunctionsMetadata(session)
+       if err != nil {
+               return err
+       }
+       aggregates, err := getAllAggregatesMetadata(session)
+       if err != nil {
+               return err
+       }
+       userTypes, err := getAllUserTypeMetadata(session)
+       if err != nil {
+               return err
+       }
+       materializedViews, err := getAllMaterializedViewsMetadata(session)
+       if err != nil {
+               return err
+       }
+
+       // organize the schema data
+       keyspaceMeta := map[string]*KeyspaceMetadata{}
+       for keyspaceName, keyspace := range keyspaces {
+               compileMetadata(session,
+                       keyspace,
+                       tables[keyspaceName],
+                       columns[keyspaceName],
+                       functions[keyspaceName],
+                       aggregates[keyspaceName],
+                       userTypes[keyspaceName],
+                       materializedViews[keyspaceName])
+               // update the cache
+               keyspaceMeta[keyspaceName] = keyspace
+       }
+       sd := session.schemaDescriber
+       sd.mu.Lock()
+       meta := sd.getSchemaMetaForUpdate()
+       meta.keyspaceMeta = keyspaceMeta
+       sd.schemaMeta.Store(meta)
+       sd.mu.Unlock()
+       // Notify policy if it supports schema refresh notifications
+       if notifier, ok := session.policy.(SchemaRefreshNotifier); ok {
+               notifier.SchemaRefreshed(sd.getSchemaMetaForRead())
+       }

Review Comment:
   we should change this to something like:
   
   ```
   // rough code, just typing it now
   notifier, notifierExists := session.policy.(SchemaRefreshNotifier)
   keyspacesChanged := false
   for keyspaceName, keyspaceMetadata := range newKeyspaceMetadata {
       var changeType string
       oldKeyspaceMetadata, exists := oldKeyspaces[keyspaceName]
       if !exists {
             changeType = "CREATED"
             keyspacesChanged = true
       } else {
              // compare keyspace replication
              // if keyspace replication is different then changeType = 
"UPDATED" and keyspacesChanged = true
       }
   
       if !notifierExists && changeType != "" {
           session.policy.KeyspaceChanged(KeyspaceUpdateEvent{Keyspace: 
keyspaceName, Change:changeType}
       }
   }
   
   // also need to do the same process for keyspaces that were dropped with 
changetype "DROPPED", ideally without having to loop the entire old keyspace 
keyset
   
   // in the end call notifier
   if keyspacesChanged && notifierExists {
       notifier.SchemaRefreshed(sd.getSchemaMetaForRead())
   }
   ```
   
   With this process we can remove `func (s *Session) 
handleKeyspaceChange(keyspace, change string)` completely.
   
   I'd probably implement the interface on the other policies that the driver 
has so we can avoid the unecessary `session.policy.KeyspaceChanged()` calls 
(empty implementations basically)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to