Skip to content

Commit 2755679

Browse files
wxing1292mindaugasrukas
authored andcommitted
Add corruption protection to ms (#4560)
1 parent 8b3426b commit 2755679

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

common/metrics/metric_defs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,7 @@ var (
14971497
WorkflowRunTimeoutOverrideCount = NewCounterDef("workflow_run_timeout_overrides")
14981498
ReplicationTaskCleanupCount = NewCounterDef("replication_task_cleanup_count")
14991499
ReplicationTaskCleanupFailure = NewCounterDef("replication_task_cleanup_failed")
1500+
MutableStateDirty = NewCounterDef("mutable_state_dirty")
15001501
MutableStateChecksumMismatch = NewCounterDef("mutable_state_checksum_mismatch")
15011502
MutableStateChecksumInvalidated = NewCounterDef("mutable_state_checksum_invalidated")
15021503
ClusterMetadataLockLatency = NewTimerDef("cluster_metadata_lock_latency")

service/history/workflow/history_builder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ func NewImmutableHistoryBuilder(
154154
}
155155
}
156156

157+
func (b *HistoryBuilder) IsDirty() bool {
158+
return len(b.memEventsBatches) > 0 ||
159+
len(b.memLatestBatch) > 0 ||
160+
len(b.memBufferBatch) > 0 ||
161+
len(b.scheduledIDToStartedID) > 0
162+
}
163+
157164
// NOTE:
158165
// originalRunID is the runID when the WorkflowExecutionStarted event is written
159166
// firstRunID is the very first runID along the chain of ContinueAsNew and Reset

service/history/workflow/mutable_state_impl.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4378,6 +4378,17 @@ func (ms *MutableStateImpl) UpdateWorkflowStateStatus(
43784378
func (ms *MutableStateImpl) StartTransaction(
43794379
namespaceEntry *namespace.Namespace,
43804380
) (bool, error) {
4381+
if ms.hBuilder.IsDirty() || len(ms.InsertTasks) > 0 {
4382+
ms.logger.Error("MutableState encountered dirty transaction",
4383+
tag.WorkflowNamespaceID(ms.executionInfo.NamespaceId),
4384+
tag.WorkflowID(ms.executionInfo.WorkflowId),
4385+
tag.WorkflowRunID(ms.executionState.RunId),
4386+
tag.Value(ms.hBuilder),
4387+
)
4388+
ms.metricsHandler.Counter(metrics.MutableStateChecksumInvalidated.GetMetricName()).Record(1)
4389+
return false, serviceerror.NewUnavailable("MutableState encountered dirty transaction")
4390+
}
4391+
43814392
namespaceEntry, err := ms.startTransactionHandleNamespaceMigration(namespaceEntry)
43824393
if err != nil {
43834394
return false, err

0 commit comments

Comments
 (0)