Search in sources :

Example 1 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 2 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)

Example 3 with IndexShardRoutingTable

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);
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) java.util(java.util) Projections(io.crate.planner.projection.Projections) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase) Inject(org.elasticsearch.common.inject.Inject) TreeMapBuilder(io.crate.core.collections.TreeMapBuilder) Routing(io.crate.metadata.Routing) Singleton(org.elasticsearch.common.inject.Singleton) ClusterService(org.elasticsearch.cluster.ClusterService) JobContextService(io.crate.jobs.JobContextService) TransportActionProvider(io.crate.executor.transport.TransportActionProvider) DistributionInfo(io.crate.planner.distribution.DistributionInfo) RamAccountingContext(io.crate.breaker.RamAccountingContext) RemoteCollector(io.crate.operation.collect.collectors.RemoteCollector) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) RemoteCollector(io.crate.operation.collect.collectors.RemoteCollector) RoutedCollectPhase(io.crate.planner.node.dql.RoutedCollectPhase)

Example 4 with IndexShardRoutingTable

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();
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable)

Example 5 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)

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