Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: move database schema caching from store to LSP layer (#18588)
Danny Xu1cae940b203d
diff --git a/backend/api/lsp/completion.go b/backend/api/lsp/completion.goindex 60551c87a9..9819fc7360 100644--- a/backend/api/lsp/completion.go+++ b/backend/api/lsp/completion.go@@ -145,6 +145,13 @@ func convertLSPCompletionItemKind(tp parserbase.CandidateType) lsp.CompletionIte}func (h *Handler) GetDatabaseMetadataFunc(ctx context.Context, instanceID, databaseName string) (string, *model.DatabaseMetadata, error) {+ // Check cache first+ cacheKey := getDatabaseMetadataCacheKey(instanceID, databaseName)+ if cached, exists := h.metadataCache.Get(cacheKey); exists {+ return databaseName, cached, nil+ }++ // Cache miss, fetch from storemetadata, err := h.store.GetDBSchema(ctx, &store.FindDBSchemaMessage{InstanceID: instanceID,DatabaseName: databaseName,@@ -155,9 +162,17 @@ func (h *Handler) GetDatabaseMetadataFunc(ctx context.Context, instanceID, databif metadata == nil {return "", nil, errors.Errorf("database %s schema for instance %s not found", databaseName, instanceID)}++ // Store in cache+ h.metadataCache.Add(cacheKey, metadata)+return databaseName, metadata, nil}+func getDatabaseMetadataCacheKey(instanceID, databaseName string) string {+ return instanceID + "/" + databaseName+}+func (h *Handler) ListDatabaseNamesFunc(ctx context.Context, instanceID string) ([]string, error) {if instanceID == "" {return nil, errors.Errorf("instance is not specified")diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.goindex 59f8087050..086ccfedec 100644--- a/backend/api/lsp/handler.go+++ b/backend/api/lsp/handler.go@@ -9,6 +9,7 @@ import ("time"lsp "github.com/bytebase/lsp-protocol"+ "github.com/hashicorp/golang-lru/v2/expirable""github.com/pkg/errors""github.com/sourcegraph/jsonrpc2"@@ -19,6 +20,7 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/parser/base""github.com/bytebase/bytebase/backend/store"+ "github.com/bytebase/bytebase/backend/store/model")type Method string@@ -55,8 +57,9 @@ func NewHandlerWithAuth(s *store.Store, profile *config.Profile, iamManager *iamuser: user,tokenExpiry: tokenExpiry,iamManager: iamManager,- diagnosticsDebouncer: NewDiagnosticsDebouncer(500 * time.Millisecond), // 500ms debounce- contentCache: NewContentCache(100), // Cache up to 100 documents+ diagnosticsDebouncer: NewDiagnosticsDebouncer(500 * time.Millisecond), // 500ms debounce+ contentCache: NewContentCache(100), // Cache up to 100 documents+ metadataCache: expirable.NewLRU[string, *model.DatabaseMetadata](128, nil, 5*time.Minute), // Cache up to 128 database metadata with 5min TTL}return lspHandler{Handler: jsonrpc2.HandlerWithError(handler.handle)}}@@ -93,6 +96,7 @@ type Handler struct {// Performance optimizationsdiagnosticsDebouncer *DiagnosticsDebouncercontentCache *ContentCache+ metadataCache *expirable.LRU[string, *model.DatabaseMetadata]}// ShutDown shuts down the handler.diff --git a/backend/store/db_schema.go b/backend/store/db_schema.goindex 44ba0a9424..8577ad7e7e 100644--- a/backend/store/db_schema.go+++ b/backend/store/db_schema.go@@ -25,10 +25,6 @@ type FindDBSchemaMessage struct {// GetDBSchema gets the schema for a database.func (s *Store) GetDBSchema(ctx context.Context, find *FindDBSchemaMessage) (*model.DatabaseMetadata, error) {- if v, ok := s.dbMetadataCache.Get(getDatabaseCacheKey(find.InstanceID, find.DatabaseName)); ok && s.enableCache {- return v, nil- }-q := qb.Q().Space(`SELECTmetadata,@@ -68,7 +64,6 @@ func (s *Store) GetDBSchema(ctx context.Context, find *FindDBSchemaMessage) (*moreturn nil, err}- s.dbMetadataCache.Add(getDatabaseCacheKey(find.InstanceID, find.DatabaseName), dbMetadata)return dbMetadata, nil}@@ -130,16 +125,8 @@ func (s *Store) UpsertDBSchema(); err != nil {return err}- if err := tx.Commit(); err != nil {- return err- }- updatedDBSchema, err := s.convertMetadataAndConfig(ctx, metadata, schema, config, instanceID)- if err != nil {- return err- }- s.dbMetadataCache.Add(getDatabaseCacheKey(instanceID, databaseName), updatedDBSchema)- return nil+ return tx.Commit()}// UpdateDBSchema updates a database schema.@@ -171,12 +158,8 @@ func (s *Store) UpdateDBSchema(ctx context.Context, instanceID, databaseName strif _, err := tx.ExecContext(ctx, query, args...); err != nil {return err}- if err := tx.Commit(); err != nil {- return err- }- // Invalid the cache and read the value again.- s.dbMetadataCache.Remove(getDatabaseCacheKey(instanceID, databaseName))- return nil++ return tx.Commit()}func (s *Store) convertMetadataAndConfig(ctx context.Context, metadata, schema, config []byte, instanceID string) (*model.DatabaseMetadata, error) {diff --git a/backend/store/store.go b/backend/store/store.goindex 13ee889e51..853252749c 100644--- a/backend/store/store.go+++ b/backend/store/store.go@@ -9,7 +9,6 @@ import (lru "github.com/hashicorp/golang-lru/v2"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store/model")// Store provides database access to all raw objects.@@ -29,8 +28,7 @@ type Store struct {groupCache *lru.Cache[string, *GroupMessage]// Large objects.- sheetFullCache *lru.Cache[string, *SheetMessage]- dbMetadataCache *lru.Cache[string, *model.DatabaseMetadata]+ sheetFullCache *lru.Cache[string, *SheetMessage]}// New creates a new instance of Store.@@ -68,10 +66,6 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {if err != nil {return nil, err}- dbMetadataCache, err := lru.New[string, *model.DatabaseMetadata](128)- if err != nil {- return nil, err- }groupCache, err := lru.New[string, *GroupMessage](1024)if err != nil {return nil, err@@ -88,16 +82,15 @@ func New(ctx context.Context, pgURL string, enableCache bool) (*Store, error) {enableCache: enableCache,// Cache.- userEmailCache: userEmailCache,- instanceCache: instanceCache,- databaseCache: databaseCache,- projectCache: projectCache,- policyCache: policyCache,- settingCache: settingCache,- rolesCache: rolesCache,- sheetFullCache: sheetFullCache,- dbMetadataCache: dbMetadataCache,- groupCache: groupCache,+ userEmailCache: userEmailCache,+ instanceCache: instanceCache,+ databaseCache: databaseCache,+ projectCache: projectCache,+ policyCache: policyCache,+ settingCache: settingCache,+ rolesCache: rolesCache,+ sheetFullCache: sheetFullCache,+ groupCache: groupCache,}return s, nil