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;
}
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;
}
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 + ").";
}
}
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"));
}
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());
}
Aggregations