Search in sources :

Example 56 with ClusterHealthResponse

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

the class IndexLifecycleActionIT method testIndexLifecycleActionsWith11Shards1Backup.

public void testIndexLifecycleActionsWith11Shards1Backup() throws Exception {
    Settings settings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, 11).put(SETTING_NUMBER_OF_REPLICAS, 1).build();
    // start one server
    logger.info("Starting sever1");
    final String server_1 = internalCluster().startNode();
    final String node1 = getLocalNodeId(server_1);
    logger.info("Creating index [test]");
    CreateIndexResponse createIndexResponse = client().admin().indices().create(createIndexRequest("test").settings(settings)).actionGet();
    assertAcked(createIndexResponse);
    ClusterState clusterState = client().admin().cluster().prepareState().get().getState();
    RoutingNode routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
    assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(11));
    logger.info("Starting server2");
    // start another server
    String server_2 = internalCluster().startNode();
    // first wait for 2 nodes in the cluster
    logger.info("Waiting for replicas to be assigned");
    ClusterHealthResponse clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNodes("2")).actionGet();
    logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    final String node2 = getLocalNodeId(server_2);
    // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
    client().admin().cluster().prepareReroute().execute().actionGet();
    clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNodes("2").waitForNoRelocatingShards(true)).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(clusterHealth.getNumberOfDataNodes(), equalTo(2));
    assertThat(clusterHealth.getInitializingShards(), equalTo(0));
    assertThat(clusterHealth.getUnassignedShards(), equalTo(0));
    assertThat(clusterHealth.getRelocatingShards(), equalTo(0));
    assertThat(clusterHealth.getActiveShards(), equalTo(22));
    assertThat(clusterHealth.getActivePrimaryShards(), equalTo(11));
    clusterState = client().admin().cluster().prepareState().get().getState();
    assertNodesPresent(clusterState.getRoutingNodes(), node1, node2);
    routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
    assertThat(routingNodeEntry1.numberOfShardsWithState(RELOCATING), equalTo(0));
    assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), equalTo(11));
    RoutingNode routingNodeEntry2 = clusterState.getRoutingNodes().node(node2);
    assertThat(routingNodeEntry2.numberOfShardsWithState(INITIALIZING), equalTo(0));
    assertThat(routingNodeEntry2.numberOfShardsWithState(STARTED), equalTo(11));
    logger.info("Starting server3");
    // start another server
    String server_3 = internalCluster().startNode();
    // first wait for 3 nodes in the cluster
    logger.info("Waiting for replicas to be assigned");
    clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNodes("3")).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    final String node3 = getLocalNodeId(server_3);
    // explicitly call reroute, so shards will get relocated to the new node (we delay it in ES in case other nodes join)
    client().admin().cluster().prepareReroute().execute().actionGet();
    clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNodes("3").waitForNoRelocatingShards(true)).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(clusterHealth.getNumberOfDataNodes(), equalTo(3));
    assertThat(clusterHealth.getInitializingShards(), equalTo(0));
    assertThat(clusterHealth.getUnassignedShards(), equalTo(0));
    assertThat(clusterHealth.getRelocatingShards(), equalTo(0));
    assertThat(clusterHealth.getActiveShards(), equalTo(22));
    assertThat(clusterHealth.getActivePrimaryShards(), equalTo(11));
    clusterState = client().admin().cluster().prepareState().get().getState();
    assertNodesPresent(clusterState.getRoutingNodes(), node1, node2, node3);
    routingNodeEntry1 = clusterState.getRoutingNodes().node(node1);
    routingNodeEntry2 = clusterState.getRoutingNodes().node(node2);
    RoutingNode routingNodeEntry3 = clusterState.getRoutingNodes().node(node3);
    assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED) + routingNodeEntry2.numberOfShardsWithState(STARTED) + routingNodeEntry3.numberOfShardsWithState(STARTED), equalTo(22));
    assertThat(routingNodeEntry1.numberOfShardsWithState(RELOCATING), equalTo(0));
    assertThat(routingNodeEntry1.numberOfShardsWithState(STARTED), anyOf(equalTo(7), equalTo(8)));
    assertThat(routingNodeEntry2.numberOfShardsWithState(RELOCATING), equalTo(0));
    assertThat(routingNodeEntry2.numberOfShardsWithState(STARTED), anyOf(equalTo(7), equalTo(8)));
    assertThat(routingNodeEntry3.numberOfShardsWithState(INITIALIZING), equalTo(0));
    assertThat(routingNodeEntry3.numberOfShardsWithState(STARTED), equalTo(7));
    logger.info("Closing server1");
    // kill the first server
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(server_1));
    // verify health
    logger.info("Running Cluster Health");
    clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNodes("2")).actionGet();
    logger.info("Done Cluster Health, status {}", clusterHealth.getStatus());
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    client().admin().cluster().prepareReroute().get();
    clusterHealth = client().admin().cluster().health(clusterHealthRequest().waitForGreenStatus().waitForNoRelocatingShards(true).waitForNodes("2")).actionGet();
    assertThat(clusterHealth.isTimedOut(), equalTo(false));
    assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(clusterHealth.getRelocatingShards(), equalTo(0));
    assertThat(clusterHealth.getActiveShards(), equalTo(22));
    assertThat(clusterHealth.getActivePrimaryShards(), equalTo(11));
    clusterState = client().admin().cluster().prepareState().get().getState();
    assertNodesPresent(clusterState.getRoutingNodes(), node3, node2);
    routingNodeEntry2 = clusterState.getRoutingNodes().node(node2);
    routingNodeEntry3 = clusterState.getRoutingNodes().node(node3);
    assertThat(routingNodeEntry2.numberOfShardsWithState(STARTED) + routingNodeEntry3.numberOfShardsWithState(STARTED), equalTo(22));
    assertThat(routingNodeEntry2.numberOfShardsWithState(RELOCATING), equalTo(0));
    assertThat(routingNodeEntry2.numberOfShardsWithState(STARTED), equalTo(11));
    assertThat(routingNodeEntry3.numberOfShardsWithState(RELOCATING), equalTo(0));
    assertThat(routingNodeEntry3.numberOfShardsWithState(STARTED), equalTo(11));
    logger.info("Deleting index [test]");
    // last, lets delete the index
    DeleteIndexResponse deleteIndexResponse = client().admin().indices().prepareDelete("test").execute().actionGet();
    assertThat(deleteIndexResponse.isAcknowledged(), equalTo(true));
    clusterState = client().admin().cluster().prepareState().get().getState();
    assertNodesPresent(clusterState.getRoutingNodes(), node3, node2);
    routingNodeEntry2 = clusterState.getRoutingNodes().node(node2);
    assertThat(routingNodeEntry2.isEmpty(), equalTo(true));
    routingNodeEntry3 = clusterState.getRoutingNodes().node(node3);
    assertThat(routingNodeEntry3.isEmpty(), equalTo(true));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Settings(org.elasticsearch.common.settings.Settings)

