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: rename state package to bus and stateCfg to bus (#18711)
Danny Xu57ac5502f465
diff --git a/backend/api/auth/auth.go b/backend/api/auth/auth.goindex 9873a6e2a2..9873d4902d 100644--- a/backend/api/auth/auth.go+++ b/backend/api/auth/auth.go@@ -19,8 +19,8 @@ import (errs "github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/enterprise"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store"@@ -54,7 +54,7 @@ type APIAuthInterceptor struct {store *store.Storesecret stringlicenseService *enterprise.LicenseService- stateCfg *state.State+ bus *bus.Busprofile *config.Profile}@@ -63,14 +63,14 @@ func New(store *store.Store,secret string,licenseService *enterprise.LicenseService,- stateCfg *state.State,+ bus *bus.Bus,profile *config.Profile,) *APIAuthInterceptor {return &APIAuthInterceptor{store: store,secret: secret,licenseService: licenseService,- stateCfg: stateCfg,+ bus: bus,profile: profile,}}diff --git a/backend/api/lsp/server.go b/backend/api/lsp/server.goindex 81ddc502eb..67b4f54844 100644--- a/backend/api/lsp/server.go+++ b/backend/api/lsp/server.go@@ -4,9 +4,9 @@ import ("sync/atomic""github.com/bytebase/bytebase/backend/api/auth"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/iam"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/enterprise""github.com/bytebase/bytebase/backend/store")@@ -18,7 +18,7 @@ type Server struct {store *store.Storeprofile *config.Profilesecret string- stateCfg *state.State+ bus *bus.BusiamManager *iam.ManagerlicenseService *enterprise.LicenseServiceauthInterceptor *auth.APIAuthInterceptor@@ -29,7 +29,7 @@ func NewServer(store *store.Store,profile *config.Profile,secret string,- stateCfg *state.State,+ bus *bus.Bus,iamManager *iam.Manager,licenseService *enterprise.LicenseService,) *Server {@@ -37,9 +37,9 @@ func NewServer(store: store,profile: profile,secret: secret,- stateCfg: stateCfg,+ bus: bus,iamManager: iamManager,licenseService: licenseService,- authInterceptor: auth.New(store, secret, licenseService, stateCfg, profile),+ authInterceptor: auth.New(store, secret, licenseService, bus, profile),}}diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 5bb7be059c..e27f249b23 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -16,8 +16,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/iam"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/component/webhook""github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -32,7 +32,7 @@ type IssueService struct {v1connect.UnimplementedIssueServiceHandlerstore *store.StorewebhookManager *webhook.Manager- stateCfg *state.State+ bus *bus.BuslicenseService *enterprise.LicenseServiceiamManager *iam.Manager}@@ -47,14 +47,14 @@ type filterIssueMessage struct {func NewIssueService(store *store.Store,webhookManager *webhook.Manager,- stateCfg *state.State,+ bus *bus.Bus,licenseService *enterprise.LicenseService,iamManager *iam.Manager,) *IssueService {return &IssueService{store: store,webhookManager: webhookManager,- stateCfg: stateCfg,+ bus: bus,licenseService: licenseService,iamManager: iamManager,}@@ -404,7 +404,7 @@ func (s *IssueService) CreateIssue(ctx context.Context, req *connect.Request[v1p// Trigger approval finding for all issues// For DATABASE_CHANGE: Will return early if plan check not ready, then retry when plan check completes// For other types: Processes immediately- s.stateCfg.ApprovalCheckChan <- int64(issue.UID)+ s.bus.ApprovalCheckChan <- int64(issue.UID)converted, err := s.convertToIssue(issue)if err != nil {@@ -741,7 +741,7 @@ func (s *IssueService) ApproveIssue(ctx context.Context, req *connect.Request[v1// Auto-create rollout if this approval completes the approval flowif issueV1.ApprovalStatus == v1pb.Issue_APPROVED {if issue.PlanUID != nil {- s.stateCfg.RolloutCreationChan <- *issue.PlanUID+ s.bus.RolloutCreationChan <- *issue.PlanUID}}diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 35d4de9a5b..6f44845d9c 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -13,8 +13,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/iam"- "github.com/bytebase/bytebase/backend/component/state"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"@@ -25,15 +25,15 @@ import (type PlanService struct {v1connect.UnimplementedPlanServiceHandlerstore *store.Store- stateCfg *state.State+ bus *bus.BusiamManager *iam.Manager}// NewPlanService returns a plan service instance.-func NewPlanService(store *store.Store, stateCfg *state.State, iamManager *iam.Manager) *PlanService {+func NewPlanService(store *store.Store, bus *bus.Bus, iamManager *iam.Manager) *PlanService {return &PlanService{store: store,- stateCfg: stateCfg,+ bus: bus,iamManager: iamManager,}}@@ -194,7 +194,7 @@ func (s *PlanService) CreatePlan(ctx context.Context, request *connect.Request[v}}// Tickle plan check scheduler.- s.stateCfg.PlanCheckTickleChan <- 0+ s.bus.PlanCheckTickleChan <- 0convertedPlan, err := convertToPlan(ctx, s.store, plan)if err != nil {@@ -346,7 +346,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[v}}// Tickle plan check scheduler.- s.stateCfg.PlanCheckTickleChan <- 0+ s.bus.PlanCheckTickleChan <- 0}convertedPlan, err := convertToPlan(ctx, s.store, updatedPlan)@@ -434,7 +434,7 @@ func (s *PlanService) RunPlanChecks(ctx context.Context, request *connect.Reques}// Tickle plan check scheduler.- s.stateCfg.PlanCheckTickleChan <- 0+ s.bus.PlanCheckTickleChan <- 0return connect.NewResponse(&v1pb.RunPlanChecksResponse{}), nil}@@ -470,7 +470,7 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.R}// Cancel the plan check run.- if cancelFunc, ok := s.stateCfg.RunningPlanCheckRunsCancelFunc.Load(planCheckRun.UID); ok {+ if cancelFunc, ok := s.bus.RunningPlanCheckRunsCancelFunc.Load(planCheckRun.UID); ok {cancelFunc.(context.CancelFunc)()}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 809b82b919..62adc37c49 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -17,9 +17,9 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/iam"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/component/webhook"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"@@ -35,17 +35,17 @@ type RolloutService struct {v1connect.UnimplementedRolloutServiceHandlerstore *store.StoredbFactory *dbfactory.DBFactory- stateCfg *state.State+ bus *bus.BuswebhookManager *webhook.ManageriamManager *iam.Manager}// NewRolloutService returns a rollout service instance.-func NewRolloutService(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, webhookManager *webhook.Manager, iamManager *iam.Manager) *RolloutService {+func NewRolloutService(store *store.Store, dbFactory *dbfactory.DBFactory, bus *bus.Bus, webhookManager *webhook.Manager, iamManager *iam.Manager) *RolloutService {return &RolloutService{store: store,dbFactory: dbFactory,- stateCfg: stateCfg,+ bus: bus,webhookManager: webhookManager,iamManager: iamManager,}@@ -280,7 +280,7 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request}// Tickle task run scheduler.- s.stateCfg.TaskRunTickleChan <- 0+ s.bus.TaskRunTickleChan <- 0return connect.NewResponse(rolloutV1), nil}@@ -322,7 +322,7 @@ func (s *RolloutService) ListTaskRuns(ctx context.Context, req *connect.Request[return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to list task runs"))}- taskRunsV1, err := convertToTaskRuns(ctx, s.store, s.stateCfg, taskRuns)+ taskRunsV1, err := convertToTaskRuns(ctx, s.store, s.bus, taskRuns)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to task runs"))}@@ -470,7 +470,7 @@ func (s *RolloutService) GetTaskRun(ctx context.Context, req *connect.Request[v1return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("task run %d not found in rollout %d", taskRunUID, planID))}- taskRunV1, err := convertToTaskRun(ctx, s.store, s.stateCfg, taskRun)+ taskRunV1, err := convertToTaskRun(ctx, s.store, s.bus, taskRun)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to task run"))}@@ -754,7 +754,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Request}// Tickle task run scheduler.- s.stateCfg.TaskRunTickleChan <- 0+ s.bus.TaskRunTickleChan <- 0return connect.NewResponse(&v1pb.BatchRunTasksResponse{}), nil}@@ -839,7 +839,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Reques}// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)- s.stateCfg.PlanCompletionCheckChan <- planID+ s.bus.PlanCompletionCheckChan <- planIDreturn connect.NewResponse(&v1pb.BatchSkipTasksResponse{}), nil}@@ -937,7 +937,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.Rfor _, taskRun := range taskRuns {if taskRun.Status == storepb.TaskRun_RUNNING {- if cancelFunc, ok := s.stateCfg.RunningTaskRunsCancelFunc.Load(taskRun.ID); ok {+ if cancelFunc, ok := s.bus.RunningTaskRunsCancelFunc.Load(taskRun.ID); ok {cancelFunc.(context.CancelFunc)()}}diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.goindex c84ff5b3c9..5754dd622d 100644--- a/backend/api/v1/rollout_service_converter.go+++ b/backend/api/v1/rollout_service_converter.go@@ -10,7 +10,7 @@ import ("google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/state"+ "github.com/bytebase/bytebase/backend/component/bus"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store"@@ -24,10 +24,10 @@ func formatEnvironmentFromStageID(stageID string) string {return stageID}-func convertToTaskRuns(ctx context.Context, s *store.Store, stateCfg *state.State, taskRuns []*store.TaskRunMessage) ([]*v1pb.TaskRun, error) {+func convertToTaskRuns(ctx context.Context, s *store.Store, bus *bus.Bus, taskRuns []*store.TaskRunMessage) ([]*v1pb.TaskRun, error) {var taskRunsV1 []*v1pb.TaskRunfor _, taskRun := range taskRuns {- taskRunV1, err := convertToTaskRun(ctx, s, stateCfg, taskRun)+ taskRunV1, err := convertToTaskRun(ctx, s, bus, taskRun)if err != nil {return nil, errors.Wrapf(err, "failed to convert task run")}@@ -36,7 +36,7 @@ func convertToTaskRuns(ctx context.Context, s *store.Store, stateCfg *state.Statreturn taskRunsV1, nil}-func convertToTaskRun(ctx context.Context, s *store.Store, stateCfg *state.State, taskRun *store.TaskRunMessage) (*v1pb.TaskRun, error) {+func convertToTaskRun(ctx context.Context, s *store.Store, bus *bus.Bus, taskRun *store.TaskRunMessage) (*v1pb.TaskRun, error) {stageID := common.FormatStageID(taskRun.Environment)t := &v1pb.TaskRun{Name: common.FormatTaskRun(taskRun.ProjectID, taskRun.PlanUID, stageID, taskRun.TaskUID, taskRun.ID),@@ -60,7 +60,7 @@ func convertToTaskRun(ctx context.Context, s *store.Store, stateCfg *state.Statet.Sheet = common.FormatSheet(taskRun.ProjectID, *taskRun.SheetSha256)}- if v, ok := stateCfg.TaskRunSchedulerInfo.Load(taskRun.ID); ok {+ if v, ok := bus.TaskRunSchedulerInfo.Load(taskRun.ID); ok {if info, ok := v.(*storepb.SchedulerInfo); ok {si, err := convertToSchedulerInfo(ctx, s, info)if err != nil {diff --git a/backend/component/state/state.go b/backend/component/bus/bus.gosimilarity index 85%rename from backend/component/state/state.gorename to backend/component/bus/bus.goindex 95f20ccbdd..afa2ae7323 100644--- a/backend/component/state/state.go+++ b/backend/component/bus/bus.go@@ -1,12 +1,12 @@-// Package state contains the synchronization state shared within the server.-package state+// Package bus contains the message bus for synchronization within the server.+package busimport ("sync")-// State is the state for all in-memory states within the server.-type State struct {+// Bus is the message bus for all in-memory communication within the server.+type Bus struct {// ApprovalCheckChan signals when an issue needs approval template finding.// Triggered by plan check completion, issue creation (if checks already done).ApprovalCheckChan chan int64 // issue UID@@ -33,8 +33,8 @@ type State struct {PlanCompletionCheckChan chan int64}-func New() (*State, error) {- return &State{+func New() (*Bus, error) {+ return &Bus{ApprovalCheckChan: make(chan int64, 1000),PlanCheckTickleChan: make(chan int, 1000),TaskRunTickleChan: make(chan int, 1000),diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex b7b93fee88..b642ac3b7f 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -16,7 +16,7 @@ import (apiv1 "github.com/bytebase/bytebase/backend/api/v1""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/component/state"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/webhook""github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -28,16 +28,16 @@ import (// Runner is the runner for finding approval templates for issues.type Runner struct {store *store.Store- stateCfg *state.State+ bus *bus.BuswebhookManager *webhook.ManagerlicenseService *enterprise.LicenseService}// NewRunner creates a new runner.-func NewRunner(store *store.Store, stateCfg *state.State, webhookManager *webhook.Manager, licenseService *enterprise.LicenseService) *Runner {+func NewRunner(store *store.Store, bus *bus.Bus, webhookManager *webhook.Manager, licenseService *enterprise.LicenseService) *Runner {return &Runner{store: store,- stateCfg: stateCfg,+ bus: bus,webhookManager: webhookManager,licenseService: licenseService,}@@ -50,7 +50,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {for {select {- case issueUID := <-r.stateCfg.ApprovalCheckChan:+ case issueUID := <-r.bus.ApprovalCheckChan:r.processIssue(ctx, issueUID)case <-ctx.Done():returndiff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.goindex dff094dd36..1eccc5b1da 100644--- a/backend/runner/plancheck/scheduler.go+++ b/backend/runner/plancheck/scheduler.go@@ -11,7 +11,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/component/state"+ "github.com/bytebase/bytebase/backend/component/bus"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store")@@ -21,10 +21,10 @@ const ()// NewScheduler creates a new plan check scheduler.-func NewScheduler(s *store.Store, stateCfg *state.State, executor *CombinedExecutor) *Scheduler {+func NewScheduler(s *store.Store, bus *bus.Bus, executor *CombinedExecutor) *Scheduler {return &Scheduler{store: s,- stateCfg: stateCfg,+ bus: bus,executor: executor,}}@@ -32,7 +32,7 @@ func NewScheduler(s *store.Store, stateCfg *state.State, executor *CombinedExecu// Scheduler is the plan check run scheduler.type Scheduler struct {store *store.Store- stateCfg *state.State+ bus *bus.Busexecutor *CombinedExecutor}@@ -46,7 +46,7 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {select {case <-ticker.C:s.runOnce(ctx)- case <-s.stateCfg.PlanCheckTickleChan:+ case <-s.bus.PlanCheckTickleChan:s.runOnce(ctx)case <-ctx.Done():return@@ -82,20 +82,20 @@ func (s *Scheduler) runOnce(ctx context.Context) {func (s *Scheduler) runPlanCheckRun(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) {// Skip the plan check run if it is already running.- if _, ok := s.stateCfg.RunningPlanChecks.Load(planCheckRun.UID); ok {+ if _, ok := s.bus.RunningPlanChecks.Load(planCheckRun.UID); ok {return}- s.stateCfg.RunningPlanChecks.Store(planCheckRun.UID, true)+ s.bus.RunningPlanChecks.Store(planCheckRun.UID, true)go func() {defer func() {- s.stateCfg.RunningPlanChecks.Delete(planCheckRun.UID)- s.stateCfg.RunningPlanCheckRunsCancelFunc.Delete(planCheckRun.UID)+ s.bus.RunningPlanChecks.Delete(planCheckRun.UID)+ s.bus.RunningPlanCheckRunsCancelFunc.Delete(planCheckRun.UID)}()ctxWithCancel, cancel := context.WithCancel(ctx)defer cancel()- s.stateCfg.RunningPlanCheckRunsCancelFunc.Store(planCheckRun.UID, cancel)+ s.bus.RunningPlanCheckRunsCancelFunc.Store(planCheckRun.UID, cancel)var results []*storepb.PlanCheckRunResult_Resultvar err error@@ -142,9 +142,9 @@ func (s *Scheduler) markPlanCheckRunDone(ctx context.Context, planCheckRun *stor}if issue != nil && issue.PlanUID != nil {// Trigger approval finding- s.stateCfg.ApprovalCheckChan <- int64(issue.UID)+ s.bus.ApprovalCheckChan <- int64(issue.UID)// Trigger rollout creation (existing behavior)- s.stateCfg.RolloutCreationChan <- planCheckRun.PlanUID+ s.bus.RolloutCreationChan <- planCheckRun.PlanUID}}diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex d9f5d293ac..1fb882a4af 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -15,10 +15,10 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/ghost"- "github.com/bytebase/bytebase/backend/component/state"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/oracle"@@ -31,11 +31,11 @@ import ()// NewDatabaseMigrateExecutor creates a database migration task executor.-func NewDatabaseMigrateExecutor(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, schemaSyncer *schemasync.Syncer, profile *config.Profile) Executor {+func NewDatabaseMigrateExecutor(store *store.Store, dbFactory *dbfactory.DBFactory, bus *bus.Bus, schemaSyncer *schemasync.Syncer, profile *config.Profile) Executor {return &DatabaseMigrateExecutor{store: store,dbFactory: dbFactory,- stateCfg: stateCfg,+ bus: bus,schemaSyncer: schemaSyncer,profile: profile,}@@ -45,7 +45,7 @@ func NewDatabaseMigrateExecutor(store *store.Store, dbFactory *dbfactory.DBFactotype DatabaseMigrateExecutor struct {store *store.StoredbFactory *dbfactory.DBFactory- stateCfg *state.State+ bus *bus.BusschemaSyncer *schemasync.Syncerprofile *config.Profile}diff --git a/backend/runner/taskrun/pending_scheduler.go b/backend/runner/taskrun/pending_scheduler.goindex 90c3ad6e5e..f885e2f70b 100644--- a/backend/runner/taskrun/pending_scheduler.go+++ b/backend/runner/taskrun/pending_scheduler.go@@ -28,7 +28,7 @@ func (s *Scheduler) runPendingTaskRunsScheduler(ctx context.Context, wg *sync.Waif err := s.schedulePendingTaskRuns(ctx); err != nil {slog.Error("failed to schedule pending task runs", log.BBError(err))}- case <-s.stateCfg.TaskRunTickleChan:+ case <-s.bus.TaskRunTickleChan:if err := s.schedulePendingTaskRuns(ctx); err != nil {slog.Error("failed to schedule pending task runs", log.BBError(err))}@@ -115,7 +115,7 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.T}func (s *Scheduler) storeWaitingCause(taskRunID int, blockingTaskID int) {- s.stateCfg.TaskRunSchedulerInfo.Store(taskRunID, &storepb.SchedulerInfo{+ s.bus.TaskRunSchedulerInfo.Store(taskRunID, &storepb.SchedulerInfo{ReportTime: timestamppb.Now(),WaitingCause: &storepb.SchedulerInfo_WaitingCause{Cause: &storepb.SchedulerInfo_WaitingCause_TaskUid{@@ -126,7 +126,7 @@ func (s *Scheduler) storeWaitingCause(taskRunID int, blockingTaskID int) {}func (s *Scheduler) storeParallelLimitCause(taskRunID int) {- s.stateCfg.TaskRunSchedulerInfo.Store(taskRunID, &storepb.SchedulerInfo{+ s.bus.TaskRunSchedulerInfo.Store(taskRunID, &storepb.SchedulerInfo{ReportTime: timestamppb.Now(),WaitingCause: &storepb.SchedulerInfo_WaitingCause{Cause: &storepb.SchedulerInfo_WaitingCause_ParallelTasksLimit{@@ -157,7 +157,7 @@ func (s *Scheduler) getMaxParallelForTask(ctx context.Context, task *store.TaskM}func (s *Scheduler) promoteTaskRun(ctx context.Context, taskRun *store.TaskRunMessage) error {- s.stateCfg.TaskRunSchedulerInfo.Delete(taskRun.ID)+ s.bus.TaskRunSchedulerInfo.Delete(taskRun.ID)if _, err := s.store.UpdateTaskRunStatus(ctx, &store.TaskRunStatusPatch{ID: taskRun.ID,@@ -175,7 +175,7 @@ func (s *Scheduler) promoteTaskRun(ctx context.Context, taskRun *store.TaskRunMe})select {- case s.stateCfg.TaskRunTickleChan <- 0:+ case s.bus.TaskRunTickleChan <- 0:default:}diff --git a/backend/runner/taskrun/rollout_creator.go b/backend/runner/taskrun/rollout_creator.goindex c9524b3136..8ac5c16416 100644--- a/backend/runner/taskrun/rollout_creator.go+++ b/backend/runner/taskrun/rollout_creator.go@@ -7,7 +7,7 @@ import (apiv1 "github.com/bytebase/bytebase/backend/api/v1""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/component/state"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/webhook"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store"@@ -18,15 +18,15 @@ import (// nolint:revivetype RolloutCreator struct {store *store.Store- stateCfg *state.State+ bus *bus.BuswebhookManager *webhook.Manager}// NewRolloutCreator creates a new rollout creator.-func NewRolloutCreator(store *store.Store, stateCfg *state.State, webhookManager *webhook.Manager) *RolloutCreator {+func NewRolloutCreator(store *store.Store, bus *bus.Bus, webhookManager *webhook.Manager) *RolloutCreator {return &RolloutCreator{store: store,- stateCfg: stateCfg,+ bus: bus,webhookManager: webhookManager,}}@@ -149,7 +149,7 @@ func (rc *RolloutCreator) tryCreateRollout(ctx context.Context, planID int64) {}// Tickle task run scheduler- rc.stateCfg.TaskRunTickleChan <- 0+ rc.bus.TaskRunTickleChan <- 0slog.Info("successfully auto-created rollout", slog.Int("plan_id", int(planID)))}diff --git a/backend/runner/taskrun/running_scheduler.go b/backend/runner/taskrun/running_scheduler.goindex d4b8a8271c..012a113c5a 100644--- a/backend/runner/taskrun/running_scheduler.go+++ b/backend/runner/taskrun/running_scheduler.go@@ -34,7 +34,7 @@ func (s *Scheduler) runRunningTaskRunsScheduler(ctx context.Context, wg *sync.Waif err := s.scheduleRunningTaskRuns(ctx); err != nil {slog.Error("failed to schedule running task runs", log.BBError(err))}- case <-s.stateCfg.TaskRunTickleChan:+ case <-s.bus.TaskRunTickleChan:if err := s.scheduleRunningTaskRuns(ctx); err != nil {slog.Error("failed to schedule running task runs", log.BBError(err))}@@ -102,12 +102,12 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *st}}()defer func() {- s.stateCfg.RunningTaskRunsCancelFunc.Delete(taskRunUID)+ s.bus.RunningTaskRunsCancelFunc.Delete(taskRunUID)}()driverCtx, cancel := context.WithCancel(ctx)defer cancel()- s.stateCfg.RunningTaskRunsCancelFunc.Store(taskRunUID, cancel)+ s.bus.RunningTaskRunsCancelFunc.Store(taskRunUID, cancel)done, result, err := RunExecutorOnce(ctx, driverCtx, executor, task, taskRunUID)@@ -256,7 +256,7 @@ func (s *Scheduler) runTaskRunOnce(ctx context.Context, taskRunUID int, task *sts.createActivityForTaskRunStatusUpdate(ctx, task, storepb.TaskRun_DONE, "")// Signal to check if plan is complete and successful (may send PIPELINE_COMPLETED)- s.stateCfg.PlanCompletionCheckChan <- task.PlanID+ s.bus.PlanCompletionCheckChan <- task.PlanIDreturndefault:// This case should not happen in normal flow, but adding for completenessdiff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex d184262f6d..7dbf548d5a 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -9,8 +9,8 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/component/webhook"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store"@@ -23,7 +23,7 @@ const (// Scheduler is the scheduler for task run.type Scheduler struct {store *store.Store- stateCfg *state.State+ bus *bus.BuswebhookManager *webhook.ManagerexecutorMap map[storepb.Task_Type]Executorprofile *config.Profile@@ -32,13 +32,13 @@ type Scheduler struct {// NewScheduler will create a new scheduler.func NewScheduler(store *store.Store,- stateCfg *state.State,+ bus *bus.Bus,webhookManager *webhook.Manager,profile *config.Profile,) *Scheduler {return &Scheduler{store: store,- stateCfg: stateCfg,+ bus: bus,webhookManager: webhookManager,profile: profile,executorMap: map[storepb.Task_Type]Executor{},@@ -63,9 +63,9 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {go s.runTaskCompletionListener(ctx)// Start rollout creator component- rolloutCreator := NewRolloutCreator(s.store, s.stateCfg, s.webhookManager)+ rolloutCreator := NewRolloutCreator(s.store, s.bus, s.webhookManager)wg.Add(3)- go rolloutCreator.Run(ctx, wg, s.stateCfg.RolloutCreationChan)+ go rolloutCreator.Run(ctx, wg, s.bus.RolloutCreationChan)go s.runPendingTaskRunsScheduler(ctx, wg)go s.runRunningTaskRunsScheduler(ctx, wg)@@ -89,7 +89,7 @@ func (s *Scheduler) runTaskCompletionListener(ctx context.Context) {select {case <-ctx.Done():return- case planID := <-s.stateCfg.PlanCompletionCheckChan:+ case planID := <-s.bus.PlanCompletionCheckChan:s.checkPlanCompletion(ctx, planID)}}diff --git a/backend/runner/taskrun/schema_update_sdl_executor.go b/backend/runner/taskrun/schema_update_sdl_executor.goindex afa7332554..9f9822a4cd 100644--- a/backend/runner/taskrun/schema_update_sdl_executor.go+++ b/backend/runner/taskrun/schema_update_sdl_executor.go@@ -6,9 +6,9 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory"- "github.com/bytebase/bytebase/backend/component/state"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/schema"@@ -18,11 +18,11 @@ import ()// NewSchemaDeclareExecutor creates a schema declare (SDL) task executor.-func NewSchemaDeclareExecutor(store *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State, schemaSyncer *schemasync.Syncer, profile *config.Profile) Executor {+func NewSchemaDeclareExecutor(store *store.Store, dbFactory *dbfactory.DBFactory, bus *bus.Bus, schemaSyncer *schemasync.Syncer, profile *config.Profile) Executor {return &SchemaDeclareExecutor{store: store,dbFactory: dbFactory,- stateCfg: stateCfg,+ bus: bus,schemaSyncer: schemaSyncer,profile: profile,}@@ -32,7 +32,7 @@ func NewSchemaDeclareExecutor(store *store.Store, dbFactory *dbfactory.DBFactorytype SchemaDeclareExecutor struct {store *store.StoredbFactory *dbfactory.DBFactory- stateCfg *state.State+ bus *bus.BusschemaSyncer *schemasync.Syncerprofile *config.Profile}diff --git a/backend/server/grpc_routes.go b/backend/server/grpc_routes.goindex 7e2d4ad6da..8afa140656 100644--- a/backend/server/grpc_routes.go+++ b/backend/server/grpc_routes.go@@ -23,12 +23,12 @@ import (apiv1 "github.com/bytebase/bytebase/backend/api/v1""github.com/bytebase/bytebase/backend/common/log""github.com/bytebase/bytebase/backend/common/stacktrace"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/iam""github.com/bytebase/bytebase/backend/component/sampleinstance""github.com/bytebase/bytebase/backend/component/sheet"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/component/webhook""github.com/bytebase/bytebase/backend/enterprise"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"@@ -45,7 +45,7 @@ func configureGrpcRouters(dbFactory *dbfactory.DBFactory,licenseService *enterprise.LicenseService,profile *config.Profile,- stateCfg *state.State,+ bus *bus.Bus,schemaSyncer *schemasync.Syncer,webhookManager *webhook.Manager,iamManager *iam.Manager,@@ -98,15 +98,15 @@ func configureGrpcRouters(identityProviderService := apiv1.NewIdentityProviderService(stores, licenseService, profile)instanceRoleService := apiv1.NewInstanceRoleService(stores)instanceService := apiv1.NewInstanceService(stores, profile, licenseService, dbFactory, schemaSyncer, sampleInstanceManager)- issueService := apiv1.NewIssueService(stores, webhookManager, stateCfg, licenseService, iamManager)+ issueService := apiv1.NewIssueService(stores, webhookManager, bus, licenseService, iamManager)orgPolicyService := apiv1.NewOrgPolicyService(stores, licenseService)- planService := apiv1.NewPlanService(stores, stateCfg, iamManager)+ planService := apiv1.NewPlanService(stores, bus, iamManager)projectService := apiv1.NewProjectService(stores, profile, iamManager)releaseService := apiv1.NewReleaseService(stores, sheetManager, dbFactory)reviewConfigService := apiv1.NewReviewConfigService(stores)revisionService := apiv1.NewRevisionService(stores)roleService := apiv1.NewRoleService(stores, iamManager, licenseService)- rolloutService := apiv1.NewRolloutService(stores, dbFactory, stateCfg, webhookManager, iamManager)+ rolloutService := apiv1.NewRolloutService(stores, dbFactory, bus, webhookManager, iamManager)settingService := apiv1.NewSettingService(stores, profile, licenseService)sheetService := apiv1.NewSheetService(stores)sqlService := apiv1.NewSQLService(stores, schemaSyncer, dbFactory, licenseService, iamManager)@@ -128,7 +128,7 @@ func configureGrpcRouters(handlerOpts := connect.WithHandlerOptions(connect.WithInterceptors(validateInterceptor,- auth.New(stores, secret, licenseService, stateCfg, profile),+ auth.New(stores, secret, licenseService, bus, profile),apiv1.NewACLInterceptor(stores, secret, iamManager, profile),apiv1.NewAuditInterceptor(stores, secret, profile),),diff --git a/backend/server/server.go b/backend/server/server.goindex 90b2771c16..d360289683 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -21,12 +21,12 @@ import ("github.com/bytebase/bytebase/backend/api/oauth2""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/bus""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/iam""github.com/bytebase/bytebase/backend/component/sampleinstance""github.com/bytebase/bytebase/backend/component/sheet"- "github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/component/webhook""github.com/bytebase/bytebase/backend/demo""github.com/bytebase/bytebase/backend/enterprise"@@ -78,8 +78,8 @@ type Server struct {// PG server stoppers.stopper []func()- // stateCfg is the shared in-momory state within the server.- stateCfg *state.State+ // bus is the message bus for inter-component communication within the server.+ bus *bus.Bus// boot specifies that whether the server boot correctlycancel context.CancelFunc@@ -149,9 +149,9 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {slog.Warn("failed to start sample instances", log.BBError(err))}- s.stateCfg, err = state.New()+ s.bus, err = bus.New()if err != nil {- return nil, errors.Wrapf(err, "failed to create state config")+ return nil, errors.Wrapf(err, "failed to create message bus")}if err := s.store.BackfillIssueTSVector(ctx); err != nil {@@ -185,22 +185,22 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {s.echoServer = echo.New()s.schemaSyncer = schemasync.NewSyncer(stores, s.dbFactory)- s.approvalRunner = approval.NewRunner(stores, s.stateCfg, s.webhookManager, s.licenseService)+ s.approvalRunner = approval.NewRunner(stores, s.bus, s.webhookManager, s.licenseService)- s.taskScheduler = taskrun.NewScheduler(stores, s.stateCfg, s.webhookManager, profile)+ s.taskScheduler = taskrun.NewScheduler(stores, s.bus, s.webhookManager, profile)s.taskScheduler.Register(storepb.Task_DATABASE_CREATE, taskrun.NewDatabaseCreateExecutor(stores, s.dbFactory, s.schemaSyncer))- s.taskScheduler.Register(storepb.Task_DATABASE_MIGRATE, taskrun.NewDatabaseMigrateExecutor(stores, s.dbFactory, s.stateCfg, s.schemaSyncer, profile))+ s.taskScheduler.Register(storepb.Task_DATABASE_MIGRATE, taskrun.NewDatabaseMigrateExecutor(stores, s.dbFactory, s.bus, s.schemaSyncer, profile))s.taskScheduler.Register(storepb.Task_DATABASE_EXPORT, taskrun.NewDataExportExecutor(stores, s.dbFactory, s.licenseService))- s.taskScheduler.Register(storepb.Task_DATABASE_SDL, taskrun.NewSchemaDeclareExecutor(stores, s.dbFactory, s.stateCfg, s.schemaSyncer, profile))+ s.taskScheduler.Register(storepb.Task_DATABASE_SDL, taskrun.NewSchemaDeclareExecutor(stores, s.dbFactory, s.bus, s.schemaSyncer, profile))combinedExecutor := plancheck.NewCombinedExecutor(stores, sheetManager, s.dbFactory)- s.planCheckScheduler = plancheck.NewScheduler(stores, s.stateCfg, combinedExecutor)+ s.planCheckScheduler = plancheck.NewScheduler(stores, s.bus, combinedExecutor)// Data cleaners.dataCleaner = cleaner.NewDataCleaner(stores)// LSP server.- s.lspServer = lsp.NewServer(s.store, profile, secret, s.stateCfg, s.iamManager, s.licenseService)+ s.lspServer = lsp.NewServer(s.store, profile, secret, s.bus, s.iamManager, s.licenseService)directorySyncServer := directorysync.NewService(s.store, s.licenseService, s.iamManager, profile)oauth2Service := oauth2.NewService(stores, profile, secret)@@ -209,7 +209,7 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {return nil, errors.Wrapf(err, "failed to create MCP server")}- if err := configureGrpcRouters(ctx, s.echoServer, s.store, sheetManager, s.dbFactory, s.licenseService, s.profile, s.stateCfg, s.schemaSyncer, s.webhookManager, s.iamManager, secret, s.sampleInstanceManager); err != nil {+ if err := configureGrpcRouters(ctx, s.echoServer, s.store, sheetManager, s.dbFactory, s.licenseService, s.profile, s.bus, s.schemaSyncer, s.webhookManager, s.iamManager, secret, s.sampleInstanceManager); err != nil {return nil, errors.Wrapf(err, "failed to configure gRPC routers")}configureEchoRouters(s.echoServer, s.lspServer, directorySyncServer, oauth2Service, mcpServer, profile)