-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
decommission.go
674 lines (597 loc) · 19.6 KB
/
decommission.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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
// Copyright 2018 The Cockroach 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. See the AUTHORS file
// for names of contributors.
package main
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"io"
"os/exec"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/kr/pretty"
_ "github.com/lib/pq"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
// TODO(tschottdorf): verify that the logs don't contain the messages
// that would spam the log before #23605. I wonder if we should really
// start grepping the logs. An alternative is to introduce a metric
// that would have signaled this and check that instead.
func runDecommission(t *test, c *cluster, nodes int, duration time.Duration) {
ctx := context.Background()
const defaultReplicationFactor = 3
// The number of nodes we're going to cycle through. Since we're sometimes
// killing the nodes and then removing them, this means having to be careful
// with loss of quorum. So only ever touch a fixed minority of nodes and
// swap them out for as long as the test runs. The math boils down to `1`,
// but conceivably we'll want to run a test with replication factor five
// at some point.
numDecom := (defaultReplicationFactor - 1) / 2
c.Put(ctx, workload, "./workload", c.Node(nodes))
c.Put(ctx, cockroach, "./cockroach", c.All())
for i := 1; i <= numDecom; i++ {
c.Start(ctx, c.Node(i), startArgs(fmt.Sprintf("-a=--attrs=node%d", i)))
}
c.Start(ctx, c.Range(numDecom+1, nodes))
c.Run(ctx, c.Node(nodes), `./workload init kv --drop`)
waitReplicatedAwayFrom := func(downNodeID string) error {
db := c.Conn(ctx, nodes)
defer db.Close()
for {
var count int
if err := db.QueryRow(
// Check if the down node has any replicas.
"SELECT count(*) FROM crdb_internal.ranges WHERE array_position(replicas, $1) IS NOT NULL",
downNodeID,
).Scan(&count); err != nil {
return err
}
if count == 0 {
fullReplicated := false
if err := db.QueryRow(
// Check if all ranges are fully replicated.
"SELECT min(array_length(replicas, 1)) >= 3 FROM crdb_internal.ranges",
).Scan(&fullReplicated); err != nil {
return err
}
if fullReplicated {
break
}
}
time.Sleep(time.Second)
}
return nil
}
waitUpReplicated := func(targetNodeID string) error {
db := c.Conn(ctx, nodes)
defer db.Close()
for ok := false; !ok; {
stmtReplicaCount := fmt.Sprintf(
`SELECT count(*) = 0 FROM crdb_internal.ranges WHERE array_position(replicas, %s) IS NULL and database = 'kv';`, targetNodeID)
t.Status(stmtReplicaCount)
if err := db.QueryRow(stmtReplicaCount).Scan(&ok); err != nil {
return err
}
time.Sleep(time.Second)
}
return nil
}
if err := waitReplicatedAwayFrom("0" /* no down node */); err != nil {
t.Fatal(err)
}
loadDuration := " --duration=" + duration.String()
workloads := []string{
// TODO(tschottdorf): in remote mode, the ui shows that we consistently write
// at 330 qps (despite asking for 500 below). Locally we get 500qps (and a lot
// more without rate limiting). Check what's up with that.
"./workload run kv --max-rate 500 --tolerate-errors" + loadDuration + " {pgurl:1-%d}",
}
run := func(stmt string) {
db := c.Conn(ctx, nodes)
defer db.Close()
t.Status(stmt)
_, err := db.ExecContext(ctx, stmt)
if err != nil {
t.Fatal(err)
}
c.l.Printf("run: %s\n", stmt)
}
var m *errgroup.Group // see comment in version.go
m, ctx = errgroup.WithContext(ctx)
for i, cmd := range workloads {
cmd := cmd // copy is important for goroutine
i := i // ditto
cmd = fmt.Sprintf(cmd, nodes)
m.Go(func() error {
quietL, err := c.l.ChildLogger("kv-"+strconv.Itoa(i), quietStdout)
if err != nil {
return err
}
defer quietL.close()
return c.RunL(ctx, quietL, c.Node(nodes), cmd)
})
}
m.Go(func() error {
nodeID := func(node int) (string, error) {
dbNode := c.Conn(ctx, node)
defer dbNode.Close()
var nodeID string
if err := dbNode.QueryRow(`SELECT node_id FROM crdb_internal.node_runtime_info LIMIT 1`).Scan(&nodeID); err != nil {
return "", nil
}
return nodeID, nil
}
stop := func(node int) error {
port := fmt.Sprintf("{pgport:%d}", node)
defer time.Sleep(time.Second) // work around quit returning too early
return c.RunE(ctx, c.Node(node), "./cockroach quit --insecure --host=:"+port)
}
decom := func(id string) error {
port := fmt.Sprintf("{pgport:%d}", nodes) // always use last node
t.Status("decommissioning node", id)
return c.RunE(ctx, c.Node(nodes), "./cockroach node decommission --insecure --wait=live --host=:"+port+" "+id)
}
for tBegin, whileDown, node := timeutil.Now(), true, 1; timeutil.Since(tBegin) <= duration; whileDown, node = !whileDown, (node%numDecom)+1 {
t.Status(fmt.Sprintf("decommissioning %d (down=%t)", node, whileDown))
id, err := nodeID(node)
if err != nil {
return err
}
run(fmt.Sprintf(`ALTER RANGE default EXPERIMENTAL CONFIGURE ZONE 'constraints: {"+node%d"}'`, node))
if err := waitUpReplicated(id); err != nil {
return err
}
if whileDown {
if err := stop(node); err != nil {
return err
}
}
run(fmt.Sprintf(`ALTER RANGE default EXPERIMENTAL CONFIGURE ZONE 'constraints: {"-node%d"}'`, node))
if err := decom(id); err != nil {
return err
}
if err := waitReplicatedAwayFrom(id); err != nil {
return err
}
if !whileDown {
if err := stop(node); err != nil {
return err
}
}
if err := c.RunE(ctx, c.Node(node), "rm -rf {store-dir}"); err != nil {
return err
}
db := c.Conn(ctx, 1)
defer db.Close()
c.Start(ctx, c.Node(node), startArgs(fmt.Sprintf("-a=--join %s --attrs=node%d",
c.InternalAddr(ctx, c.Node(nodes))[0], node)))
}
// TODO(tschottdorf): run some ui sanity checks about decommissioned nodes
// having disappeared. Verify that the workloads don't dip their qps or
// show spikes in latencies.
return nil
})
if err := m.Wait(); err != nil {
t.Fatal(err)
}
}
func registerDecommission(r *registry) {
const numNodes = 4
duration := time.Hour
r.Add(testSpec{
Name: fmt.Sprintf("decommission/nodes=%d/duration=%s", numNodes, duration),
Nodes: nodes(numNodes),
Stable: true, // DO NOT COPY to new tests
Run: func(ctx context.Context, t *test, c *cluster) {
if local {
duration = 3 * time.Minute
c.l.Printf("running with duration=%s in local mode\n", duration)
}
runDecommission(t, c, numNodes, duration)
},
})
}
func runDecommissionAcceptance(ctx context.Context, t *test, c *cluster) {
args := startArgs("--sequential", "--env=COCKROACH_SCAN_MAX_IDLE_TIME=5ms")
c.Put(ctx, cockroach, "./cockroach")
c.Start(ctx, args)
execCLI := func(
ctx context.Context,
runNode int,
extraArgs ...string,
) (string, error) {
args := []string{"ssh", c.makeNodes(c.Node(runNode)), "--", cockroach}
args = append(args, extraArgs...)
args = append(args, "--insecure")
args = append(args, fmt.Sprintf("--port={pgport:%d}", runNode))
c.l.Printf("> roachprod %s\n", strings.Join(args, " "))
var buf bytes.Buffer
cmd := exec.CommandContext(ctx, roachprod, args...)
cmd.Stdout = io.MultiWriter(c.l.stdout, &buf)
cmd.Stderr = cmd.Stdout
err := cmd.Run()
return buf.String(), err
}
decommission := func(
ctx context.Context,
runNode int,
targetNodes nodeListOption,
verbs ...string,
) (string, error) {
args := []string{"node"}
args = append(args, verbs...)
for _, target := range targetNodes {
args = append(args, strconv.Itoa(target))
}
return execCLI(ctx, runNode, args...)
}
matchCSV := func(csvStr string, matchColRow [][]string) (err error) {
defer func() {
if err != nil {
err = errors.Errorf("csv input:\n%v\nexpected:\n%s\nerrors:%s",
csvStr, pretty.Sprint(matchColRow), err)
}
}()
reader := csv.NewReader(strings.NewReader(csvStr))
reader.FieldsPerRecord = -1
records, err := reader.ReadAll()
if err != nil {
return err
}
lr, lm := len(records), len(matchColRow)
if lr < lm {
return errors.Errorf("csv has %d rows, but expected at least %d", lr, lm)
}
// Compare only the last len(matchColRow) records. That is, if we want to
// match 4 rows and we have 100 records, we only really compare
// records[96:], that is, the last four rows.
records = records[lr-lm:]
for i := range records {
if lr, lm := len(records[i]), len(matchColRow[i]); lr != lm {
return errors.Errorf("row #%d: csv has %d columns, but expected %d", i+1, lr, lm)
}
for j := range records[i] {
pat, str := matchColRow[i][j], records[i][j]
re := regexp.MustCompile(pat)
if !re.MatchString(str) {
err = errors.Errorf("%v\nrow #%d, col #%d: found %q which does not match %q",
err, i+1, j+1, str, pat)
}
}
}
return err
}
decommissionHeader := []string{
"id", "is_live", "replicas", "is_decommissioning", "is_draining",
}
decommissionFooter := []string{
"No more data reported on target nodes. " +
"Please verify cluster health before removing the nodes.",
}
statusHeader := []string{
"id", "address", "build", "started_at", "updated_at", "is_live",
}
waitLiveDeprecated := "--wait=live is deprecated and is treated as --wait=all"
c.l.Printf("decommissioning first node from the second, polling the status manually\n")
retryOpts := retry.Options{
InitialBackoff: time.Second,
MaxBackoff: 5 * time.Second,
Multiplier: 1,
MaxRetries: 20,
}
for r := retry.Start(retryOpts); r.Next(); {
o, err := decommission(ctx, 2, c.Node(1),
"decommission", "--wait", "none", "--format", "csv")
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
exp := [][]string{
decommissionHeader,
{"1", "true", "0", "true", "false"},
decommissionFooter,
}
if err := matchCSV(o, exp); err != nil {
continue
}
break
}
// Check that even though the node is decommissioned, we still see it (since
// it remains live) in `node ls`.
{
o, err := execCLI(ctx, 2, "node", "ls", "--format", "csv")
if err != nil {
t.Fatalf("node-ls failed: %v", err)
}
exp := [][]string{
{"id"},
{"1"},
{"2"},
{"3"},
{"4"},
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
}
// Ditto `node status`.
{
o, err := execCLI(ctx, 2, "node", "status", "--format", "csv")
if err != nil {
t.Fatalf("node-status failed: %v", err)
}
exp := [][]string{
statusHeader,
{`1`, `.*`, `.*`, `.*`, `.*`, `.*`},
{`2`, `.*`, `.*`, `.*`, `.*`, `.*`},
{`3`, `.*`, `.*`, `.*`, `.*`, `.*`},
{`4`, `.*`, `.*`, `.*`, `.*`, `.*`},
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
}
c.l.Printf("recommissioning first node (from third node)\n")
if _, err := decommission(ctx, 3, c.Node(1), "recommission"); err != nil {
t.Fatalf("recommission failed: %v", err)
}
c.l.Printf("decommissioning second node from third, using --wait=all\n")
{
o, err := decommission(ctx, 3, c.Node(2),
"decommission", "--wait", "all", "--format", "csv")
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
exp := [][]string{
decommissionHeader,
{"2", "true", "0", "true", "false"},
decommissionFooter,
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
}
c.l.Printf("recommissioning second node from itself\n")
if _, err := decommission(ctx, 2, c.Node(2), "recommission"); err != nil {
t.Fatalf("recommission failed: %v", err)
}
c.l.Printf("decommissioning third node via `quit --decommission`\n")
func() {
// This should not take longer than five minutes, and if it does, it's
// likely stuck forever and we want to see the output.
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
if _, err := execCLI(timeoutCtx, 3, "quit", "--decommission"); err != nil {
if timeoutCtx.Err() != nil {
t.Fatalf("quit --decommission failed: %s", err)
}
// TODO(tschottdorf): grep the process output for the string announcing success?
c.l.Errorf("WARNING: ignoring error on quit --decommission: %s\n", err)
}
}()
// Now that the third node is down and decommissioned, decommissioning it
// again should be a no-op. We do it from node one but as always it doesn't
// matter.
c.l.Printf("checking that other nodes see node three as successfully decommissioned\n")
{
o, err := decommission(ctx, 2, c.Node(3),
"decommission", "--format", "csv") // wait=all is implied
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
exp := [][]string{
decommissionHeader,
// Expect the same as usual, except this time the node should be draining
// because it shut down cleanly (thanks to `quit --decommission`).
{"3", "true", "0", "true", "true"},
decommissionFooter,
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
// Bring the node back up. It's still decommissioned, so it won't be of much use.
c.Stop(ctx, c.Node(3))
c.Start(ctx, c.Node(3), args)
// Recommission. Welcome back!
if _, err = decommission(ctx, 2, c.Node(3), "recommission"); err != nil {
t.Fatalf("recommission failed: %v", err)
}
}
// Kill the first node and verify that we can decommission it while it's down,
// bringing it back up to verify that its replicas still get removed.
c.l.Printf("intentionally killing first node\n")
c.Stop(ctx, c.Node(1))
c.l.Printf("decommission first node, starting with it down but restarting it for verification\n")
{
o, err := decommission(ctx, 2, c.Node(1),
"decommission", "--wait", "live")
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
if strings.Split(o, "\n")[1] != waitLiveDeprecated {
t.Fatal("missing deprecate message for --wait=live")
}
c.Start(ctx, c.Node(1), args)
// Run a second time to wait until the replicas have all been GC'ed.
// Note that we specify "all" because even though the first node is
// now running, it may not be live by the time the command runs.
o, err = decommission(ctx, 2, c.Node(1),
"decommission", "--wait", "all", "--format", "csv")
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
exp := [][]string{
decommissionHeader,
{"1", "true|false", "0", "true", "false"},
decommissionFooter,
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
}
// Now we want to test decommissioning a truly dead node. Make sure we don't
// waste too much time waiting for the node to be recognized as dead. Note that
// we don't want to set this number too low or everything will seem dead to the
// allocator at all times, so nothing will ever happen.
func() {
db := c.Conn(ctx, 2)
defer db.Close()
const stmt = "SET CLUSTER SETTING server.time_until_store_dead = '1m15s'"
if _, err := db.ExecContext(ctx, stmt); err != nil {
t.Fatal(err)
}
}()
c.l.Printf("intentionally killing first node\n")
c.Stop(ctx, c.Node(1))
// It is being decommissioned in absentia, meaning that its replicas are
// being removed due to deadness. We can't see that reflected in the output
// since the current mechanism gets its replica counts from what the node
// reports about itself, so our assertion here is somewhat weak.
c.l.Printf("decommission first node in absentia using --wait=live\n")
{
o, err := decommission(ctx, 3, c.Node(1),
"decommission", "--wait", "live", "--format", "csv")
if err != nil {
t.Fatalf("decommission failed: %v", err)
}
// Note we don't check precisely zero replicas (which the node would write
// itself, but it's dead). We do check that the node isn't live, though, which
// is essentially what `--wait=live` waits for.
// Note that the target node may still be "live" when it's marked as
// decommissioned, as its replica count may drop to zero faster than
// liveness times out.
exp := [][]string{
decommissionHeader,
{"1", `true|false`, "0", `true`, `false`},
decommissionFooter,
}
if err := matchCSV(o, exp); err != nil {
t.Fatal(err)
}
if strings.Split(o, "\n")[1] != waitLiveDeprecated {
t.Fatal("missing deprecate message for --wait=live")
}
}
// Check that (at least after a bit) the node disappears from `node ls`
// because it is decommissioned and not live.
for {
o, err := execCLI(ctx, 2, "node", "ls", "--format", "csv")
if err != nil {
t.Fatalf("node-ls failed: %v", err)
}
exp := [][]string{
{"id"},
{"2"},
{"3"},
{"4"},
}
if err := matchCSV(o, exp); err != nil {
time.Sleep(time.Second)
continue
}
break
}
for {
o, err := execCLI(ctx, 2, "node", "status", "--format", "csv")
if err != nil {
t.Fatalf("node-status failed: %v", err)
}
exp := [][]string{
statusHeader,
{`2`, `.*`, `.*`, `.*`, `.*`, `.*`},
{`3`, `.*`, `.*`, `.*`, `.*`, `.*`},
{`4`, `.*`, `.*`, `.*`, `.*`, `.*`},
}
if err := matchCSV(o, exp); err != nil {
time.Sleep(time.Second)
continue
}
break
}
if err := retry.ForDuration(time.Minute, func() error {
// Verify the event log has recorded exactly one decommissioned or
// recommissioned event for each commissioning operation.
//
// Spurious errors appear to be possible since we might be trying to
// send RPCs to the (relatively recently) down node:
//
// pq: rpc error: code = Unavailable desc = grpc: the connection is
// unavailable
//
// Seen in https://teamcity.cockroachdb.com/viewLog.html?buildId=344802.
db := c.Conn(ctx, 2)
defer db.Close()
rows, err := db.Query(`
SELECT "eventType", "targetID" FROM system.eventlog
WHERE "eventType" IN ($1, $2) ORDER BY timestamp`,
"node_decommissioned", "node_recommissioned",
)
if err != nil {
c.l.Printf("retrying: %v\n", err)
return err
}
defer rows.Close()
matrix, err := sqlutils.RowsToStrMatrix(rows)
if err != nil {
return err
}
expMatrix := [][]string{
{"node_decommissioned", "1"},
{"node_recommissioned", "1"},
{"node_decommissioned", "2"},
{"node_recommissioned", "2"},
{"node_decommissioned", "3"},
{"node_recommissioned", "3"},
{"node_decommissioned", "1"},
}
if !reflect.DeepEqual(matrix, expMatrix) {
t.Fatalf("unexpected diff(matrix, expMatrix):\n%s", pretty.Diff(matrix, expMatrix))
}
return nil
}); err != nil {
t.Fatal(err)
}
// Last, verify that the operator can't shoot themselves in the foot by
// accidentally decommissioning all nodes.
//
// Specify wait=none because the command would block forever (the replicas have
// nowhere to go).
if _, err := decommission(ctx, 2, c.All(), "decommission", "--wait", "none"); err != nil {
t.Fatalf("decommission failed: %v", err)
}
// Check that we can still do stuff. Creating a database should be good enough.
db := c.Conn(ctx, 2)
defer db.Close()
if _, err := db.Exec(`CREATE DATABASE still_working;`); err != nil {
t.Fatal(err)
}
// Recommission all nodes.
if _, err := decommission(ctx, 2, c.All(), "recommission"); err != nil {
t.Fatalf("recommission failed: %v", err)
}
// To verify that all nodes are actually accepting replicas again, decommission
// the first nodes (blocking until it's done). This proves that the other nodes
// absorb the first one's replicas.
if _, err := decommission(ctx, 2, c.Node(1), "decommission"); err != nil {
t.Fatalf("decommission failed: %v", err)
}
}