@@ -1826,9 +1826,6 @@ func (s *matchingEngineSuite) TestForceUnloadTaskQueue() {
18261826}
18271827
18281828func (s * matchingEngineSuite ) TestMultipleEnginesActivitiesRangeStealing () {
1829- if s .newMatcher {
1830- s .T ().Skip ("test is flaky with new matcher" )
1831- }
18321829 runID := uuid .NewString ()
18331830 workflowID := "workflow1"
18341831 workflowExecution := & commonpb.WorkflowExecution {RunId : runID , WorkflowId : workflowID }
@@ -1923,10 +1920,12 @@ func (s *matchingEngineSuite) TestMultipleEnginesActivitiesRangeStealing() {
19231920 }),
19241921 }, nil
19251922 }).AnyTimes ()
1923+ // If tasks are lost, we'd poll forever. Give up after a deadline and check below.
1924+ pollDeadline := time .Now ().Add (30 * time .Second )
19261925 for j := 0 ; j < iterations ; j ++ {
19271926 for p := 0 ; p < engineCount ; p ++ {
19281927 engine := engines [p ]
1929- for i := int64 (0 ); i < taskCount ; /* incremented explicitly to skip empty polls */ {
1928+ for i := int64 (0 ); i < taskCount && time . Now (). Before ( pollDeadline ) ; /* incremented explicitly to skip empty polls */ {
19301929 result , err := engine .PollActivityTaskQueue (context .Background (), & matchingservice.PollActivityTaskQueueRequest {
19311930 NamespaceId : namespaceID ,
19321931 PollRequest : & workflowservice.PollActivityTaskQueueRequest {
@@ -1971,8 +1970,12 @@ func (s *matchingEngineSuite) TestMultipleEnginesActivitiesRangeStealing() {
19711970 e .Stop ()
19721971 }
19731972
1974- s .EqualValues (0 , s .taskManager .getTaskCount (tlID ))
19751973 totalTasks := taskCount * engineCount * iterations
1974+ s .Len (startedTasks , totalTasks , "some tasks were never dispatched" )
1975+ if ! s .newMatcher {
1976+ // new matcher does gc lazily so some acked tasks may remain
1977+ s .EqualValues (0 , s .taskManager .getTaskCount (tlID ))
1978+ }
19761979 persisted := s .taskManager .getCreateTaskCount (tlID )
19771980 // No sync matching as all messages are published first
19781981 s .EqualValues (totalTasks , persisted )
@@ -1982,9 +1985,6 @@ func (s *matchingEngineSuite) TestMultipleEnginesActivitiesRangeStealing() {
19821985}
19831986
19841987func (s * matchingEngineSuite ) TestMultipleEnginesWorkflowTasksRangeStealing () {
1985- if s .newMatcher {
1986- s .T ().Skip ("test is flaky with new matcher" )
1987- }
19881988 runID := uuid .NewString ()
19891989 workflowID := "workflow1"
19901990 workflowExecution := & commonpb.WorkflowExecution {RunId : runID , WorkflowId : workflowID }
@@ -2068,10 +2068,12 @@ func (s *matchingEngineSuite) TestMultipleEnginesWorkflowTasksRangeStealing() {
20682068 }, nil
20692069 }).AnyTimes ()
20702070
2071+ // If tasks are lost, we'd poll forever. Give up after a deadline and check below.
2072+ pollDeadline := time .Now ().Add (30 * time .Second )
20712073 for j := 0 ; j < iterations ; j ++ {
20722074 for p := 0 ; p < engineCount ; p ++ {
20732075 engine := engines [p ]
2074- for i := int64 (0 ); i < taskCount ; /* incremented explicitly to skip empty polls */ {
2076+ for i := int64 (0 ); i < taskCount && time . Now (). Before ( pollDeadline ) ; /* incremented explicitly to skip empty polls */ {
20752077 result , err := engine .PollWorkflowTaskQueue (context .Background (), & matchingservice.PollWorkflowTaskQueueRequest {
20762078 NamespaceId : namespaceID ,
20772079 PollRequest : & workflowservice.PollWorkflowTaskQueueRequest {
@@ -2115,8 +2117,12 @@ func (s *matchingEngineSuite) TestMultipleEnginesWorkflowTasksRangeStealing() {
21152117 e .Stop ()
21162118 }
21172119
2118- s .EqualValues (0 , s .taskManager .getTaskCount (tlID ))
21192120 totalTasks := taskCount * engineCount * iterations
2121+ s .Len (startedTasks , totalTasks , "some tasks were never dispatched" )
2122+ if ! s .newMatcher {
2123+ // new matcher does gc lazily so some acked tasks may remain
2124+ s .EqualValues (0 , s .taskManager .getTaskCount (tlID ))
2125+ }
21202126 persisted := s .taskManager .getCreateTaskCount (tlID )
21212127 // No sync matching as all messages are published first
21222128 s .EqualValues (totalTasks , persisted )
@@ -3476,8 +3482,18 @@ func (s *matchingEngineSuite) concurrentPublishAndConsumeValidateBacklogCounter(
34763482 wg .Wait ()
34773483
34783484 ptqMgr := s .getPhysicalTaskQueueManagerImplFromKey (ptq )
3479- dbTasks := int64 (s .taskManager .getTaskCount (ptq ))
34803485 backlogCount := totalApproximateBacklogCount (ptqMgr .backlogMgr )
3486+
3487+ // Only count tasks above the ack level: acked tasks may not have been gc'ed yet.
3488+ db := ptqMgr .backlogMgr .getDB ()
3489+ db .Lock ()
3490+ ackLevel := fairLevel {id : db .subqueues [subqueueZero ].AckLevel }
3491+ if s .fairness {
3492+ ackLevel = fairLevelFromProto (db .subqueues [subqueueZero ].FairAckLevel )
3493+ }
3494+ db .Unlock ()
3495+ dbTasks := int64 (s .taskManager .getTaskCountAbove (ptq , ackLevel ))
3496+
34813497 if s .fairness {
34823498 // Relax this condition for fairBacklogManager: it can sometimes reset backlog count on
34833499 // read, making it more accurate in theory, but breaking this test's assumptions.
@@ -4920,6 +4936,20 @@ func (m *testTaskManager) minTaskID(dbq *PhysicalTaskQueueKey) (int64, bool) {
49204936 return key .id , ok
49214937}
49224938
4939+ // getTaskCountAbove returns the number of tasks in a task queue above the given level.
4940+ func (m * testTaskManager ) getTaskCountAbove (q * PhysicalTaskQueueKey , level fairLevel ) int {
4941+ tlm := m .getQueueDataByKey (q )
4942+ tlm .Lock ()
4943+ defer tlm .Unlock ()
4944+ count := 0
4945+ for _ , k := range tlm .tasks .Keys () {
4946+ if level .less (k .(fairLevel )) {
4947+ count ++
4948+ }
4949+ }
4950+ return count
4951+ }
4952+
49234953// maxTaskID returns the maximum value of the TaskID present in testTaskManager
49244954func (m * testTaskManager ) maxTaskID (dbq * PhysicalTaskQueueKey ) (int64 , bool ) {
49254955 tlm := m .getQueueDataByKey (dbq )
@@ -4936,7 +4966,7 @@ func (m *testTaskManager) CompleteTasksLessThan(
49364966) (int , error ) {
49374967 if m .fairness && request .ExclusiveMaxPass < 1 {
49384968 return 0 , serviceerror .NewInternal ("invalid CompleteTasksLessThan request on fair queue" )
4939- } else if ! m .fairness && request .ExclusiveMaxPass != 0 {
4969+ } else if ! m .fairness && ( request .ExclusiveMaxPass != 0 || request . ConditionRangeID != 0 ) {
49404970 return 0 , serviceerror .NewInternal ("invalid CompleteTasksLessThan request on queue" )
49414971 }
49424972
@@ -4946,6 +4976,12 @@ func (m *testTaskManager) CompleteTasksLessThan(
49464976 tlm := m .getQueueData (request .TaskQueueName , request .NamespaceID , request .TaskType )
49474977 tlm .Lock ()
49484978 defer tlm .Unlock ()
4979+ if request .ConditionRangeID != 0 && tlm .rangeID != request .ConditionRangeID {
4980+ return 0 , & persistence.ConditionFailedError {
4981+ Msg : fmt .Sprintf ("CompleteTasksLessThan failed, range id mismatch. rangeID: %v, db rangeID: %v" ,
4982+ request .ConditionRangeID , tlm .rangeID ),
4983+ }
4984+ }
49494985 keys := tlm .tasks .Keys ()
49504986 for _ , key := range keys {
49514987 level := key .(fairLevel )
0 commit comments