Example 57 with ClusterHealthResponse

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

the class MinimumMasterNodesIT method testMultipleNodesShutdownNonMasterNodes.

public void testMultipleNodesShutdownNonMasterNodes() throws Exception {
    Settings settings = Settings.builder().put("discovery.zen.minimum_master_nodes", 3).put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "1s").put("discovery.initial_state_timeout", "500ms").build();
    logger.info("--> start first 2 nodes");
    internalCluster().startNodes(2, settings);
    ClusterState state;
    assertBusy(() -> {
        for (Client client : clients()) {
            ClusterState state1 = client.admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
            assertThat(state1.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
        }
    });
    logger.info("--> start two more nodes");
    internalCluster().startNodes(2, settings);
    ensureGreen();
    ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("4").execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.nodes().getSize(), equalTo(4));
    createIndex("test");
    NumShards numShards = getNumShards("test");
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", "value").execute().actionGet();
    }
    ensureGreen();
    // make sure that all shards recovered before trying to flush
    assertThat(client().admin().cluster().prepareHealth("test").setWaitForActiveShards(numShards.totalNumShards).execute().actionGet().isTimedOut(), equalTo(false));
    // flush for simpler debugging
    client().admin().indices().prepareFlush().execute().actionGet();
    refresh();
    logger.info("--> verify we the data back");
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setSize(0).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(), 100);
    }
    internalCluster().stopRandomNonMasterNode();
    internalCluster().stopRandomNonMasterNode();
    logger.info("--> verify that there is no master anymore on remaining nodes");
    // spin here to wait till the state is set
    assertNoMasterBlockOnAllNodes();
    logger.info("--> start back the 2 nodes ");
    String[] newNodes = internalCluster().startNodes(2, settings).stream().toArray(String[]::new);
    ensureGreen();
    clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("4").execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.nodes().getSize(), equalTo(4));
    // we prefer to elect up and running nodes
    assertThat(state.nodes().getMasterNodeId(), not(isOneOf(newNodes)));
    logger.info("--> verify we the data back");
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setSize(0).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(), 100);
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings)

Example 58 with ClusterHealthResponse

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

the class ClusterHealthIT method testHealth.

public void testHealth() {
    logger.info("--> running cluster health on an index that does not exists");
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth("test1").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(true));
    assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.RED));
    assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
    logger.info("--> running cluster wide health");
    healthResponse = client().admin().cluster().prepareHealth().setWaitForGreenStatus().setTimeout("10s").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(healthResponse.getIndices().isEmpty(), equalTo(true));
    logger.info("--> Creating index test1 with zero replicas");
    createIndex("test1");
    logger.info("--> running cluster health on an index that does exists");
    healthResponse = client().admin().cluster().prepareHealth("test1").setWaitForGreenStatus().setTimeout("10s").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(healthResponse.getIndices().get("test1").getStatus(), equalTo(ClusterHealthStatus.GREEN));
    logger.info("--> running cluster health on an index that does exists and an index that doesn't exists");
    healthResponse = client().admin().cluster().prepareHealth("test1", "test2").setWaitForYellowStatus().setTimeout("1s").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(true));
    assertThat(healthResponse.getStatus(), equalTo(ClusterHealthStatus.RED));
    assertThat(healthResponse.getIndices().get("test1").getStatus(), equalTo(ClusterHealthStatus.GREEN));
    assertThat(healthResponse.getIndices().size(), equalTo(1));
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)

