Search in sources :

Example 1 with ClusterRerouteResponse

use of org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse in project elasticsearch by elastic.

the class ShrinkIndexIT method testCreateShrinkIndexFails.

/**
     * Tests that we can manually recover from a failed allocation due to shards being moved away etc.
     */
public void testCreateShrinkIndexFails() throws Exception {
    internalCluster().ensureAtLeastNumDataNodes(2);
    prepareCreate("source").setSettings(Settings.builder().put(indexSettings()).put("number_of_shards", randomIntBetween(2, 7)).put("number_of_replicas", 0)).get();
    for (int i = 0; i < 20; i++) {
        client().prepareIndex("source", randomFrom("t1", "t2", "t3")).setSource("{\"foo\" : \"bar\", \"i\" : " + i + "}", XContentType.JSON).get();
    }
    ImmutableOpenMap<String, DiscoveryNode> dataNodes = client().admin().cluster().prepareState().get().getState().nodes().getDataNodes();
    assertTrue("at least 2 nodes but was: " + dataNodes.size(), dataNodes.size() >= 2);
    DiscoveryNode[] discoveryNodes = dataNodes.values().toArray(DiscoveryNode.class);
    String spareNode = discoveryNodes[0].getName();
    String mergeNode = discoveryNodes[1].getName();
    // ensure all shards are allocated otherwise the ensure green below might not succeed since we require the merge node
    // if we change the setting too quickly we will end up with one replica unassigned which can't be assigned anymore due
    // to the require._name below.
    ensureGreen();
    // relocate all shards to one node such that we can merge it.
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode).put("index.blocks.write", true)).get();
    ensureGreen();
    // now merge source into a single shard index
    client().admin().indices().prepareShrinkIndex("source", "target").setWaitForActiveShards(ActiveShardCount.NONE).setSettings(Settings.builder().put("index.routing.allocation.exclude._name", // we manually exclude the merge node to forcefully fuck it up
    mergeNode).put("index.number_of_replicas", 0).put("index.allocation.max_retries", 1).build()).get();
    client().admin().cluster().prepareHealth("target").setWaitForEvents(Priority.LANGUID).get();
    // now we move all shards away from the merge node
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", spareNode).put("index.blocks.write", true)).get();
    ensureGreen("source");
    // erase the forcefully fuckup!
    client().admin().indices().prepareUpdateSettings("target").setSettings(Settings.builder().putNull("index.routing.allocation.exclude._name")).get();
    // wait until it fails
    assertBusy(() -> {
        ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
        RoutingTable routingTables = clusterStateResponse.getState().routingTable();
        assertTrue(routingTables.index("target").shard(0).getShards().get(0).unassigned());
        assertEquals(UnassignedInfo.Reason.ALLOCATION_FAILED, routingTables.index("target").shard(0).getShards().get(0).unassignedInfo().getReason());
        assertEquals(1, routingTables.index("target").shard(0).getShards().get(0).unassignedInfo().getNumFailedAllocations());
    });
    // now relocate them all to the right node
    client().admin().indices().prepareUpdateSettings("source").setSettings(Settings.builder().put("index.routing.allocation.require._name", mergeNode)).get();
    ensureGreen("source");
    final InternalClusterInfoService infoService = (InternalClusterInfoService) internalCluster().getInstance(ClusterInfoService.class, internalCluster().getMasterName());
    infoService.refresh();
    // kick off a retry and wait until it's done!
    ClusterRerouteResponse clusterRerouteResponse = client().admin().cluster().prepareReroute().setRetryFailed(true).get();
    long expectedShardSize = clusterRerouteResponse.getState().routingTable().index("target").shard(0).getShards().get(0).getExpectedShardSize();
    // we support the expected shard size in the allocator to sum up over the source index shards
    assertTrue("expected shard size must be set but wasn't: " + expectedShardSize, expectedShardSize > 0);
    ensureGreen();
    assertHitCount(client().prepareSearch("target").setSize(100).setQuery(new TermsQueryBuilder("foo", "bar")).get(), 20);
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService) ClusterInfoService(org.elasticsearch.cluster.ClusterInfoService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) InternalClusterInfoService(org.elasticsearch.cluster.InternalClusterInfoService) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 2 with ClusterRerouteResponse

