forked from containers/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockfile_test.go
574 lines (522 loc) · 15.1 KB
/
lockfile_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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package storage
import (
"fmt"
"io"
"io/ioutil"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/containers/storage/pkg/reexec"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Warning: this is not an exhaustive set of tests.
func TestMain(m *testing.M) {
if reexec.Init() {
return
}
os.Exit(m.Run())
}
// subTouchMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the lock, waits for stdin to get closed,
// updates the last-writer for the lockfile, and then unlocks the file.
func subTouchMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockfile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.Lock()
os.Stdout.Close()
io.Copy(ioutil.Discard, os.Stdin)
tf.Touch()
tf.Unlock()
}
// subTouch starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired the lock. It can then signal
// that the child should Touch() the lock by closing the WriteCloser. The
// second ReadCloser will be closed when the child has finished.
func subTouch(l *namedLocker) (io.WriteCloser, io.ReadCloser, io.ReadCloser, error) {
cmd := reexec.Command("subTouch", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, nil, err
}
ec, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, err
}
go func() {
if err = cmd.Run(); err != nil {
logrus.Errorf("error running subTouch: %v", err)
}
}()
return wc, rc, ec, nil
}
// subLockMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the lock, waits for stdin to get closed, and
// then unlocks the file.
func subLockMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockfile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.Lock()
os.Stdout.Close()
io.Copy(ioutil.Discard, os.Stdin)
tf.Unlock()
}
// subLock starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired the lock. It can then signal
// that the child should release the lock by closing the WriteCloser.
func subLock(l *namedLocker) (io.WriteCloser, io.ReadCloser, error) {
cmd := reexec.Command("subLock", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
go func() {
if err = cmd.Run(); err != nil {
logrus.Errorf("error running subLock: %v", err)
}
}()
return wc, rc, nil
}
// subRLockMain is a child process which opens the lock file, closes stdout to
// indicate that it has acquired the read lock, waits for stdin to get closed,
// and then unlocks the file.
func subRLockMain() {
if len(os.Args) != 2 {
logrus.Fatalf("expected two args, got %d", len(os.Args))
}
tf, err := GetLockfile(os.Args[1])
if err != nil {
logrus.Fatalf("error opening lock file %q: %v", os.Args[1], err)
}
tf.RLock()
os.Stdout.Close()
io.Copy(ioutil.Discard, os.Stdin)
tf.Unlock()
}
// subRLock starts a child process. If it doesn't return an error, the caller
// should wait for the first ReadCloser by reading it until it receives an EOF.
// At that point, the child will have acquired a read lock. It can then signal
// that the child should release the lock by closing the WriteCloser.
func subRLock(l *namedLocker) (io.WriteCloser, io.ReadCloser, error) {
cmd := reexec.Command("subRLock", l.name)
wc, err := cmd.StdinPipe()
if err != nil {
return nil, nil, err
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, err
}
go func() {
if err = cmd.Run(); err != nil {
logrus.Errorf("error running subRLock: %v", err)
}
}()
return wc, rc, nil
}
func init() {
reexec.Register("subTouch", subTouchMain)
reexec.Register("subRLock", subRLockMain)
reexec.Register("subLock", subLockMain)
}
type namedLocker struct {
Locker
name string
}
func getNamedLocker(ro bool) (*namedLocker, error) {
var l Locker
tf, err := ioutil.TempFile("", "lockfile")
if err != nil {
return nil, err
}
name := tf.Name()
tf.Close()
if ro {
l, err = GetROLockfile(name)
} else {
l, err = GetLockfile(name)
}
if err != nil {
return nil, err
}
return &namedLocker{Locker: l, name: name}, nil
}
func getTempLockfile() (*namedLocker, error) {
return getNamedLocker(false)
}
func getTempROLockfile() (*namedLocker, error) {
return getNamedLocker(true)
}
func TestLockfileName(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.RLock()
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.Unlock()
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
l.Lock()
assert.True(t, l.Locked(), "Locked() said we didn't have a write lock")
l.Unlock()
assert.NotEmpty(t, l.name, "lockfile name should be recorded correctly")
}
func TestLockfileRead(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.RLock()
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.Unlock()
}
func TestROLockfileRead(t *testing.T) {
l, err := getTempROLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.Unlock()
l.RLock()
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.Unlock()
}
func TestLockfileWrite(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
assert.True(t, l.Locked(), "Locked() said we didn't have a write lock")
l.Unlock()
}
func TestROLockfileWrite(t *testing.T) {
l, err := getTempROLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
assert.False(t, l.Locked(), "Locked() said we have a write lock")
l.Unlock()
}
func TestLockfileTouch(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
l.Lock()
m, err := l.Modified()
require.Nil(t, err, "got an error from Modified()")
assert.True(t, m, "new lock file does not appear to have changed")
now := time.Now()
assert.False(t, l.TouchedSince(now), "file timestamp was updated for no reason")
time.Sleep(2 * time.Second)
err = l.Touch()
require.Nil(t, err, "got an error from Touch()")
assert.True(t, l.TouchedSince(now), "file timestamp was not updated by Touch()")
m, err = l.Modified()
require.Nil(t, err, "got an error from Modified()")
assert.False(t, m, "lock file mistakenly indicated that someone else has modified it")
stdin, stdout, stderr, err := subTouch(l)
require.Nil(t, err, "got an error starting a subprocess to touch the lockfile")
l.Unlock()
io.Copy(ioutil.Discard, stdout)
stdin.Close()
io.Copy(ioutil.Discard, stderr)
l.Lock()
m, err = l.Modified()
l.Unlock()
require.Nil(t, err, "got an error from Modified()")
assert.True(t, m, "lock file failed to notice that someone else modified it")
}
func TestLockfileWriteConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var highestMutex sync.Mutex
var counter, highest int64
for i := 0; i < 100000; i++ {
wg.Add(1)
go func() {
l.Lock()
tmp := atomic.AddInt64(&counter, 1)
assert.True(t, tmp >= 0, "counter should never be less than zero")
highestMutex.Lock()
if tmp > highest {
// multiple writers should not be able to hold
// this lock at the same time, so there should
// be no point at which two goroutines are
// between the AddInt64() above and the one
// below
highest = tmp
}
highestMutex.Unlock()
atomic.AddInt64(&counter, -1)
l.Unlock()
wg.Done()
}()
}
wg.Wait()
assert.True(t, highest == 1, "counter should never have gone above 1, got to %d", highest)
}
func TestLockfileReadConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
// the test below is inspired by the stdlib's rwmutex tests
numReaders := 1000
locked := make(chan bool)
unlocked := make(chan bool)
done := make(chan bool)
for i := 0; i < numReaders; i++ {
go func() {
l.RLock()
locked <- true
<-unlocked
l.Unlock()
done <- true
}()
}
// Wait for all parallel locks to succeed
for i := 0; i < numReaders; i++ {
<-locked
}
// Instruct all parallel locks to unlock
for i := 0; i < numReaders; i++ {
unlocked <- true
}
// Wait for all parallel locks to be unlocked
for i := 0; i < numReaders; i++ {
<-done
}
}
func TestLockfileMixedConcurrent(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
counter := int32(0)
diff := int32(10000)
numIterations := 10
numReaders := 100
numWriters := 50
done := make(chan bool)
// A writer always adds `diff` to the counter. Hence, `diff` is the
// only valid value in the critical section.
writer := func(c *int32) {
for i := 0; i < numIterations; i++ {
l.Lock()
tmp := atomic.AddInt32(c, diff)
assert.True(t, tmp == diff, "counter should be %d but instead is %d", diff, tmp)
time.Sleep(100 * time.Millisecond)
atomic.AddInt32(c, diff*(-1))
l.Unlock()
}
done <- true
}
// A reader always adds `1` to the counter. Hence,
// [1,`numReaders*numIterations`] are valid values.
reader := func(c *int32) {
for i := 0; i < numIterations; i++ {
l.RLock()
tmp := atomic.AddInt32(c, 1)
assert.True(t, tmp >= 1 && tmp < diff)
time.Sleep(100 * time.Millisecond)
atomic.AddInt32(c, -1)
l.Unlock()
}
done <- true
}
for i := 0; i < numReaders; i++ {
go reader(&counter)
// schedule a writer every 2nd iteration
if i%2 == 1 {
go writer(&counter)
}
}
for i := 0; i < numReaders+numWriters; i++ {
<-done
}
}
func TestLockfileMultiprocessRead(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var rcounter, rhighest int64
var highestMutex sync.Mutex
subs := make([]struct {
stdin io.WriteCloser
stdout io.ReadCloser
}, 100)
for i := range subs {
stdin, stdout, err := subRLock(l)
require.Nil(t, err, "error starting subprocess %d to take a read lock", i+1)
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
io.Copy(ioutil.Discard, subs[i].stdout)
if testing.Verbose() {
fmt.Printf("\tchild %4d acquired the read lock\n", i+1)
}
atomic.AddInt64(&rcounter, 1)
highestMutex.Lock()
if rcounter > rhighest {
rhighest = rcounter
}
highestMutex.Unlock()
time.Sleep(1 * time.Second)
atomic.AddInt64(&rcounter, -1)
if testing.Verbose() {
fmt.Printf("\ttelling child %4d to release the read lock\n", i+1)
}
subs[i].stdin.Close()
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, rhighest > 1, "expected to have multiple reader locks at least once, only had %d", rhighest)
}
func TestLockfileMultiprocessWrite(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var wcounter, whighest int64
var highestMutex sync.Mutex
subs := make([]struct {
stdin io.WriteCloser
stdout io.ReadCloser
}, 10)
for i := range subs {
stdin, stdout, err := subLock(l)
require.Nil(t, err, "error starting subprocess %d to take a write lock", i+1)
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
io.Copy(ioutil.Discard, subs[i].stdout)
if testing.Verbose() {
fmt.Printf("\tchild %4d acquired the write lock\n", i+1)
}
atomic.AddInt64(&wcounter, 1)
highestMutex.Lock()
if wcounter > whighest {
whighest = wcounter
}
highestMutex.Unlock()
time.Sleep(1 * time.Second)
atomic.AddInt64(&wcounter, -1)
if testing.Verbose() {
fmt.Printf("\ttelling child %4d to release the write lock\n", i+1)
}
subs[i].stdin.Close()
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, whighest == 1, "expected to have no more than one writer lock active at a time, had %d", whighest)
}
func TestLockfileMultiprocessMixed(t *testing.T) {
l, err := getTempLockfile()
require.Nil(t, err, "error getting temporary lock file")
defer os.Remove(l.name)
var wg sync.WaitGroup
var rcounter, wcounter, rhighest, whighest int64
var rhighestMutex, whighestMutex sync.Mutex
bias_p := 1
bias_q := 10
groups := 15
writer := func(i int) bool { return (i % bias_q) < bias_p }
subs := make([]struct {
stdin io.WriteCloser
stdout io.ReadCloser
}, bias_q*groups)
for i := range subs {
var stdin io.WriteCloser
var stdout io.ReadCloser
if writer(i) {
stdin, stdout, err = subLock(l)
require.Nil(t, err, "error starting subprocess %d to take a write lock", i+1)
} else {
stdin, stdout, err = subRLock(l)
require.Nil(t, err, "error starting subprocess %d to take a read lock", i+1)
}
subs[i].stdin = stdin
subs[i].stdout = stdout
}
for i := range subs {
wg.Add(1)
go func(i int) {
// wait for the child to acquire whatever lock it wants
io.Copy(ioutil.Discard, subs[i].stdout)
if writer(i) {
// child acquired a write lock
if testing.Verbose() {
fmt.Printf("\tchild %4d acquired the write lock\n", i+1)
}
atomic.AddInt64(&wcounter, 1)
whighestMutex.Lock()
if wcounter > whighest {
whighest = wcounter
}
require.Zero(t, rcounter, "acquired a write lock while we appear to have read locks")
whighestMutex.Unlock()
} else {
// child acquired a read lock
if testing.Verbose() {
fmt.Printf("\tchild %4d acquired the read lock\n", i+1)
}
atomic.AddInt64(&rcounter, 1)
rhighestMutex.Lock()
if rcounter > rhighest {
rhighest = rcounter
}
require.Zero(t, wcounter, "acquired a read lock while we appear to have write locks")
rhighestMutex.Unlock()
}
time.Sleep(1 * time.Second)
if writer(i) {
atomic.AddInt64(&wcounter, -1)
if testing.Verbose() {
fmt.Printf("\ttelling child %4d to release the write lock\n", i+1)
}
} else {
atomic.AddInt64(&rcounter, -1)
if testing.Verbose() {
fmt.Printf("\ttelling child %4d to release the read lock\n", i+1)
}
}
subs[i].stdin.Close()
wg.Done()
}(i)
}
wg.Wait()
assert.True(t, rhighest > 1, "expected to have more than one reader lock active at a time at least once, only had %d", rhighest)
assert.True(t, whighest == 1, "expected to have no more than one writer lock active at a time, had %d", whighest)
}