Example #1
0
  @Test
  public void testClusterRerouteNoAcknowledgementDryRun() throws InterruptedException {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            settingsBuilder()
                .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));
  }
  @Test
  public void rerouteExplain() {
    Settings commonSettings = settingsBuilder().build();

    logger.info("--> starting a node");
    String node_1 = cluster().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(
            settingsBuilder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
        .execute()
        .actionGet();

    ensureGreen("test");

    logger.info("--> disable allocation");
    Settings newSettings =
        settingsBuilder()
            .put(
                EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE,
                EnableAllocationDecider.Allocation.NONE.name())
            .build();
    client()
        .admin()
        .cluster()
        .prepareUpdateSettings()
        .setTransientSettings(newSettings)
        .execute()
        .actionGet();

    logger.info("--> starting a second node");
    String node_2 = cluster().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(new ShardId("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));
  }