@@ -27,6 +27,7 @@ package shard
27
27
import (
28
28
"context"
29
29
"errors"
30
+ "fmt"
30
31
"math/rand"
31
32
"testing"
32
33
"time"
@@ -36,6 +37,7 @@ import (
36
37
"github.com/stretchr/testify/suite"
37
38
38
39
persistencespb "go.temporal.io/server/api/persistence/v1"
40
+ "go.temporal.io/server/common/backoff"
39
41
"go.temporal.io/server/common/clock"
40
42
"go.temporal.io/server/common/cluster"
41
43
"go.temporal.io/server/common/convert"
53
55
* require.Assertions
54
56
55
57
controller * gomock.Controller
56
- mockShard Context
58
+ mockShard * ContextTest
57
59
mockClusterMetadata * cluster.MockMetadata
58
60
mockShardManager * persistence.MockShardManager
59
61
mockExecutionManager * persistence.MockExecutionManager
@@ -157,7 +159,7 @@ func (s *contextSuite) TestTimerMaxReadLevelInitialization() {
157
159
)
158
160
159
161
// clear shardInfo and load from persistence
160
- shardContextImpl := s .mockShard .( * ContextTest )
162
+ shardContextImpl := s .mockShard
161
163
shardContextImpl .shardInfo = nil
162
164
err := shardContextImpl .loadShardMetadata (convert .BoolPtr (false ))
163
165
s .NoError (err )
@@ -211,7 +213,7 @@ func (s *contextSuite) TestTimerMaxReadLevelUpdate_SingleProcessor() {
211
213
212
214
// update in single processor mode
213
215
s .mockShard .UpdateScheduledQueueExclusiveHighReadWatermark (cluster .TestCurrentClusterName , true )
214
- scheduledTaskMaxReadLevelMap := s .mockShard .( * ContextTest ). scheduledTaskMaxReadLevelMap
216
+ scheduledTaskMaxReadLevelMap := s .mockShard .scheduledTaskMaxReadLevelMap
215
217
s .Len (scheduledTaskMaxReadLevelMap , 2 )
216
218
s .True (scheduledTaskMaxReadLevelMap [cluster .TestCurrentClusterName ].After (now ))
217
219
s .True (scheduledTaskMaxReadLevelMap [cluster .TestAlternativeClusterName ].After (now ))
@@ -365,3 +367,56 @@ func (s *contextSuite) TestDeleteWorkflowExecution_ErrorAndContinue_Success() {
365
367
s .NoError (err )
366
368
s .Equal (tasks .DeleteWorkflowExecutionStageCurrent | tasks .DeleteWorkflowExecutionStageMutableState | tasks .DeleteWorkflowExecutionStageVisibility | tasks .DeleteWorkflowExecutionStageHistory , stage )
367
369
}
370
+
371
+ func (s * contextSuite ) TestAcquireShardOwnershipLostErrorIsNotRetried () {
372
+ s .mockShard .state = contextStateAcquiring
373
+ s .mockShard .acquireShardRetryPolicy = backoff .NewExponentialRetryPolicy (time .Nanosecond ).
374
+ WithMaximumAttempts (5 )
375
+ s .mockShardManager .EXPECT ().UpdateShard (gomock .Any (), gomock .Any ()).
376
+ Return (& persistence.ShardOwnershipLostError {}).Times (1 )
377
+
378
+ s .mockShard .acquireShard ()
379
+
380
+ s .Assert ().Equal (contextStateStopping , s .mockShard .state )
381
+ }
382
+
383
+ func (s * contextSuite ) TestAcquireShardNonOwnershipLostErrorIsRetried () {
384
+ s .mockShard .state = contextStateAcquiring
385
+ s .mockShard .acquireShardRetryPolicy = backoff .NewExponentialRetryPolicy (time .Nanosecond ).
386
+ WithMaximumAttempts (5 )
387
+ // TODO: make this 5 times instead of 6 when retry policy is fixed
388
+ s .mockShardManager .EXPECT ().UpdateShard (gomock .Any (), gomock .Any ()).
389
+ Return (fmt .Errorf ("temp error" )).Times (6 )
390
+
391
+ s .mockShard .acquireShard ()
392
+
393
+ s .Assert ().Equal (contextStateStopping , s .mockShard .state )
394
+ }
395
+
396
+ func (s * contextSuite ) TestAcquireShardEventuallySucceeds () {
397
+ s .mockShard .state = contextStateAcquiring
398
+ s .mockShard .acquireShardRetryPolicy = backoff .NewExponentialRetryPolicy (time .Nanosecond ).
399
+ WithMaximumAttempts (5 )
400
+ s .mockShardManager .EXPECT ().UpdateShard (gomock .Any (), gomock .Any ()).
401
+ Return (fmt .Errorf ("temp error" )).Times (3 )
402
+ s .mockShardManager .EXPECT ().UpdateShard (gomock .Any (), gomock .Any ()).
403
+ Return (nil ).Times (1 )
404
+ s .mockHistoryEngine .EXPECT ().NotifyNewTasks (gomock .Any (), gomock .Any ()).MinTimes (1 )
405
+
406
+ s .mockShard .acquireShard ()
407
+
408
+ s .Assert ().Equal (contextStateAcquired , s .mockShard .state )
409
+ }
410
+
411
+ func (s * contextSuite ) TestAcquireShardNoError () {
412
+ s .mockShard .state = contextStateAcquiring
413
+ s .mockShard .acquireShardRetryPolicy = backoff .NewExponentialRetryPolicy (time .Nanosecond ).
414
+ WithMaximumAttempts (5 )
415
+ s .mockShardManager .EXPECT ().UpdateShard (gomock .Any (), gomock .Any ()).
416
+ Return (nil ).Times (1 )
417
+ s .mockHistoryEngine .EXPECT ().NotifyNewTasks (gomock .Any (), gomock .Any ()).MinTimes (1 )
418
+
419
+ s .mockShard .acquireShard ()
420
+
421
+ s .Assert ().Equal (contextStateAcquired , s .mockShard .state )
422
+ }
0 commit comments