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());
}
use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project crate by crate.
the class RemoteCollectorFactory method createCollector.
/**
* create a RemoteCollector
* The RemoteCollector will collect data from another node using a wormhole as if it was collecting on this node.
* <p>
* This should only be used if a shard is not available on the current node due to a relocation
*/
public CrateCollector.Builder createCollector(String index, Integer shardId, RoutedCollectPhase collectPhase, final RamAccountingContext ramAccountingContext) {
// new job because subContexts can't be merged into an existing job
final UUID childJobId = UUID.randomUUID();
IndexShardRoutingTable shardRoutings = clusterService.state().routingTable().shardRoutingTable(index, shardId);
// for update operations primaryShards must be used
// (for others that wouldn't be the case, but at this point it is not easily visible which is the case)
ShardRouting shardRouting = shardRoutings.primaryShard();
final String remoteNodeId = shardRouting.currentNodeId();
assert remoteNodeId != null : "primaryShard not assigned :(";
final String localNodeId = clusterService.localNode().getId();
final RoutedCollectPhase newCollectPhase = createNewCollectPhase(childJobId, collectPhase, index, shardId, remoteNodeId);
return consumer -> new RemoteCollector(childJobId, localNodeId, remoteNodeId, transportActionProvider.transportJobInitAction(), transportActionProvider.transportKillJobsNodeAction(), jobContextService, ramAccountingContext, consumer, newCollectPhase);
}
use of org.elasticsearch.cluster.routing.IndexShardRoutingTable in project elasticsearch by elastic.
the class GeoShapeIntegrationIT method findNodeName.
private String findNodeName(String index) {
ClusterState state = client().admin().cluster().prepareState().get().getState();
IndexShardRoutingTable shard = state.getRoutingTable().index(index).shard(0);
String nodeId = shard.assignedShards().get(0).currentNodeId();
return state.getNodes().get(nodeId).getName();
}
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;
}
Aggregations