Search in sources :

Example 26 with IndexShardRoutingTable

use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project elasticsearch by elastic.

the class ActiveShardCount method enoughShardsActive.

/**
     * Returns true iff the given cluster state's routing table contains enough active
     * shards for the given index to meet the required shard count represented by this instance.
     */
public boolean enoughShardsActive(final ClusterState clusterState, final String indexName) {
    if (this == ActiveShardCount.NONE) {
        // not waiting for any active shards
        return true;
    }
    final IndexMetaData indexMetaData = clusterState.metaData().index(indexName);
    if (indexMetaData == null) {
        // and we can stop waiting
        return true;
    }
    final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName);
    assert indexRoutingTable != null;
    if (indexRoutingTable.allPrimaryShardsActive() == false) {
        // all primary shards aren't active yet
        return false;
    }
    ActiveShardCount waitForActiveShards = this;
    if (waitForActiveShards == ActiveShardCount.DEFAULT) {
        waitForActiveShards = SETTING_WAIT_FOR_ACTIVE_SHARDS.get(indexMetaData.getSettings());
    }
    for (final IntObjectCursor<IndexShardRoutingTable> shardRouting : indexRoutingTable.getShards()) {
        if (waitForActiveShards.enoughShardsActive(shardRouting.value) == false) {
            // not enough active shard copies yet
            return false;
        }
    }
    return true;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 27 with IndexShardRoutingTable

use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project elasticsearch by elastic.

the class TransportBroadcastReplicationAction method shards.

/**
     * @return all shard ids the request should run on
     */
