1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package execpool
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)
// implements BatchProcessor interface for testing purposes
type mockBatchProcessor struct {
notify chan struct{} // notify the test that cleanup was called
}
func (mbp *mockBatchProcessor) ProcessBatch(jobs []InputJob) {
for i := range jobs {
job := jobs[i].(*mockJob)
job.processed = true
job.batchSize = len(jobs)
job.batchOrder = i
if job.callback != nil {
job.callback(job.id)
}
}
}
func (mbp *mockBatchProcessor) GetErredUnprocessed(ue InputJob, err error) {
job := ue.(*mockJob)
job.returnError = err
}
func (mbp *mockBatchProcessor) Cleanup(ue []InputJob, err error) {
for i := range ue {
mbp.GetErredUnprocessed(ue[i], err)
}
if mbp.notify != nil && len(ue) > 0 {
mbp.notify <- struct{}{}
}
}
// implements InputJob interface
type mockJob struct {
id int
numberOfItems uint64
jobError error
returnError error
processed bool
batchSize int
batchOrder int
callback func(id int)
}
func (mj *mockJob) GetNumberOfBatchableItems() (count uint64, err error) {
return mj.numberOfItems, mj.jobError
}
type mockPool struct {
pool
hold chan struct{} // used to sync the EnqueueBacklog call with the test
err error // when not nil, EnqueueBacklog will return the err instead of executing the task
poolCapacity chan struct{} // mimics the pool capacity which blocks EnqueueBacklog
asyncDelay chan struct{} // used to control when the task gets executed after EnqueueBacklog queues and returns
}
func (mp *mockPool) EnqueueBacklog(enqueueCtx context.Context, t ExecFunc, arg interface{}, out chan interface{}) error {
// allow the test to know when the exec pool is executing the job
<-mp.hold
// simulate the execution of the job by the pool
if mp.err != nil {
// return the mock error
return mp.err
}
mp.poolCapacity <- struct{}{}
go func() {
mp.asyncDelay <- struct{}{}
t(arg)
}()
return nil
}
func (mp *mockPool) BufferSize() (length, capacity int) {
return len(mp.poolCapacity), cap(mp.poolCapacity)
}
func testStreamToBatchCore(wg *sync.WaitGroup, mockJobs <-chan *mockJob, done <-chan struct{}, t *testing.T) {
defer wg.Done()
ctx, cancel := context.WithCancel(context.Background())
verificationPool := MakeBacklog(nil, 0, LowPriority, t)
defer verificationPool.Shutdown()
inputChan := make(chan InputJob)
mbp := mockBatchProcessor{}
sv := MakeStreamToBatch(inputChan, verificationPool, &mbp)
sv.Start(ctx)
for j := range mockJobs {
inputChan <- j
}
<-done
cancel()
sv.WaitForStop()
}
// TestStreamToBatchBasic tests the basic functionality
func TestStreamToBatchBasic(t *testing.T) {
partitiontest.PartitionTest(t)
numJobs := 400
// for GetNumberOfBatchableItems errors: 400 / 99
numJobsToProcess := 400 - (400/99 + 1)
// processedChan will notify whenn all the jobs are processed
processedChan := make(chan struct{}, numJobsToProcess-1)
done := make(chan struct{})
// callback is needed to know when the processing should stop
callback := func(id int) {
select {
case processedChan <- struct{}{}:
default:
// this was the last job
close(done)
}
}
numError := fmt.Errorf("err on GetNumberOfBatchableItems")
mockJobs := make([]*mockJob, numJobs, numJobs)
for i := 0; i < numJobs; i++ {
mockJobs[i] = &mockJob{
id: i,
// get some jobs with 0 items too
numberOfItems: uint64(i % 5),
callback: callback}
if i%99 == 0 {
// get GetNumberOfBatchableItems to report an error
mockJobs[i].jobError = numError
}
if i%101 == 0 {
// have a batch exceeding batchSizeBlockLimit limit
mockJobs[i].numberOfItems = batchSizeBlockLimit + 1
}
}
jobChan := make(chan *mockJob)
wg := sync.WaitGroup{}
wg.Add(2)
go testStreamToBatchCore(&wg, jobChan, done, t)
go func() {
defer wg.Done()
for i := range mockJobs {
jobChan <- mockJobs[i]
}
close(jobChan)
<-done
}()
wg.Wait()
for i := 0; i < numJobs; i++ {
if i%99 == 0 {
// this should be GetNumberOfBatchableItems
require.ErrorIs(t, mockJobs[i].returnError, numError)
require.False(t, mockJobs[i].processed)
continue
}
if i%5 == 0 {
// this should be processed alone
if 1 != mockJobs[i].batchSize {
require.Equal(t, 1, mockJobs[i].batchSize)
}
}
if i%101 == 0 {
// this should be the last in the batch
require.Equal(t, mockJobs[i].batchSize-1, mockJobs[i].batchOrder)
}
if mockJobs[i].returnError != nil {
require.Nil(t, mockJobs[i].returnError)
}
require.True(t, mockJobs[i].processed)
}
}
// TestNoInputYet let the service start and get to the timeout without any inputs
func TestNoInputYet(t *testing.T) {
partitiontest.PartitionTest(t)
numJobs := 1
done := make(chan struct{})
jobChan := make(chan *mockJob)
wg := sync.WaitGroup{}
wg.Add(1)
go testStreamToBatchCore(&wg, jobChan, done, t)
callback := func(id int) {
if id == numJobs-1 {
close(done)
}
}
// Wait to trigger the timer once with 0 elements
time.Sleep(2 * waitForNextJobDuration)
// send a job, make sure it goes through
mockJob := &mockJob{
numberOfItems: uint64(0),
callback: callback}
jobChan <- mockJob
<-done
require.Nil(t, mockJob.returnError)
require.True(t, mockJob.processed)
require.Equal(t, 1, mockJob.batchSize)
close(jobChan)
wg.Wait()
}
// TestMutipleBatchAttempts tests the behavior when multiple batch attempts will fail and the stream blocks
func TestMutipleBatchAttempts(t *testing.T) {
partitiontest.PartitionTest(t)
mp := mockPool{
hold: make(chan struct{}),
err: nil,
poolCapacity: make(chan struct{}, 1),
asyncDelay: make(chan struct{}, 10),
}
ctx, cancel := context.WithCancel(context.Background())
inputChan := make(chan InputJob)
mbp := mockBatchProcessor{}
sv := MakeStreamToBatch(inputChan, &mp, &mbp)
sv.Start(ctx)
var jobCalled int
jobCalledRef := &jobCalled
callbackFeedback := make(chan struct{})
mj := mockJob{
numberOfItems: uint64(txnPerWorksetThreshold + 1),
id: 1,
callback: func(id int) {
*jobCalledRef = *jobCalledRef + id
<-callbackFeedback
},
}
// first saturate the pool
mp.poolCapacity <- struct{}{}
inputChan <- &mj
// wait for the job to be submitted to the pool
// since this is only a single job with 1 task, and the pool is at capacity,
// this will only happen when the numberOfBatchAttempts == 1
mp.hold <- struct{}{}
// here, the pool is saturated, and the stream should be blocked
select {
case inputChan <- &mj:
require.Fail(t, "the stream should be blocked here")
default:
}
// now let the pool regian capacity
<-mp.poolCapacity
// make sure it is processed before reading the value
callbackFeedback <- struct{}{}
require.Equal(t, 1, jobCalled)
// the stream should be unblocked now
inputChan <- &mj
// let the next job go through
mp.hold <- struct{}{}
// give the pool the capacity for it to process
<-mp.poolCapacity
// make sure it is processed before reading the value
callbackFeedback <- struct{}{}
require.Equal(t, 2, jobCalled)
cancel()
sv.WaitForStop()
}
// TestErrors tests all the cases where exec pool returned error is handled
// by ending the stream processing
func TestErrors(t *testing.T) {
partitiontest.PartitionTest(t)
mp := mockPool{
hold: make(chan struct{}),
err: fmt.Errorf("Test error"),
poolCapacity: make(chan struct{}, 5),
asyncDelay: make(chan struct{}, 10),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
inputChan := make(chan InputJob)
mbp := mockBatchProcessor{}
sv := MakeStreamToBatch(inputChan, &mp, &mbp)
/***************************************************/
// error adding to the pool when numberOfBatchable=0
/***************************************************/
sv.Start(ctx)
mj := mockJob{
numberOfItems: 0,
}
inputChan <- &mj
// let the enqueue pool process and return an error
mp.hold <- struct{}{}
// if errored, should not process the callback on the job
// This is based on the mockPool EnqueueBacklog behavior
require.False(t, mj.processed)
// the service should end
sv.WaitForStop()
/***************************************************/
// error adding to the pool when < txnPerWorksetThreshold
/***************************************************/
// Case where the timer ticks
sv.Start(ctx)
mj.numberOfItems = txnPerWorksetThreshold - 1
inputChan <- &mj
// let the enqueue pool process and return an error
mp.hold <- struct{}{}
require.False(t, mj.processed)
// the service should end
sv.WaitForStop()
/***************************************************/
// error adding to the pool when <= batchSizeBlockLimit
/***************************************************/
// Case where the timer ticks
sv.Start(ctx)
mj.numberOfItems = batchSizeBlockLimit
inputChan <- &mj
// let the enqueue pool process and return an error
mp.hold <- struct{}{}
require.False(t, mj.processed)
// the service should end
sv.WaitForStop()
/***************************************************/
// error adding to the pool when > batchSizeBlockLimit
/***************************************************/
// Case where the timer ticks
sv.Start(ctx)
mj.numberOfItems = batchSizeBlockLimit + 1
inputChan <- &mj
// let the enqueue pool process and return an error
mp.hold <- struct{}{}
require.False(t, mj.processed)
// the service should end
sv.WaitForStop()
}
// TestPendingJobOnRestart makes sure a pending job in the exec pool is cancled
// when the Stream ctx is cancled, and a now one started with a new ctx
func TestPendingJobOnRestart(t *testing.T) {
partitiontest.PartitionTest(t)
mp := mockPool{
hold: make(chan struct{}),
poolCapacity: make(chan struct{}, 2),
asyncDelay: make(chan struct{}),
}
ctx, cancel := context.WithCancel(context.Background())
inputChan := make(chan InputJob)
mbp := mockBatchProcessor{
notify: make(chan struct{}, 1),
}
sv := MakeStreamToBatch(inputChan, &mp, &mbp)
// start with a saturated pool so that the job will not go through before
// the ctx is cancled
mp.poolCapacity <- struct{}{}
sv.Start(ctx)
mj := mockJob{
numberOfItems: 1,
}
inputChan <- &mj
// wait for the job to be submitted to the exec pool, waiting for capacity
mp.hold <- struct{}{}
// now the job should be waiting in the exec pool queue waiting to be executed
// cancel the ctx
cancel()
// make sure EnqueueBacklog has returned and the stream can terminate
sv.WaitForStop()
// start a new session
ctx, cancel = context.WithCancel(context.Background())
sv.Start(ctx)
// submit a new job
callbackFeedback := make(chan struct{}, 1)
mjNew := mockJob{
numberOfItems: 1,
callback: func(id int) {
callbackFeedback <- struct{}{}
},
}
inputChan <- &mjNew
mp.hold <- struct{}{}
<-mp.poolCapacity
// when the exec pool tries to execute the jobs,
// the function in addBatchToThePoolNow should abort the old and process the new
<-mp.asyncDelay
<-mp.asyncDelay
// wait for the notifiation from cleanup before checking the TestPendingJobOnRestart
<-mbp.notify
require.Error(t, mj.returnError)
require.False(t, mj.processed)
<-callbackFeedback
require.True(t, mjNew.processed)
cancel()
sv.WaitForStop()
}
|