Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: cache sheet (#13189)

Danny Xu56318490bd26
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index ee7058a626..e1c3fed1aa 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1566,15 +1566,12 @@ func (s *SQLService) GenerateRestoreSQL(ctx context.Context, request *v1pb.Gener
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to get sheet UID: %v", err)
}
- sheet, err := s.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID, LoadFull: true})
+ statement, err := s.store.GetSheetStatementByID(ctx, sheetUID)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get sheet: %v", err)
}
- if sheet == nil {
- return nil, status.Errorf(codes.NotFound, "sheet %q not found", sheetUID)
- }
- list, err := base.SplitMultiSQL(storepb.Engine_MYSQL, sheet.Statement)
+ list, err := base.SplitMultiSQL(storepb.Engine_MYSQL, statement)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "failed to split SQL: %v", err)
}
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 3dbd454c90..604d74376d 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -84,7 +84,10 @@ func (e *StatementAdviseExecutor) Run(ctx context.Context, config *storepb.PlanC
},
}, nil
}
- statement := sheet.Statement
+ statement, err := e.store.GetSheetStatementByID(ctx, sheetUID)
+ if err != nil {
+ return nil, err
+ }
changeType := config.ChangeDatabaseType
preUpdateBackupDetail := config.PreUpdateBackupDetail
diff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.go
index 3c86a987fb..1bb3358195 100644
--- a/backend/runner/plancheck/statement_report_executor.go
+++ b/backend/runner/plancheck/statement_report_executor.go
@@ -62,7 +62,10 @@ func (e *StatementReportExecutor) Run(ctx context.Context, config *storepb.PlanC
},
}, nil
}
- statement := sheet.Statement
+ statement, err := e.store.GetSheetStatementByID(ctx, sheetUID)
+ if err != nil {
+ return nil, err
+ }
isDML := config.ChangeDatabaseType == storepb.PlanCheckRunConfig_DML
instanceUID := int(config.InstanceUid)
diff --git a/backend/store/sheet.go b/backend/store/sheet.go
index 1c82607af1..d8fe12c4d7 100644
--- a/backend/store/sheet.go
+++ b/backend/store/sheet.go
@@ -65,7 +65,7 @@ type PatchSheetMessage struct {
// GetSheetStatementByID gets the statement of a sheet by ID.
func (s *Store) GetSheetStatementByID(ctx context.Context, id int) (string, error) {
- if v, ok := s.sheetCache.Get(id); ok {
+ if v, ok := s.sheetStatementCache.Get(id); ok {
return v, nil
}
@@ -78,13 +78,19 @@ func (s *Store) GetSheetStatementByID(ctx context.Context, id int) (string, erro
}
statement := sheet.Statement
- s.sheetCache.Add(id, statement)
+ s.sheetStatementCache.Add(id, statement)
return statement, nil
}
// GetSheet gets a sheet.
func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage) (*SheetMessage, error) {
- // TODO(d): cache sheet.
+ shouldCache := !find.LoadFull && find.UID != nil
+ if shouldCache {
+ if v, ok := s.sheetCache.Get(*find.UID); ok {
+ return v, nil
+ }
+ }
+
sheets, err := s.listSheets(ctx, find)
if err != nil {
return nil, err
@@ -97,6 +103,10 @@ func (s *Store) GetSheet(ctx context.Context, find *FindSheetMessage) (*SheetMes
}
sheet := sheets[0]
+ if shouldCache {
+ s.sheetCache.Add(sheet.UID, sheet)
+ }
+
return sheet, nil
}
@@ -270,8 +280,10 @@ func (s *Store) PatchSheet(ctx context.Context, patch *PatchSheetMessage) (*Shee
return nil, errors.Wrapf(err, "failed to commit transaction")
}
if v := patch.Statement; v != nil {
- s.sheetCache.Add(patch.UID, *v)
+ s.sheetStatementCache.Add(patch.UID, *v)
}
+
+ s.sheetCache.Remove(patch.UID)
return sheet, nil
}
diff --git a/backend/store/store.go b/backend/store/store.go
index a3a9d5f53b..0e67d3764a 100644
--- a/backend/store/store.go
+++ b/backend/store/store.go
@@ -41,10 +41,11 @@ type Store struct {
vcsIDCache *lru.Cache[int, *VCSProviderMessage]
rolesCache *lru.Cache[string, *RoleMessage]
groupCache *lru.Cache[string, *GroupMessage]
+ sheetCache *lru.Cache[int, *SheetMessage]
// Large objects.
- sheetCache *lru.Cache[int, string]
- dbSchemaCache *lru.Cache[int, *model.DBSchema]
+ sheetStatementCache *lru.Cache[int, string]
+ dbSchemaCache *lru.Cache[int, *model.DBSchema]
}
// New creates a new instance of Store.
@@ -137,7 +138,11 @@ func New(db *DB, profile *config.Profile) (*Store, error) {
if err != nil {
return nil, err
}
- sheetCache, err := lru.New[int, string](10)
+ sheetCache, err := lru.New[int, *SheetMessage](64)
+ if err != nil {
+ return nil, err
+ }
+ sheetStatementCache, err := lru.New[int, string](10)
if err != nil {
return nil, err
}
@@ -178,6 +183,7 @@ func New(db *DB, profile *config.Profile) (*Store, error) {
vcsIDCache: vcsIDCache,
rolesCache: rolesCache,
sheetCache: sheetCache,
+ sheetStatementCache: sheetStatementCache,
dbSchemaCache: dbSchemaCache,
groupCache: groupCache,
}, nil