Search in sources :

Example 16 with ClusterHealthResponse

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

the class AwarenessAllocationIT method testAwarenessZones.

public void testAwarenessZones() throws Exception {
    Settings commonSettings = Settings.builder().put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING.getKey() + "zone.values", "a,b").put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), "zone").put(ZenDiscovery.JOIN_TIMEOUT_SETTING.getKey(), "10s").build();
    logger.info("--> starting 4 nodes on different zones");
    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(), Settings.builder().put(commonSettings).put("node.attr.zone", "b").build(), Settings.builder().put(commonSettings).put("node.attr.zone", "a").build());
    String A_0 = nodes.get(0);
    String B_0 = nodes.get(1);
    String B_1 = nodes.get(2);
    String A_1 = nodes.get(3);
    logger.info("--> waiting for nodes to form a cluster");
    ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("4").execute().actionGet();
    assertThat(health.isTimedOut(), equalTo(false));
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 1)).execute().actionGet();
    logger.info("--> waiting for shards to be allocated");
    health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().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_1), anyOf(equalTo(2), equalTo(3)));
    assertThat(counts.get(B_1), anyOf(equalTo(2), equalTo(3)));
    assertThat(counts.get(A_0), anyOf(equalTo(2), equalTo(3)));
    assertThat(counts.get(B_0), anyOf(equalTo(2), equalTo(3)));
}
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 17 with ClusterHealthResponse

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

the class ClusterRerouteIT method testClusterRerouteWithBlocks.

public void testClusterRerouteWithBlocks() throws Exception {
    List<String> nodesIds = internalCluster().startNodes(2);
    logger.info("--> create an index with 1 shard and 0 replicas");
    assertAcked(prepareCreate("test-blocks").setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0)));
    ensureGreen("test-blocks");
    logger.info("--> check that the index has 1 shard");
    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    List<ShardRouting> shards = state.routingTable().allShards("test-blocks");
    assertThat(shards, hasSize(1));
    logger.info("--> check that the shard is allocated");
    ShardRouting shard = shards.get(0);
    assertThat(shard.assignedToNode(), equalTo(true));
    logger.info("--> retrieve the node where the shard is allocated");
    DiscoveryNode node = state.nodes().resolveNode(shard.currentNodeId());
    assertNotNull(node);
    // toggle is used to mve the shard from one node to another
    int toggle = nodesIds.indexOf(node.getName());
    // Rerouting shards is not blocked
    for (String blockSetting : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE, SETTING_READ_ONLY, SETTING_BLOCKS_METADATA)) {
        try {
            enableIndexBlock("test-blocks", blockSetting);
            assertAcked(client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test-blocks", 0, nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
            ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForYellowStatus().setWaitForNoRelocatingShards(true).execute().actionGet();
            assertThat(healthResponse.isTimedOut(), equalTo(false));
        } finally {
            disableIndexBlock("test-blocks", blockSetting);
        }
    }
    // Rerouting shards is blocked when the cluster is read only
    try {
        setClusterReadOnly(true);
        assertBlocked(client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test-blocks", 1, nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
    } finally {
        setClusterReadOnly(false);
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 18 with ClusterHealthResponse

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

the class ClusterRerouteIT method rerouteWithAllocateLocalGateway.

private void rerouteWithAllocateLocalGateway(Settings commonSettings) throws Exception {
    logger.info("--> starting 2 nodes");
    String node_1 = internalCluster().startNode(commonSettings);
    internalCluster().startNode(commonSettings);
    assertThat(cluster().size(), equalTo(2));
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> create an index with 1 shard, 1 replica, nothing should allocate");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE).setSettings(Settings.builder().put("index.number_of_shards", 1)).execute().actionGet();
    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(2));
    logger.info("--> explicitly allocate shard 1, actually allocating, no dry run");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean()).add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> get the state, verify shard 1 primary allocated");
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
    client().prepareIndex("test", "type", "1").setSource("field", "value").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    final Index index = resolveIndex("test");
    logger.info("--> closing all nodes");
    Path[] shardLocation = internalCluster().getInstance(NodeEnvironment.class, node_1).availableShardPaths(new ShardId(index, 0));
    // make sure the data is there!
    assertThat(FileSystemUtils.exists(shardLocation), equalTo(true));
    // don't wipe data directories the index needs to be there!
    internalCluster().closeNonSharedNodes(false);
    logger.info("--> deleting the shard data [{}] ", Arrays.toString(shardLocation));
    // verify again after cluster was shut down
    assertThat(FileSystemUtils.exists(shardLocation), equalTo(true));
    IOUtils.rm(shardLocation);
    logger.info("--> starting nodes back, will not allocate the shard since it has no data, but the index will be there");
    node_1 = internalCluster().startNode(commonSettings);
    internalCluster().startNode(commonSettings);
    // wait a bit for the cluster to realize that the shard is not there...
    // TODO can we get around this? the cluster is RED, so what do we wait for?
    client().admin().cluster().prepareReroute().get();
    assertThat(client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet().getStatus(), equalTo(ClusterHealthStatus.RED));
    logger.info("--> explicitly allocate primary");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean()).add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    logger.info("--> get the state, verify shard 1 primary allocated");
    final String nodeToCheck = node_1;
    assertBusy(() -> {
        ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
        String nodeId = clusterState.nodes().resolveNode(nodeToCheck).getId();
        assertThat(clusterState.getRoutingNodes().node(nodeId).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
    });
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) Index(org.elasticsearch.index.Index)

Example 19 with ClusterHealthResponse

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

the class ClusterRerouteIT method rerouteWithCommands.

private void rerouteWithCommands(Settings commonSettings) throws Exception {
    List<String> nodesIds = internalCluster().startNodes(2, commonSettings);
    final String node_1 = nodesIds.get(0);
    final String node_2 = nodesIds.get(1);
    logger.info("--> create an index with 1 shard, 1 replica, nothing should allocate");
    client().admin().indices().prepareCreate("test").setWaitForActiveShards(ActiveShardCount.NONE).setSettings(Settings.builder().put("index.number_of_shards", 1)).execute().actionGet();
    ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(2));
    logger.info("--> explicitly allocate shard 1, *under dry_run*");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean()).add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).setDryRun(true).execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    logger.info("--> get the state, verify nothing changed because of the dry run");
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(2));
    logger.info("--> explicitly allocate shard 1, actually allocating, no dry run");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean()).add(new AllocateEmptyPrimaryAllocationCommand("test", 0, node_1, true)).execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> get the state, verify shard 1 primary allocated");
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
    logger.info("--> move shard 1 primary from node1 to node2");
    state = client().admin().cluster().prepareReroute().setExplain(randomBoolean()).add(new MoveAllocationCommand("test", 0, node_1, node_2)).execute().actionGet().getState();
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(), equalTo(ShardRoutingState.RELOCATING));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_2).getId()).iterator().next().state(), equalTo(ShardRoutingState.INITIALIZING));
    healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().setWaitForNoRelocatingShards(true).execute().actionGet();
    assertThat(healthResponse.isTimedOut(), equalTo(false));
    logger.info("--> get the state, verify shard 1 primary moved from node1 to node2");
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.getRoutingNodes().unassigned().size(), equalTo(1));
    assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_2).getId()).iterator().next().state(), equalTo(ShardRoutingState.STARTED));
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) AllocateEmptyPrimaryAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)

