-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Health Indicator Plugin (#83205)
This change introduces initial model for health indicators as well as extension point that allows to create health indicators outside of server module. Co-authored-by: Tanguy Leroux <tlrx.dev@gmail.com>
- Loading branch information
1 parent
de756c1
commit 2982591
Showing
18 changed files
with
489 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 83205 | ||
summary: Add Health Indicator Plugin | ||
area: Health | ||
type: feature | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ava/org/elasticsearch/cluster/coordination/InstanceHasMasterHealthIndicatorServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.cluster.coordination; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.health.GetHealthAction; | ||
import org.elasticsearch.plugins.Plugin; | ||
import org.elasticsearch.test.ESIntegTestCase; | ||
import org.elasticsearch.test.disruption.NetworkDisruption; | ||
import org.elasticsearch.test.transport.MockTransportService; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.elasticsearch.cluster.coordination.InstanceHasMasterHealthIndicatorService.NAME; | ||
import static org.elasticsearch.health.HealthStatus.GREEN; | ||
import static org.elasticsearch.health.HealthStatus.RED; | ||
import static org.elasticsearch.health.ServerHealthComponents.CLUSTER_COORDINATION; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE) | ||
public class InstanceHasMasterHealthIndicatorServiceIT extends ESIntegTestCase { | ||
|
||
@Override | ||
protected Collection<Class<? extends Plugin>> nodePlugins() { | ||
return List.of(MockTransportService.TestPlugin.class); | ||
} | ||
|
||
@Override | ||
protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { | ||
return Settings.builder() | ||
.put(super.nodeSettings(nodeOrdinal, otherSettings)) | ||
.put(NoMasterBlockService.NO_MASTER_BLOCK_SETTING.getKey(), "all") | ||
.build(); | ||
} | ||
|
||
public void testGetHealthWhenMasterIsElected() throws Exception { | ||
var client = client(); | ||
|
||
var response = client.execute(GetHealthAction.INSTANCE, new GetHealthAction.Request()).get(); | ||
|
||
assertThat(response.findComponent(CLUSTER_COORDINATION).findIndicator(NAME).status(), equalTo(GREEN)); | ||
} | ||
|
||
public void testGetHealthWhenNoMaster() throws Exception { | ||
var client = internalCluster().coordOnlyNodeClient(); | ||
|
||
var disruptionScheme = new NetworkDisruption( | ||
new NetworkDisruption.IsolateAllNodes(Set.of(internalCluster().getNodeNames())), | ||
NetworkDisruption.DISCONNECT | ||
); | ||
|
||
internalCluster().setDisruptionScheme(disruptionScheme); | ||
disruptionScheme.startDisrupting(); | ||
|
||
try { | ||
assertBusy(() -> { | ||
ClusterState state = client.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState(); | ||
assertTrue(state.blocks().hasGlobalBlockWithId(NoMasterBlockService.NO_MASTER_BLOCK_ID)); | ||
|
||
var response = client.execute(GetHealthAction.INSTANCE, new GetHealthAction.Request()).get(); | ||
|
||
assertThat(response.findComponent(CLUSTER_COORDINATION).findIndicator(NAME).status(), equalTo(RED)); | ||
}); | ||
} finally { | ||
internalCluster().clearDisruptionScheme(true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../java/org/elasticsearch/cluster/coordination/InstanceHasMasterHealthIndicatorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.cluster.coordination; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
import org.elasticsearch.cluster.service.ClusterService; | ||
import org.elasticsearch.health.HealthIndicatorResult; | ||
import org.elasticsearch.health.HealthIndicatorService; | ||
import org.elasticsearch.health.HealthStatus; | ||
|
||
import static org.elasticsearch.health.ServerHealthComponents.CLUSTER_COORDINATION; | ||
|
||
public class InstanceHasMasterHealthIndicatorService implements HealthIndicatorService { | ||
|
||
public static final String NAME = "instance_has_master"; | ||
|
||
private static final String INSTANCE_HAS_MASTER_GREEN_SUMMARY = "Health coordinating instance has a master node."; | ||
private static final String INSTANCE_HAS_MASTER_RED_SUMMARY = "Health coordinating instance does not have a master node."; | ||
|
||
private final ClusterService clusterService; | ||
|
||
public InstanceHasMasterHealthIndicatorService(ClusterService clusterService) { | ||
this.clusterService = clusterService; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public String component() { | ||
return CLUSTER_COORDINATION; | ||
} | ||
|
||
@Override | ||
public HealthIndicatorResult calculate() { | ||
|
||
DiscoveryNode coordinatingNode = clusterService.localNode(); | ||
ClusterState clusterState = clusterService.state(); | ||
DiscoveryNodes nodes = clusterState.nodes(); | ||
DiscoveryNode masterNode = nodes.getMasterNode(); | ||
|
||
HealthStatus instanceHasMasterStatus = masterNode == null ? HealthStatus.RED : HealthStatus.GREEN; | ||
String instanceHasMasterSummary = masterNode == null ? INSTANCE_HAS_MASTER_RED_SUMMARY : INSTANCE_HAS_MASTER_GREEN_SUMMARY; | ||
|
||
return createIndicator(instanceHasMasterStatus, instanceHasMasterSummary, (builder, params) -> { | ||
builder.startObject(); | ||
builder.object("coordinating_node", xContentBuilder -> { | ||
builder.field("node_id", coordinatingNode.getId()); | ||
builder.field("name", coordinatingNode.getName()); | ||
}); | ||
builder.object("master_node", xContentBuilder -> { | ||
if (masterNode != null) { | ||
builder.field("node_id", masterNode.getId()); | ||
builder.field("name", masterNode.getName()); | ||
} else { | ||
builder.nullField("node_id"); | ||
builder.nullField("name"); | ||
} | ||
}); | ||
return builder.endObject(); | ||
}); | ||
} | ||
} |
Oops, something went wrong.