@@ -39,6 +39,7 @@ import (
39
39
"go.temporal.io/server/api/historyservice/v1"
40
40
"go.temporal.io/server/common"
41
41
"go.temporal.io/server/common/clock"
42
+ "go.temporal.io/server/common/metrics"
42
43
"go.temporal.io/server/common/namespace"
43
44
"go.temporal.io/server/common/primitives/timestamp"
44
45
)
90
91
91
92
// scheduled to started event ID mapping
92
93
scheduledIDToStartedID map [int64 ]int64
94
+
95
+ metricsHandler metrics.Handler
93
96
}
94
97
)
95
98
@@ -99,6 +102,7 @@ func NewMutableHistoryBuilder(
99
102
version int64 ,
100
103
nextEventID int64 ,
101
104
dbBufferBatch []* historypb.HistoryEvent ,
105
+ metricsHandler metrics.Handler ,
102
106
) * HistoryBuilder {
103
107
return & HistoryBuilder {
104
108
state : HistoryBuilderStateMutable ,
@@ -116,6 +120,8 @@ func NewMutableHistoryBuilder(
116
120
memLatestBatch : nil ,
117
121
memBufferBatch : nil ,
118
122
scheduledIDToStartedID : make (map [int64 ]int64 ),
123
+
124
+ metricsHandler : metricsHandler ,
119
125
}
120
126
}
121
127
@@ -139,6 +145,8 @@ func NewImmutableHistoryBuilder(
139
145
memLatestBatch : history ,
140
146
memBufferBatch : nil ,
141
147
scheduledIDToStartedID : nil ,
148
+
149
+ metricsHandler : nil ,
142
150
}
143
151
}
144
152
@@ -1401,6 +1409,7 @@ func (b *HistoryBuilder) wireEventIDs(
1401
1409
func (b * HistoryBuilder ) reorderBuffer (
1402
1410
bufferEvents []* historypb.HistoryEvent ,
1403
1411
) []* historypb.HistoryEvent {
1412
+ b .emitInorderedBufferedEvents (bufferEvents )
1404
1413
reorderBuffer := make ([]* historypb.HistoryEvent , 0 , len (bufferEvents ))
1405
1414
reorderEvents := make ([]* historypb.HistoryEvent , 0 , len (bufferEvents ))
1406
1415
for _ , event := range bufferEvents {
@@ -1423,6 +1432,47 @@ func (b *HistoryBuilder) reorderBuffer(
1423
1432
return append (reorderEvents , reorderBuffer ... )
1424
1433
}
1425
1434
1435
+ func (b * HistoryBuilder ) emitInorderedBufferedEvents (bufferedEvents []* historypb.HistoryEvent ) {
1436
+ completedActivities := make (map [int64 ]struct {})
1437
+ completedChildWorkflows := make (map [int64 ]struct {})
1438
+ var inorderedEventsCount int64
1439
+ for _ , event := range bufferedEvents {
1440
+ switch event .GetEventType () {
1441
+ case enumspb .EVENT_TYPE_ACTIVITY_TASK_STARTED :
1442
+ if _ , seenCompleted := completedActivities [event .GetEventId ()]; seenCompleted {
1443
+ inorderedEventsCount ++
1444
+ }
1445
+ case enumspb .EVENT_TYPE_ACTIVITY_TASK_COMPLETED :
1446
+ completedActivities [event .GetActivityTaskCompletedEventAttributes ().GetStartedEventId ()] = struct {}{}
1447
+ case enumspb .EVENT_TYPE_ACTIVITY_TASK_FAILED :
1448
+ completedActivities [event .GetActivityTaskFailedEventAttributes ().GetStartedEventId ()] = struct {}{}
1449
+ case enumspb .EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT :
1450
+ completedActivities [event .GetActivityTaskTimedOutEventAttributes ().GetStartedEventId ()] = struct {}{}
1451
+ case enumspb .EVENT_TYPE_ACTIVITY_TASK_CANCELED :
1452
+ completedActivities [event .GetActivityTaskCanceledEventAttributes ().GetStartedEventId ()] = struct {}{}
1453
+
1454
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED :
1455
+ if _ , seenCompleted := completedChildWorkflows [event .GetEventId ()]; seenCompleted {
1456
+ inorderedEventsCount ++
1457
+ }
1458
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_COMPLETED :
1459
+ completedChildWorkflows [event .GetChildWorkflowExecutionCompletedEventAttributes ().GetStartedEventId ()] = struct {}{}
1460
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_FAILED :
1461
+ completedChildWorkflows [event .GetChildWorkflowExecutionFailedEventAttributes ().GetStartedEventId ()] = struct {}{}
1462
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TIMED_OUT :
1463
+ completedChildWorkflows [event .GetChildWorkflowExecutionTimedOutEventAttributes ().GetStartedEventId ()] = struct {}{}
1464
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_CANCELED :
1465
+ completedChildWorkflows [event .GetChildWorkflowExecutionCanceledEventAttributes ().GetStartedEventId ()] = struct {}{}
1466
+ case enumspb .EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_TERMINATED :
1467
+ completedChildWorkflows [event .GetChildWorkflowExecutionTerminatedEventAttributes ().GetStartedEventId ()] = struct {}{}
1468
+ }
1469
+ }
1470
+
1471
+ if inorderedEventsCount > 0 && b .metricsHandler != nil {
1472
+ b .metricsHandler .Counter (metrics .InorderBufferedEventsCounter .GetMetricName ()).Record (inorderedEventsCount )
1473
+ }
1474
+ }
1475
+
1426
1476
func (b * HistoryBuilder ) HasActivityFinishEvent (
1427
1477
scheduledEventID int64 ,
1428
1478
) bool {
0 commit comments