diff --git a/control.go b/control.go index bc610e4ac..f2717c0e6 100644 --- a/control.go +++ b/control.go @@ -363,6 +363,11 @@ func (c *controlConn) reconnect() { if err != nil { c.session.logger.Printf("gocql: unable to refresh ring: %v\n", err) } + + err = c.session.metadataDescriber.refreshAllSchema() + if err != nil { + c.session.logger.Printf("gocql: unable to refresh the schema: %v\n", err) + } } func (c *controlConn) attemptReconnect() (*Conn, error) { diff --git a/metadata_scylla.go b/metadata_scylla.go index 13b5e60e3..d824e3f7a 100644 --- a/metadata_scylla.go +++ b/metadata_scylla.go @@ -374,6 +374,56 @@ func (s *metadataDescriber) clearSchema(keyspaceName string) { s.metadata.keyspaceMetadata.remove(keyspaceName) } +func (s *metadataDescriber) refreshAllSchema() error { + s.mu.Lock() + defer s.mu.Unlock() + + copiedMap := make(map[string]*KeyspaceMetadata) + + for key, value := range s.metadata.keyspaceMetadata.get() { + if value != nil { + copiedMap[key] = &KeyspaceMetadata{ + Name: value.Name, + DurableWrites: value.DurableWrites, + StrategyClass: value.StrategyClass, + StrategyOptions: value.StrategyOptions, + Tables: value.Tables, + Functions: value.Functions, + Aggregates: value.Aggregates, + Types: value.Types, + Indexes: value.Indexes, + Views: value.Views, + CreateStmts: value.CreateStmts, + } + } else { + copiedMap[key] = nil + } + } + + for keyspaceName, metadata := range copiedMap { + // refresh the cache for this keyspace + err := s.refreshSchema(keyspaceName) + if err == ErrKeyspaceDoesNotExist { + s.clearSchema(keyspaceName) + s.removeTabletsWithKeyspace(keyspaceName) + } else if err != nil { + return err + } + + updatedMetadata, err := s.getSchema(keyspaceName) + if err != nil { + return err + } + + for tableName := range metadata.Tables { + if _, ok := updatedMetadata.Tables[tableName]; !ok { + s.removeTabletsWithTable(keyspaceName, tableName) + } + } + } + return nil +} + // forcibly updates the current KeyspaceMetadata held by the schema describer // for a given named keyspace. func (s *metadataDescriber) refreshSchema(keyspaceName string) error {