use of org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse in project elasticsearch by elastic.

the class AckIT method testClusterRerouteNoAcknowledgement.

public void testClusterRerouteNoAcknowledgement() throws InterruptedException {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS)).put(SETTING_NUMBER_OF_REPLICAS, 0)).get();
    ensureGreen();
    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();
    ClusterRerouteResponse clusterRerouteResponse = client().admin().cluster().prepareReroute().setTimeout("0s").add(moveAllocationCommand).get();
    assertThat(clusterRerouteResponse.isAcknowledged(), equalTo(false));
}
Also used : MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 3 with ClusterRerouteResponse

use of org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse in project elasticsearch by elastic.

the class AckIT method testClusterRerouteNoAcknowledgementDryRun.

public void testClusterRerouteNoAcknowledgementDryRun() throws InterruptedException {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS)).put(SETTING_NUMBER_OF_REPLICAS, 0)).get();
    ensureGreen();
    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();
    ClusterRerouteResponse clusterRerouteResponse = client().admin().cluster().prepareReroute().setTimeout("0s").setDryRun(true).add(moveAllocationCommand).get();
    //acknowledged anyway as no changes were made
    assertThat(clusterRerouteResponse.isAcknowledged(), equalTo(true));
}
Also used : MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 4 with ClusterRerouteResponse

use of org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse in project elasticsearch by elastic.

the class RestClusterRerouteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ClusterRerouteRequest clusterRerouteRequest = createRequest(request);
    // by default, return everything but metadata
    final String metric = request.param("metric");
    if (metric == null) {
        request.params().put("metric", DEFAULT_METRICS);
    }
    return channel -> client.admin().cluster().reroute(clusterRerouteRequest, new AcknowledgedRestListener<ClusterRerouteResponse>(channel) {

        @Override
        protected void addCustomFields(XContentBuilder builder, ClusterRerouteResponse response) throws IOException {
            builder.startObject("state");
            settingsFilter.addFilterSettingParams(request);
            response.getState().toXContent(builder, request);
            builder.endObject();
            if (clusterRerouteRequest.explain()) {
                assert response.getExplanations() != null;
                response.getExplanations().toXContent(builder, ToXContent.EMPTY_PARAMS);
            }
        }
    });
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) SettingsFilter(org.elasticsearch.common.settings.SettingsFilter) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse) Strings(org.elasticsearch.common.Strings) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) AllocationCommands(org.elasticsearch.cluster.routing.allocation.command.AllocationCommands) ValueType(org.elasticsearch.common.xcontent.ObjectParser.ValueType) ObjectParser(org.elasticsearch.common.xcontent.ObjectParser) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) Requests(org.elasticsearch.client.Requests) ParseField(org.elasticsearch.common.ParseField) Collections(java.util.Collections) EnumSet(java.util.EnumSet) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) ClusterRerouteRequest(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ClusterRerouteResponse(org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)

Example 5 with ClusterRerouteResponse

use of org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse 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

ClusterRerouteResponse (org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse)5 MoveAllocationCommand (org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand)3 Settings (org.elasticsearch.common.settings.Settings)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)1 ClusterRerouteRequest (org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest)1 ClusterStateResponse (org.elasticsearch.action.admin.cluster.state.ClusterStateResponse)1 Requests (org.elasticsearch.client.Requests)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 ClusterInfoService (org.elasticsearch.cluster.ClusterInfoService)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 InternalClusterInfoService (org.elasticsearch.cluster.InternalClusterInfoService)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 RerouteExplanation (org.elasticsearch.cluster.routing.allocation.RerouteExplanation)1 RoutingExplanations (org.elasticsearch.cluster.routing.allocation.RoutingExplanations)1