@@ -40,6 +40,8 @@ import (
40
40
persistencespb "go.temporal.io/server/api/persistence/v1"
41
41
"go.temporal.io/server/common"
42
42
"go.temporal.io/server/common/clock"
43
+ "go.temporal.io/server/common/cluster"
44
+ "go.temporal.io/server/common/convert"
43
45
"go.temporal.io/server/common/definition"
44
46
"go.temporal.io/server/common/locks"
45
47
"go.temporal.io/server/common/log"
@@ -147,13 +149,14 @@ type (
147
149
148
150
type (
149
151
ContextImpl struct {
150
- shard shard.Context
151
- workflowKey definition.WorkflowKey
152
- logger log.Logger
153
- metricsHandler metrics.Handler
154
- timeSource clock.TimeSource
155
- config * configs.Config
156
- transaction Transaction
152
+ shard shard.Context
153
+ workflowKey definition.WorkflowKey
154
+ logger log.Logger
155
+ metricsHandler metrics.Handler
156
+ clusterMetadata cluster.Metadata
157
+ timeSource clock.TimeSource
158
+ config * configs.Config
159
+ transaction Transaction
157
160
158
161
mutex locks.PriorityMutex
159
162
MutableState MutableState
@@ -169,14 +172,15 @@ func NewContext(
169
172
logger log.Logger ,
170
173
) * ContextImpl {
171
174
return & ContextImpl {
172
- shard : shard ,
173
- workflowKey : workflowKey ,
174
- logger : logger ,
175
- metricsHandler : shard .GetMetricsHandler ().WithTags (metrics .OperationTag (metrics .WorkflowContextScope )),
176
- timeSource : shard .GetTimeSource (),
177
- config : shard .GetConfig (),
178
- mutex : locks .NewPriorityMutex (),
179
- transaction : NewTransaction (shard ),
175
+ shard : shard ,
176
+ workflowKey : workflowKey ,
177
+ logger : logger ,
178
+ metricsHandler : shard .GetMetricsHandler ().WithTags (metrics .OperationTag (metrics .WorkflowContextScope )),
179
+ clusterMetadata : shard .GetClusterMetadata (),
180
+ timeSource : shard .GetTimeSource (),
181
+ config : shard .GetConfig (),
182
+ mutex : locks .NewPriorityMutex (),
183
+ transaction : NewTransaction (shard ),
180
184
stats : & persistencespb.ExecutionStats {
181
185
HistorySize : 0 ,
182
186
},
@@ -349,6 +353,7 @@ func (c *ContextImpl) CreateWorkflowExecution(
349
353
resp , err := createWorkflowExecution (
350
354
ctx ,
351
355
c .shard ,
356
+ newMutableState .GetCurrentVersion (),
352
357
createRequest ,
353
358
)
354
359
if err != nil {
@@ -361,7 +366,7 @@ func (c *ContextImpl) CreateWorkflowExecution(
361
366
return err
362
367
}
363
368
NotifyWorkflowSnapshotTasks (engine , newWorkflow )
364
- emitStateTransitionCount (c .metricsHandler , newMutableState )
369
+ emitStateTransitionCount (c .metricsHandler , c . clusterMetadata , newMutableState )
365
370
366
371
return nil
367
372
}
@@ -451,10 +456,13 @@ func (c *ContextImpl) ConflictResolveWorkflowExecution(
451
456
if resetWorkflowSizeDiff , newWorkflowSizeDiff , currentWorkflowSizeDiff , err := c .transaction .ConflictResolveWorkflowExecution (
452
457
ctx ,
453
458
conflictResolveMode ,
459
+ resetMutableState .GetCurrentVersion (),
454
460
resetWorkflow ,
455
461
resetWorkflowEventsSeq ,
462
+ MutableStateFailoverVersion (newMutableState ),
456
463
newWorkflow ,
457
464
newWorkflowEventsSeq ,
465
+ MutableStateFailoverVersion (currentMutableState ),
458
466
currentWorkflow ,
459
467
currentWorkflowEventsSeq ,
460
468
); err != nil {
@@ -469,9 +477,9 @@ func (c *ContextImpl) ConflictResolveWorkflowExecution(
469
477
}
470
478
}
471
479
472
- emitStateTransitionCount (c .metricsHandler , resetMutableState )
473
- emitStateTransitionCount (c .metricsHandler , newMutableState )
474
- emitStateTransitionCount (c .metricsHandler , currentMutableState )
480
+ emitStateTransitionCount (c .metricsHandler , c . clusterMetadata , resetMutableState )
481
+ emitStateTransitionCount (c .metricsHandler , c . clusterMetadata , newMutableState )
482
+ emitStateTransitionCount (c .metricsHandler , c . clusterMetadata , currentMutableState )
475
483
476
484
return nil
477
485
}
@@ -628,8 +636,10 @@ func (c *ContextImpl) UpdateWorkflowExecutionWithNew(
628
636
if currentWorkflowSizeDiff , newWorkflowSizeDiff , err := c .transaction .UpdateWorkflowExecution (
629
637
ctx ,
630
638
updateMode ,
639
+ c .MutableState .GetCurrentVersion (),
631
640
currentWorkflow ,
632
641
currentWorkflowEventsSeq ,
642
+ MutableStateFailoverVersion (newMutableState ),
633
643
newWorkflow ,
634
644
newWorkflowEventsSeq ,
635
645
); err != nil {
@@ -641,8 +651,8 @@ func (c *ContextImpl) UpdateWorkflowExecutionWithNew(
641
651
}
642
652
}
643
653
644
- emitStateTransitionCount (c .metricsHandler , c .MutableState )
645
- emitStateTransitionCount (c .metricsHandler , newMutableState )
654
+ emitStateTransitionCount (c .metricsHandler , c .clusterMetadata , c . MutableState )
655
+ emitStateTransitionCount (c .metricsHandler , c . clusterMetadata , newMutableState )
646
656
647
657
// finally emit session stats
648
658
namespace := c .GetNamespace ()
@@ -922,12 +932,58 @@ func (c *ContextImpl) enforceSizeCheck(
922
932
923
933
func emitStateTransitionCount (
924
934
metricsHandler metrics.Handler ,
935
+ clusterMetadata cluster.Metadata ,
925
936
mutableState MutableState ,
926
937
) {
927
938
if mutableState == nil {
928
939
return
929
940
}
930
941
931
- metricsHandler .Histogram (metrics .StateTransitionCount .GetMetricName (), metrics .StateTransitionCount .GetMetricUnit ()).
932
- Record (mutableState .GetExecutionInfo ().StateTransitionCount , metrics .NamespaceTag (mutableState .GetNamespaceEntry ().Name ().String ()))
942
+ namespaceEntry := mutableState .GetNamespaceEntry ()
943
+ metricsHandler .Histogram (
944
+ metrics .StateTransitionCount .GetMetricName (),
945
+ metrics .StateTransitionCount .GetMetricUnit (),
946
+ ).Record (
947
+ mutableState .GetExecutionInfo ().StateTransitionCount ,
948
+ metrics .NamespaceTag (namespaceEntry .Name ().String ()),
949
+ metrics .NamespaceStateTag (namespaceState (clusterMetadata , convert .Int64Ptr (mutableState .GetCurrentVersion ()))),
950
+ )
951
+ }
952
+
953
+ const (
954
+ namespaceStateActive = "active"
955
+ namespaceStatePassive = "passive"
956
+ namespaceStateUnknown = "_unknown_"
957
+ )
958
+
959
+ func namespaceState (
960
+ clusterMetadata cluster.Metadata ,
961
+ mutableStateCurrentVersion * int64 ,
962
+ ) string {
963
+
964
+ if mutableStateCurrentVersion == nil {
965
+ return namespaceStateUnknown
966
+ }
967
+
968
+ // default value, need to special handle
969
+ if * mutableStateCurrentVersion == 0 {
970
+ return namespaceStateActive
971
+ }
972
+
973
+ if clusterMetadata .IsVersionFromSameCluster (
974
+ clusterMetadata .GetClusterID (),
975
+ * mutableStateCurrentVersion ,
976
+ ) {
977
+ return namespaceStateActive
978
+ }
979
+ return namespaceStatePassive
980
+ }
981
+
982
+ func MutableStateFailoverVersion (
983
+ mutableState MutableState ,
984
+ ) * int64 {
985
+ if mutableState == nil {
986
+ return nil
987
+ }
988
+ return convert .Int64Ptr (mutableState .GetCurrentVersion ())
933
989
}
0 commit comments