Search in sources :

Example 61 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class ESIntegTestCase method ensureColor.

private ClusterHealthStatus ensureColor(ClusterHealthStatus clusterHealthStatus, TimeValue timeout, String... indices) {
    String color = clusterHealthStatus.name().toLowerCase(Locale.ROOT);
    String method = "ensure" + Strings.capitalize(color);
    ClusterHealthRequest healthRequest = Requests.clusterHealthRequest(indices).timeout(timeout).waitForStatus(clusterHealthStatus).waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(true).waitForNodes(Integer.toString(cluster().size()));
    ClusterHealthResponse actionGet = client().admin().cluster().health(healthRequest).actionGet();
    if (actionGet.isTimedOut()) {
        logger.info("{} timed out, cluster state:\n{}\n{}", method, client().admin().cluster().prepareState().get().getState(), client().admin().cluster().preparePendingClusterTasks().get());
        fail("timed out waiting for " + color + " state");
    }
    assertThat("Expected at least " + clusterHealthStatus + " but got " + actionGet.getStatus(), actionGet.getStatus().value(), lessThanOrEqualTo(clusterHealthStatus.value()));
    logger.debug("indices {} are {}", indices.length == 0 ? "[_all]" : indices, color);
    return actionGet.getStatus();
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)

Example 62 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class ClusterStateHealthTests method testClusterHealthWaitsForClusterStateApplication.

public void testClusterHealthWaitsForClusterStateApplication() throws InterruptedException, ExecutionException {
    final CountDownLatch applyLatch = new CountDownLatch(1);
    final CountDownLatch listenerCalled = new CountDownLatch(1);
    setState(clusterService, ClusterState.builder(clusterService.state()).nodes(DiscoveryNodes.builder(clusterService.state().nodes()).masterNodeId(null)).build());
    clusterService.addStateApplier(event -> {
        listenerCalled.countDown();
        try {
            applyLatch.await();
        } catch (InterruptedException e) {
            logger.debug("interrupted", e);
        }
    });
    logger.info("--> submit task to restore master");
    clusterService.submitStateUpdateTask("restore master", new LocalClusterUpdateTask() {

        @Override
        public ClusterTasksResult<LocalClusterUpdateTask> execute(ClusterState currentState) throws Exception {
            return newState(ClusterState.builder(currentState).nodes(DiscoveryNodes.builder(currentState.nodes()).masterNodeId(currentState.nodes().getLocalNodeId())).build());
        }

        @Override
        public void onFailure(String source, Exception e) {
            logger.warn("unexpected failure", e);
        }
    });
    logger.info("--> waiting for listener to be called and cluster state being blocked");
    listenerCalled.await();
    TransportClusterHealthAction action = new TransportClusterHealthAction(Settings.EMPTY, transportService, clusterService, threadPool, new ActionFilters(new HashSet<>()), indexNameExpressionResolver, new TestGatewayAllocator());
    PlainActionFuture<ClusterHealthResponse> listener = new PlainActionFuture<>();
    action.execute(new ClusterHealthRequest().waitForGreenStatus(), listener);
    assertFalse(listener.isDone());
    logger.info("--> realising task to restore master");
    applyLatch.countDown();
    listener.get();
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) LocalClusterUpdateTask(org.elasticsearch.cluster.LocalClusterUpdateTask) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) ActionFilters(org.elasticsearch.action.support.ActionFilters) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) TransportClusterHealthAction(org.elasticsearch.action.admin.cluster.health.TransportClusterHealthAction) HashSet(java.util.HashSet)

Example 63 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class AwarenessAllocationIT method testSimpleAwareness.

public void testSimpleAwareness() throws Exception {
    Settings commonSettings = Settings.builder().put("cluster.routing.allocation.awareness.attributes", "rack_id").build();
    logger.info("--> starting 2 nodes on the same rack");
    internalCluster().startNodes(2, Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_1").build());
    createIndex("test1");
    createIndex("test2");
    NumShards test1 = getNumShards("test1");
    NumShards test2 = getNumShards("test2");
    //no replicas will be allocated as both indices end up on a single node
    final int totalPrimaries = test1.numPrimaries + test2.numPrimaries;
    ensureGreen();
    logger.info("--> starting 1 node on a different rack");
    final String node3 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_2").build());
    // On slow machines the initial relocation might be delayed
    assertThat(awaitBusy(() -> {
        logger.info("--> waiting for no relocation");
        ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("3").setWaitForNoRelocatingShards(true).get();
        if (clusterHealth.isTimedOut()) {
            return false;
        }
        logger.info("--> checking current state");
        ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
        // verify that we have all the primaries on node3
        ObjectIntHashMap<String> counts = new ObjectIntHashMap<>();
        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
                }
            }
        }
        return counts.get(node3) == totalPrimaries;
    }, 10, TimeUnit.SECONDS), equalTo(true));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings)

Example 64 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class AwarenessAllocationIT method testAwarenessZonesIncrementalNodes.

