-
Notifications
You must be signed in to change notification settings - Fork 1
/
distributed_test.go
327 lines (311 loc) · 11.1 KB
/
distributed_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
package distributed_test
import (
"context"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/arunsworld/distributed"
"github.com/arunsworld/distributed/provider"
"github.com/google/uuid"
)
func Test_NewConcurrency(t *testing.T) {
t.Run("acquires only one lease across multiple registration requests", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
// When
c.RegisterLeadershipRequest("jobA")
c.RegisterLeadershipRequest("jobB")
c.RegisterLeadershipRequest("jobC")
close(leaseCreationTrigger)
time.Sleep(time.Millisecond) // give it a bit of propagation time
c.RegisterLeadershipRequest("jobD")
c.RegisterLeadershipRequest("jobE")
c.RegisterLeadershipRequest("jobF")
time.Sleep(time.Millisecond) // give it a bit of propagation time
// Then
if provider.calledCount() != 1 {
t.Fatalf("expected lease acquisition once, got: %d", provider.calledCount())
}
})
t.Run("acquires a new lease on lease loss", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
leaseTimeoutTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger, leaseTimeoutTrigger: leaseTimeoutTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
// When
c.RegisterLeadershipRequest("jobA")
leaseCreationTrigger <- struct{}{}
time.Sleep(time.Millisecond) // give it a bit of propagation time
// now timeout and re-creation
leaseTimeoutTrigger <- struct{}{}
leaseCreationTrigger <- struct{}{}
time.Sleep(time.Millisecond) // give it a bit of propagation time
// Then
if provider.calledCount() != 2 {
t.Fatalf("expected lease acquisition twice, got: %d", provider.calledCount())
}
})
t.Run("triggers leader election and campaign when we have a valid lease", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
close(leaseCreationTrigger) // whenever lease creation is desired it will succeed
// When
leadershipAcquired, _, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
// Then
if len(provider.lease.elections) != 1 {
t.Fatalf("expected one election, got: %d", len(provider.lease.elections))
}
// And
provider.lease.elections[0].campaignTrigger <- struct{}{}
<-leadershipAcquired
})
t.Run("allows for a leader to resign", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
close(leaseCreationTrigger) // whenever lease creation is desired it will succeed
// And
leadershipAcquired, _, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
provider.lease.elections[0].campaignTrigger <- struct{}{}
<-leadershipAcquired
// When
err := c.ResignLeadership(context.Background(), "jobA")
if err != nil {
t.Fatal(err)
}
if !provider.lease.elections[0].resigned {
t.Fatal("expected leadership to have resigned, didn't find that to be the case")
}
})
t.Run("close closes the lease", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
close(leaseCreationTrigger) // whenever lease creation is desired it will succeed
// And
// first job - leader
leadershipAcquired, _, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
provider.lease.elections[0].campaignTrigger <- struct{}{}
<-leadershipAcquired
// second job - not leader
c.RegisterLeadershipRequest("jobB")
time.Sleep(time.Millisecond) // give it a bit of propagation time
// When
c.Close()
if !provider.lease.elections[0].resigned {
t.Fatal("expected leadership to have resigned, didn't find that to be the case")
}
if !provider.lease.leaseClosed {
t.Fatal("expected lease to be closed, didn't find that to be the case")
}
})
t.Run("leaders are notified of leadership loss when lease is lost (eg. due to network parition)", func(t *testing.T) {
//Given
leaseCreationTrigger := make(chan struct{})
leaseTimeoutTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger, leaseTimeoutTrigger: leaseTimeoutTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
close(leaseCreationTrigger) // whenever lease creation is desired it will succeed
// And
leadershipAcquired, leadershipLost, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
provider.lease.elections[0].campaignTrigger <- struct{}{}
<-leadershipAcquired
// When - trigger lease loss
leaseTimeoutTrigger <- struct{}{}
// Then
<-leadershipLost // no further assertion required; because it will hang if there is a bug
})
t.Run("on campaign failure, campaign is retried until the lease is valid", func(t *testing.T) {
//Given
leaseCreationTrigger := make(chan struct{})
close(leaseCreationTrigger) // whenever lease creation is desired it will succeed
ct := make(chan struct{})
close(ct) // campaign should trigger whenever desired
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger, electionErrorUntil: 1, campaignTrigger: ct}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
// And
leadershipAcquired, _, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
<-leadershipAcquired
})
t.Run("obsolete campaign failure should not interfere with new campaign if lease is refreshed meanwhile", func(t *testing.T) {
//Given we get a lease but have an error filled campaign
leaseCreationTrigger := make(chan struct{}, 10)
close(leaseCreationTrigger)
leaseTimeoutTrigger := make(chan struct{}, 10)
ct := make(chan struct{}, 10)
// close(ct) // campaign should trigger whenever desired
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger, leaseTimeoutTrigger: leaseTimeoutTrigger, campaignTrigger: ct, electionErrorUntil: 3}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
// And then lease expires; but subsequent campaign is good
leadershipAcquired, _, _ := c.RegisterLeadershipRequest("jobA")
time.Sleep(time.Millisecond) // give it a bit of propagation time
provider.electionErrorUntil = 0
ct <- struct{}{}
leaseTimeoutTrigger <- struct{}{}
time.Sleep(time.Millisecond) // give it a bit of propagation time
time.Sleep(time.Second * 2) // give enough time for campaign retry timeout to have taken effect
// Then the failed campaign retry does not interfere and we acquire leadership with the right lease
<-leadershipAcquired
})
t.Run("able to register hundreds of jobs even when lease acquisition takes time", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger}
c := distributed.NewConcurrency("testApp", "node1", provider, -1)
// When
wg := sync.WaitGroup{}
for i := 0; i < 500; i++ {
wg.Add(1)
go func(i int) {
_, _, err := c.RegisterLeadershipRequest(fmt.Sprintf("job-%d", i))
if err != nil {
panic(err)
}
wg.Done()
}(i)
}
wg.Wait()
})
t.Run("able to limit the number of concurrent jobs on node", func(t *testing.T) {
// Given
leaseCreationTrigger := make(chan struct{})
close(leaseCreationTrigger) // create lease immediately upon request
cp := make(chan struct{})
close(cp) // campaigns should immediately result in leaders
provider := &testLeaseProvider{leaseCreationTrigger: leaseCreationTrigger, campaignTrigger: cp}
c := distributed.NewConcurrency("testApp", "node1", provider, 50)
allJobs := make([]<-chan struct{}, 0, 100)
for i := 0; i < 100; i++ {
la, _, err := c.RegisterLeadershipRequest(fmt.Sprintf("job-%d", i))
if err != nil {
panic(err)
}
allJobs = append(allJobs, la)
}
time.Sleep(time.Millisecond * 100) // enough time to propagate leadership
numberOfLeaders := 0
for _, al := range allJobs {
select {
case <-al:
numberOfLeaders++
default:
}
}
if numberOfLeaders != 50 {
t.Fatalf("expected leaders to be limited to 50, got: %d", numberOfLeaders)
}
})
}
type testLeaseProvider struct {
count int64
lease *testLease
leaseCreationTrigger chan struct{}
leaseTimeoutTrigger chan struct{}
campaignTrigger chan struct{}
electionErrorUntil int
}
func (p *testLeaseProvider) AcquireLease(ctx context.Context) (provider.Lease, error) {
select {
case <-ctx.Done():
return nil, fmt.Errorf("conext cancelled")
case <-p.leaseCreationTrigger:
atomic.AddInt64(&p.count, 1)
p.lease = newTestLease(p.leaseTimeoutTrigger, p.campaignTrigger, p.electionErrorUntil)
return p.lease, nil
}
}
func (p *testLeaseProvider) calledCount() int {
return int(atomic.LoadInt64(&p.count))
}
func newTestLease(leaseTimeoutTrigger chan struct{}, campaignTrigger chan struct{}, electionErrorUntil int) *testLease {
result := &testLease{
id: uuid.New().String(),
done: make(chan struct{}),
electionErrorUntil: electionErrorUntil,
campaignTrigger: campaignTrigger,
}
go func() {
<-leaseTimeoutTrigger
close(result.done)
}()
return result
}
type testLease struct {
id string
done chan struct{}
elections []*election
leaseClosed bool
campaignTrigger chan struct{}
electionErrorUntil int
errorCount int
}
func (l *testLease) ID() string {
return l.id
}
func (l *testLease) Expired() <-chan struct{} {
return l.done
}
func (l *testLease) ElectionFor(constituency string) provider.Election {
var electionErr error
if l.electionErrorUntil > 0 {
if l.errorCount < l.electionErrorUntil {
electionErr = fmt.Errorf("simulated error")
l.errorCount++
}
}
ct := l.campaignTrigger
if ct == nil {
ct = make(chan struct{})
}
el := &election{
constituency: constituency,
campaignTrigger: ct,
err: electionErr,
}
l.elections = append(l.elections, el)
return el
}
func (l *testLease) Close() {
l.leaseClosed = true
}
type election struct {
count int64
constituency string
campaignTrigger chan struct{}
resigned bool
err error
}
func (e *election) Campaign(ctx context.Context, val string) error {
if e.err != nil {
// time.Sleep(time.Second)
return e.err
}
select {
case <-ctx.Done():
return fmt.Errorf("context cancelled")
case <-e.campaignTrigger:
atomic.AddInt64(&e.count, 1)
}
return nil
}
func (e *election) ReassureLeadership(context.Context, string) error {
return nil
}
func (e *election) Resign(context.Context) error {
e.resigned = true
return nil
}