Debian Codegraph @

dsync_test.go

raw symbols SCIP
1 /*
2 * Minio Cloud Storage, (C) 2016 Minio, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // GOMAXPROCS=10 go test
18
19 package dsync_test
20
21 import (
22 "fmt"
23 "log"
24 "math/rand"
25 "net"
26 "net/http"
27 "net/rpc"
28 "os"
29 "strconv"
30 "sync"
31 "testing"
32 "time"
33
34 . "github.com/minio/dsync"
35 )
36
37 const RpcPath = "/dsync"
38 const N = 4 // number of lock servers for tests.
39 var nodes []string // list of node IP addrs or hostname with ports.
40 var rpcPaths []string // list of rpc paths where lock server is serving.
41
42 func startRPCServers(nodes []string) {
43
44 for i := range nodes {
45 server := rpc.NewServer()
46 server.RegisterName("Dsync", &lockServer{
47 mutex: sync.Mutex{},
48 lockMap: make(map[string]int64),
49 })
50 // For some reason the registration paths need to be different (even for different server objs)
51 server.HandleHTTP(rpcPaths[i], fmt.Sprintf("%s-debug", rpcPaths[i]))
52 l, e := net.Listen("tcp", ":"+strconv.Itoa(i+12345))
53 if e != nil {
54 log.Fatal("listen error:", e)
55 }
56 go http.Serve(l, nil)
57 }
58
59 // Let servers start
60 time.Sleep(10 * time.Millisecond)
61 }
62
63 // TestMain initializes the testing framework
64 func TestMain(m *testing.M) {
65
66 rand.Seed(time.Now().UTC().UnixNano())
67
68 nodes = make([]string, 4)
69 for i := range nodes {
70 nodes[i] = fmt.Sprintf("127.0.0.1:%d", i+12345)
71 }
72 for i := range nodes {
73 rpcPaths = append(rpcPaths, RpcPath+"-"+strconv.Itoa(i))
74 }
75
76 // Initialize net/rpc clients for dsync.
77 var clnts []NetLocker
78 for i := 0; i < len(nodes); i++ {
79 clnts = append(clnts, newClient(nodes[i], rpcPaths[i]))
80 }
81
82 if err := Init(clnts, 5); err == nil {
83 log.Fatalf("Should have failed")
84 }
85
86 if err := Init(clnts[0:1], 0); err == nil {
87 log.Fatalf("Should have failed")
88 }
89
90 nclnts := make([]NetLocker, 17)
91 if err := Init(nclnts, 0); err == nil {
92 log.Fatalf("Should have failed")
93 }
94
95 nclnts = make([]NetLocker, 15)
96 if err := Init(nclnts, 0); err == nil {
97 log.Fatalf("Should have failed")
98 }
99
100 rpcOwnNodeFakeForTest := 0
101 if err := Init(clnts, rpcOwnNodeFakeForTest); err != nil {
102 log.Fatalf("set nodes failed with %v", err)
103 }
104 if err := Init(clnts, rpcOwnNodeFakeForTest); err == nil {
105 log.Fatalf("Should have failed")
106 }
107 startRPCServers(nodes)
108
109 os.Exit(m.Run())
110 }
111
112 func TestSimpleLock(t *testing.T) {
113
114 dm := NewDRWMutex("test")
115
116 dm.Lock()
117
118 // fmt.Println("Lock acquired, waiting...")
119 time.Sleep(2500 * time.Millisecond)
120
121 dm.Unlock()
122 }
123
124 func TestSimpleLockUnlockMultipleTimes(t *testing.T) {
125
126 dm := NewDRWMutex("test")
127
128 dm.Lock()
129 time.Sleep(time.Duration(10+(rand.Float32()*50)) * time.Millisecond)
130 dm.Unlock()
131
132 dm.Lock()
133 time.Sleep(time.Duration(10+(rand.Float32()*50)) * time.Millisecond)
134 dm.Unlock()
135
136 dm.Lock()
137 time.Sleep(time.Duration(10+(rand.Float32()*50)) * time.Millisecond)
138 dm.Unlock()
139
140 dm.Lock()
141 time.Sleep(time.Duration(10+(rand.Float32()*50)) * time.Millisecond)
142 dm.Unlock()
143
144 dm.Lock()
145 time.Sleep(time.Duration(10+(rand.Float32()*50)) * time.Millisecond)
146 dm.Unlock()
147 }
148
149 // Test two locks for same resource, one succeeds, one fails (after timeout)
150 func TestTwoSimultaneousLocksForSameResource(t *testing.T) {
151
152 dm1st := NewDRWMutex("aap")
153 dm2nd := NewDRWMutex("aap")
154
155 dm1st.Lock()
156
157 // Release lock after 10 seconds
158 go func() {
159 time.Sleep(10 * time.Second)
160 // fmt.Println("Unlocking dm1")
161
162 dm1st.Unlock()
163 }()
164
165 dm2nd.Lock()
166
167 // fmt.Printf("2nd lock obtained after 1st lock is released\n")
168 time.Sleep(2500 * time.Millisecond)
169
170 dm2nd.Unlock()
171 }
172
173 // Test three locks for same resource, one succeeds, one fails (after timeout)
174 func TestThreeSimultaneousLocksForSameResource(t *testing.T) {
175
176 dm1st := NewDRWMutex("aap")
177 dm2nd := NewDRWMutex("aap")
178 dm3rd := NewDRWMutex("aap")
179
180 dm1st.Lock()
181
182 // Release lock after 10 seconds
183 go func() {
184 time.Sleep(10 * time.Second)
185 // fmt.Println("Unlocking dm1")
186
187 dm1st.Unlock()
188 }()
189
190 var wg sync.WaitGroup
191 wg.Add(2)
192
193 go func() {
194 defer wg.Done()
195
196 dm2nd.Lock()
197
198 // Release lock after 10 seconds
199 go func() {
200 time.Sleep(2500 * time.Millisecond)
201 // fmt.Println("Unlocking dm2")
202
203 dm2nd.Unlock()
204 }()
205
206 dm3rd.Lock()
207
208 // fmt.Printf("3rd lock obtained after 1st & 2nd locks are released\n")
209 time.Sleep(2500 * time.Millisecond)
210
211 dm3rd.Unlock()
212 }()
213
214 go func() {
215 defer wg.Done()
216
217 dm3rd.Lock()
218
219 // Release lock after 10 seconds
220 go func() {
221 time.Sleep(2500 * time.Millisecond)
222 // fmt.Println("Unlocking dm3")
223
224 dm3rd.Unlock()
225 }()
226
227 dm2nd.Lock()
228
229 // fmt.Printf("2nd lock obtained after 1st & 3rd locks are released\n")
230 time.Sleep(2500 * time.Millisecond)
231
232 dm2nd.Unlock()
233 }()
234
235 wg.Wait()
236 }
237
238 // Test two locks for different resources, both succeed
239 func TestTwoSimultaneousLocksForDifferentResources(t *testing.T) {
240
241 dm1 := NewDRWMutex("aap")
242 dm2 := NewDRWMutex("noot")
243
244 dm1.Lock()
245 dm2.Lock()
246
247 // fmt.Println("Both locks acquired, waiting...")
248 time.Sleep(2500 * time.Millisecond)
249
250 dm1.Unlock()
251 dm2.Unlock()
252
253 time.Sleep(10 * time.Millisecond)
254 }
255
256 // Borrowed from mutex_test.go
257 func HammerMutex(m *DRWMutex, loops int, cdone chan bool) {
258 for i := 0; i < loops; i++ {
259 m.Lock()
260 m.Unlock()
261 }
262 cdone <- true
263 }
264
265 // Borrowed from mutex_test.go
266 func TestMutex(t *testing.T) {
267 c := make(chan bool)
268 m := NewDRWMutex("test")
269 for i := 0; i < 10; i++ {
270 go HammerMutex(m, 1000, c)
271 }
272 for i := 0; i < 10; i++ {
273 <-c
274 }
275 }
276
277 func BenchmarkMutexUncontended(b *testing.B) {
278 type PaddedMutex struct {
279 DRWMutex
280 pad [128]uint8
281 }
282 b.RunParallel(func(pb *testing.PB) {
283 var mu PaddedMutex
284 for pb.Next() {
285 mu.Lock()
286 mu.Unlock()
287 }
288 })
289 }
290
291 func benchmarkMutex(b *testing.B, slack, work bool) {
292 mu := NewDRWMutex("")
293 if slack {
294 b.SetParallelism(10)
295 }
296 b.RunParallel(func(pb *testing.PB) {
297 foo := 0
298 for pb.Next() {
299 mu.Lock()
300 mu.Unlock()
301 if work {
302 for i := 0; i < 100; i++ {
303 foo *= 2
304 foo /= 2
305 }
306 }
307 }
308 _ = foo
309 })
310 }
311
312 func BenchmarkMutex(b *testing.B) {
313 benchmarkMutex(b, false, false)
314 }
315
316 func BenchmarkMutexSlack(b *testing.B) {
317 benchmarkMutex(b, true, false)
318 }
319
320 func BenchmarkMutexWork(b *testing.B) {
321 benchmarkMutex(b, false, true)
322 }
323
324 func BenchmarkMutexWorkSlack(b *testing.B) {
325 benchmarkMutex(b, true, true)
326 }
327
328 func BenchmarkMutexNoSpin(b *testing.B) {
329 // This benchmark models a situation where spinning in the mutex should be
330 // non-profitable and allows to confirm that spinning does not do harm.
331 // To achieve this we create excess of goroutines most of which do local work.
332 // These goroutines yield during local work, so that switching from
333 // a blocked goroutine to other goroutines is profitable.
334 // As a matter of fact, this benchmark still triggers some spinning in the mutex.
335 m := NewDRWMutex("")
336 var acc0, acc1 uint64
337 b.SetParallelism(4)
338 b.RunParallel(func(pb *testing.PB) {
339 c := make(chan bool)
340 var data [4 << 10]uint64
341 for i := 0; pb.Next(); i++ {
342 if i%4 == 0 {
343 m.Lock()
344 acc0 -= 100
345 acc1 += 100
346 m.Unlock()
347 } else {
348 for i := 0; i < len(data); i += 4 {
349 data[i]++
350 }
351 // Elaborate way to say runtime.Gosched
352 // that does not put the goroutine onto global runq.
353 go func() {
354 c <- true
355 }()
356 <-c
357 }
358 }
359 })
360 }
361
362 func BenchmarkMutexSpin(b *testing.B) {
363 // This benchmark models a situation where spinning in the mutex should be
364 // profitable. To achieve this we create a goroutine per-proc.
365 // These goroutines access considerable amount of local data so that
366 // unnecessary rescheduling is penalized by cache misses.
367 m := NewDRWMutex("")
368 var acc0, acc1 uint64
369 b.RunParallel(func(pb *testing.PB) {
370 var data [16 << 10]uint64
371 for i := 0; pb.Next(); i++ {
372 m.Lock()
373 acc0 -= 100
374 acc1 += 100
375 m.Unlock()
376 for i := 0; i < len(data); i += 4 {
377 data[i]++
378 }
379 }
380 })
381 }