Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(ha): persist task run scheduler info in database (#18878)
p0ny5e12c7917fe7
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yamlindex dc2cefff67..02d474e61c 100644--- a/backend/api/mcp/gen/openapi.yaml+++ b/backend/api/mcp/gen/openapi.yaml@@ -6426,7 +6426,6 @@ components:description: |-The default schema to execute the statement. Equals to the current schemain Oracle and search path in Postgres.- nullable: truecontainer:type:- string@@ -6435,7 +6434,6 @@ components:description: |-Container is the container name to execute the query against, used forCosmosDB only.- nullable: truetitle: AdminExecuteRequestadditionalProperties: falsebytebase.v1.AdminExecuteResponse:@@ -7192,7 +7190,6 @@ components:runTime:title: run_timedescription: The task run should run after run_time.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'title: BatchRunTasksRequestadditionalProperties: false@@ -7676,7 +7673,6 @@ components:objectSchema:title: object_schemadescription: Object schema for complex column types like JSON.- nullable: true$ref: '#/components/schemas/bytebase.v1.ObjectSchema'title: ColumnCatalogadditionalProperties: false@@ -8049,7 +8045,6 @@ components:Format: environments/{environment}If unspecified, all stages are created.If set to "", no stages are created.- nullable: truetitle: CreateRolloutRequestadditionalProperties: falsebytebase.v1.CreateSheetRequest:@@ -8153,7 +8148,6 @@ components:- string- "null"title: level_id- nullable: truetitle: DataClassificationadditionalProperties: falsebytebase.v1.DataClassificationSetting.DataClassificationConfig.Level:@@ -8631,7 +8625,6 @@ components:description: |-The environment resource.Format: environments/prod where prod is the environment resource ID.- nullable: trueeffectiveEnvironment:type:- string@@ -8641,7 +8634,6 @@ components:The effective environment based on environment tag above and environmenttag on the instance. Inheritance followshttps://cloud.google.com/resource-manager/docs/tags/tags-overview.- nullable: truelabels:type: objecttitle: labels@@ -9436,7 +9428,6 @@ components:description: |-The default schema to search objects. Equals to the current schema inOracle and search path in Postgres.- nullable: truetitle: ExportRequestadditionalProperties: falsebytebase.v1.ExportResponse:@@ -10417,7 +10408,6 @@ components:description: |-The environment resource.Format: environments/prod where prod is the environment resource ID.- nullable: trueactivation:type: booleantitle: activation@@ -10505,7 +10495,6 @@ components:description: |-The environment resource.Format: environments/prod where prod is the environment resource ID.- nullable: truetitle: InstanceResourceadditionalProperties: falsebytebase.v1.InstanceRole:@@ -10528,7 +10517,6 @@ components:- "null"title: passworddescription: The role password.- nullable: trueconnectionLimit:type:- integer@@ -10536,14 +10524,12 @@ components:title: connection_limitformat: int32description: The connection count limit for this role.- nullable: truevalidUntil:type:- string- "null"title: valid_untildescription: The expiration for the role's password.- nullable: trueattribute:type:- string@@ -10553,7 +10539,6 @@ components:The role attribute.For PostgreSQL, it contains super_user, no_inherit, create_role, create_db, can_login, replication, and bypass_rls. Docs: https://www.postgresql.org/docs/current/role-attributes.htmlFor MySQL, it's the global privileges as GRANT statements, which means it only contains "GRANT ... ON *.* TO ...". Docs: https://dev.mysql.com/doc/refman/8.0/en/grant.html- nullable: truetitle: InstanceRoleadditionalProperties: falsedescription: InstanceRole is the API message for instance role.@@ -10753,32 +10738,26 @@ components:- string- "null"title: from_title- nullable: truetoTitle:type:- string- "null"title: to_title- nullable: truefromDescription:type:- string- "null"title: from_description- nullable: truetoDescription:type:- string- "null"title: to_description- nullable: truefromStatus:title: from_status- nullable: true$ref: '#/components/schemas/bytebase.v1.IssueStatus'toStatus:title: to_status- nullable: true$ref: '#/components/schemas/bytebase.v1.IssueStatus'fromLabels:type: array@@ -10810,7 +10789,6 @@ components:description: |-The previous sheet.Format: projects/{project}/sheets/{sheet}- nullable: truetoSheet:type:- string@@ -10819,7 +10797,6 @@ components:description: |-The new sheet.Format: projects/{project}/sheets/{sheet}- nullable: truetitle: PlanSpecUpdateadditionalProperties: falsedescription: Plan spec update event information (tracks sheet changes to plan specs).@@ -11223,7 +11200,6 @@ components:instance:title: instancedescription: The target instance. We need to set this field if the target instance is not created yet.- nullable: true$ref: '#/components/schemas/bytebase.v1.Instance'title: ListInstanceDatabaseRequestadditionalProperties: false@@ -11580,7 +11556,6 @@ components:policyType:title: policy_typedescription: Filter by specific policy type.- nullable: true$ref: '#/components/schemas/bytebase.v1.PolicyType'showDeleted:type: boolean@@ -11989,21 +11964,18 @@ components:- "null"title: otp_codedescription: The otp_code is used to verify the user's identity by MFA.- nullable: truerecoveryCode:type:- string- "null"title: recovery_codedescription: The recovery_code is used to recovery the user's identity with MFA.- nullable: truemfaTempToken:type:- string- "null"title: mfa_temp_tokendescription: The mfa_temp_token is used to verify the user's identity by MFA.- nullable: truetitle: LoginRequestadditionalProperties: falsebytebase.v1.LoginResponse:@@ -12021,7 +11993,6 @@ components:- "null"title: mfa_temp_tokendescription: Temporary token for MFA verification.- nullable: truerequireResetPassword:type: booleantitle: require_reset_password@@ -12648,7 +12619,6 @@ components:description: |-The zip password provide by users.Leave it empty if no needs to encrypt the zip file.- nullable: truetitle: ExportDataConfigadditionalProperties: falsebytebase.v1.Plan.PlanCheckRunStatusCountEntry:@@ -13317,7 +13287,6 @@ components:- string- "null"title: error- nullable: trueduration:title: duration$ref: '#/components/schemas/google.protobuf.Duration'@@ -13396,7 +13365,6 @@ components:description: |-The default schema to search objects. Equals to the current schema inOracle and search path in Postgres.- nullable: truequeryOption:title: query_option$ref: '#/components/schemas/bytebase.v1.QueryOption'@@ -13408,7 +13376,6 @@ components:description: |-Container is the container name to execute the query against, used forCosmosDB only.- nullable: truetitle: QueryRequestadditionalProperties: falsebytebase.v1.QueryResponse:@@ -14221,7 +14188,6 @@ components:The UUID of the specific spec to run plan checks for.This should match the spec.id field in Plan.Spec.If not set, all specs in the plan will be used.- nullable: truetitle: RunPlanChecksRequestadditionalProperties: falsebytebase.v1.RunPlanChecksResponse:@@ -15731,14 +15697,12 @@ components:description: |-The update_time is the update time of latest task run.If there are no task runs, it will be empty.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'runTime:title: run_timedescription: |-The run_time is the scheduled run time of latest task run.If there are no task runs or the task run is not scheduled, it will be empty.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'- oneOf:- properties:@@ -15936,7 +15900,6 @@ components:description: |-The task run should run after run_time.This can only be set when creating the task run calling BatchRunTasks.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'title: TaskRunadditionalProperties: false@@ -16372,35 +16335,30 @@ components:- "null"title: statedescription: Session state (active, idle, etc.).- nullable: truewaitEventType:type:- string- "null"title: wait_event_typedescription: Wait event type if session is waiting.- nullable: truewaitEvent:type:- string- "null"title: wait_eventdescription: Specific wait event if session is waiting.- nullable: truedatname:type:- string- "null"title: datnamedescription: Database name.- nullable: trueusename:type:- string- "null"title: usenamedescription: User name.- nullable: trueapplicationName:type: stringtitle: application_name@@ -16411,14 +16369,12 @@ components:- "null"title: client_addrdescription: Client IP address.- nullable: trueclientPort:type:- string- "null"title: client_portdescription: Client port number.- nullable: truebackendStart:title: backend_startdescription: When the backend process started.@@ -16426,12 +16382,10 @@ components:xactStart:title: xact_startdescription: When the current transaction started.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'queryStart:title: query_startdescription: When the current query started.- nullable: true$ref: '#/components/schemas/google.protobuf.Timestamp'title: SessionadditionalProperties: false@@ -17059,7 +17013,6 @@ components:- "null"title: otp_codedescription: The otp_code is used to verify the user's identity by MFA.- nullable: trueregenerateTempMfaSecret:type: booleantitle: regenerate_temp_mfa_secretdiff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex d4e0b7db03..1551ff089d 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -323,7 +323,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.bus, taskRuns)+ taskRunsV1, err := convertToTaskRuns(ctx, s.store, taskRuns)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to task runs"))}@@ -467,7 +467,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.bus, taskRun)+ taskRunV1, err := convertToTaskRun(ctx, s.store, taskRun)if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to convert to task run"))}diff --git a/backend/api/v1/rollout_service_converter.go b/backend/api/v1/rollout_service_converter.goindex b05b325b75..6c7c9a12c3 100644--- a/backend/api/v1/rollout_service_converter.go+++ b/backend/api/v1/rollout_service_converter.go@@ -10,7 +10,6 @@ import ("google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/common"- "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 +23,10 @@ func formatEnvironmentFromStageID(stageID string) string {return stageID}-func convertToTaskRuns(ctx context.Context, s *store.Store, bus *bus.Bus, taskRuns []*store.TaskRunMessage) ([]*v1pb.TaskRun, error) {+func convertToTaskRuns(ctx context.Context, s *store.Store, taskRuns []*store.TaskRunMessage) ([]*v1pb.TaskRun, error) {var taskRunsV1 []*v1pb.TaskRunfor _, taskRun := range taskRuns {- taskRunV1, err := convertToTaskRun(ctx, s, bus, taskRun)+ taskRunV1, err := convertToTaskRun(ctx, s, taskRun)if err != nil {return nil, errors.Wrapf(err, "failed to convert task run")}@@ -36,7 +35,7 @@ func convertToTaskRuns(ctx context.Context, s *store.Store, bus *bus.Bus, taskRureturn taskRunsV1, nil}-func convertToTaskRun(ctx context.Context, s *store.Store, bus *bus.Bus, taskRun *store.TaskRunMessage) (*v1pb.TaskRun, error) {+func convertToTaskRun(ctx context.Context, s *store.Store, 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),@@ -53,10 +52,8 @@ func convertToTaskRun(ctx context.Context, s *store.Store, bus *bus.Bus, taskRunt.RunTime = timestamppb.New(*taskRun.RunAt)}- if v, ok := bus.TaskRunSchedulerInfo.Load(taskRun.ID); ok {- if info, ok := v.(*storepb.SchedulerInfo); ok {- t.SchedulerInfo = convertToSchedulerInfo(info)- }+ if taskRun.PayloadProto != nil && taskRun.PayloadProto.SchedulerInfo != nil {+ t.SchedulerInfo = convertToSchedulerInfo(taskRun.PayloadProto.SchedulerInfo)}if taskRun.ResultProto.ExportArchiveUid != 0 {diff --git a/backend/component/bus/bus.go b/backend/component/bus/bus.goindex 2d0cb4a6c5..881c4d85a2 100644--- a/backend/component/bus/bus.go+++ b/backend/component/bus/bus.go@@ -11,8 +11,6 @@ type Bus struct {// Triggered by plan check completion, issue creation (if checks already done).ApprovalCheckChan chan int64 // issue UID- TaskRunSchedulerInfo sync.Map // map[taskRunID]*storepb.SchedulerInfo-// RunningTaskRunsCancelFunc is the cancelFunc of running taskruns.RunningTaskRunsCancelFunc sync.Map // map[taskRunID]context.CancelFuncdiff --git a/backend/generated-go/store/task_run.pb.go b/backend/generated-go/store/task_run.pb.goindex b4fe475d74..4fc5e56b5f 100644--- a/backend/generated-go/store/task_run.pb.go+++ b/backend/generated-go/store/task_run.pb.go@@ -256,6 +256,54 @@ func (x *SchedulerInfo) GetWaitingCause() *SchedulerInfo_WaitingCause {return nil}+// TaskRunPayload contains extensible runtime data for a task run.+// Stored in the payload JSONB column. New fields can be added here+// without database schema changes.+type TaskRunPayload struct {+ state protoimpl.MessageState `protogen:"open.v1"`+ // Scheduler information about why a task is waiting.+ SchedulerInfo *SchedulerInfo `protobuf:"bytes,1,opt,name=scheduler_info,json=schedulerInfo,proto3" json:"scheduler_info,omitempty"`+ unknownFields protoimpl.UnknownFields+ sizeCache protoimpl.SizeCache+}++func (x *TaskRunPayload) Reset() {+ *x = TaskRunPayload{}+ mi := &file_store_task_run_proto_msgTypes[3]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+}++func (x *TaskRunPayload) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*TaskRunPayload) ProtoMessage() {}++func (x *TaskRunPayload) ProtoReflect() protoreflect.Message {+ mi := &file_store_task_run_proto_msgTypes[3]+ if x != nil {+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ if ms.LoadMessageInfo() == nil {+ ms.StoreMessageInfo(mi)+ }+ return ms+ }+ return mi.MessageOf(x)+}++// Deprecated: Use TaskRunPayload.ProtoReflect.Descriptor instead.+func (*TaskRunPayload) Descriptor() ([]byte, []int) {+ return file_store_task_run_proto_rawDescGZIP(), []int{3}+}++func (x *TaskRunPayload) GetSchedulerInfo() *SchedulerInfo {+ if x != nil {+ return x.SchedulerInfo+ }+ return nil+}+// WaitingCause indicates why a task run is waiting to execute.type SchedulerInfo_WaitingCause struct {state protoimpl.MessageState `protogen:"open.v1"`@@ -269,7 +317,7 @@ type SchedulerInfo_WaitingCause struct {func (x *SchedulerInfo_WaitingCause) Reset() {*x = SchedulerInfo_WaitingCause{}- mi := &file_store_task_run_proto_msgTypes[3]+ mi := &file_store_task_run_proto_msgTypes[4]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}@@ -281,7 +329,7 @@ func (x *SchedulerInfo_WaitingCause) String() string {func (*SchedulerInfo_WaitingCause) ProtoMessage() {}func (x *SchedulerInfo_WaitingCause) ProtoReflect() protoreflect.Message {- mi := &file_store_task_run_proto_msgTypes[3]+ mi := &file_store_task_run_proto_msgTypes[4]if x != nil {ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))if ms.LoadMessageInfo() == nil {@@ -351,7 +399,9 @@ const file_store_task_run_proto_rawDesc = "" +"\rwaiting_cause\x18\x02 \x01(\v2*.bytebase.store.SchedulerInfo.WaitingCauseR\fwaitingCause\x1aK\n" +"\fWaitingCause\x122\n" +"\x14parallel_tasks_limit\x18\x03 \x01(\bH\x00R\x12parallelTasksLimitB\a\n" +- "\x05causeB\x8f\x01\n" ++ "\x05cause\"V\n" ++ "\x0eTaskRunPayload\x12D\n" ++ "\x0escheduler_info\x18\x01 \x01(\v2\x1d.bytebase.store.SchedulerInfoR\rschedulerInfoB\x8f\x01\n" +"\x12com.bytebase.storeB\fTaskRunProtoP\x01Z\x12generated-go/store\xa2\x02\x03BSX\xaa\x02\x0eBytebase.Store\xca\x02\x0eBytebase\\Store\xe2\x02\x1aBytebase\\Store\\GPBMetadata\xea\x02\x0fBytebase::Storeb\x06proto3"var (@@ -367,23 +417,25 @@ func file_store_task_run_proto_rawDescGZIP() []byte {}var file_store_task_run_proto_enumTypes = make([]protoimpl.EnumInfo, 1)-var file_store_task_run_proto_msgTypes = make([]protoimpl.MessageInfo, 4)+var file_store_task_run_proto_msgTypes = make([]protoimpl.MessageInfo, 5)var file_store_task_run_proto_goTypes = []any{(TaskRun_Status)(0), // 0: bytebase.store.TaskRun.Status(*TaskRun)(nil), // 1: bytebase.store.TaskRun(*TaskRunResult)(nil), // 2: bytebase.store.TaskRunResult(*SchedulerInfo)(nil), // 3: bytebase.store.SchedulerInfo- (*SchedulerInfo_WaitingCause)(nil), // 4: bytebase.store.SchedulerInfo.WaitingCause- (*timestamppb.Timestamp)(nil), // 5: google.protobuf.Timestamp+ (*TaskRunPayload)(nil), // 4: bytebase.store.TaskRunPayload+ (*SchedulerInfo_WaitingCause)(nil), // 5: bytebase.store.SchedulerInfo.WaitingCause+ (*timestamppb.Timestamp)(nil), // 6: google.protobuf.Timestamp}var file_store_task_run_proto_depIdxs = []int32{- 5, // 0: bytebase.store.SchedulerInfo.report_time:type_name -> google.protobuf.Timestamp- 4, // 1: bytebase.store.SchedulerInfo.waiting_cause:type_name -> bytebase.store.SchedulerInfo.WaitingCause- 2, // [2:2] is the sub-list for method output_type- 2, // [2:2] is the sub-list for method input_type- 2, // [2:2] is the sub-list for extension type_name- 2, // [2:2] is the sub-list for extension extendee- 0, // [0:2] is the sub-list for field type_name+ 6, // 0: bytebase.store.SchedulerInfo.report_time:type_name -> google.protobuf.Timestamp+ 5, // 1: bytebase.store.SchedulerInfo.waiting_cause:type_name -> bytebase.store.SchedulerInfo.WaitingCause+ 3, // 2: bytebase.store.TaskRunPayload.scheduler_info:type_name -> bytebase.store.SchedulerInfo+ 3, // [3:3] is the sub-list for method output_type+ 3, // [3:3] is the sub-list for method input_type+ 3, // [3:3] is the sub-list for extension type_name+ 3, // [3:3] is the sub-list for extension extendee+ 0, // [0:3] is the sub-list for field type_name}func init() { file_store_task_run_proto_init() }@@ -391,7 +443,7 @@ func file_store_task_run_proto_init() {if File_store_task_run_proto != nil {return}- file_store_task_run_proto_msgTypes[3].OneofWrappers = []any{+ file_store_task_run_proto_msgTypes[4].OneofWrappers = []any{(*SchedulerInfo_WaitingCause_ParallelTasksLimit)(nil),}type x struct{}@@ -400,7 +452,7 @@ func file_store_task_run_proto_init() {GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: unsafe.Slice(unsafe.StringData(file_store_task_run_proto_rawDesc), len(file_store_task_run_proto_rawDesc)),NumEnums: 1,- NumMessages: 4,+ NumMessages: 5,NumExtensions: 0,NumServices: 0,},diff --git a/backend/generated-go/store/task_run_equal.pb.go b/backend/generated-go/store/task_run_equal.pb.goindex 3880bec621..1490facbc5 100644--- a/backend/generated-go/store/task_run_equal.pb.go+++ b/backend/generated-go/store/task_run_equal.pb.go@@ -60,3 +60,16 @@ func (x *SchedulerInfo) Equal(y *SchedulerInfo) bool {}return true}++func (x *TaskRunPayload) Equal(y *TaskRunPayload) bool {+ if x == y {+ return true+ }+ if x == nil || y == nil {+ return x == nil && y == nil+ }+ if !x.SchedulerInfo.Equal(y.SchedulerInfo) {+ return false+ }+ return true+}diff --git a/backend/migrator/migration/3.14/0032##task_run_payload.sql b/backend/migrator/migration/3.14/0032##task_run_payload.sqlnew file mode 100644index 0000000000..fc0ef952c7--- /dev/null+++ b/backend/migrator/migration/3.14/0032##task_run_payload.sql@@ -0,0 +1 @@+ALTER TABLE task_run ADD COLUMN payload jsonb NOT NULL DEFAULT '{}';diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 4e8021f21a..cce2cef93b 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -265,7 +265,9 @@ CREATE TABLE task_run (-- result saves the task run result in json format-- Stored as TaskRunResult (proto/store/store/task_run.proto)result jsonb NOT NULL DEFAULT '{}',- replica_id TEXT+ replica_id TEXT,+ -- Stored as TaskRunPayload (proto/store/store/task_run.proto)+ payload jsonb NOT NULL DEFAULT '{}');CREATE INDEX idx_task_run_task_id ON task_run(task_id);diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.goindex 32418bb532..dca3ae5bc5 100644--- a/backend/migrator/migrator_test.go+++ b/backend/migrator/migrator_test.go@@ -12,8 +12,8 @@ import (func TestLatestVersion(t *testing.T) {files, err := getSortedVersionedFiles()require.NoError(t, err)- require.Equal(t, semver.MustParse("3.14.31"), *files[len(files)-1].version)- require.Equal(t, "migration/3.14/0031##normalize_statement_types.sql", files[len(files)-1].path)+ require.Equal(t, semver.MustParse("3.14.32"), *files[len(files)-1].version)+ require.Equal(t, "migration/3.14/0032##task_run_payload.sql", files[len(files)-1].path)}func TestVersionUnique(t *testing.T) {diff --git a/backend/runner/taskrun/pending_scheduler.go b/backend/runner/taskrun/pending_scheduler.goindex 4d608b63ae..a1b775d1ff 100644--- a/backend/runner/taskrun/pending_scheduler.go+++ b/backend/runner/taskrun/pending_scheduler.go@@ -99,7 +99,7 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.Treturn errors.Wrapf(err, "failed to get max parallel limit")}if !sc.checkParallelLimit(task.PlanID, maxParallel) {- s.storeParallelLimitCause(taskRun.ID)+ s.storeParallelLimitCause(ctx, taskRun.ID)return nil}@@ -112,15 +112,20 @@ func (s *Scheduler) schedulePendingTaskRun(ctx context.Context, taskRun *store.Treturn nil}-func (s *Scheduler) storeParallelLimitCause(taskRunID int) {- s.bus.TaskRunSchedulerInfo.Store(taskRunID, &storepb.SchedulerInfo{- ReportTime: timestamppb.Now(),- WaitingCause: &storepb.SchedulerInfo_WaitingCause{- Cause: &storepb.SchedulerInfo_WaitingCause_ParallelTasksLimit{- ParallelTasksLimit: true,+func (s *Scheduler) storeParallelLimitCause(ctx context.Context, taskRunID int) {+ payload := &storepb.TaskRunPayload{+ SchedulerInfo: &storepb.SchedulerInfo{+ ReportTime: timestamppb.Now(),+ WaitingCause: &storepb.SchedulerInfo_WaitingCause{+ Cause: &storepb.SchedulerInfo_WaitingCause_ParallelTasksLimit{+ ParallelTasksLimit: true,+ },},},- })+ }+ if err := s.store.UpdateTaskRunPayload(ctx, taskRunID, payload); err != nil {+ slog.Error("failed to store parallel limit cause", log.BBError(err))+ }}func (s *Scheduler) getMaxParallelForTask(ctx context.Context, task *store.TaskMessage) (int, error) {@@ -144,7 +149,10 @@ func (s *Scheduler) getMaxParallelForTask(ctx context.Context, task *store.TaskM}func (s *Scheduler) promoteTaskRun(ctx context.Context, taskRun *store.TaskRunMessage) error {- s.bus.TaskRunSchedulerInfo.Delete(taskRun.ID)+ // Clear scheduler info by writing empty payload+ if err := s.store.UpdateTaskRunPayload(ctx, taskRun.ID, &storepb.TaskRunPayload{}); err != nil {+ slog.Error("failed to clear scheduler info", log.BBError(err))+ }if _, err := s.store.UpdateTaskRunStatus(ctx, &store.TaskRunStatusPatch{ID: taskRun.ID,diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex 076177c603..7e75a12d83 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -15,11 +15,12 @@ import (// TaskRunMessage is message for task run.type TaskRunMessage struct {- TaskUID int- Environment string // Refer to the task's environment.- PlanUID int64- Status storepb.TaskRun_Status- ResultProto *storepb.TaskRunResult+ TaskUID int+ Environment string // Refer to the task's environment.+ PlanUID int64+ Status storepb.TaskRun_Status+ ResultProto *storepb.TaskRunResult+ PayloadProto *storepb.TaskRunPayload// Output only.ID int@@ -66,6 +67,7 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*task_run.started_at,task_run.run_at,task_run.result,+ task_run.payload,task.plan_id,task.environment,project.resource_id@@ -117,7 +119,7 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*var taskRun TaskRunMessagevar startedAt, runAt sql.NullTimevar statusString string- var resultJSON string+ var resultJSON, payloadJSON stringif err := rows.Scan(&taskRun.ID,&taskRun.CreatorEmail,@@ -128,6 +130,7 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*&startedAt,&runAt,&resultJSON,+ &payloadJSON,&taskRun.PlanUID,&taskRun.Environment,&taskRun.ProjectID,@@ -151,6 +154,11 @@ func (s *Store) ListTaskRuns(ctx context.Context, find *FindTaskRunMessage) ([]*return nil, errors.Wrapf(err, "failed to unmarshal task run result: %s", resultJSON)}taskRun.ResultProto = &resultProto+ var payloadProto storepb.TaskRunPayload+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(payloadJSON), &payloadProto); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal task run payload: %s", payloadJSON)+ }+ taskRun.PayloadProto = &payloadPrototaskRuns = append(taskRuns, &taskRun)}@@ -450,3 +458,31 @@ func (s *Store) FailStaleTaskRuns(ctx context.Context, stalenessThreshold time.D}return result.RowsAffected()}++// UpdateTaskRunPayload updates the payload column for a task run.+func (s *Store) UpdateTaskRunPayload(ctx context.Context, taskRunID int, payload *storepb.TaskRunPayload) error {+ payloadBytes, err := protojson.Marshal(payload)+ if err != nil {+ return errors.Wrapf(err, "failed to marshal task run payload")+ }+ payloadStr := string(payloadBytes)+ if payloadStr == "" {+ payloadStr = "{}"+ }++ q := qb.Q().Space(`+ UPDATE task_run+ SET payload = ?, updated_at = now()+ WHERE id = ?+ `, payloadStr, taskRunID)++ query, args, err := q.ToSQL()+ if err != nil {+ return errors.Wrapf(err, "failed to build sql")+ }++ if _, err := s.GetDB().ExecContext(ctx, query, args...); err != nil {+ return errors.Wrapf(err, "failed to update task run payload")+ }+ return nil+}diff --git a/docs/plans/2026-01-13-task-run-payload-ha-design.md b/docs/plans/2026-01-13-task-run-payload-ha-design.mdnew file mode 100644index 0000000000..672ae0750a--- /dev/null+++ b/docs/plans/2026-01-13-task-run-payload-ha-design.md@@ -0,0 +1,67 @@+# Task Run Payload for HA Scheduler State++## Problem++`TaskRunSchedulerInfo` is stored in-memory in `Bus.sync.Map`. This breaks HA:+- Lost on replica crash+- Not shared across replicas+- UI can't show why a task is waiting if the tracking replica dies++## Solution++Add a `payload` JSONB column to `task_run` table, storing a `TaskRunPayload` proto that contains `SchedulerInfo`. Future fields can be added to the proto without schema changes.++## Design++### Proto++`proto/store/store/task_run.proto`:+```protobuf+message TaskRunPayload {+ SchedulerInfo scheduler_info = 1;+ // Future fields added here without schema changes+}+```++### Schema++Migration `backend/migrator/migration/3.14/0032##task_run_payload.sql`:+```sql+ALTER TABLE task_run ADD COLUMN payload jsonb NOT NULL DEFAULT '{}';+```++### Store Layer++`backend/store/task_run.go`:+- Add `PayloadProto *storepb.TaskRunPayload` to `TaskRunMessage`+- Read: Add `payload` to SELECT, unmarshal with `protojson`+- Write: `UpdateTaskRunPayload(ctx, taskRunID, payload)` method++### Scheduler++`backend/runner/taskrun/pending_scheduler.go`:+- `storeParallelLimitCause()`: Write `TaskRunPayload{SchedulerInfo: ...}` to DB+- `promoteTaskRun()`: Write empty `TaskRunPayload{}` to clear++### API++`backend/api/v1/rollout_service_converter.go`:+- Remove `bus *bus.Bus` parameter+- Read `SchedulerInfo` from `taskRun.PayloadProto.SchedulerInfo`++### Cleanup++`backend/component/bus/bus.go`:+- Remove `TaskRunSchedulerInfo sync.Map`++## Files to Modify++1. `proto/store/store/task_run.proto` - Add `TaskRunPayload`+2. `backend/migrator/migration/3.14/0032##task_run_payload.sql` - New+3. `backend/migrator/migration/LATEST.sql` - Add column+4. `backend/migrator/migrator_test.go` - Update version+5. `backend/store/task_run.go` - Add field and methods+6. `backend/runner/taskrun/pending_scheduler.go` - Use store+7. `backend/api/v1/rollout_service_converter.go` - Remove Bus+8. `backend/api/v1/rollout_service.go` - Update converter calls+9. `backend/component/bus/bus.go` - Remove fielddiff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 56e50ea50f..dfc003bdb2 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -289,6 +289,7 @@- [SchedulerInfo](#bytebase-store-SchedulerInfo)- [SchedulerInfo.WaitingCause](#bytebase-store-SchedulerInfo-WaitingCause)- [TaskRun](#bytebase-store-TaskRun)+ - [TaskRunPayload](#bytebase-store-TaskRunPayload)- [TaskRunResult](#bytebase-store-TaskRunResult)- [TaskRun.Status](#bytebase-store-TaskRun-Status)@@ -4761,6 +4762,23 @@ TaskRun represents an execution attempt of a task.+<a name="bytebase-store-TaskRunPayload"></a>++### TaskRunPayload+TaskRunPayload contains extensible runtime data for a task run.+Stored in the payload JSONB column. New fields can be added here+without database schema changes.+++| Field | Type | Label | Description |+| ----- | ---- | ----- | ----------- |+| scheduler_info | [SchedulerInfo](#bytebase-store-SchedulerInfo) | | Scheduler information about why a task is waiting. |++++++<a name="bytebase-store-TaskRunResult"></a>### TaskRunResultdiff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex e3fd0036bb..8490adc72d 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -1328,6 +1328,10 @@<a href="#bytebase.store.TaskRun"><span class="badge">M</span>TaskRun</a></li>+ <li>+ <a href="#bytebase.store.TaskRunPayload"><span class="badge">M</span>TaskRunPayload</a>+ </li>+<li><a href="#bytebase.store.TaskRunResult"><span class="badge">M</span>TaskRunResult</a></li>@@ -12970,6 +12974,30 @@ or sheet content for non-release tasks.</p></td>+ <h3 id="bytebase.store.TaskRunPayload">TaskRunPayload</h3>+ <p>TaskRunPayload contains extensible runtime data for a task run.</p><p>Stored in the payload JSONB column. New fields can be added here</p><p>without database schema changes.</p>+++ <table class="field-table">+ <thead>+ <tr><td>Field</td><td>Type</td><td>Label</td><td>Description</td></tr>+ </thead>+ <tbody>++ <tr>+ <td>scheduler_info</td>+ <td><a href="#bytebase.store.SchedulerInfo">SchedulerInfo</a></td>+ <td></td>+ <td><p>Scheduler information about why a task is waiting. </p></td>+ </tr>++ </tbody>+ </table>+++++<h3 id="bytebase.store.TaskRunResult">TaskRunResult</h3><p>TaskRunResult contains the outcome and metadata from a task run execution.</p>diff --git a/proto/store/store/task_run.proto b/proto/store/store/task_run.protoindex a12679aeff..cd578ccb36 100644--- a/proto/store/store/task_run.proto+++ b/proto/store/store/task_run.proto@@ -59,3 +59,11 @@ message SchedulerInfo {// Reason why the task run is currently waiting.WaitingCause waiting_cause = 2;}++// TaskRunPayload contains extensible runtime data for a task run.+// Stored in the payload JSONB column. New fields can be added here+// without database schema changes.+message TaskRunPayload {+ // Scheduler information about why a task is waiting.+ SchedulerInfo scheduler_info = 1;+}