protected List<ShardId> shards(Request request, ClusterState clusterState) {
    List<ShardId> shardIds = new ArrayList<>();
    String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
    for (String index : concreteIndices) {
        IndexMetaData indexMetaData = clusterState.metaData().getIndices().get(index);
        if (indexMetaData != null) {
            for (IntObjectCursor<IndexShardRoutingTable> shardRouting : clusterState.getRoutingTable().indicesRouting().get(index).getShards()) {
                shardIds.add(shardRouting.value.shardId());
            }
        }
    }
    return shardIds;
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 28 with IndexShardRoutingTable

use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project elasticsearch by elastic.

the class ReplicationOperation method checkActiveShardCount.

/**
     * Checks whether we can perform a write based on the required active shard count setting.
     * Returns **null* if OK to proceed, or a string describing the reason to stop
     */
protected String checkActiveShardCount() {
    final ShardId shardId = primary.routingEntry().shardId();
    final String indexName = shardId.getIndexName();
    final ClusterState state = clusterStateSupplier.get();
    assert state != null : "replication operation must have access to the cluster state";
    final ActiveShardCount waitForActiveShards = request.waitForActiveShards();
    if (waitForActiveShards == ActiveShardCount.NONE) {
        // not waiting for any shards
        return null;
    }
    IndexRoutingTable indexRoutingTable = state.getRoutingTable().index(indexName);
    if (indexRoutingTable == null) {
        logger.trace("[{}] index not found in the routing table", shardId);
        return "Index " + indexName + " not found in the routing table";
    }
    IndexShardRoutingTable shardRoutingTable = indexRoutingTable.shard(shardId.getId());
    if (shardRoutingTable == null) {
        logger.trace("[{}] shard not found in the routing table", shardId);
        return "Shard " + shardId + " not found in the routing table";
    }
    if (waitForActiveShards.enoughShardsActive(shardRoutingTable)) {
        return null;
    } else {
        final String resolvedShards = waitForActiveShards == ActiveShardCount.ALL ? Integer.toString(shardRoutingTable.shards().size()) : waitForActiveShards.toString();
        logger.trace("[{}] not enough active copies to meet shard count of [{}] (have {}, needed {}), scheduling a retry. op [{}], " + "request [{}]", shardId, waitForActiveShards, shardRoutingTable.activeShards().size(), resolvedShards, opType, request);
        return "Not enough active copies to meet shard count of [" + waitForActiveShards + "] (have " + shardRoutingTable.activeShards().size() + ", needed " + resolvedShards + ").";
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount)

Example 29 with IndexShardRoutingTable

use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project crate by crate.

the class RemoteCollectorIntegrationTest method testUpdateWithExpressionAndRelocatedShard.

@Test
public void testUpdateWithExpressionAndRelocatedShard() throws Exception {
    execute("create table t (id int primary key, x int) " + "clustered into 2 shards " + "with (number_of_replicas = 0)");
    ensureGreen();
    execute("insert into t (id, x) values (1, 10)");
    execute("insert into t (id, x) values (2, 20)");
    execute("refresh table t");
    PlanForNode plan = plan("update t set x = x * 2");
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class);
    IndexShardRoutingTable t = clusterService.state().routingTable().shardRoutingTable("t", 0);
    String sourceNodeId = t.primaryShard().currentNodeId();
    assert sourceNodeId != null;
    String targetNodeId = null;
    for (ObjectCursor<String> cursor : clusterService.state().nodes().dataNodes().keys()) {
        if (!sourceNodeId.equals(cursor.value)) {
            targetNodeId = cursor.value;
        }
    }
    assert targetNodeId != null;
    client().admin().cluster().prepareReroute().add(new MoveAllocationCommand(new ShardId("t", 0), sourceNodeId, targetNodeId)).execute().actionGet();
    client().admin().cluster().prepareHealth("t").setWaitForEvents(Priority.LANGUID).setWaitForRelocatingShards(0).setTimeout(TimeValue.timeValueSeconds(5)).execute().actionGet();
    execute(plan).getResult();
    execute("refresh table t");
    assertThat(TestingHelpers.printedTable(execute("select * from t order by id").rows()), is("1| 20\n" + "2| 40\n"));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ClusterService(org.elasticsearch.cluster.ClusterService) MoveAllocationCommand(org.elasticsearch.cluster.routing.allocation.command.MoveAllocationCommand) Test(org.junit.Test)

Example 30 with IndexShardRoutingTable

use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project crate by crate.

the class TransportBulkCreateIndicesActionTest method testRoutingOfIndicesIsNotOverridden.

@Test
public void testRoutingOfIndicesIsNotOverridden() throws Exception {
    cluster().client().admin().indices().prepareCreate("index_0").setSettings(Settings.builder().put("number_of_shards", 1).put("number_of_replicas", 0)).execute().actionGet();
    ensureYellow("index_0");
    ClusterState currentState = internalCluster().clusterService().state();
    BulkCreateIndicesRequest request = new BulkCreateIndicesRequest(Arrays.asList("index_0", "index_1"), UUID.randomUUID());
    currentState = action.executeCreateIndices(currentState, request);
    ImmutableOpenIntMap<IndexShardRoutingTable> newRouting = currentState.routingTable().indicesRouting().get("index_0").getShards();
    assertTrue("[index_0][0] must be started already", newRouting.get(0).primaryShard().started());
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) Test(org.junit.Test) SQLTransportIntegrationTest(io.crate.integrationtests.SQLTransportIntegrationTest)

Aggregations

IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)54 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)39 ClusterState (org.elasticsearch.cluster.ClusterState)33 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)22 ShardId (org.elasticsearch.index.shard.ShardId)21 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)15 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)14 PlainActionFuture (org.elasticsearch.action.support.PlainActionFuture)8 Settings (org.elasticsearch.common.settings.Settings)8 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)6 IndexService (org.elasticsearch.index.IndexService)6 IndicesService (org.elasticsearch.indices.IndicesService)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Set (java.util.Set)5 MetaData (org.elasticsearch.cluster.metadata.MetaData)5 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)5 TransportRequest (org.elasticsearch.transport.TransportRequest)5 Matchers.anyString (org.mockito.Matchers.anyString)5