/
lock.go
176 lines (147 loc) · 5.01 KB
/
lock.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
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"path"
"testing"
"time"
"context"
"vitess.io/vitess/go/vt/topo"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
// timeUntilLockIsTaken is the time to wait until a lock is taken.
// We haven't found a better simpler way to guarantee a routine is stuck
// waiting for a topo lock than sleeping that amount.
var timeUntilLockIsTaken = 10 * time.Millisecond
// checkLock checks we can lock / unlock as expected. It's using a keyspace
// as the lock target.
func checkLock(t *testing.T, ts *topo.Server) {
ctx := context.Background()
if err := ts.CreateKeyspace(ctx, "test_keyspace", &topodatapb.Keyspace{}); err != nil {
t.Fatalf("CreateKeyspace: %v", err)
}
conn, err := ts.ConnForCell(context.Background(), topo.GlobalCell)
if err != nil {
t.Fatalf("ConnForCell(global) failed: %v", err)
}
t.Log("=== checkLockTimeout")
checkLockTimeout(ctx, t, conn)
t.Log("=== checkLockMissing")
checkLockMissing(ctx, t, conn)
t.Log("=== checkLockUnblocks")
checkLockUnblocks(ctx, t, conn)
}
func checkLockTimeout(ctx context.Context, t *testing.T, conn topo.Conn) {
keyspacePath := path.Join(topo.KeyspacesPath, "test_keyspace")
lockDescriptor, err := conn.Lock(ctx, keyspacePath, "")
if err != nil {
t.Fatalf("Lock: %v", err)
}
// We have the lock, list the keyspace directory.
// It should not contain anything, except Ephemeral files.
entries, err := conn.ListDir(ctx, keyspacePath, true /*full*/)
if err != nil {
t.Fatalf("Listdir(%v) failed: %v", keyspacePath, err)
}
for _, e := range entries {
if e.Name == "Keyspace" {
continue
}
if e.Ephemeral {
t.Logf("skipping ephemeral node %v in %v", e, keyspacePath)
continue
}
// Non-ephemeral entries better have only ephemeral children.
p := path.Join(keyspacePath, e.Name)
entries, err := conn.ListDir(ctx, p, true /*full*/)
if err != nil {
t.Fatalf("Listdir(%v) failed: %v", p, err)
}
for _, e := range entries {
if e.Ephemeral {
t.Logf("skipping ephemeral node %v in %v", e, p)
} else {
t.Errorf("Entry in %v has non-ephemeral DirEntry: %v", p, e)
}
}
}
// test we can't take the lock again
fastCtx, cancel := context.WithTimeout(ctx, timeUntilLockIsTaken)
if _, err := conn.Lock(fastCtx, keyspacePath, "again"); !topo.IsErrType(err, topo.Timeout) {
t.Fatalf("Lock(again): %v", err)
}
cancel()
// test we can interrupt taking the lock
interruptCtx, cancel := context.WithCancel(ctx)
go func() {
time.Sleep(timeUntilLockIsTaken)
cancel()
}()
if _, err := conn.Lock(interruptCtx, keyspacePath, "interrupted"); !topo.IsErrType(err, topo.Interrupted) {
t.Fatalf("Lock(interrupted): %v", err)
}
if err := lockDescriptor.Check(ctx); err != nil {
t.Errorf("Check(): %v", err)
}
if err := lockDescriptor.Unlock(ctx); err != nil {
t.Fatalf("Unlock(): %v", err)
}
// test we can't unlock again
if err := lockDescriptor.Unlock(ctx); err == nil {
t.Fatalf("Unlock(again) worked")
}
}
// checkLockMissing makes sure we can't lock a non-existing directory.
func checkLockMissing(ctx context.Context, t *testing.T, conn topo.Conn) {
keyspacePath := path.Join(topo.KeyspacesPath, "test_keyspace_666")
if _, err := conn.Lock(ctx, keyspacePath, "missing"); err == nil {
t.Fatalf("Lock(test_keyspace_666) worked for non-existing keyspace")
}
}
// checkLockUnblocks makes sure that a routine waiting on a lock
// is unblocked when another routine frees the lock
func checkLockUnblocks(ctx context.Context, t *testing.T, conn topo.Conn) {
keyspacePath := path.Join(topo.KeyspacesPath, "test_keyspace")
unblock := make(chan struct{})
finished := make(chan struct{})
// As soon as we're unblocked, we try to lock the keyspace.
go func() {
<-unblock
lockDescriptor, err := conn.Lock(ctx, keyspacePath, "unblocks")
if err != nil {
t.Errorf("Lock(test_keyspace) failed: %v", err)
}
if err = lockDescriptor.Unlock(ctx); err != nil {
t.Errorf("Unlock(test_keyspace): %v", err)
}
close(finished)
}()
// Lock the keyspace.
lockDescriptor2, err := conn.Lock(ctx, keyspacePath, "")
if err != nil {
t.Fatalf("Lock(test_keyspace) failed: %v", err)
}
// unblock the go routine so it starts waiting
close(unblock)
// sleep for a while so we're sure the go routine is blocking
time.Sleep(timeUntilLockIsTaken)
if err = lockDescriptor2.Unlock(ctx); err != nil {
t.Fatalf("Unlock(test_keyspace): %v", err)
}
timeout := time.After(10 * time.Second)
select {
case <-finished:
case <-timeout:
t.Fatalf("unlocking timed out")
}
}