Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: tickle channel once for batch sync instances (#9024)
Danny Xue431690df342
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.goindex 25507718fa..b9dbe6512d 100644--- a/backend/api/v1/instance_service.go+++ b/backend/api/v1/instance_service.go@@ -155,7 +155,8 @@ func (s *InstanceService) CreateInstance(ctx context.Context, request *v1pb.Crealog.BBError(err))}// Sync all databases in the instance asynchronously.- s.stateCfg.InstanceDatabaseSyncChan <- instance+ s.stateCfg.InstanceSyncs.Store(instance.UID, instance)+ s.stateCfg.InstanceSyncTickleChan <- 0}s.metricReporter.Report(ctx, &metric.Metric{@@ -270,7 +271,8 @@ func (s *InstanceService) UpdateInstance(ctx context.Context, request *v1pb.Updalog.BBError(err))}// Sync all databases in the instance asynchronously.- s.stateCfg.InstanceDatabaseSyncChan <- instance+ s.stateCfg.InstanceSyncs.Store(instance.UID, instance)+ s.stateCfg.InstanceSyncTickleChan <- 0return convertToInstance(ins), nil}@@ -528,7 +530,8 @@ func (s *InstanceService) SyncInstance(ctx context.Context, request *v1pb.SyncInreturn nil, err}// Sync all databases in the instance asynchronously.- s.stateCfg.InstanceDatabaseSyncChan <- instance+ s.stateCfg.InstanceSyncs.Store(instance.UID, instance)+ s.stateCfg.InstanceSyncTickleChan <- 0return &v1pb.SyncInstanceResponse{}, nil}@@ -543,13 +546,10 @@ func (s *InstanceService) BatchSyncInstance(ctx context.Context, request *v1pb.Bif instance.Deleted {return nil, status.Errorf(codes.NotFound, "instance %q has been deleted", r.Name)}-- if err := s.schemaSyncer.SyncInstance(ctx, instance); err != nil {- return nil, err- }// Sync all databases in the instance asynchronously.- s.stateCfg.InstanceDatabaseSyncChan <- instance+ s.stateCfg.InstanceSyncs.Store(instance.UID, instance)}+ s.stateCfg.InstanceSyncTickleChan <- 0return &v1pb.BatchSyncInstanceResponse{}, nil}diff --git a/backend/component/state/state.go b/backend/component/state/state.goindex cbb573d9ab..477d967ee0 100644--- a/backend/component/state/state.go+++ b/backend/component/state/state.go@@ -9,8 +9,6 @@ import ("github.com/dgraph-io/ristretto""github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/store")// InstanceMaximumConnectionNumber is the maximum number of connections outstanding per instance.@@ -19,7 +17,7 @@ const InstanceMaximumConnectionNumber = 10// State is the state for all in-memory states within the server.type State struct {// InstanceDatabaseSyncChan is the channel for synchronizing schemas for instances.- InstanceDatabaseSyncChan chan *store.InstanceMessage+ InstanceSyncs sync.Map // map[instance.ID]*store.InstanceMessage// InstanceSlowQuerySyncChan is the channel for synchronizing slow query logs for instances.InstanceSlowQuerySyncChan chan *InstanceSlowQuerySyncMessage@@ -63,6 +61,8 @@ type State struct {// TaskSkippedOrDoneChan is the channel for notifying the task is skipped or done.TaskSkippedOrDoneChan chan int+ // InstanceSyncTickleChan is the tickler for syncing instances.+ InstanceSyncTickleChan chan int// PlanCheckTickleChan is the tickler for plan check scheduler.PlanCheckTickleChan chan int// TaskRunTickleChan is the tickler for task run scheduler.@@ -83,11 +83,11 @@ func New() (*State, error) {return nil, errors.Wrapf(err, "failed to create auth expire cache")}return &State{- InstanceDatabaseSyncChan: make(chan *store.InstanceMessage, 100),InstanceSlowQuerySyncChan: make(chan *InstanceSlowQuerySyncMessage, 100),InstanceOutstandingConnections: make(map[int]int),IssueExternalApprovalRelayCancelChan: make(chan int, 1),TaskSkippedOrDoneChan: make(chan int, 1000),+ InstanceSyncTickleChan: make(chan int, 1000),PlanCheckTickleChan: make(chan int, 1000),TaskRunTickleChan: make(chan int, 1000),ExpireCache: expireCache,diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex 4975d08995..902f46d376 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -64,9 +64,20 @@ func (s *Syncer) Run(ctx context.Context, wg *sync.WaitGroup) {select {case <-ticker.C:s.trySyncAll(ctx)- case instance := <-s.stateCfg.InstanceDatabaseSyncChan:- // Sync all databases for instance.- s.syncAllDatabases(ctx, instance)+ case <-s.stateCfg.InstanceSyncTickleChan:+ s.stateCfg.InstanceSyncs.Range(func(key, value any) bool {+ s.stateCfg.InstanceSyncs.Delete(key)+ instance, ok := value.(*store.InstanceMessage)+ if !ok {+ return true+ }+ // Sync all databases for instance.+ if err := s.SyncInstance(ctx, instance); err != nil {+ slog.Error("failed to sync instance", log.BBError(err))+ }+ s.syncAllDatabases(ctx, instance)+ return true+ })case <-ctx.Done(): // if cancel() executereturn}