forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 58
/
shutdown_test.go
606 lines (478 loc) · 16.1 KB
/
shutdown_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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//go:build linux || (darwin && !cgo)
// +build linux darwin,!cgo
// Use OS build flags since signals are system-dependent.
package tarantool_test
import (
"fmt"
"sync"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/test_helpers"
)
var shtdnServer = "127.0.0.1:3014"
var shtdnDialer = NetDialer{
Address: shtdnServer,
User: dialer.User,
Password: dialer.Password,
}
var shtdnClntOpts = Opts{
Timeout: 20 * time.Second,
Reconnect: 500 * time.Millisecond,
MaxReconnects: 10,
}
var shtdnSrvOpts = test_helpers.StartOpts{
Dialer: shtdnDialer,
InitScript: "config.lua",
Listen: shtdnServer,
WaitStart: 100 * time.Millisecond,
ConnectRetry: 10,
RetryTimeout: 500 * time.Millisecond,
}
var evalMsg = "got enough sleep"
var evalBody = `
local fiber = require('fiber')
local time, msg = ...
fiber.sleep(time)
return msg
`
func testGracefulShutdown(t *testing.T, conn *Connection, inst *test_helpers.TarantoolInstance) {
var err error
// Set a big timeout so it would be easy to differ
// if server went down on timeout or after all connections were terminated.
serverShutdownTimeout := 60 // in seconds
_, err = conn.Call("box.ctl.set_on_shutdown_timeout", []interface{}{serverShutdownTimeout})
require.Nil(t, err)
// Send request with sleep.
evalSleep := 1 // in seconds
require.Lessf(t,
time.Duration(evalSleep)*time.Second,
shtdnClntOpts.Timeout,
"test request won't be failed by timeout")
// Create a helper watcher to ensure that async
// shutdown is set up.
helperCh := make(chan WatchEvent, 10)
helperW, herr := conn.NewWatcher("box.shutdown", func(event WatchEvent) {
helperCh <- event
})
require.Nil(t, herr)
defer helperW.Unregister()
<-helperCh
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
fut := conn.Do(req)
// SIGTERM the server.
shutdownStart := time.Now()
require.Nil(t, inst.Cmd.Process.Signal(syscall.SIGTERM))
// Check that we can't send new requests after shutdown starts.
// Retry helps to wait a bit until server starts to shutdown
// and send us the shutdown event.
shutdownWaitRetries := 5
shutdownWaitTimeout := 100 * time.Millisecond
err = test_helpers.Retry(func(interface{}) error {
_, err = conn.Do(NewPingRequest()).Get()
if err == nil {
return fmt.Errorf("expected error for requests sent on shutdown")
}
if err.Error() != "server shutdown in progress (0x4005)" {
return err
}
return nil
}, nil, shutdownWaitRetries, shutdownWaitTimeout)
require.Nil(t, err)
// Check that requests started before the shutdown finish successfully.
data, err := fut.Get()
require.Nil(t, err)
require.Equal(t, data, []interface{}{evalMsg})
// Wait until server go down.
// Server will go down only when it process all requests from our connection
// (or on timeout).
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
shutdownFinish := time.Now()
shutdownTime := shutdownFinish.Sub(shutdownStart)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
// Check that it wasn't a timeout.
require.Lessf(t,
shutdownTime,
time.Duration(serverShutdownTimeout/2)*time.Second,
"server went down not by timeout")
// Connection is unavailable when server is down.
require.Equal(t, false, conn.ConnectedNow())
}
func TestGracefulShutdown(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var conn *Connection
var err error
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn = test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
testGracefulShutdown(t, conn, &inst)
}
func TestCloseGraceful(t *testing.T) {
opts := Opts{
Timeout: shtdnClntOpts.Timeout,
}
testDialer := shtdnDialer
testDialer.RequiredProtocolInfo = ProtocolInfo{}
testSrvOpts := shtdnSrvOpts
testSrvOpts.Dialer = testDialer
inst, err := test_helpers.StartTarantool(testSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn := test_helpers.ConnectWithValidation(t, testDialer, opts)
defer conn.Close()
// Send request with sleep.
evalSleep := 3 // In seconds.
require.Lessf(t,
time.Duration(evalSleep)*time.Second,
shtdnClntOpts.Timeout,
"test request won't be failed by timeout")
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
fut := conn.Do(req)
go func() {
// CloseGraceful closes the connection gracefully.
conn.CloseGraceful()
// Connection is closed.
assert.Equal(t, true, conn.ClosedNow())
}()
// Check that a request rejected if graceful shutdown in progress.
time.Sleep((time.Duration(evalSleep) * time.Second) / 2)
_, err = conn.Do(NewPingRequest()).Get()
assert.ErrorContains(t, err, "server shutdown in progress")
// Check that a previous request was successful.
resp, err := fut.Get()
assert.Nilf(t, err, "sleep request no error")
assert.NotNilf(t, resp, "sleep response exists")
}
func TestGracefulShutdownWithReconnect(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var err error
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn := test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
testGracefulShutdown(t, conn, &inst)
err = test_helpers.RestartTarantool(&inst)
require.Nilf(t, err, "Failed to restart tarantool")
connected := test_helpers.WaitUntilReconnected(conn, shtdnClntOpts.MaxReconnects,
shtdnClntOpts.Reconnect)
require.Truef(t, connected, "Reconnect success")
testGracefulShutdown(t, conn, &inst)
}
func TestNoGracefulShutdown(t *testing.T) {
// No watchers = no graceful shutdown.
noSthdClntOpts := opts
noShtdDialer := shtdnDialer
noShtdDialer.RequiredProtocolInfo = ProtocolInfo{}
test_helpers.SkipIfWatchersSupported(t)
var inst test_helpers.TarantoolInstance
var conn *Connection
var err error
testSrvOpts := shtdnSrvOpts
testSrvOpts.Dialer = noShtdDialer
inst, err = test_helpers.StartTarantool(testSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn = test_helpers.ConnectWithValidation(t, noShtdDialer, noSthdClntOpts)
defer conn.Close()
evalSleep := 10 // in seconds
serverShutdownTimeout := 60 // in seconds
require.Less(t, evalSleep, serverShutdownTimeout)
// Send request with sleep.
require.Lessf(t,
time.Duration(evalSleep)*time.Second,
shtdnClntOpts.Timeout,
"test request won't be failed by timeout")
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
fut := conn.Do(req)
// SIGTERM the server.
shutdownStart := time.Now()
require.Nil(t, inst.Cmd.Process.Signal(syscall.SIGTERM))
// Check that request was interrupted.
_, err = fut.Get()
require.NotNilf(t, err, "sleep request error")
// Wait until server go down.
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
shutdownFinish := time.Now()
shutdownTime := shutdownFinish.Sub(shutdownStart)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
// Check that server finished without waiting for eval to finish.
require.Lessf(t,
shutdownTime,
time.Duration(evalSleep/2)*time.Second,
"server went down without any additional waiting")
}
func TestGracefulShutdownRespectsClose(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var conn *Connection
var err error
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn = test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
// Create a helper watcher to ensure that async
// shutdown is set up.
helperCh := make(chan WatchEvent, 10)
helperW, herr := conn.NewWatcher("box.shutdown", func(event WatchEvent) {
helperCh <- event
})
require.Nil(t, herr)
defer helperW.Unregister()
<-helperCh
// Set a big timeout so it would be easy to differ
// if server went down on timeout or after all connections were terminated.
serverShutdownTimeout := 60 // in seconds
_, err = conn.Call("box.ctl.set_on_shutdown_timeout", []interface{}{serverShutdownTimeout})
require.Nil(t, err)
// Send request with sleep.
evalSleep := 10 // in seconds
require.Lessf(t,
time.Duration(evalSleep)*time.Second,
shtdnClntOpts.Timeout,
"test request won't be failed by timeout")
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
fut := conn.Do(req)
// SIGTERM the server.
shutdownStart := time.Now()
require.Nil(t, inst.Cmd.Process.Signal(syscall.SIGTERM))
// Close the connection.
conn.Close()
// Connection is closed.
require.Equal(t, true, conn.ClosedNow())
// Check that request was interrupted.
_, err = fut.Get()
require.NotNilf(t, err, "sleep request error")
// Wait until server go down.
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
shutdownFinish := time.Now()
shutdownTime := shutdownFinish.Sub(shutdownStart)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
// Check that server finished without waiting for eval to finish.
require.Lessf(t,
shutdownTime,
time.Duration(evalSleep/2)*time.Second,
"server went down without any additional waiting")
// Check that it wasn't a timeout.
require.Lessf(t,
shutdownTime,
time.Duration(serverShutdownTimeout/2)*time.Second,
"server went down not by timeout")
// Connection is still closed.
require.Equal(t, true, conn.ClosedNow())
}
func TestGracefulShutdownNotRacesWithRequestReconnect(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var conn *Connection
var err error
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn = test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
// Create a helper watcher to ensure that async
// shutdown is set up.
helperCh := make(chan WatchEvent, 10)
helperW, herr := conn.NewWatcher("box.shutdown", func(event WatchEvent) {
helperCh <- event
})
require.Nil(t, herr)
defer helperW.Unregister()
<-helperCh
// Set a small timeout so server will shutdown before requesst finishes.
serverShutdownTimeout := 1 // in seconds
_, err = conn.Call("box.ctl.set_on_shutdown_timeout", []interface{}{serverShutdownTimeout})
require.Nil(t, err)
// Send request with sleep.
evalSleep := 5 // in seconds
require.Lessf(t,
serverShutdownTimeout,
evalSleep,
"test request will be failed by timeout")
require.Lessf(t,
time.Duration(serverShutdownTimeout)*time.Second,
shtdnClntOpts.Timeout,
"test request will be failed by timeout")
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
evalStart := time.Now()
fut := conn.Do(req)
// SIGTERM the server.
require.Nil(t, inst.Cmd.Process.Signal(syscall.SIGTERM))
// Wait until server go down.
// Server is expected to go down on timeout.
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
// Check that request failed by server disconnect, not a client timeout.
_, err = fut.Get()
require.NotNil(t, err)
require.NotContains(t, err.Error(), "client timeout for request")
evalFinish := time.Now()
evalTime := evalFinish.Sub(evalStart)
// Check that it wasn't a client timeout.
require.Lessf(t,
evalTime,
shtdnClntOpts.Timeout,
"server went down not by timeout")
}
func TestGracefulShutdownCloseConcurrent(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var err error
var srvShtdnStart, srvShtdnFinish time.Time
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn := test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
// Create a helper watcher to ensure that async
// shutdown is set up.
helperCh := make(chan WatchEvent, 10)
helperW, herr := conn.NewWatcher("box.shutdown", func(event WatchEvent) {
helperCh <- event
})
require.Nil(t, herr)
defer helperW.Unregister()
<-helperCh
// Set a big timeout so it would be easy to differ
// if server went down on timeout or after all connections were terminated.
serverShutdownTimeout := 60 // in seconds
_, err = conn.Call("box.ctl.set_on_shutdown_timeout", []interface{}{serverShutdownTimeout})
require.Nil(t, err)
conn.Close()
const testConcurrency = 50
var caseWg, srvToStop, srvStop sync.WaitGroup
caseWg.Add(testConcurrency)
srvToStop.Add(testConcurrency)
srvStop.Add(1)
// Create many connections.
for i := 0; i < testConcurrency; i++ {
go func(i int) {
defer caseWg.Done()
ctx, cancel := test_helpers.GetConnectContext()
defer cancel()
// Do not wait till Tarantool register out watcher,
// test everything is ok even on async.
conn, err := Connect(ctx, shtdnDialer, shtdnClntOpts)
if err != nil {
t.Errorf("Failed to connect: %s", err)
} else {
defer conn.Close()
}
// Wait till all connections created.
srvToStop.Done()
srvStop.Wait()
}(i)
}
var sret error
go func(inst *test_helpers.TarantoolInstance) {
srvToStop.Wait()
srvShtdnStart = time.Now()
cerr := inst.Cmd.Process.Signal(syscall.SIGTERM)
if cerr != nil {
sret = cerr
}
srvStop.Done()
}(&inst)
srvStop.Wait()
require.Nil(t, sret, "No errors on server SIGTERM")
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
srvShtdnFinish = time.Now()
srvShtdnTime := srvShtdnFinish.Sub(srvShtdnStart)
require.Less(t,
srvShtdnTime,
time.Duration(serverShutdownTimeout/2)*time.Second,
"server went down not by timeout")
}
func TestGracefulShutdownConcurrent(t *testing.T) {
test_helpers.SkipIfWatchersUnsupported(t)
var inst test_helpers.TarantoolInstance
var err error
var srvShtdnStart, srvShtdnFinish time.Time
inst, err = test_helpers.StartTarantool(shtdnSrvOpts)
require.Nil(t, err)
defer test_helpers.StopTarantoolWithCleanup(inst)
conn := test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
// Set a big timeout so it would be easy to differ
// if server went down on timeout or after all connections were terminated.
serverShutdownTimeout := 60 // in seconds
_, err = conn.Call("box.ctl.set_on_shutdown_timeout", []interface{}{serverShutdownTimeout})
require.Nil(t, err)
conn.Close()
const testConcurrency = 50
var caseWg, srvToStop, srvStop sync.WaitGroup
caseWg.Add(testConcurrency)
srvToStop.Add(testConcurrency)
srvStop.Add(1)
// Create many connections.
var ret error
for i := 0; i < testConcurrency; i++ {
go func(i int) {
defer caseWg.Done()
conn := test_helpers.ConnectWithValidation(t, shtdnDialer, shtdnClntOpts)
defer conn.Close()
// Create a helper watcher to ensure that async
// shutdown is set up.
helperCh := make(chan WatchEvent, 10)
helperW, _ := conn.NewWatcher("box.shutdown", func(event WatchEvent) {
helperCh <- event
})
defer helperW.Unregister()
<-helperCh
evalSleep := 1 // in seconds
req := NewEvalRequest(evalBody).Args([]interface{}{evalSleep, evalMsg})
fut := conn.Do(req)
// Wait till all connections had started sleeping.
srvToStop.Done()
srvStop.Wait()
_, gerr := fut.Get()
if gerr != nil {
ret = gerr
}
}(i)
}
var sret error
go func(inst *test_helpers.TarantoolInstance) {
srvToStop.Wait()
srvShtdnStart = time.Now()
cerr := inst.Cmd.Process.Signal(syscall.SIGTERM)
if cerr != nil {
sret = cerr
}
srvStop.Done()
}(&inst)
srvStop.Wait()
require.Nil(t, sret, "No errors on server SIGTERM")
caseWg.Wait()
require.Nil(t, ret, "No errors on concurrent wait")
_, err = inst.Cmd.Process.Wait()
require.Nil(t, err)
// Help test helpers to properly clean up.
inst.Cmd.Process = nil
srvShtdnFinish = time.Now()
srvShtdnTime := srvShtdnFinish.Sub(srvShtdnStart)
require.Less(t,
srvShtdnTime,
time.Duration(serverShutdownTimeout/2)*time.Second,
"server went down not by timeout")
}