forked from gocraft/work
-
Notifications
You must be signed in to change notification settings - Fork 12
/
dead_pool_reaper_test.go
355 lines (295 loc) · 11.1 KB
/
dead_pool_reaper_test.go
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
package work
import (
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)
func TestDeadPoolReaper(t *testing.T) {
pool := newTestPool(t)
ns := "work"
cleanKeyspace(ns, pool)
conn := pool.Get()
defer conn.Close()
workerPoolsKey := redisKeyWorkerPools(ns)
// Create redis data
var err error
err = conn.Send("SADD", workerPoolsKey, "1")
assert.NoError(t, err)
err = conn.Send("SADD", workerPoolsKey, "2")
assert.NoError(t, err)
err = conn.Send("SADD", workerPoolsKey, "3")
assert.NoError(t, err)
err = conn.Send("HMSET", redisKeyHeartbeat(ns, "1"),
"heartbeat_at", time.Now().Unix(),
"job_names", "type1,type2",
)
assert.NoError(t, err)
err = conn.Send("HMSET", redisKeyHeartbeat(ns, "2"),
"heartbeat_at", time.Now().Add(-1*time.Hour).Unix(),
"job_names", "type1,type2",
)
assert.NoError(t, err)
err = conn.Send("HMSET", redisKeyHeartbeat(ns, "3"),
"heartbeat_at", time.Now().Add(-1*time.Hour).Unix(),
"job_names", "type1,type2",
)
assert.NoError(t, err)
err = conn.Flush()
assert.NoError(t, err)
// Without this, the assertion for deadPools fails on GitHub CI. Could have
// something to do with in-memory redis instance.
time.Sleep(10 * time.Millisecond)
// Test getting dead pool
reaper := newDeadPoolReaper(ns, pool, []string{})
deadPools, err := reaper.findDeadPools()
assert.NoError(t, err)
assert.Equal(t, map[string][]string{"2": {"type1", "type2"}, "3": {"type1", "type2"}}, deadPools)
// Test requeueing jobs
_, err = conn.Do("lpush", redisKeyJobsInProgress(ns, "2", "type1"), "foo")
assert.NoError(t, err)
_, err = conn.Do("incr", redisKeyJobsLock(ns, "type1"))
assert.NoError(t, err)
_, err = conn.Do("hincrby", redisKeyJobsLockInfo(ns, "type1"), "2", 1) // worker pool 2 has lock
assert.NoError(t, err)
// Ensure 0 jobs in jobs queue
jobsCount, err := redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Ensure 1 job in inprogress queue
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Reap
err = reaper.reap()
assert.NoError(t, err)
// Ensure 1 jobs in jobs queue
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Ensure 0 job in inprogress queue
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Locks should get cleaned up
assert.EqualValues(t, 0, getInt64(pool, redisKeyJobsLock(ns, "type1")))
v, _ := conn.Do("HGET", redisKeyJobsLockInfo(ns, "type1"), "2")
assert.Nil(t, v)
}
func TestDeadPoolReaperNoHeartbeat(t *testing.T) {
pool := newTestPool(t)
ns := "work"
conn := pool.Get()
defer conn.Close()
workerPoolsKey := redisKeyWorkerPools(ns)
// Create redis data
var err error
cleanKeyspace(ns, pool)
err = conn.Send("SADD", workerPoolsKey, "1")
assert.NoError(t, err)
err = conn.Send("SADD", workerPoolsKey, "2")
assert.NoError(t, err)
err = conn.Send("SADD", workerPoolsKey, "3")
assert.NoError(t, err)
// stale lock info
err = conn.Send("SET", redisKeyJobsLock(ns, "type1"), 3)
assert.NoError(t, err)
err = conn.Send("HSET", redisKeyJobsLockInfo(ns, "type1"), "1", 1)
assert.NoError(t, err)
err = conn.Send("HSET", redisKeyJobsLockInfo(ns, "type1"), "2", 1)
assert.NoError(t, err)
err = conn.Send("HSET", redisKeyJobsLockInfo(ns, "type1"), "3", 1)
assert.NoError(t, err)
err = conn.Flush()
assert.NoError(t, err)
// make sure test data was created
numPools, err := redis.Int(conn.Do("scard", workerPoolsKey))
assert.NoError(t, err)
assert.EqualValues(t, 3, numPools)
// Test getting dead pool ids
reaper := newDeadPoolReaper(ns, pool, []string{"type1"})
deadPools, err := reaper.findDeadPools()
assert.NoError(t, err)
assert.Equal(t, map[string][]string{"1": {}, "2": {}, "3": {}}, deadPools)
// Test requeueing jobs
_, err = conn.Do("lpush", redisKeyJobsInProgress(ns, "2", "type1"), "foo")
assert.NoError(t, err)
// Ensure 0 jobs in jobs queue
jobsCount, err := redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Ensure 1 job in inprogress queue
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Ensure dead worker pools still in the set
jobsCount, err = redis.Int(conn.Do("scard", redisKeyWorkerPools(ns)))
assert.NoError(t, err)
assert.Equal(t, 3, jobsCount)
// Reap
err = reaper.reap()
assert.NoError(t, err)
// Ensure jobs queue was not altered
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Ensure inprogress queue was not altered
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Ensure dead worker pools were removed from the set
jobsCount, err = redis.Int(conn.Do("scard", redisKeyWorkerPools(ns)))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Stale lock info was cleaned up using reap.curJobTypes
assert.EqualValues(t, 0, getInt64(pool, redisKeyJobsLock(ns, "type1")))
for _, poolID := range []string{"1", "2", "3"} {
v, _ := conn.Do("HGET", redisKeyJobsLockInfo(ns, "type1"), poolID)
assert.Nil(t, v)
}
}
func TestDeadPoolReaperNoJobTypes(t *testing.T) {
pool := newTestPool(t)
ns := "work"
cleanKeyspace(ns, pool)
conn := pool.Get()
defer conn.Close()
workerPoolsKey := redisKeyWorkerPools(ns)
// Create redis data
var err error
err = conn.Send("SADD", workerPoolsKey, "1")
assert.NoError(t, err)
err = conn.Send("SADD", workerPoolsKey, "2")
assert.NoError(t, err)
err = conn.Send("HMSET", redisKeyHeartbeat(ns, "1"),
"heartbeat_at", time.Now().Add(-1*time.Hour).Unix(),
)
assert.NoError(t, err)
err = conn.Send("HMSET", redisKeyHeartbeat(ns, "2"),
"heartbeat_at", time.Now().Add(-1*time.Hour).Unix(),
"job_names", "type1,type2",
)
assert.NoError(t, err)
err = conn.Flush()
assert.NoError(t, err)
// Without this, the assertion for deadPools fails on GitHub CI. Could have
// something to do with in-memory redis instance.
time.Sleep(10 * time.Millisecond)
// Test getting dead pool
reaper := newDeadPoolReaper(ns, pool, []string{})
deadPools, err := reaper.findDeadPools()
assert.NoError(t, err)
assert.Equal(t, map[string][]string{"2": {"type1", "type2"}}, deadPools)
// Test requeueing jobs
_, err = conn.Do("lpush", redisKeyJobsInProgress(ns, "1", "type1"), "foo")
assert.NoError(t, err)
_, err = conn.Do("lpush", redisKeyJobsInProgress(ns, "2", "type1"), "foo")
assert.NoError(t, err)
// Ensure 0 jobs in jobs queue
jobsCount, err := redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
// Ensure 1 job in inprogress queue for each job
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "1", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Reap. Ensure job 2 is requeued but not job 1
err = reaper.reap()
assert.NoError(t, err)
// Ensure 1 jobs in jobs queue
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobs(ns, "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Ensure 1 job in inprogress queue for 1
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "1", "type1")))
assert.NoError(t, err)
assert.Equal(t, 1, jobsCount)
// Ensure 0 jobs in inprogress queue for 2
jobsCount, err = redis.Int(conn.Do("llen", redisKeyJobsInProgress(ns, "2", "type1")))
assert.NoError(t, err)
assert.Equal(t, 0, jobsCount)
}
func TestDeadPoolReaperWithWorkerPools(t *testing.T) {
pool := newTestPool(t)
ns := "work"
job1 := "job1"
stalePoolID := "aaa"
cleanKeyspace(ns, pool)
// test vars
expectedDeadTime := 5 * time.Millisecond
// create a stale job with a heartbeat
conn := pool.Get()
defer conn.Close()
_, err := conn.Do("SADD", redisKeyWorkerPools(ns), stalePoolID)
assert.NoError(t, err)
_, err = conn.Do("LPUSH", redisKeyJobsInProgress(ns, stalePoolID, job1), `{"sleep": 10}`)
assert.NoError(t, err)
jobTypes := map[string]*jobType{"job1": nil}
staleHeart := newWorkerPoolHeartbeater(ns, pool, stalePoolID, jobTypes, 1, []string{"id1"})
staleHeart.start()
// should have 1 stale job and empty job queue
assert.EqualValues(t, 1, listSize(pool, redisKeyJobsInProgress(ns, stalePoolID, job1)))
assert.EqualValues(t, 0, listSize(pool, redisKeyJobs(ns, job1)))
// setup a worker pool and start the reaper, which should restart the stale job above
wp := setupTestWorkerPool(pool, ns, job1, 1, JobOptions{Priority: 1})
wp.deadPoolReaper = newDeadPoolReaper(wp.namespace, wp.pool, []string{"job1"})
wp.deadPoolReaper.deadTime = expectedDeadTime
wp.deadPoolReaper.start()
// sleep long enough for staleJob to be considered dead
time.Sleep(expectedDeadTime * 2)
// now we should have 1 job in queue and no more stale jobs
assert.EqualValues(t, 1, listSize(pool, redisKeyJobs(ns, job1)))
assert.EqualValues(t, 0, listSize(pool, redisKeyJobsInProgress(ns, wp.workerPoolID, job1)))
staleHeart.stop()
wp.deadPoolReaper.stop()
}
func TestDeadPoolReaperCleanStaleLocks(t *testing.T) {
pool := newTestPool(t)
ns := "work"
cleanKeyspace(ns, pool)
conn := pool.Get()
defer conn.Close()
job1, job2 := "type1", "type2"
jobNames := []string{job1, job2}
workerPoolID1, workerPoolID2 := "1", "2"
lock1 := redisKeyJobsLock(ns, job1)
lock2 := redisKeyJobsLock(ns, job2)
lockInfo1 := redisKeyJobsLockInfo(ns, job1)
lockInfo2 := redisKeyJobsLockInfo(ns, job2)
// Create redis data
var err error
err = conn.Send("SET", lock1, 3)
assert.NoError(t, err)
err = conn.Send("SET", lock2, 1)
assert.NoError(t, err)
err = conn.Send("HSET", lockInfo1, workerPoolID1, 1) // workerPoolID1 holds 1 lock on job1
assert.NoError(t, err)
err = conn.Send("HSET", lockInfo1, workerPoolID2, 2) // workerPoolID2 holds 2 locks on job1
assert.NoError(t, err)
err = conn.Send("HSET", lockInfo2, workerPoolID2, 2) // test that we don't go below 0 on job2 lock
assert.NoError(t, err)
err = conn.Flush()
assert.NoError(t, err)
reaper := newDeadPoolReaper(ns, pool, jobNames)
// clean lock info for workerPoolID1
err = reaper.cleanStaleLockInfo(workerPoolID1, jobNames)
assert.NoError(t, err)
assert.EqualValues(t, 2, getInt64(pool, lock1)) // job1 lock should be decr by 1
assert.EqualValues(t, 1, getInt64(pool, lock2)) // job2 lock is unchanged
v, _ := conn.Do("HGET", lockInfo1, workerPoolID1) // workerPoolID1 removed from job1's lock info
assert.Nil(t, v)
// now clean lock info for workerPoolID2
err = reaper.cleanStaleLockInfo(workerPoolID2, jobNames)
assert.NoError(t, err)
// both locks should be at 0
assert.EqualValues(t, 0, getInt64(pool, lock1))
assert.EqualValues(t, 0, getInt64(pool, lock2))
// worker pool ID 2 removed from both lock info hashes
v, err = conn.Do("HGET", lockInfo1, workerPoolID2)
assert.Nil(t, v)
v, err = conn.Do("HGET", lockInfo2, workerPoolID2)
assert.Nil(t, v)
}