-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdecommissionbench.go
317 lines (279 loc) · 9.57 KB
/
decommissionbench.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
// Copyright 2022 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package tests
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/cluster"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/option"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/registry"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/spec"
"github.com/cockroachdb/cockroach/pkg/cmd/roachtest/test"
"github.com/cockroachdb/cockroach/pkg/roachprod/install"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
"github.com/stretchr/testify/require"
)
const (
defaultTimeout = 1 * time.Hour
defaultSnapshotRateMb = 32
)
type decommissionBenchSpec struct {
nodes int
cpus int
warehouses int
load bool
admissionControl bool
whileDown bool
snapshotRate int
}
// registerDecommissionBench defines all decommission benchmark configurations
// and adds them to the roachtest registry.
func registerDecommissionBench(r registry.Registry) {
for _, tc := range []decommissionBenchSpec{
{
nodes: 4,
cpus: 4,
warehouses: 100,
load: true,
admissionControl: true,
},
{
nodes: 4,
cpus: 4,
warehouses: 100,
load: true,
admissionControl: true,
whileDown: true,
},
{
nodes: 4,
cpus: 16,
warehouses: 1000,
load: true,
admissionControl: true,
},
{
nodes: 4,
cpus: 16,
warehouses: 1000,
load: true,
admissionControl: false,
},
{
nodes: 4,
cpus: 16,
warehouses: 1000,
load: false,
admissionControl: false,
},
} {
registerDecommissionBenchSpec(r, tc)
}
}
// registerDecommissionBenchSpec adds a test using the specified configuration to the registry.
func registerDecommissionBenchSpec(r registry.Registry, tc decommissionBenchSpec) {
timeout := defaultTimeout
extraNameParts := []string{""}
if tc.snapshotRate == 0 {
tc.snapshotRate = defaultSnapshotRateMb
} else {
extraNameParts = append(extraNameParts, fmt.Sprintf("snapshotRate=%dmb", tc.snapshotRate))
}
if tc.whileDown {
extraNameParts = append(extraNameParts, "while-down")
}
if !tc.load {
extraNameParts = append(extraNameParts, "no-load")
}
if !tc.admissionControl {
extraNameParts = append(extraNameParts, "no-admission")
}
extraName := strings.Join(extraNameParts, "/")
r.Add(registry.TestSpec{
Name: fmt.Sprintf("decommissionBench/nodes=%d/cpu=%d/warehouses=%d%s",
tc.nodes, tc.cpus, tc.warehouses, extraName),
Owner: registry.OwnerKV,
Cluster: r.MakeClusterSpec(tc.nodes+1, spec.CPU(tc.cpus)),
Timeout: timeout,
NonReleaseBlocker: true,
Run: func(ctx context.Context, t test.Test, c cluster.Cluster) {
runDecommissionBench(ctx, t, c, tc, timeout)
},
})
}
// decommBenchTicker is a simple struct for encapsulating function
// closures to be called before and after an operation to be benchmarked.
type decommBenchTicker struct {
pre func()
post func()
}
// runDecommissionBench initializes a cluster with TPCC and attempts to
// benchmark the decommissioning of a single node picked at random. The cluster
// may or may not be running under load.
func runDecommissionBench(
ctx context.Context,
t test.Test,
c cluster.Cluster,
tc decommissionBenchSpec,
testTimeout time.Duration,
) {
// node1 is kept pinned (i.e. not decommissioned/restarted), and is the node
// through which we run decommissions. The last node is used for the workload.
pinnedNode := 1
workloadNode := tc.nodes + 1
crdbNodes := c.Range(pinnedNode, tc.nodes)
c.Put(ctx, t.Cockroach(), "./cockroach", c.All())
c.Put(ctx, t.DeprecatedWorkload(), "./workload", c.Node(workloadNode))
for i := 1; i <= tc.nodes; i++ {
startOpts := option.DefaultStartOpts()
startOpts.RoachprodOpts.ExtraArgs = append(startOpts.RoachprodOpts.ExtraArgs, fmt.Sprintf("--attrs=node%d", i))
c.Start(ctx, t.L(), startOpts, install.MakeClusterSettings(), c.Node(i))
}
database := "tpcc"
importCmd := fmt.Sprintf(`./cockroach workload fixtures import tpcc --warehouses=%d`, tc.warehouses)
workloadCmd := fmt.Sprintf("./workload run tpcc --warehouses=%d --duration=%s --histograms=%s/stats.json --tolerate-errors {pgurl:1-%d}", tc.warehouses, testTimeout, t.PerfArtifactsDir(), tc.nodes)
t.Status(fmt.Sprintf("initializing cluster with %d warehouses", tc.warehouses))
c.Run(ctx, c.Node(pinnedNode), importCmd)
SetAdmissionControl(ctx, t, c, tc.admissionControl)
{
db := c.Conn(ctx, t.L(), pinnedNode)
defer db.Close()
for _, stmt := range []string{
fmt.Sprintf(`SET CLUSTER SETTING kv.snapshot_rebalance.max_rate='%dMiB'`, tc.snapshotRate),
fmt.Sprintf(`SET CLUSTER SETTING kv.snapshot_recovery.max_rate='%dMiB'`, tc.snapshotRate),
} {
t.Status(stmt)
_, err := db.ExecContext(ctx, stmt)
if err != nil {
t.Fatal(err)
}
t.L().Printf("run: %s\n", stmt)
}
// Wait for initial up-replication.
err := WaitFor3XReplication(ctx, t, db)
require.NoError(t, err)
}
workloadCtx, workloadCancel := context.WithCancel(ctx)
m := c.NewMonitor(workloadCtx, crdbNodes)
if tc.load {
m.Go(
func(ctx context.Context) error {
// Run workload effectively indefinitely, to be later killed by context
// cancellation once decommission has completed.
err := c.RunE(ctx, c.Node(workloadNode), workloadCmd)
if errors.Is(ctx.Err(), context.Canceled) {
// Workload intentionally cancelled via context, so don't return error.
return nil
}
if err != nil {
t.L().Printf("workload error: %s", err)
}
return err
},
)
}
tick, perfBuf := initBulkJobPerfArtifacts(t.Name(), defaultTimeout)
recorder := &decommBenchTicker{pre: tick, post: tick}
m.Go(func(ctx context.Context) error {
defer workloadCancel()
h := newDecommTestHelper(t, c)
h.blockFromRandNode(workloadNode)
m.ExpectDeath()
defer m.ResetDeaths()
return runSingleDecommission(ctx, h, pinnedNode, database, tc.whileDown, false /* reuse */, recorder, nil /* upreplicateTicker */)
})
if err := m.WaitE(); err != nil {
t.Fatal(err)
}
// Store the perf artifacts on the pinned node so that the test
// runner copies it into an appropriate directory path.
dest := filepath.Join(t.PerfArtifactsDir(), "stats.json")
if err := c.RunE(ctx, c.Node(pinnedNode), "mkdir -p "+filepath.Dir(dest)); err != nil {
t.L().Errorf("failed to create perf dir: %+v", err)
}
if err := c.PutString(ctx, perfBuf.String(), dest, 0755, c.Node(pinnedNode)); err != nil {
t.L().Errorf("failed to upload perf artifacts to node: %s", err.Error())
}
}
// runSingleDecommission picks a random node and attempts to decommission that
// node from the pinned (i.e. first) node, validating and recording the duration.
// If the reuse flag is passed in, the node will be wiped and re-added to the
// cluster, with the upreplication duration tracked by upreplicateTicker.
func runSingleDecommission(
ctx context.Context,
h *decommTestHelper,
pinnedNode int,
database string,
stopFirst, reuse bool,
decommTicker, upreplicateTicker *decommBenchTicker,
) error {
target := h.getRandNodeOtherThan(pinnedNode)
h.t.Status(fmt.Sprintf("targeting node%d (n%d) for decommission", target, target))
// Pin all default ranges (all warehouses) to the node, for uniformity.
h.t.Status(fmt.Sprintf("ensuring all default ranges present on node%d", target))
if err := h.pinRangesOnNode(ctx, target, pinnedNode); err != nil {
return err
}
if err := h.waitUpReplicated(ctx, target, target, database); err != nil {
return err
}
// Gather metadata for logging purposes.
var bytesUsed, rangeCount int64
{
dbNode := h.c.Conn(ctx, h.t.L(), target)
defer dbNode.Close()
if err := dbNode.QueryRow(`SELECT sum(range_count), sum(used) FROM crdb_internal.kv_store_status where node_id = $1`, target).Scan(&rangeCount, &bytesUsed); err != nil {
return err
}
}
if stopFirst {
h.t.Status(fmt.Sprintf("gracefully stopping node%d", target))
if err := h.stop(ctx, target); err != nil {
return err
}
}
// Remove pinned ranges, so we can decommission cleanly.
// TODO(sarkesian): Consider adding a future test for decommissions that get
// stuck with replicas in purgatory.
if err := h.unpinRangesOnNode(ctx, target, pinnedNode); err != nil {
return err
}
h.t.Status(fmt.Sprintf("decommissioning node%d (n%d)", target, target))
tBegin := timeutil.Now()
decommTicker.pre()
if _, err := h.decommission(ctx, h.c.Node(target), pinnedNode, "--wait=all"); err != nil {
return err
}
if err := h.waitReplicatedAwayFrom(ctx, target, pinnedNode); err != nil {
return err
}
if err := h.checkDecommissioned(ctx, target, pinnedNode); err != nil {
return err
}
decommTicker.post()
elapsed := timeutil.Since(tBegin)
h.t.Status(fmt.Sprintf("decommissioned node%d (n%d) in %s (ranges: %d, size: %s)",
target, target, elapsed, rangeCount, humanizeutil.IBytes(bytesUsed)))
if reuse {
if !stopFirst {
h.t.Status(fmt.Sprintf("gracefully stopping node%d", target))
if err := h.stop(ctx, target); err != nil {
return err
}
}
// TODO(sarkesian): Wipe the node and re-add to cluster.
}
return nil
}