Example 20 with ClusterHealthResponse

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

the class MinimumMasterNodesIT method testSimpleMinimumMasterNodes.

public void testSimpleMinimumMasterNodes() throws Exception {
    Settings settings = Settings.builder().put("discovery.zen.minimum_master_nodes", 2).put(ZenDiscovery.PING_TIMEOUT_SETTING.getKey(), "200ms").put("discovery.initial_state_timeout", "500ms").build();
    logger.info("--> start first node");
    internalCluster().startNode(settings);
    logger.info("--> should be blocked, no master...");
    ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
    // verify that we still see the local node in the cluster state
    assertThat(state.nodes().getSize(), equalTo(1));
    logger.info("--> start second node, cluster should be formed");
    internalCluster().startNode(settings);
    ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("2").execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.nodes().getSize(), equalTo(2));
    assertThat(state.metaData().indices().containsKey("test"), equalTo(false));
    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();
    }
    // make sure that all shards recovered before trying to flush
    assertThat(client().admin().cluster().prepareHealth("test").setWaitForActiveShards(numShards.totalNumShards).execute().actionGet().getActiveShards(), equalTo(numShards.totalNumShards));
    // flush for simpler debugging
    flushAndRefresh();
    logger.info("--> verify we the data back");
    for (int i = 0; i < 10; i++) {
        assertThat(client().prepareSearch().setSize(0).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet().getHits().getTotalHits(), equalTo(100L));
    }
    internalCluster().stopCurrentMasterNode();
    awaitBusy(() -> {
        ClusterState clusterState = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
        return clusterState.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID);
    });
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
    // verify that both nodes are still in the cluster state but there is no master
    assertThat(state.nodes().getSize(), equalTo(2));
    assertThat(state.nodes().getMasterNode(), equalTo(null));
    logger.info("--> starting the previous master node again...");
    internalCluster().startNode(settings);
    clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus().setWaitForNodes("2").execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.nodes().getSize(), equalTo(2));
    assertThat(state.metaData().indices().containsKey("test"), equalTo(true));
    ensureGreen();
    logger.info("--> verify we the data back after cluster reform");
    for (int i = 0; i < 10; i++) {
        assertHitCount(client().prepareSearch().setSize(0).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet(), 100);
    }
    internalCluster().stopRandomNonMasterNode();
    assertBusy(new Runnable() {

        @Override
        public void run() {
            ClusterState state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
            assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(true));
        }
    });
    logger.info("--> starting the previous master node again...");
    internalCluster().startNode(settings);
    ensureGreen();
    clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes("2").setWaitForGreenStatus().execute().actionGet();
    assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().setLocal(true).execute().actionGet().getState();
    assertThat(state.blocks().hasGlobalBlock(DiscoverySettings.NO_MASTER_BLOCK_ID), equalTo(false));
    state = client().admin().cluster().prepareState().execute().actionGet().getState();
    assertThat(state.nodes().getSize(), equalTo(2));
    assertThat(state.metaData().indices().containsKey("test"), equalTo(true));
    logger.info("Running Cluster Health");
    ensureGreen();
    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) Settings(org.elasticsearch.common.settings.Settings) DiscoverySettings(org.elasticsearch.discovery.DiscoverySettings)

Aggregations

ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)123 Settings (org.elasticsearch.common.settings.Settings)29 Client (org.elasticsearch.client.Client)24 ClusterState (org.elasticsearch.cluster.ClusterState)16 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)15 Test (org.junit.Test)15 IOException (java.io.IOException)13 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)12 SearchResponse (org.elasticsearch.action.search.SearchResponse)11 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 CountDownLatch (java.util.concurrent.CountDownLatch)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)7 Index (org.elasticsearch.index.Index)7 MockTransportService (org.elasticsearch.test.transport.MockTransportService)6 TransportService (org.elasticsearch.transport.TransportService)6 Path (java.nio.file.Path)5