-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
node.go
454 lines (402 loc) · 12.2 KB
/
node.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
// Copyright 2015 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 cli
import (
"context"
"fmt"
"math"
"os"
"reflect"
"strconv"
"time"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
localTimeFormat = "2006-01-02 15:04:05.999999-07:00"
)
var lsNodesColumnHeaders = []string{
"id",
}
var lsNodesCmd = &cobra.Command{
Use: "ls",
Short: "lists the IDs of all nodes in the cluster",
Long: `
Display the node IDs for all active (that is, running and not decommissioned) members of the cluster.
To retrieve the IDs for inactive members, see 'node status --decommission'.
`,
Args: cobra.NoArgs,
RunE: MaybeDecorateGRPCError(runLsNodes),
}
func runLsNodes(cmd *cobra.Command, args []string) error {
conn, err := makeSQLClient("cockroach node ls", useSystemDb)
if err != nil {
return err
}
defer conn.Close()
if cliCtx.cmdTimeout != 0 {
if err := conn.Exec(fmt.Sprintf("SET statement_timeout=%d", cliCtx.cmdTimeout), nil); err != nil {
return err
}
}
_, rows, err := runQuery(
conn,
makeQuery(`SELECT node_id FROM crdb_internal.gossip_liveness
WHERE decommissioning = false OR split_part(expiration,',',1)::decimal > now()::decimal`),
false,
)
if err != nil {
return err
}
return printQueryOutput(os.Stdout, lsNodesColumnHeaders, newRowSliceIter(rows, "r"))
}
var baseNodeColumnHeaders = []string{
"id",
"address",
"sql_address",
"build",
"started_at",
"updated_at",
"locality",
"is_available",
"is_live",
}
var statusNodesColumnHeadersForRanges = []string{
"replicas_leaders",
"replicas_leaseholders",
"ranges",
"ranges_unavailable",
"ranges_underreplicated",
}
var statusNodesColumnHeadersForStats = []string{
"live_bytes",
"key_bytes",
"value_bytes",
"intent_bytes",
"system_bytes",
}
var statusNodesColumnHeadersForDecommission = []string{
"gossiped_replicas",
"is_decommissioning",
"is_draining",
}
var statusNodeCmd = &cobra.Command{
Use: "status [<node id>]",
Short: "shows the status of a node or all nodes",
Long: `
If a node ID is specified, this will show the status for the corresponding node. If no node ID
is specified, this will display the status for all nodes in the cluster.
`,
Args: cobra.MaximumNArgs(1),
RunE: MaybeDecorateGRPCError(runStatusNode),
}
func runStatusNode(cmd *cobra.Command, args []string) error {
_, rows, err := runStatusNodeInner(nodeCtx.statusShowDecommission || nodeCtx.statusShowAll, args)
if err != nil {
return err
}
sliceIter := newRowSliceIter(rows, getStatusNodeAlignment())
return printQueryOutput(os.Stdout, getStatusNodeHeaders(), sliceIter)
}
func runStatusNodeInner(showDecommissioned bool, args []string) ([]string, [][]string, error) {
joinUsingID := func(queries []string) (query string) {
for i, q := range queries {
if i == 0 {
query = q
continue
}
query = "(" + query + ") LEFT JOIN (" + q + ") USING (id)"
}
return
}
maybeAddActiveNodesFilter := func(query string) string {
if !showDecommissioned {
query += " WHERE decommissioning = false OR split_part(expiration,',',1)::decimal > now()::decimal"
}
return query
}
baseQuery := maybeAddActiveNodesFilter(
`SELECT node_id AS id,
address,
sql_address,
build_tag AS build,
started_at,
updated_at,
locality,
CASE WHEN split_part(expiration,',',1)::decimal > now()::decimal
THEN true
ELSE false
END AS is_available,
ifnull(is_live, false)
FROM crdb_internal.gossip_liveness LEFT JOIN crdb_internal.gossip_nodes USING (node_id)`,
)
const rangesQuery = `
SELECT node_id AS id,
sum((metrics->>'replicas.leaders')::DECIMAL)::INT AS replicas_leaders,
sum((metrics->>'replicas.leaseholders')::DECIMAL)::INT AS replicas_leaseholders,
sum((metrics->>'replicas')::DECIMAL)::INT AS ranges,
sum((metrics->>'ranges.unavailable')::DECIMAL)::INT AS ranges_unavailable,
sum((metrics->>'ranges.underreplicated')::DECIMAL)::INT AS ranges_underreplicated
FROM crdb_internal.kv_store_status
GROUP BY node_id`
const statsQuery = `
SELECT node_id AS id,
sum((metrics->>'livebytes')::DECIMAL)::INT AS live_bytes,
sum((metrics->>'keybytes')::DECIMAL)::INT AS key_bytes,
sum((metrics->>'valbytes')::DECIMAL)::INT AS value_bytes,
sum((metrics->>'intentbytes')::DECIMAL)::INT AS intent_bytes,
sum((metrics->>'sysbytes')::DECIMAL)::INT AS system_bytes
FROM crdb_internal.kv_store_status
GROUP BY node_id`
const decommissionQuery = `
SELECT node_id AS id,
ranges AS gossiped_replicas,
decommissioning AS is_decommissioning,
draining AS is_draining
FROM crdb_internal.gossip_liveness LEFT JOIN crdb_internal.gossip_nodes USING (node_id)`
conn, err := makeSQLClient("cockroach node status", useSystemDb)
if err != nil {
return nil, nil, err
}
defer conn.Close()
queriesToJoin := []string{baseQuery}
if nodeCtx.statusShowAll || nodeCtx.statusShowRanges {
queriesToJoin = append(queriesToJoin, rangesQuery)
}
if nodeCtx.statusShowAll || nodeCtx.statusShowStats {
queriesToJoin = append(queriesToJoin, statsQuery)
}
if nodeCtx.statusShowAll || nodeCtx.statusShowDecommission {
queriesToJoin = append(queriesToJoin, decommissionQuery)
}
if cliCtx.cmdTimeout != 0 {
if err := conn.Exec(fmt.Sprintf("SET statement_timeout=%d", cliCtx.cmdTimeout), nil); err != nil {
return nil, nil, err
}
}
queryString := "SELECT * FROM (" + joinUsingID(queriesToJoin) + ")"
switch len(args) {
case 0:
query := makeQuery(queryString + " ORDER BY id")
return runQuery(conn, query, false)
case 1:
nodeID, err := strconv.Atoi(args[0])
if err != nil {
return nil, nil, errors.Errorf("could not parse node_id %s", args[0])
}
query := makeQuery(queryString+" WHERE id = $1", nodeID)
headers, rows, err := runQuery(conn, query, false)
if err != nil {
return nil, nil, err
}
if len(rows) == 0 {
return nil, nil, errors.Errorf("node %d doesn't exist", nodeID)
}
return headers, rows, nil
default:
return nil, nil, errors.Errorf("expected no arguments or a single node ID")
}
}
func getStatusNodeHeaders() []string {
headers := baseNodeColumnHeaders
if nodeCtx.statusShowAll || nodeCtx.statusShowRanges {
headers = append(headers, statusNodesColumnHeadersForRanges...)
}
if nodeCtx.statusShowAll || nodeCtx.statusShowStats {
headers = append(headers, statusNodesColumnHeadersForStats...)
}
if nodeCtx.statusShowAll || nodeCtx.statusShowDecommission {
headers = append(headers, statusNodesColumnHeadersForDecommission...)
}
return headers
}
func getStatusNodeAlignment() string {
align := "rlllll"
if nodeCtx.statusShowAll || nodeCtx.statusShowRanges {
align += "rrrrrr"
}
if nodeCtx.statusShowAll || nodeCtx.statusShowStats {
align += "rrrrrr"
}
if nodeCtx.statusShowAll || nodeCtx.statusShowDecommission {
align += decommissionResponseAlignment()
}
return align
}
var decommissionNodesColumnHeaders = []string{
"id",
"is_live",
"replicas",
"is_decommissioning",
"is_draining",
}
var decommissionNodeCmd = &cobra.Command{
Use: "decommission <node id 1> [<node id 2> ...]",
Short: "decommissions the node(s)",
Long: `
Marks the nodes with the supplied IDs as decommissioning.
This will cause leases and replicas to be removed from these nodes.`,
Args: cobra.MinimumNArgs(1),
RunE: MaybeDecorateGRPCError(runDecommissionNode),
}
func parseNodeIDs(strNodeIDs []string) ([]roachpb.NodeID, error) {
nodeIDs := make([]roachpb.NodeID, 0, len(strNodeIDs))
for _, str := range strNodeIDs {
i, err := strconv.ParseInt(str, 10, 32)
if err != nil {
return nil, errors.Errorf("unable to parse %s: %s", str, err)
}
nodeIDs = append(nodeIDs, roachpb.NodeID(i))
}
return nodeIDs, nil
}
func runDecommissionNode(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, finish, err := getAdminClient(ctx, serverCfg)
if err != nil {
return err
}
defer finish()
return runDecommissionNodeImpl(ctx, c, nodeCtx.nodeDecommissionWait, args)
}
func runDecommissionNodeImpl(
ctx context.Context, c serverpb.AdminClient, wait nodeDecommissionWaitType, args []string,
) error {
if wait == nodeDecommissionWaitLive {
fmt.Fprintln(stderr, "\n--wait=live is deprecated and is treated as --wait=all")
}
nodeIDs, err := parseNodeIDs(args)
if err != nil {
return err
}
minReplicaCount := int64(math.MaxInt64)
opts := retry.Options{
InitialBackoff: 5 * time.Millisecond,
Multiplier: 2,
MaxBackoff: 20 * time.Second,
}
prevResponse := serverpb.DecommissionStatusResponse{}
for r := retry.StartWithCtx(ctx, opts); r.Next(); {
req := &serverpb.DecommissionRequest{
NodeIDs: nodeIDs,
Decommissioning: true,
}
resp, err := c.Decommission(ctx, req)
if err != nil {
fmt.Fprintln(stderr)
return errors.Wrap(err, "while trying to mark as decommissioning")
}
if !reflect.DeepEqual(&prevResponse, resp) {
fmt.Fprintln(stderr)
if err := printDecommissionStatus(*resp); err != nil {
return err
}
prevResponse = *resp
} else {
fmt.Fprintf(stderr, ".")
}
var replicaCount int64
allDecommissioning := true
for _, status := range resp.Status {
replicaCount += status.ReplicaCount
allDecommissioning = allDecommissioning && status.Decommissioning
}
if replicaCount == 0 && allDecommissioning {
fmt.Fprintln(os.Stdout, "\nNo more data reported on target nodes. "+
"Please verify cluster health before removing the nodes.")
return nil
}
if wait == nodeDecommissionWaitNone {
return nil
}
if replicaCount < minReplicaCount {
minReplicaCount = replicaCount
r.Reset()
}
}
return errors.New("maximum number of retries exceeded")
}
func decommissionResponseAlignment() string {
return "rcrcc"
}
// decommissionResponseValueToRows converts DecommissionStatusResponse_Status to
// SQL-like result rows, so that we can pretty-print them.
func decommissionResponseValueToRows(
statuses []serverpb.DecommissionStatusResponse_Status,
) [][]string {
// Create results that are like the results for SQL results, so that we can pretty-print them.
var rows [][]string
for _, node := range statuses {
rows = append(rows, []string{
strconv.FormatInt(int64(node.NodeID), 10),
strconv.FormatBool(node.IsLive),
strconv.FormatInt(node.ReplicaCount, 10),
strconv.FormatBool(node.Decommissioning),
strconv.FormatBool(node.Draining),
})
}
return rows
}
var recommissionNodeCmd = &cobra.Command{
Use: "recommission <node id 1> [<node id 2> ...]",
Short: "recommissions the node(s)",
Long: `
For the nodes with the supplied IDs, resets the decommissioning states,
signaling the affected nodes to participate in the cluster again.
`,
Args: cobra.MinimumNArgs(1),
RunE: MaybeDecorateGRPCError(runRecommissionNode),
}
func printDecommissionStatus(resp serverpb.DecommissionStatusResponse) error {
return printQueryOutput(os.Stdout, decommissionNodesColumnHeaders,
newRowSliceIter(decommissionResponseValueToRows(resp.Status), decommissionResponseAlignment()))
}
func runRecommissionNode(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
nodeIDs, err := parseNodeIDs(args)
if err != nil {
return err
}
c, finish, err := getAdminClient(ctx, serverCfg)
if err != nil {
return err
}
defer finish()
req := &serverpb.DecommissionRequest{
NodeIDs: nodeIDs,
Decommissioning: false,
}
resp, err := c.Decommission(ctx, req)
if err != nil {
return err
}
return printDecommissionStatus(*resp)
}
// Sub-commands for node command.
var nodeCmds = []*cobra.Command{
lsNodesCmd,
statusNodeCmd,
decommissionNodeCmd,
recommissionNodeCmd,
}
var nodeCmd = &cobra.Command{
Use: "node [command]",
Short: "list, inspect or remove nodes",
Long: "List, inspect or remove nodes.",
RunE: usageAndErr,
}
func init() {
nodeCmd.AddCommand(nodeCmds...)
}