-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
nodes.ts
606 lines (557 loc) · 17.6 KB
/
nodes.ts
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
// Copyright 2018 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.
import _ from "lodash";
import { createSelector } from "reselect";
import * as protos from "src/js/protos";
import { AdminUIState } from "./state";
import { util } from "@cockroachlabs/cluster-ui";
import { Pick } from "src/util/pick";
import { NoConnection } from "src/views/reports/containers/network";
import { nullOfReturnType } from "src/util/types";
/**
* LivenessStatus is a type alias for the fully-qualified NodeLivenessStatus
* enumeration. As an enum, it needs to be imported rather than using the 'type'
* keyword.
*/
export import LivenessStatus = protos.cockroach.kv.kvserver.liveness.livenesspb.NodeLivenessStatus;
import { cockroach } from "src/js/protos";
import MembershipStatus = cockroach.kv.kvserver.liveness.livenesspb.MembershipStatus;
import INodeStatus = cockroach.server.status.statuspb.INodeStatus;
import ILocality = cockroach.roachpb.ILocality;
const { MetricConstants, BytesUsed } = util;
/**
* livenessNomenclature resolves a mismatch between the terms used for liveness
* status on our Admin UI and the terms used by the backend. Examples:
* + "Live" on the server is "Healthy" on the Admin UI
* + "Unavailable" on the server is "Suspect" on the Admin UI
*/
export function livenessNomenclature(liveness: LivenessStatus) {
switch (liveness) {
case LivenessStatus.NODE_STATUS_LIVE:
return "healthy";
case LivenessStatus.NODE_STATUS_UNAVAILABLE:
return "suspect";
case LivenessStatus.NODE_STATUS_DECOMMISSIONING:
return "decommissioning";
case LivenessStatus.NODE_STATUS_DECOMMISSIONED:
return "decommissioned";
case LivenessStatus.NODE_STATUS_DRAINING:
return "draining";
default:
return "dead";
}
}
// Functions to select data directly from the redux state.
const livenessesSelector = (state: AdminUIState) =>
state.cachedData.liveness.data;
/*
* nodeStatusesSelector returns the current status for each node in the cluster.
*/
type NodeStatusState = Pick<AdminUIState, "cachedData", "nodes">;
export const nodeStatusesSelector = (state: NodeStatusState) =>
state.cachedData.nodes.data;
// partialNodeStatusesSelector returns NodeStatus items without fields that constantly change
// and causes selectors to recompute.
const partialNodeStatusesSelector = createSelector(
nodeStatusesSelector,
(nodeStatuses: INodeStatus[]) => {
return nodeStatuses?.map((ns: INodeStatus) => {
const { metrics, store_statuses, updated_at, activity, ...rest } = ns;
return {
...rest,
store_statuses: store_statuses?.map(ss => ({ desc: ss.desc })),
};
});
},
);
export const selectNodesLastError = createSelector(
(state: AdminUIState) => state.cachedData.nodes,
nodes => nodes.lastError,
);
/*
* clusterSelector returns information about cluster.
*/
export const clusterSelector = (state: AdminUIState) =>
state.cachedData.cluster.data;
/*
* clusterIdSelector returns Cluster Id (as UUID string).
*/
export const clusterIdSelector = createSelector(
clusterSelector,
clusterInfo => clusterInfo && clusterInfo.cluster_id,
);
/*
* selectNodeRequestStatus returns the current status of the node status request.
*/
export function selectNodeRequestStatus(state: AdminUIState) {
return state.cachedData.nodes;
}
/**
* livenessByNodeIDSelector returns a map from NodeID to the Liveness record for
* that node.
*/
export const livenessByNodeIDSelector = createSelector(
livenessesSelector,
livenesses => {
if (livenesses) {
return _.keyBy(livenesses.livenesses, l => l.node_id);
}
return {};
},
);
/*
* selectLivenessRequestStatus returns the current status of the liveness request.
*/
export function selectLivenessRequestStatus(state: AdminUIState) {
return state.cachedData.liveness;
}
/**
* livenessStatusByNodeIDSelector returns a map from NodeID to the
* LivenessStatus of that node.
*/
export const livenessStatusByNodeIDSelector = createSelector(
livenessesSelector,
livenesses => (livenesses ? livenesses.statuses || {} : {}),
);
/*
* selectCommissionedNodeStatuses returns the node statuses for nodes that have
* not been decommissioned.
*/
export const selectCommissionedNodeStatuses = createSelector(
nodeStatusesSelector,
livenessStatusByNodeIDSelector,
(nodeStatuses, livenessStatuses) => {
return _.filter(nodeStatuses, node => {
const livenessStatus = livenessStatuses[`${node.desc.node_id}`];
return (
_.isNil(livenessStatus) ||
livenessStatus !== LivenessStatus.NODE_STATUS_DECOMMISSIONED
);
});
},
);
/**
* nodeIDsSelector returns the NodeID of all nodes currently on the cluster.
*/
export const nodeIDsSelector = createSelector(
partialNodeStatusesSelector,
nodeStatuses => _.map(nodeStatuses, ns => ns.desc.node_id),
);
/**
* nodeIDsStringifiedSelector returns available node IDs on cluster as list of strings.
*/
export const nodeIDsStringifiedSelector = createSelector(nodeIDsSelector, ids =>
ids.map(id => id.toString()),
);
/**
* nodeStatusByIDSelector returns a map from NodeID to a current INodeStatus.
*/
export const nodeStatusByIDSelector = createSelector(
nodeStatusesSelector,
nodeStatuses => {
const statuses: { [s: string]: INodeStatus } = {};
_.each(nodeStatuses, ns => {
statuses[ns.desc.node_id.toString()] = ns;
});
return statuses;
},
);
export type NodeSummaryStats = {
nodeCounts: {
total: number;
healthy: number;
suspect: number;
dead: number;
decommissioned: number;
draining: number;
};
capacityUsed: number;
capacityAvailable: number;
capacityTotal: number;
capacityUsable: number;
usedBytes: number;
usedMem: number;
totalRanges: number;
underReplicatedRanges: number;
unavailableRanges: number;
replicas: number;
};
export type LivenessResponseStatuses = { [id: string]: LivenessStatus };
/**
* nodeSumsSelector returns an object with certain cluster-wide totals which are
* used in different places in the UI.
*/
export const nodeSumsSelector = createSelector(
nodeStatusesSelector,
(state: AdminUIState): LivenessResponseStatuses =>
state.cachedData.liveness.data?.statuses,
(state: AdminUIState) =>
state.cachedData.liveness.valid && state.cachedData.nodes.valid,
sumNodeStats,
);
export function sumNodeStats(
nodeStatuses: INodeStatus[],
livenessStatusByNodeID: LivenessResponseStatuses,
): NodeSummaryStats {
const result = {
nodeCounts: {
total: 0,
healthy: 0,
suspect: 0,
dead: 0,
decommissioned: 0,
draining: 0,
},
capacityUsed: 0,
capacityAvailable: 0,
capacityTotal: 0,
capacityUsable: 0,
usedBytes: 0,
usedMem: 0,
totalRanges: 0,
underReplicatedRanges: 0,
unavailableRanges: 0,
replicas: 0,
};
if (_.isArray(nodeStatuses) && !_.isEmpty(livenessStatusByNodeID)) {
nodeStatuses.forEach(n => {
const status = livenessStatusByNodeID[n.desc.node_id];
if (status !== LivenessStatus.NODE_STATUS_DECOMMISSIONED) {
result.nodeCounts.total += 1;
}
switch (status) {
case LivenessStatus.NODE_STATUS_LIVE:
result.nodeCounts.healthy++;
break;
case LivenessStatus.NODE_STATUS_UNAVAILABLE:
case LivenessStatus.NODE_STATUS_DECOMMISSIONING:
result.nodeCounts.suspect++;
break;
case LivenessStatus.NODE_STATUS_DECOMMISSIONED:
result.nodeCounts.decommissioned++;
break;
case LivenessStatus.NODE_STATUS_DRAINING:
result.nodeCounts.draining++;
break;
case LivenessStatus.NODE_STATUS_DEAD:
default:
result.nodeCounts.dead++;
break;
}
if (
status !== LivenessStatus.NODE_STATUS_DEAD &&
status !== LivenessStatus.NODE_STATUS_DECOMMISSIONED
) {
const { available, used, usable } = nodeCapacityStats(n);
result.capacityUsed += used;
result.capacityAvailable += available;
result.capacityUsable += usable;
result.capacityTotal += n.metrics[MetricConstants.capacity];
result.usedBytes += BytesUsed(n);
result.usedMem += n.metrics[MetricConstants.rss];
result.totalRanges += n.metrics[MetricConstants.ranges];
result.underReplicatedRanges +=
n.metrics[MetricConstants.underReplicatedRanges];
result.unavailableRanges +=
n.metrics[MetricConstants.unavailableRanges];
result.replicas += n.metrics[MetricConstants.replicas];
}
});
}
return result;
}
export interface CapacityStats {
used: number;
usable: number;
available: number;
}
export function nodeCapacityStats(n: INodeStatus): CapacityStats {
const used = n.metrics[MetricConstants.usedCapacity];
const available = n.metrics[MetricConstants.availableCapacity];
return {
used,
available,
usable: used + available,
};
}
export function getDisplayName(
node: INodeStatus | NoConnection,
livenessStatus = LivenessStatus.NODE_STATUS_LIVE,
) {
const decommissionedString =
livenessStatus === LivenessStatus.NODE_STATUS_DECOMMISSIONED
? "[decommissioned] "
: "";
if (isNoConnection(node)) {
return `${decommissionedString}(n${node.from.nodeID})`;
}
// as the only other type possible right now is INodeStatus we don't have a type guard for that
return `${decommissionedString}(n${node.desc.node_id}) ${node.desc.address.address_field}`;
}
function isNoConnection(
node: INodeStatus | NoConnection,
): node is NoConnection {
return (
(node as NoConnection).to !== undefined &&
(node as NoConnection).from !== undefined
);
}
// nodeDisplayNameByIDSelector provides a unique, human-readable display name
// for each node.
// This function will never be passed decommissioned nodes because
// #56529 removed a node's status entry once it's decommissioned.
export const nodeDisplayNameByIDSelector = createSelector(
partialNodeStatusesSelector,
livenessStatusByNodeIDSelector,
(nodeStatuses, livenessStatusByNodeID) => {
const result: { [key: string]: string } = {};
if (!_.isEmpty(nodeStatuses)) {
nodeStatuses.forEach(ns => {
result[ns.desc.node_id] = getDisplayName(
ns,
livenessStatusByNodeID[ns.desc.node_id],
);
});
}
return result;
},
);
export function getRegionFromLocality(locality: ILocality): string {
for (let i = 0; i < locality.tiers.length; i++) {
if (locality.tiers[i].key === "region") return locality.tiers[i].value;
}
return "";
}
// nodeRegionsByIDSelector provides the region for each node.
export const nodeRegionsByIDSelector = createSelector(
nodeStatusesSelector,
nodeStatuses => {
const result: { [key: string]: string } = {};
if (!_.isEmpty(nodeStatuses)) {
nodeStatuses.forEach(ns => {
result[ns.desc.node_id] = getRegionFromLocality(ns.desc.locality);
});
}
return result;
},
);
// selectIsMoreThanOneNode returns a boolean describing whether or not there
// exists more than one node in the cluster.
export const selectIsMoreThanOneNode = createSelector(
(state: AdminUIState) => nodeRegionsByIDSelector(state),
(nodeRegions): boolean => {
return Object.keys(nodeRegions).length > 1;
},
);
// selectStoreIDsByNodeID returns a map from node ID to a list of store IDs for
// that node. Like nodeIDsSelector, the store ids are converted to strings.
export const selectStoreIDsByNodeID = createSelector(
partialNodeStatusesSelector,
nodeStatuses => {
const result: { [key: string]: string[] } = {};
_.each(
nodeStatuses,
ns =>
(result[ns.desc.node_id] = _.map(ns.store_statuses, ss =>
ss.desc.store_id.toString(),
)),
);
return result;
},
);
/**
* nodesSummarySelector returns a directory object containing a variety of
* computed information based on the current nodes. This object is easy to
* connect to components on child pages.
*/
export const nodesSummarySelector = createSelector(
nodeStatusesSelector,
nodeIDsStringifiedSelector,
nodeStatusByIDSelector,
nodeDisplayNameByIDSelector,
livenessStatusByNodeIDSelector,
livenessByNodeIDSelector,
selectStoreIDsByNodeID,
selectNodesLastError,
(
nodeStatuses,
nodeIDs,
nodeStatusByID,
nodeDisplayNameByID,
livenessStatusByNodeID,
livenessByNodeID,
storeIDsByNodeID,
nodeLastError,
) => {
return {
nodeStatuses,
nodeIDs,
nodeStatusByID,
nodeDisplayNameByID,
livenessStatusByNodeID,
livenessByNodeID,
storeIDsByNodeID,
nodeLastError,
};
},
);
const nodesSummaryType = nullOfReturnType(nodesSummarySelector);
export type NodesSummary = typeof nodesSummaryType;
// selectNodesSummaryValid is a selector that returns true if the current
// nodesSummary is "valid" (i.e. based on acceptably recent data). This is
// included in the redux-connected state of some pages in order to support
// automatically refreshing data.
export function selectNodesSummaryValid(state: AdminUIState) {
return state.cachedData.nodes.valid && state.cachedData.liveness.valid;
}
/*
* clusterNameSelector returns the name of cluster which has to be the same for every node in the cluster.
* - That is why it is safe to get first non empty cluster name.
* - Empty cluster name is possible in case `DisableClusterNameVerification` flag is used (see pkg/base/config.go:176).
*/
export const clusterNameSelector = createSelector(
nodeStatusesSelector,
livenessStatusByNodeIDSelector,
(nodeStatuses, livenessStatusByNodeID): string => {
if (_.isUndefined(nodeStatuses) || _.isEmpty(livenessStatusByNodeID)) {
return undefined;
}
const liveNodesOnCluster = nodeStatuses.filter(
nodeStatus =>
livenessStatusByNodeID[nodeStatus.desc.node_id] ===
LivenessStatus.NODE_STATUS_LIVE,
);
const nodesWithUniqClusterNames = _.chain(liveNodesOnCluster)
.filter(node => !_.isEmpty(node.desc.cluster_name))
.uniqBy(node => node.desc.cluster_name)
.value();
if (_.isEmpty(nodesWithUniqClusterNames)) {
return undefined;
} else {
return _.head(nodesWithUniqClusterNames).desc.cluster_name;
}
},
);
export const validateNodesSelector = createSelector(
nodeStatusesSelector,
livenessByNodeIDSelector,
(nodeStatuses, livenessStatusByNodeID) => {
if (!nodeStatuses) {
return undefined;
}
return (
nodeStatuses
// Ignore nodes for which we don't have any build info.
.filter(status => !!status.build_info)
// Exclude this node if it's known to be decommissioning.
.filter(
status =>
!status.desc ||
!livenessStatusByNodeID[status.desc.node_id] ||
!livenessStatusByNodeID[status.desc.node_id].membership ||
!(
livenessStatusByNodeID[status.desc.node_id].membership !==
MembershipStatus.ACTIVE
),
)
);
},
);
export const versionsSelector = createSelector(validateNodesSelector, nodes =>
_.chain(nodes)
// Collect the surviving nodes' build tags.
.map(status => status.build_info.tag)
.uniq()
.value(),
);
export const numNodesByVersionsTagSelector = createSelector(
validateNodesSelector,
nodes => {
if (!nodes) {
return new Map();
}
return new Map(
Object.entries(_.countBy(nodes, node => node?.build_info?.tag)),
);
},
);
export const numNodesByVersionsSelector = createSelector(
validateNodesSelector,
nodes => {
if (!nodes) {
return new Map();
}
return new Map(
Object.entries(
_.countBy(nodes, node => {
const serverVersion = node?.desc?.ServerVersion;
if (serverVersion) {
return `${serverVersion.major_val}.${serverVersion.minor_val}`;
}
return "";
}),
),
);
},
);
// Select the current build version of the cluster, returning undefined if the
// cluster's version is currently staggered.
export const singleVersionSelector = createSelector(
versionsSelector,
builds => {
if (!builds || builds.length !== 1) {
return undefined;
}
return builds[0];
},
);
// clusterVersionSelector returns build version of the cluster, or returns the lowest version
// if cluster's version is staggered.
export const clusterVersionLabelSelector = createSelector(
versionsSelector,
builds => {
if (!builds) {
return undefined;
}
if (builds.length > 1) {
const lowestVersion = _.chain(builds)
.sortBy(b => b)
.first()
.value();
return `${lowestVersion} - Mixed Versions`;
}
return builds[0];
},
);
/**
* partitionedStatuses divides the list of node statuses into "live" and "dead".
*/
export const partitionedStatuses = createSelector(
nodesSummarySelector,
summary => {
return _.groupBy(summary.nodeStatuses, ns => {
switch (summary.livenessByNodeID[ns.desc.node_id]) {
case MembershipStatus.ACTIVE:
case MembershipStatus.DECOMMISSIONING:
return "live";
case MembershipStatus.DECOMMISSIONED:
return "decommissioned";
default:
// TODO (koorosh): "live" has to be renamed to some partition which
// represent all except "partitioned" nodes.
return "live";
}
});
},
);
export const isSingleNodeCluster = createSelector(
nodeStatusesSelector,
nodeStatuses => nodeStatuses && nodeStatuses.length === 1,
);