@@ -24,6 +24,7 @@ import (
24
24
"math"
25
25
"math/rand"
26
26
"strings"
27
+ "sync"
27
28
"sync/atomic"
28
29
"time"
29
30
65
66
MetadataManager MetadataManager
66
67
VisibilityMgr VisibilityManager
67
68
ShardInfo * ShardInfo
68
- ShardContext * testShardContext
69
+ ShardContext * TestShardContext
69
70
readLevel int64
70
71
CassandraTestCluster
71
72
}
@@ -77,10 +78,12 @@ type (
77
78
session * gocql.Session
78
79
}
79
80
80
- testShardContext struct {
81
+ // TestShardContext shard context for testing.
82
+ // TODO: Cleanup, move this out of persistence
83
+ TestShardContext struct {
84
+ sync.RWMutex
81
85
shardInfo * ShardInfo
82
86
transferSequenceNumber int64
83
- timerSequeceNumber int64
84
87
historyMgr HistoryManager
85
88
executionMgr ExecutionManager
86
89
logger bark.Logger
95
98
)
96
99
97
100
func newTestShardContext (shardInfo * ShardInfo , transferSequenceNumber int64 , historyMgr HistoryManager ,
98
- executionMgr ExecutionManager , logger bark.Logger ) * testShardContext {
99
- return & testShardContext {
101
+ executionMgr ExecutionManager , logger bark.Logger ) * TestShardContext {
102
+ return & TestShardContext {
100
103
shardInfo : shardInfo ,
101
104
transferSequenceNumber : transferSequenceNumber ,
102
105
historyMgr : historyMgr ,
@@ -106,66 +109,102 @@ func newTestShardContext(shardInfo *ShardInfo, transferSequenceNumber int64, his
106
109
}
107
110
}
108
111
109
- func (s * testShardContext ) GetExecutionManager () ExecutionManager {
112
+ // GetExecutionManager test implementation
113
+ func (s * TestShardContext ) GetExecutionManager () ExecutionManager {
110
114
return s .executionMgr
111
115
}
112
116
113
- func (s * testShardContext ) GetHistoryManager () HistoryManager {
117
+ // GetHistoryManager test implementation
118
+ func (s * TestShardContext ) GetHistoryManager () HistoryManager {
114
119
return s .historyMgr
115
120
}
116
121
117
- func (s * testShardContext ) GetNextTransferTaskID () (int64 , error ) {
122
+ // GetNextTransferTaskID test implementation
123
+ func (s * TestShardContext ) GetNextTransferTaskID () (int64 , error ) {
118
124
return atomic .AddInt64 (& s .transferSequenceNumber , 1 ), nil
119
125
}
120
126
121
- func (s * testShardContext ) GetTransferMaxReadLevel () int64 {
127
+ // GetTransferMaxReadLevel test implementation
128
+ func (s * TestShardContext ) GetTransferMaxReadLevel () int64 {
122
129
return atomic .LoadInt64 (& s .transferSequenceNumber )
123
130
}
124
131
125
- func (s * testShardContext ) GetTransferAckLevel () int64 {
132
+ // GetTransferAckLevel test implementation
133
+ func (s * TestShardContext ) GetTransferAckLevel () int64 {
126
134
return atomic .LoadInt64 (& s .shardInfo .TransferAckLevel )
127
135
}
128
136
129
- func (s * testShardContext ) GetTimerSequenceNumber () int64 {
130
- return atomic .AddInt64 (& s .timerSequeceNumber , 1 )
131
- }
132
-
133
- func (s * testShardContext ) UpdateAckLevel (ackLevel int64 ) error {
137
+ // UpdateTransferAckLevel test implementation
138
+ func (s * TestShardContext ) UpdateTransferAckLevel (ackLevel int64 ) error {
134
139
atomic .StoreInt64 (& s .shardInfo .TransferAckLevel , ackLevel )
135
140
return nil
136
141
}
137
142
138
- func (s * testShardContext ) GetTransferSequenceNumber () int64 {
143
+ // GetTransferSequenceNumber test implementation
144
+ func (s * TestShardContext ) GetTransferSequenceNumber () int64 {
139
145
return atomic .LoadInt64 (& s .transferSequenceNumber )
140
146
}
141
147
142
- func (s * testShardContext ) CreateWorkflowExecution (request * CreateWorkflowExecutionRequest ) (
148
+ // GetTimerAckLevel test implementation
149
+ func (s * TestShardContext ) GetTimerAckLevel () time.Time {
150
+ s .RLock ()
151
+ defer s .RLock ()
152
+ return s .shardInfo .TimerAckLevel
153
+ }
154
+
155
+ // UpdateTimerAckLevel test implementation
156
+ func (s * TestShardContext ) UpdateTimerAckLevel (ackLevel time.Time ) error {
157
+ s .Lock ()
158
+ defer s .Unlock ()
159
+ s .shardInfo .TimerAckLevel = ackLevel
160
+ return nil
161
+ }
162
+
163
+ // CreateWorkflowExecution test implementation
164
+ func (s * TestShardContext ) CreateWorkflowExecution (request * CreateWorkflowExecutionRequest ) (
143
165
* CreateWorkflowExecutionResponse , error ) {
144
166
return s .executionMgr .CreateWorkflowExecution (request )
145
167
}
146
168
147
- func (s * testShardContext ) UpdateWorkflowExecution (request * UpdateWorkflowExecutionRequest ) error {
169
+ // UpdateWorkflowExecution test implementation
170
+ func (s * TestShardContext ) UpdateWorkflowExecution (request * UpdateWorkflowExecutionRequest ) error {
171
+ // assign IDs for the timer tasks. They need to be assigned under shard lock.
172
+ // TODO: This needs to be moved out of persistence.
173
+ for _ , task := range request .TimerTasks {
174
+ seqID , err := s .GetNextTransferTaskID ()
175
+ if err != nil {
176
+ panic (err )
177
+ }
178
+ task .SetTaskID (seqID )
179
+ s .logger .Infof ("%v: TestShardContext: Assigning timer (timestamp: %v, seq: %v)" ,
180
+ time .Now ().UTC (), GetVisibilityTSFrom (task ).UTC (), task .GetTaskID ())
181
+ }
148
182
return s .executionMgr .UpdateWorkflowExecution (request )
149
183
}
150
184
151
- func (s * testShardContext ) AppendHistoryEvents (request * AppendHistoryEventsRequest ) error {
185
+ // AppendHistoryEvents test implementation
186
+ func (s * TestShardContext ) AppendHistoryEvents (request * AppendHistoryEventsRequest ) error {
152
187
return s .historyMgr .AppendHistoryEvents (request )
153
188
}
154
189
155
- func (s * testShardContext ) GetLogger () bark.Logger {
190
+ // GetLogger test implementation
191
+ func (s * TestShardContext ) GetLogger () bark.Logger {
156
192
return s .logger
157
193
}
158
194
159
- func (s * testShardContext ) GetMetricsClient () metrics.Client {
195
+ // GetMetricsClient test implementation
196
+ func (s * TestShardContext ) GetMetricsClient () metrics.Client {
160
197
return s .metricsClient
161
198
}
162
199
163
- func (s * testShardContext ) Reset () {
200
+ // Reset test implementation
201
+ func (s * TestShardContext ) Reset () {
164
202
atomic .StoreInt64 (& s .shardInfo .RangeID , 0 )
165
203
atomic .StoreInt64 (& s .shardInfo .TransferAckLevel , 0 )
166
204
}
167
205
168
- func (s * testShardContext ) GetRangeID () int64 {
206
+ // GetRangeID test implementation
207
+ func (s * TestShardContext ) GetRangeID () int64 {
169
208
return atomic .LoadInt64 (& s .shardInfo .RangeID )
170
209
}
171
210
@@ -598,9 +637,11 @@ func (s *TestBase) CompleteTransferTask(taskID int64) error {
598
637
}
599
638
600
639
// GetTimerIndexTasks is a utility method to get tasks from transfer task queue
601
- func (s * TestBase ) GetTimerIndexTasks (minKey int64 , maxKey int64 ) ([]* TimerTaskInfo , error ) {
640
+ func (s * TestBase ) GetTimerIndexTasks () ([]* TimerTaskInfo , error ) {
602
641
response , err := s .WorkflowMgr .GetTimerIndexTasks (& GetTimerIndexTasksRequest {
603
- MinKey : minKey , MaxKey : maxKey , BatchSize : 10 })
642
+ MinTimestamp : time.Time {},
643
+ MaxTimestamp : time .Unix (0 , math .MaxInt64 ),
644
+ BatchSize : 10 })
604
645
605
646
if err != nil {
606
647
return nil , err
@@ -609,6 +650,14 @@ func (s *TestBase) GetTimerIndexTasks(minKey int64, maxKey int64) ([]*TimerTaskI
609
650
return response .Timers , nil
610
651
}
611
652
653
+ // CompleteTimerTask is a utility method to complete a timer task
654
+ func (s * TestBase ) CompleteTimerTask (ts time.Time , taskID int64 ) error {
655
+ return s .WorkflowMgr .CompleteTimerTask (& CompleteTimerTaskRequest {
656
+ VisibilityTimestamp : ts ,
657
+ TaskID : taskID ,
658
+ })
659
+ }
660
+
612
661
// CreateDecisionTask is a utility method to create a task
613
662
func (s * TestBase ) CreateDecisionTask (domainID string , workflowExecution workflow.WorkflowExecution , taskList string ,
614
663
decisionScheduleID int64 ) (int64 , error ) {
@@ -623,7 +672,7 @@ func (s *TestBase) CreateDecisionTask(domainID string, workflowExecution workflo
623
672
624
673
taskID := s .GetNextSequenceNumber ()
625
674
tasks := []* CreateTaskInfo {
626
- & CreateTaskInfo {
675
+ {
627
676
TaskID : taskID ,
628
677
Execution : workflowExecution ,
629
678
Data : & TaskInfo {
@@ -667,7 +716,7 @@ func (s *TestBase) CreateActivityTasks(domainID string, workflowExecution workfl
667
716
}
668
717
taskID := s .GetNextSequenceNumber ()
669
718
tasks := []* CreateTaskInfo {
670
- & CreateTaskInfo {
719
+ {
671
720
TaskID : taskID ,
672
721
Execution : workflowExecution ,
673
722
Data : & TaskInfo {
0 commit comments