10
10
11
11
import org .apache .logging .log4j .LogManager ;
12
12
import org .apache .logging .log4j .Logger ;
13
+ import org .elasticsearch .cluster .ClusterState ;
13
14
import org .elasticsearch .cluster .metadata .IndexMetadata ;
14
15
import org .elasticsearch .cluster .node .DiscoveryNode ;
15
16
import org .elasticsearch .cluster .node .DiscoveryNodeRole ;
29
30
import java .util .List ;
30
31
import java .util .Locale ;
31
32
import java .util .Map ;
32
- import java .util .Objects ;
33
33
import java .util .Set ;
34
34
import java .util .stream .Collectors ;
35
35
import java .util .stream .Stream ;
@@ -62,17 +62,17 @@ public HealthIndicatorResult calculate(boolean explain, HealthInfo healthInfo) {
62
62
Collections .emptyList ()
63
63
);
64
64
}
65
- Set < String > indicesWithBlock = clusterService .state ()
66
- .blocks ()
65
+ ClusterState clusterState = clusterService .state ();
66
+ Set < String > indicesWithBlock = clusterState .blocks ()
67
67
.indices ()
68
68
.entrySet ()
69
69
.stream ()
70
70
.filter (entry -> entry .getValue ().contains (IndexMetadata .INDEX_READ_ONLY_ALLOW_DELETE_BLOCK ))
71
71
.map (Map .Entry ::getKey )
72
72
.collect (Collectors .toSet ());
73
73
boolean hasAtLeastOneIndexReadOnlyAllowDeleteBlock = indicesWithBlock .isEmpty () == false ;
74
- logMissingHealthInfoData (diskHealthInfoMap );
75
- HealthIndicatorDetails details = getDetails (explain , diskHealthInfoMap );
74
+ logMissingHealthInfoData (diskHealthInfoMap , clusterState );
75
+ HealthIndicatorDetails details = getDetails (explain , diskHealthInfoMap , clusterState );
76
76
final HealthStatus healthStatusFromNodes = HealthStatus .merge (
77
77
diskHealthInfoMap .values ().stream ().map (DiskHealthInfo ::healthStatus )
78
78
);
@@ -93,26 +93,32 @@ public HealthIndicatorResult calculate(boolean explain, HealthInfo healthInfo) {
93
93
.filter (entry -> HealthStatus .RED .equals (entry .getValue ().healthStatus ()))
94
94
.map (Map .Entry ::getKey )
95
95
.collect (Collectors .toSet ());
96
- Set <DiscoveryNodeRole > rolesOnRedNodes = getRolesOnNodes (nodesReportingRed );
96
+ Set <DiscoveryNodeRole > rolesOnRedNodes = getRolesOnNodes (nodesReportingRed , clusterState );
97
97
if (hasAtLeastOneIndexReadOnlyAllowDeleteBlock || rolesOnRedNodes .stream ().anyMatch (DiscoveryNodeRole ::canContainData )) {
98
98
healthIndicatorResult = getResultForRedIndicesOrDataNodes (
99
99
nodesReportingRed ,
100
- Stream .concat (indicesWithBlock .stream (), getIndicesForNodes (nodesReportingRed ).stream ())
100
+ Stream .concat (indicesWithBlock .stream (), getIndicesForNodes (nodesReportingRed , clusterState ).stream ())
101
101
.collect (Collectors .toSet ()),
102
102
true ,
103
103
details ,
104
104
healthStatus
105
105
);
106
106
} else {
107
- healthIndicatorResult = getResultForNonDataNodeProblem (rolesOnRedNodes , nodesReportingRed , details , healthStatus );
107
+ healthIndicatorResult = getResultForNonDataNodeProblem (
108
+ rolesOnRedNodes ,
109
+ nodesReportingRed ,
110
+ details ,
111
+ healthStatus ,
112
+ clusterState
113
+ );
108
114
}
109
115
} else {
110
116
final Set <String > nodesReportingYellow = diskHealthInfoMap .entrySet ()
111
117
.stream ()
112
118
.filter (entry -> HealthStatus .YELLOW .equals (entry .getValue ().healthStatus ()))
113
119
.map (Map .Entry ::getKey )
114
120
.collect (Collectors .toSet ());
115
- Set <DiscoveryNodeRole > rolesOnYellowNodes = getRolesOnNodes (nodesReportingYellow );
121
+ Set <DiscoveryNodeRole > rolesOnYellowNodes = getRolesOnNodes (nodesReportingYellow , clusterState );
116
122
if (hasAtLeastOneIndexReadOnlyAllowDeleteBlock ) {
117
123
healthIndicatorResult = getResultForRedIndicesOrDataNodes (
118
124
nodesReportingYellow ,
@@ -122,9 +128,15 @@ public HealthIndicatorResult calculate(boolean explain, HealthInfo healthInfo) {
122
128
healthStatus
123
129
);
124
130
} else if (rolesOnYellowNodes .stream ().anyMatch (DiscoveryNodeRole ::canContainData )) {
125
- healthIndicatorResult = getResultForYellowDataNodes (nodesReportingYellow , details , healthStatus );
131
+ healthIndicatorResult = getResultForYellowDataNodes (nodesReportingYellow , details , healthStatus , clusterState );
126
132
} else {
127
- healthIndicatorResult = getResultForNonDataNodeProblem (rolesOnYellowNodes , nodesReportingYellow , details , healthStatus );
133
+ healthIndicatorResult = getResultForNonDataNodeProblem (
134
+ rolesOnYellowNodes ,
135
+ nodesReportingYellow ,
136
+ details ,
137
+ healthStatus ,
138
+ clusterState
139
+ );
128
140
}
129
141
}
130
142
}
@@ -135,14 +147,14 @@ private HealthIndicatorResult getResultForNonDataNodeProblem(
135
147
Set <DiscoveryNodeRole > roles ,
136
148
Set <String > problemNodes ,
137
149
HealthIndicatorDetails details ,
138
- HealthStatus status
150
+ HealthStatus status ,
151
+ ClusterState clusterState
139
152
) {
140
153
String symptom ;
141
154
final List <HealthIndicatorImpact > impacts ;
142
155
final List <Diagnosis > diagnosisList ;
143
156
if (roles .contains (DiscoveryNodeRole .MASTER_ROLE )) {
144
- Set <DiscoveryNode > problemMasterNodes = clusterService .state ()
145
- .nodes ()
157
+ Set <DiscoveryNode > problemMasterNodes = clusterState .nodes ()
146
158
.getNodes ()
147
159
.values ()
148
160
.stream ()
@@ -259,9 +271,10 @@ public HealthIndicatorResult getResultForRedIndicesOrDataNodes(
259
271
public HealthIndicatorResult getResultForYellowDataNodes (
260
272
Set <String > problemNodes ,
261
273
HealthIndicatorDetails details ,
262
- HealthStatus status
274
+ HealthStatus status ,
275
+ ClusterState clusterState
263
276
) {
264
- final Set <String > problemIndices = getIndicesForNodes (problemNodes );
277
+ final Set <String > problemIndices = getIndicesForNodes (problemNodes , clusterState );
265
278
final String symptom = String .format (
266
279
Locale .ROOT ,
267
280
"%d data node%s increased disk usage. As a result %d %s at risk of not being able to process any more " + "updates." ,
@@ -298,9 +311,8 @@ public HealthIndicatorResult getResultForYellowDataNodes(
298
311
return createIndicator (status , symptom , details , impacts , diagnosisList );
299
312
}
300
313
301
- private Set <DiscoveryNodeRole > getRolesOnNodes (Set <String > nodeIds ) {
302
- return clusterService .state ()
303
- .nodes ()
314
+ private Set <DiscoveryNodeRole > getRolesOnNodes (Set <String > nodeIds , ClusterState clusterState ) {
315
+ return clusterState .nodes ()
304
316
.getNodes ()
305
317
.values ()
306
318
.stream ()
@@ -310,9 +322,8 @@ private Set<DiscoveryNodeRole> getRolesOnNodes(Set<String> nodeIds) {
310
322
.collect (Collectors .toSet ());
311
323
}
312
324
313
- private Set <String > getIndicesForNodes (Set <String > nodes ) {
314
- return clusterService .state ()
315
- .routingTable ()
325
+ private Set <String > getIndicesForNodes (Set <String > nodes , ClusterState clusterState ) {
326
+ return clusterState .routingTable ()
316
327
.allShards ()
317
328
.stream ()
318
329
.filter (routing -> nodes .contains (routing .currentNodeId ()))
@@ -325,9 +336,9 @@ private Set<String> getIndicesForNodes(Set<String> nodes) {
325
336
* not ordinarly important, but could be useful in tracking down problems where nodes have stopped reporting health node information.
326
337
* @param diskHealthInfoMap A map of nodeId to DiskHealthInfo
327
338
*/
328
- private void logMissingHealthInfoData (Map <String , DiskHealthInfo > diskHealthInfoMap ) {
339
+ private void logMissingHealthInfoData (Map <String , DiskHealthInfo > diskHealthInfoMap , ClusterState clusterState ) {
329
340
if (logger .isDebugEnabled ()) {
330
- Set <DiscoveryNode > nodesInClusterState = new HashSet <>(clusterService . state () .nodes ());
341
+ Set <DiscoveryNode > nodesInClusterState = new HashSet <>(clusterState .nodes ());
331
342
Set <String > nodeIdsInClusterState = nodesInClusterState .stream ().map (DiscoveryNode ::getId ).collect (Collectors .toSet ());
332
343
Set <String > nodeIdsInHealthInfo = diskHealthInfoMap .keySet ();
333
344
if (nodeIdsInHealthInfo .containsAll (nodeIdsInClusterState ) == false ) {
@@ -340,7 +351,7 @@ private void logMissingHealthInfoData(Map<String, DiskHealthInfo> diskHealthInfo
340
351
}
341
352
}
342
353
343
- private HealthIndicatorDetails getDetails (boolean explain , Map <String , DiskHealthInfo > diskHealthInfoMap ) {
354
+ private HealthIndicatorDetails getDetails (boolean explain , Map <String , DiskHealthInfo > diskHealthInfoMap , ClusterState clusterState ) {
344
355
if (explain == false ) {
345
356
return HealthIndicatorDetails .EMPTY ;
346
357
}
@@ -351,7 +362,7 @@ private HealthIndicatorDetails getDetails(boolean explain, Map<String, DiskHealt
351
362
builder .startObject ();
352
363
String nodeId = entry .getKey ();
353
364
builder .field ("node_id" , nodeId );
354
- String nodeName = getNameForNodeId (nodeId );
365
+ String nodeName = getNameForNodeId (nodeId , clusterState );
355
366
if (nodeName != null ) {
356
367
builder .field ("name" , nodeName );
357
368
}
@@ -374,13 +385,8 @@ private HealthIndicatorDetails getDetails(boolean explain, Map<String, DiskHealt
374
385
* @return The current name of the node, or null if the node is not in the cluster state or does not have a name
375
386
*/
376
387
@ Nullable
377
- private String getNameForNodeId (String nodeId ) {
378
- DiscoveryNode node = clusterService .state ().nodes ().get (nodeId );
379
- if (node == null ) {
380
- return null ;
381
- } else {
382
- String nodeName = node .getName ();
383
- return Objects .requireNonNullElse (nodeName , null );
384
- }
388
+ private String getNameForNodeId (String nodeId , ClusterState clusterState ) {
389
+ DiscoveryNode node = clusterState .nodes ().get (nodeId );
390
+ return node == null ? null : node .getName ();
385
391
}
386
392
}
0 commit comments