Example 59 with ClusterHealthResponse

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

the class IndexPrimaryRelocationIT method testPrimaryRelocationWhileIndexing.

@TestLogging("_root:DEBUG,org.elasticsearch.action.bulk:TRACE,org.elasticsearch.index.shard:TRACE,org.elasticsearch.cluster.service:TRACE")
public void testPrimaryRelocationWhileIndexing() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(randomIntBetween(2, 3));
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)).addMapping("type", "field", "type=text").get();
    ensureGreen("test");
    AtomicInteger numAutoGenDocs = new AtomicInteger();
    final AtomicBoolean finished = new AtomicBoolean(false);
    Thread indexingThread = new Thread() {

        @Override
        public void run() {
            while (finished.get() == false) {
                IndexResponse indexResponse = client().prepareIndex("test", "type", "id").setSource("field", "value").get();
                assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
                DeleteResponse deleteResponse = client().prepareDelete("test", "type", "id").get();
                assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
                client().prepareIndex("test", "type").setSource("auto", true).get();
                numAutoGenDocs.incrementAndGet();
            }
        }
    };
    indexingThread.start();
    ClusterState initialState = client().admin().cluster().prepareState().get().getState();
    DiscoveryNode[] dataNodes = initialState.getNodes().getDataNodes().values().toArray(DiscoveryNode.class);
    DiscoveryNode relocationSource = initialState.getNodes().getDataNodes().get(initialState.getRoutingTable().shardRoutingTable("test", 0).primaryShard().currentNodeId());
    for (int i = 0; i < RELOCATION_COUNT; i++) {
        DiscoveryNode relocationTarget = randomFrom(dataNodes);
        while (relocationTarget.equals(relocationSource)) {
            relocationTarget = randomFrom(dataNodes);
        }
        logger.info("--> [iteration {}] relocating from {} to {} ", i, relocationSource.getName(), relocationTarget.getName());
        client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, relocationSource.getId(), relocationTarget.getId())).execute().actionGet();
        ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNoRelocatingShards(true).execute().actionGet();
        assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
        logger.info("--> [iteration {}] relocation complete", i);
        relocationSource = relocationTarget;
        if (indexingThread.isAlive() == false) {
            // indexing process aborted early, no need for more relocations as test has already failed
            break;
        }
        if (i > 0 && i % 5 == 0) {
            logger.info("--> [iteration {}] flushing index", i);
            client().admin().indices().prepareFlush("test").get();
        }
    }
    finished.set(true);
    indexingThread.join();
    refresh("test");
    ElasticsearchAssertions.assertHitCount(client().prepareSearch("test").get(), numAutoGenDocs.get());
    ElasticsearchAssertions.assertHitCount(// extra paranoia ;)
    client().prepareSearch("test").setQuery(QueryBuilders.termQuery("auto", true)).get(), numAutoGenDocs.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexResponse(org.elasticsearch.action.index.IndexResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) TestLogging(org.elasticsearch.test.junit.annotations.TestLogging)

Example 60 with ClusterHealthResponse

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

the class SimpleIndexStateIT method testFastCloseAfterCreateContinuesCreateAfterOpen.

public void testFastCloseAfterCreateContinuesCreateAfterOpen() {
    logger.info("--> creating test index that cannot be allocated");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE).setSettings(Settings.builder().put("index.routing.allocation.include.tag", "no_such_node").build()).get();
    ClusterHealthResponse health = client().admin().cluster().prepareHealth("test").setWaitForNodes(">=2").get();
    assertThat(health.isTimedOut(), equalTo(false));
    assertThat(health.getStatus(), equalTo(ClusterHealthStatus.RED));
    client().admin().indices().prepareClose("test").get();
    logger.info("--> updating test index settings to allow allocation");
    client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put("index.routing.allocation.include.tag", "").build()).get();
    client().admin().indices().prepareOpen("test").get();
    logger.info("--> waiting for green status");
    ensureGreen();
    NumShards numShards = getNumShards("test");
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metaData().index("test").getState(), equalTo(IndexMetaData.State.OPEN));
    assertThat(stateResponse.getState().routingTable().index("test").shards().size(), equalTo(numShards.numPrimaries));
    assertEquals(stateResponse.getState().routingTable().index("test").shardsWithState(ShardRoutingState.STARTED).size(), numShards.totalNumShards);
    logger.info("--> indexing a simple document");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1").get();
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)

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