Search in sources :

Example 56 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class IndexRequestTests method testIndexResponse.

public void testIndexResponse() {
    ShardId shardId = new ShardId(randomAsciiOfLengthBetween(3, 10), randomAsciiOfLengthBetween(3, 10), randomIntBetween(0, 1000));
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    long version = randomLong();
    boolean created = randomBoolean();
    IndexResponse indexResponse = new IndexResponse(shardId, type, id, SequenceNumbersService.UNASSIGNED_SEQ_NO, version, created);
    int total = randomIntBetween(1, 10);
    int successful = randomIntBetween(1, 10);
    ReplicationResponse.ShardInfo shardInfo = new ReplicationResponse.ShardInfo(total, successful);
    indexResponse.setShardInfo(shardInfo);
    boolean forcedRefresh = false;
    if (randomBoolean()) {
        forcedRefresh = randomBoolean();
        indexResponse.setForcedRefresh(forcedRefresh);
    }
    assertEquals(type, indexResponse.getType());
    assertEquals(id, indexResponse.getId());
    assertEquals(version, indexResponse.getVersion());
    assertEquals(shardId, indexResponse.getShardId());
    assertEquals(created ? RestStatus.CREATED : RestStatus.OK, indexResponse.status());
    assertEquals(total, indexResponse.getShardInfo().getTotal());
    assertEquals(successful, indexResponse.getShardInfo().getSuccessful());
    assertEquals(forcedRefresh, indexResponse.forcedRefresh());
    assertEquals("IndexResponse[index=" + shardId.getIndexName() + ",type=" + type + ",id=" + id + ",version=" + version + ",result=" + (created ? "created" : "updated") + ",seqNo=" + SequenceNumbersService.UNASSIGNED_SEQ_NO + ",shards={\"total\":" + total + ",\"successful\":" + successful + ",\"failed\":0}]", indexResponse.toString());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Matchers.containsString(org.hamcrest.Matchers.containsString) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse)

Example 57 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class ShardSearchFailureTests method testToXContent.

public void testToXContent() throws IOException {
    ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(0, 0, "some message", null), new SearchShardTarget("nodeId", new ShardId(new Index("indexName", "indexUuid"), 123)));
    BytesReference xContent = toXContent(failure, XContentType.JSON, randomBoolean());
    assertEquals("{\"shard\":123," + "\"index\":\"indexName\"," + "\"node\":\"nodeId\"," + "\"reason\":{" + "\"type\":\"parsing_exception\"," + "\"reason\":\"some message\"," + "\"line\":0," + "\"col\":0" + "}" + "}", xContent.utf8ToString());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) BytesReference(org.elasticsearch.common.bytes.BytesReference) ParsingException(org.elasticsearch.common.ParsingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Index(org.elasticsearch.index.Index)

Example 58 with ShardId

use of org.elasticsearch.index.shard.ShardId 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 59 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class AckIT method testClusterRerouteAcknowledgement.

public void testClusterRerouteAcknowledgement() throws InterruptedException {
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS)).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    ensureGreen();
    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();
    final Index index = client().admin().cluster().prepareState().get().getState().metaData().index("test").getIndex();
    final ShardId commandShard = new ShardId(index, moveAllocationCommand.shardId());
    assertAcked(client().admin().cluster().prepareReroute().add(moveAllocationCommand));
    for (Client client : clients()) {
        ClusterState clusterState = getLocalClusterState(client);
        for (ShardRouting shardRouting : clusterState.getRoutingNodes().node(moveAllocationCommand.fromNode())) {
            //if the shard that we wanted to move is still on the same node, it must be relocating
            if (shardRouting.shardId().equals(commandShard)) {
                assertThat(shardRouting.relocating(), equalTo(true));
            }
        }
        boolean found = false;
        for (ShardRouting shardRouting : clusterState.getRoutingNodes().node(moveAllocationCommand.toNode())) {
            if (shardRouting.shardId().equals(commandShard)) {
                assertThat(shardRouting.state(), anyOf(equalTo(ShardRoutingState.INITIALIZING), equalTo(ShardRoutingState.STARTED)));
                found = true;
                break;
            }
        }
        assertThat(found, equalTo(true));
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AliasOrIndex(org.elasticsearch.cluster.metadata.AliasOrIndex) Index(org.elasticsearch.index.Index) Client(org.elasticsearch.client.Client) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 60 with ShardId

use of org.elasticsearch.index.shard.ShardId in project elasticsearch by elastic.

the class AckIT method testClusterRerouteAcknowledgementDryRun.

public void testClusterRerouteAcknowledgementDryRun() 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();
    final Index index = client().admin().cluster().prepareState().get().getState().metaData().index("test").getIndex();
    final ShardId commandShard = new ShardId(index, moveAllocationCommand.shardId());
    assertAcked(client().admin().cluster().prepareReroute().setDryRun(true).add(moveAllocationCommand));
    //testing only on master with the latest cluster state as we didn't make any change thus we cannot guarantee that
    //all nodes hold the same cluster state version. We only know there was no need to change anything, thus no need for ack on this update.
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
    boolean found = false;
    for (ShardRouting shardRouting : clusterStateResponse.getState().getRoutingNodes().node(moveAllocationCommand.fromNode())) {
        //the shard that we wanted to move is still on the same node, as we had dryRun flag
        if (shardRouting.shardId().equals(commandShard)) {
            assertThat(shardRouting.started(), equalTo(true));
            found = true;
            break;
        }
    }
    assertThat(found, equalTo(true));
    for (ShardRouting shardRouting : clusterStateResponse.getState().getRoutingNodes().node(moveAllocationCommand.toNode())) {
        if (shardRouting.shardId().equals(commandShard)) {
            fail("shard [" + shardRouting + "] shouldn't be on node [" + moveAllocationCommand.toString() + "]");
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) AliasOrIndex(org.elasticsearch.cluster.metadata.AliasOrIndex) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Aggregations

ShardId (org.elasticsearch.index.shard.ShardId)478 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)106 Index (org.elasticsearch.index.Index)101 ClusterState (org.elasticsearch.cluster.ClusterState)98 Test (org.junit.Test)83 ArrayList (java.util.ArrayList)78 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)69 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)67 HashMap (java.util.HashMap)66 IOException (java.io.IOException)61 IndexShard (org.elasticsearch.index.shard.IndexShard)61 Map (java.util.Map)55 List (java.util.List)51 Settings (org.elasticsearch.common.settings.Settings)49 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)45 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)44 HashSet (java.util.HashSet)43 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)43 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)42 Matchers.containsString (org.hamcrest.Matchers.containsString)42