@@ -123,10 +123,12 @@ func TestSchedule_Assign(t *testing.T) {
123
123
}
124
124
125
125
scheduler := NewScheduler (config , store , nil )
126
+ // The job plans are accessed when it's getting assigned.
127
+ // Their content is not important for the test.
126
128
plans := []* raft_log.CompactionJobPlan {
127
- {Name : "2" , Tenant : "A" , Shard : 1 , CompactionLevel : 0 , SourceBlocks : [] string { "d" , "e" , "f" } },
128
- {Name : "3" , Tenant : "A" , Shard : 1 , CompactionLevel : 0 , SourceBlocks : [] string { "j" , "h" , "i" } },
129
- {Name : "1" , Tenant : "A" , Shard : 1 , CompactionLevel : 1 , SourceBlocks : [] string { "a" , "b" , "c" } },
129
+ {Name : "2" , CompactionLevel : 0 },
130
+ {Name : "3" , CompactionLevel : 0 },
131
+ {Name : "1" , CompactionLevel : 1 },
130
132
}
131
133
for _ , p := range plans {
132
134
store .On ("GetJobPlan" , mock .Anything , p .Name ).Return (p , nil )
@@ -169,37 +171,55 @@ func TestSchedule_ReAssign(t *testing.T) {
169
171
170
172
scheduler := NewScheduler (config , store , nil )
171
173
plans := []* raft_log.CompactionJobPlan {
172
- {Name : "1" , Tenant : "A" , Shard : 1 , SourceBlocks : []string {"a" , "b" , "c" }},
173
- {Name : "2" , Tenant : "A" , Shard : 1 , SourceBlocks : []string {"d" , "e" , "f" }},
174
- {Name : "3" , Tenant : "A" , Shard : 1 , SourceBlocks : []string {"j" , "h" , "i" }},
174
+ {Name : "1" },
175
+ {Name : "2" },
176
+ {Name : "3" },
177
+ {Name : "4" },
178
+ {Name : "5" },
179
+ {Name : "6" },
175
180
}
176
181
for _ , p := range plans {
177
182
store .On ("GetJobPlan" , mock .Anything , p .Name ).Return (p , nil )
178
183
}
179
184
185
+ now := int64 (5 )
180
186
states := []* raft_log.CompactionJobState {
181
- {Name : "1" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 0 },
182
- {Name : "2" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 0 },
183
- {Name : "3" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 0 },
187
+ // Jobs with expired leases (now > LeaseExpiresAt).
188
+ {Name : "1" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 1 },
189
+ {Name : "2" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 1 },
190
+ {Name : "3" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 1 },
191
+ // This job can't be reassigned as its lease is still valid.
192
+ {Name : "4" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 10 },
193
+ // The job has already failed in the past.
194
+ {Name : "5" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 1 , Failures : 1 },
195
+ // The job has already failed in the past and exceeded the error threshold.
196
+ {Name : "6" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 1 , LeaseExpiresAt : 1 , Failures : 3 },
184
197
}
185
198
for _ , s := range states {
186
199
scheduler .queue .put (s )
187
200
}
188
201
202
+ lease := now + int64 (config .LeaseDuration )
203
+ expected := []* raft_log.CompactionJobState {
204
+ {Name : "1" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 2 , LeaseExpiresAt : lease , Failures : 1 },
205
+ {Name : "2" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 2 , LeaseExpiresAt : lease , Failures : 1 },
206
+ {Name : "3" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 2 , LeaseExpiresAt : lease , Failures : 1 },
207
+ {Name : "5" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , Token : 2 , LeaseExpiresAt : lease , Failures : 2 },
208
+ }
209
+
189
210
test .AssertIdempotent (t , func (t * testing.T ) {
190
- s := scheduler .NewSchedule (nil , & raft.Log {Index : 2 , AppendedAt : time .Unix (0 , 1 )})
191
- for j := range plans {
211
+ s := scheduler .NewSchedule (nil , & raft.Log {Index : 2 , AppendedAt : time .Unix (0 , now )})
212
+ assigned := make ([]* raft_log.CompactionJobState , 0 , len (expected ))
213
+ for {
192
214
update , err := s .AssignJob ()
193
215
require .NoError (t , err )
194
- assert . Equal ( t , plans [ j ], update . Plan )
195
- assert . Equal ( t , metastorev1 . CompactionJobStatus_COMPACTION_STATUS_IN_PROGRESS , update . State . Status )
196
- assert . Equal ( t , int64 ( config . LeaseDuration ) + 1 , update . State . LeaseExpiresAt )
197
- assert . Equal ( t , uint64 ( 2 ) , update .State . Token )
216
+ if update == nil {
217
+ break
218
+ }
219
+ assigned = append ( assigned , update .State )
198
220
}
199
221
200
- update , err := s .AssignJob ()
201
- require .NoError (t , err )
202
- assert .Nil (t , update )
222
+ assert .Equal (t , expected , assigned )
203
223
})
204
224
}
205
225
@@ -212,9 +232,9 @@ func TestSchedule_UpdateAssign(t *testing.T) {
212
232
213
233
scheduler := NewScheduler (config , store , nil )
214
234
plans := []* raft_log.CompactionJobPlan {
215
- {Name : "1" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "a" , "b" , "c" } },
216
- {Name : "2" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "d" , "e" , "f" } },
217
- {Name : "3" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "j" , "h" , "i" } },
235
+ {Name : "1" },
236
+ {Name : "2" },
237
+ {Name : "3" },
218
238
}
219
239
for _ , p := range plans {
220
240
store .On ("GetJobPlan" , mock .Anything , p .Name ).Return (p , nil )
@@ -301,9 +321,9 @@ func TestSchedule_Add(t *testing.T) {
301
321
302
322
scheduler := NewScheduler (config , store , nil )
303
323
plans := []* raft_log.CompactionJobPlan {
304
- {Name : "1" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "a" , "b" , "c" } },
305
- {Name : "2" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "d" , "e" , "f" } },
306
- {Name : "3" , Tenant : "A" , Shard : 1 , SourceBlocks : [] string { "j" , "h" , "i" } },
324
+ {Name : "1" },
325
+ {Name : "2" },
326
+ {Name : "3" },
307
327
}
308
328
309
329
states := []* raft_log.CompactionJobState {
@@ -319,3 +339,31 @@ func TestSchedule_Add(t *testing.T) {
319
339
}
320
340
})
321
341
}
342
+
343
+ func TestSchedule_QueueSizeLimit (t * testing.T ) {
344
+ store := new (mockscheduler.MockJobStore )
345
+ config := Config {
346
+ MaxFailures : 3 ,
347
+ LeaseDuration : 10 * time .Second ,
348
+ MaxQueueSize : 2 ,
349
+ }
350
+
351
+ scheduler := NewScheduler (config , store , nil )
352
+ plans := []* raft_log.CompactionJobPlan {
353
+ {Name : "1" },
354
+ {Name : "2" },
355
+ {Name : "3" },
356
+ }
357
+
358
+ states := []* raft_log.CompactionJobState {
359
+ {Name : "1" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_UNSPECIFIED , AddedAt : 1 , Token : 1 },
360
+ {Name : "2" , Status : metastorev1 .CompactionJobStatus_COMPACTION_STATUS_UNSPECIFIED , AddedAt : 1 , Token : 1 },
361
+ }
362
+
363
+ test .AssertIdempotent (t , func (t * testing.T ) {
364
+ s := scheduler .NewSchedule (nil , & raft.Log {Index : 1 , AppendedAt : time .Unix (0 , 1 )})
365
+ assert .Equal (t , states [0 ], s .AddJob (plans [0 ]))
366
+ assert .Equal (t , states [1 ], s .AddJob (plans [1 ]))
367
+ assert .Nil (t , s .AddJob (plans [2 ]))
368
+ })
369
+ }
0 commit comments