public void testAwarenessZonesIncrementalNodes() throws Exception {
    Settings commonSettings = Settings.builder().put("cluster.routing.allocation.awareness.force.zone.values", "a,b").put("cluster.routing.allocation.awareness.attributes", "zone").build();
    logger.info("--> starting 2 nodes on zones 'a' & 'b'");
    List<String> nodes = internalCluster().startNodes(Settings.builder().put(commonSettings).put("node.attr.zone", "a").build(), Settings.builder().put(commonSettings).put("node.attr.zone", "b").build());
    String A_0 = nodes.get(0);
    String B_0 = nodes.get(1);
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1)).execute().actionGet();
    ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("2").setWaitForNoRelocatingShards(true).execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    ObjectIntHashMap<String> counts = new ObjectIntHashMap<>();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
            }
        }
    }
    assertThat(counts.get(A_0), equalTo(5));
    assertThat(counts.get(B_0), equalTo(5));
    logger.info("--> starting another node in zone 'b'");
    String B_1 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.zone", "b").build());
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("3").execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    client().admin().cluster().prepareReroute().get();
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("3").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    counts = new ObjectIntHashMap<>();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
            }
        }
    }
    assertThat(counts.get(A_0), equalTo(5));
    assertThat(counts.get(B_0), equalTo(3));
    assertThat(counts.get(B_1), equalTo(2));
    String noZoneNode = internalCluster().startNode();
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    client().admin().cluster().prepareReroute().get();
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    counts = new ObjectIntHashMap<>();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
            }
        }
    }
    assertThat(counts.get(A_0), equalTo(5));
    assertThat(counts.get(B_0), equalTo(3));
    assertThat(counts.get(B_1), equalTo(2));
    assertThat(counts.containsKey(noZoneNode), equalTo(false));
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "").build()).get();
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().setWaitForNodes("4").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
    counts = new ObjectIntHashMap<>();
    for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            for (ShardRouting shardRouting : indexShardRoutingTable) {
                counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1);
            }
        }
    }
    assertThat(counts.get(A_0), equalTo(3));
    assertThat(counts.get(B_0), equalTo(3));
    assertThat(counts.get(B_1), equalTo(2));
    assertThat(counts.get(noZoneNode), equalTo(2));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Settings(org.elasticsearch.common.settings.Settings)

Example 65 with ClusterHealthResponse

use of org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse in project elasticsearch by elastic.

the class ClusterRerouteIT method testRerouteExplain.

public void testRerouteExplain() {
    Settings commonSettings = Settings.builder().build();
    logger.info("--> starting a node");
    String node_1 = internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(1));
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("1").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> create an index with 1 shard");
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).execute().actionGet();
    ensureGreen("test");
    logger.info("--> disable allocation");
    Settings newSettings = Settings.builder().put(CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), Allocation.NONE.name()).build();
    client().admin().cluster().prepareUpdateSettings().setTransientSettings(newSettings).execute().actionGet();
    logger.info("--> starting a second node");
    String node_2 = internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(2));
    healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> try to move the shard from node1 to node2");
    MoveAllocationCommand cmd = new MoveAllocationCommand("test", 0, node_1, node_2);
    ClusterRerouteResponse resp = client().admin().cluster().prepareReroute().add(cmd).setExplain(true).execute().actionGet();
    RoutingExplanations e = resp.getExplanations();
    assertThat(e.explanations().size(), equalTo(1));
    RerouteExplanation explanation = e.explanations().get(0);
    assertThat(explanation.command().name(), equalTo(cmd.name()));
    assertThat(((MoveAllocationCommand) explanation.command()).shardId(), equalTo(cmd.shardId()));
    assertThat(((MoveAllocationCommand) explanation.command()).fromNode(), equalTo(cmd.fromNode()));
    assertThat(((MoveAllocationCommand) explanation.command()).toNode(), equalTo(cmd.toNode()));
    assertThat(explanation.decisions().type(), equalTo(Decision.Type.YES));
}
Also used : RoutingExplanations(org.elasticsearch.cluster.routing.allocation.RoutingExplanations) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) RerouteExplanation(org.elasticsearch.cluster.routing.allocation.RerouteExplanation) Settings(org.elasticsearch.common.settings.Settings) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Aggregations

ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)93 Settings (org.elasticsearch.common.settings.Settings)25 Client (org.elasticsearch.client.Client)22 ClusterState (org.elasticsearch.cluster.ClusterState)16 IOException (java.io.IOException)11 SearchResponse (org.elasticsearch.action.search.SearchResponse)10 MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)10 CloseIndexResponse (org.elasticsearch.action.admin.indices.close.CloseIndexResponse)9 OpenIndexResponse (org.elasticsearch.action.admin.indices.open.OpenIndexResponse)9 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)8 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)7 Index (org.elasticsearch.index.Index)7 ArrayList (java.util.ArrayList)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Path (java.nio.file.Path)5 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NodesInfoResponse (org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse)4