Skip to content

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

chore: sync instance on creation (#6426)

Danny Xub1def7f5a388
diff --git a/backend/api/v1/instance_service.go b/backend/api/v1/instance_service.go
index a1ee502c47..373582b781 100644
--- a/backend/api/v1/instance_service.go
+++ b/backend/api/v1/instance_service.go
@@ -5,6 +5,7 @@ import (
"fmt"
"go.uber.org/multierr"
+ "go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
@@ -12,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/dbfactory"
"github.com/bytebase/bytebase/backend/component/state"
enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"
@@ -21,6 +23,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/db/pg"
"github.com/bytebase/bytebase/backend/plugin/metric"
"github.com/bytebase/bytebase/backend/runner/metricreport"
+ "github.com/bytebase/bytebase/backend/runner/schemasync"
"github.com/bytebase/bytebase/backend/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -34,10 +37,11 @@ type InstanceService struct {
secret string
stateCfg *state.State
dbFactory *dbfactory.DBFactory
+ schemaSyncer *schemasync.Syncer
}
// NewInstanceService creates a new InstanceService.
-func NewInstanceService(store *store.Store, licenseService enterpriseAPI.LicenseService, metricReporter *metricreport.Reporter, secret string, stateCfg *state.State, dbFactory *dbfactory.DBFactory) *InstanceService {
+func NewInstanceService(store *store.Store, licenseService enterpriseAPI.LicenseService, metricReporter *metricreport.Reporter, secret string, stateCfg *state.State, dbFactory *dbfactory.DBFactory, schemaSyncer *schemasync.Syncer) *InstanceService {
return &InstanceService{
store: store,
licenseService: licenseService,
@@ -45,6 +49,7 @@ func NewInstanceService(store *store.Store, licenseService enterpriseAPI.License
secret: secret,
stateCfg: stateCfg,
dbFactory: dbFactory,
+ schemaSyncer: schemaSyncer,
}
}
@@ -122,6 +127,18 @@ func (s *InstanceService) CreateInstance(ctx context.Context, request *v1pb.Crea
return nil, status.Errorf(codes.Internal, err.Error())
}
+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* databaseName */)
+ if err == nil {
+ defer driver.Close(ctx)
+ if _, err := s.schemaSyncer.SyncInstance(ctx, instance); err != nil {
+ log.Warn("Failed to sync instance",
+ zap.String("instance", instance.ResourceID),
+ zap.Error(err))
+ }
+ // Sync all databases in the instance asynchronously.
+ s.stateCfg.InstanceDatabaseSyncChan <- instance
+ }
+
s.metricReporter.Report(ctx, &metric.Metric{
Name: metricAPI.InstanceCreateMetricName,
Value: 1,
@@ -130,7 +147,6 @@ func (s *InstanceService) CreateInstance(ctx context.Context, request *v1pb.Crea
},
})
- // TODO(d): sync instance databases.
return convertToInstance(instance), nil
}
diff --git a/backend/component/state/state.go b/backend/component/state/state.go
index 7990d64bf4..11b1f92e24 100644
--- a/backend/component/state/state.go
+++ b/backend/component/state/state.go
@@ -5,6 +5,7 @@ import (
"sync"
api "github.com/bytebase/bytebase/backend/legacyapi"
+ "github.com/bytebase/bytebase/backend/store"
)
// InstanceMaximumConnectionNumber is the maximum number of connections outstanding per instance.
@@ -13,7 +14,7 @@ const InstanceMaximumConnectionNumber = 20
// 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 *api.Instance
+ InstanceDatabaseSyncChan chan *store.InstanceMessage
// InstanceSlowQuerySyncChan is the channel for synchronizing slow query logs for instances.
InstanceSlowQuerySyncChan chan *api.Instance
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index 0a190c9998..a8b2bdbf7a 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -101,7 +101,7 @@ func (s *Syncer) syncAllInstances(ctx context.Context) {
instanceWG.Wait()
}
-func (s *Syncer) syncAllDatabases(ctx context.Context, instance *api.Instance) {
+func (s *Syncer) syncAllDatabases(ctx context.Context, instance *store.InstanceMessage) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
diff --git a/backend/server/server.go b/backend/server/server.go
index d483b21c00..91f25204b3 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -318,7 +318,7 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
}
s.stateCfg = &state.State{
- InstanceDatabaseSyncChan: make(chan *api.Instance, 100),
+ InstanceDatabaseSyncChan: make(chan *store.InstanceMessage, 100),
InstanceSlowQuerySyncChan: make(chan *api.Instance, 100),
InstanceOutstandingConnections: make(map[int]int),
}
@@ -566,7 +566,8 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.MetricReporter,
s.secret,
s.stateCfg,
- s.dbFactory))
+ s.dbFactory,
+ s.SchemaSyncer))
v1pb.RegisterProjectServiceServer(s.grpcServer, v1.NewProjectService(s.store, s.ActivityManager, s.licenseService))
v1pb.RegisterDatabaseServiceServer(s.grpcServer, v1.NewDatabaseService(s.store, s.BackupRunner, s.licenseService))
v1pb.RegisterInstanceRoleServiceServer(s.grpcServer, v1.NewInstanceRoleService(s.store, s.dbFactory))
diff --git a/backend/server/sql.go b/backend/server/sql.go
index 8929428893..918b966711 100644
--- a/backend/server/sql.go
+++ b/backend/server/sql.go
@@ -54,15 +54,11 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
if instance == nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", *sync.InstanceID))
}
- composedInstance, err := s.store.GetInstanceByID(ctx, *sync.InstanceID)
- if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %d", *sync.InstanceID)).SetInternal(err)
- }
if _, err := s.SchemaSyncer.SyncInstance(ctx, instance); err != nil {
resultSet.Error = err.Error()
}
// Sync all databases in the instance asynchronously.
- s.stateCfg.InstanceDatabaseSyncChan <- composedInstance
+ s.stateCfg.InstanceDatabaseSyncChan <- instance
}
if sync.DatabaseID != nil {
database, err := s.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{UID: sync.DatabaseID})