Search in sources :

Example 96 with ShardRouting

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

the class ShardsSyncedFlushResult method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    failureReason = in.readOptionalString();
    int numResponses = in.readInt();
    shardResponses = new HashMap<>();
    for (int i = 0; i < numResponses; i++) {
        ShardRouting shardRouting = new ShardRouting(in);
        SyncedFlushService.ShardSyncedFlushResponse response = SyncedFlushService.ShardSyncedFlushResponse.readSyncedFlushResponse(in);
        shardResponses.put(shardRouting, response);
    }
    syncId = in.readOptionalString();
    shardId = ShardId.readShardId(in);
    totalShards = in.readInt();
}
Also used : ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 97 with ShardRouting

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

the class SyncedFlushService method innerAttemptSyncedFlush.

private void innerAttemptSyncedFlush(final ShardId shardId, final ClusterState state, final ActionListener<ShardsSyncedFlushResult> actionListener) {
    try {
        final IndexShardRoutingTable shardRoutingTable = getShardRoutingTable(shardId, state);
        final List<ShardRouting> activeShards = shardRoutingTable.activeShards();
        final int totalShards = shardRoutingTable.getSize();
        if (activeShards.size() == 0) {
            actionListener.onResponse(new ShardsSyncedFlushResult(shardId, totalShards, "no active shards"));
            return;
        }
        final ActionListener<Map<String, Engine.CommitId>> commitIdsListener = new ActionListener<Map<String, Engine.CommitId>>() {

            @Override
            public void onResponse(final Map<String, Engine.CommitId> commitIds) {
                if (commitIds.isEmpty()) {
                    actionListener.onResponse(new ShardsSyncedFlushResult(shardId, totalShards, "all shards failed to commit on pre-sync"));
                    return;
                }
                final ActionListener<InFlightOpsResponse> inflightOpsListener = new ActionListener<InFlightOpsResponse>() {

                    @Override
                    public void onResponse(InFlightOpsResponse response) {
                        final int inflight = response.opCount();
                        assert inflight >= 0;
                        if (inflight != 0) {
                            actionListener.onResponse(new ShardsSyncedFlushResult(shardId, totalShards, "[" + inflight + "] ongoing operations on primary"));
                        } else {
                            // 3. now send the sync request to all the shards
                            String syncId = UUIDs.base64UUID();
                            sendSyncRequests(syncId, activeShards, state, commitIds, shardId, totalShards, actionListener);
                        }
                    }

                    @Override
                    public void onFailure(Exception e) {
                        actionListener.onFailure(e);
                    }
                };
                // 2. fetch in flight operations
                getInflightOpsCount(shardId, state, shardRoutingTable, inflightOpsListener);
            }

            @Override
            public void onFailure(Exception e) {
                actionListener.onFailure(e);
            }
        };
        // 1. send pre-sync flushes to all replicas
        sendPreSyncRequests(activeShards, state, shardId, commitIdsListener);
    } catch (Exception e) {
        actionListener.onFailure(e);
    }
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexClosedException(org.elasticsearch.indices.IndexClosedException) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException) TransportException(org.elasticsearch.transport.TransportException) ActionListener(org.elasticsearch.action.ActionListener) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) Engine(org.elasticsearch.index.engine.Engine)

Example 98 with ShardRouting

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

the class DiskUsageTests method testFillShardLevelInfo.

public void testFillShardLevelInfo() {
    final Index index = new Index("test", "0xdeadbeef");
    ShardRouting test_0 = ShardRouting.newUnassigned(new ShardId(index, 0), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_0 = ShardRoutingHelper.initialize(test_0, "node1");
    test_0 = ShardRoutingHelper.moveToStarted(test_0);
    Path test0Path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve("0");
    CommonStats commonStats0 = new CommonStats();
    commonStats0.store = new StoreStats(100);
    ShardRouting test_1 = ShardRouting.newUnassigned(new ShardId(index, 1), false, PeerRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "foo"));
    test_1 = ShardRoutingHelper.initialize(test_1, "node2");
    test_1 = ShardRoutingHelper.moveToStarted(test_1);
    Path test1Path = createTempDir().resolve("indices").resolve(index.getUUID()).resolve("1");
    CommonStats commonStats1 = new CommonStats();
    commonStats1.store = new StoreStats(1000);
    ShardStats[] stats = new ShardStats[] { new ShardStats(test_0, new ShardPath(false, test0Path, test0Path, test_0.shardId()), commonStats0, null, null), new ShardStats(test_1, new ShardPath(false, test1Path, test1Path, test_1.shardId()), commonStats1, null, null) };
    ImmutableOpenMap.Builder<String, Long> shardSizes = ImmutableOpenMap.builder();
    ImmutableOpenMap.Builder<ShardRouting, String> routingToPath = ImmutableOpenMap.builder();
    ClusterState state = ClusterState.builder(new ClusterName("blarg")).version(0).build();
    InternalClusterInfoService.buildShardLevelInfo(logger, stats, shardSizes, routingToPath, state);
    assertEquals(2, shardSizes.size());
    assertTrue(shardSizes.containsKey(ClusterInfo.shardIdentifierFromRouting(test_0)));
    assertTrue(shardSizes.containsKey(ClusterInfo.shardIdentifierFromRouting(test_1)));
    assertEquals(100L, shardSizes.get(ClusterInfo.shardIdentifierFromRouting(test_0)).longValue());
    assertEquals(1000L, shardSizes.get(ClusterInfo.shardIdentifierFromRouting(test_1)).longValue());
    assertEquals(2, routingToPath.size());
    assertTrue(routingToPath.containsKey(test_0));
    assertTrue(routingToPath.containsKey(test_1));
    assertEquals(test0Path.getParent().getParent().getParent().toAbsolutePath().toString(), routingToPath.get(test_0));
    assertEquals(test1Path.getParent().getParent().getParent().toAbsolutePath().toString(), routingToPath.get(test_1));
}
Also used : Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) StoreStats(org.elasticsearch.index.store.StoreStats) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) Index(org.elasticsearch.index.Index) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardId(org.elasticsearch.index.shard.ShardId) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) ShardPath(org.elasticsearch.index.shard.ShardPath) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 99 with ShardRouting

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

the class ClusterInfoTests method randomRoutingToDataPath.

private static ImmutableOpenMap<ShardRouting, String> randomRoutingToDataPath() {
    int numEntries = randomIntBetween(0, 128);
    ImmutableOpenMap.Builder<ShardRouting, String> builder = ImmutableOpenMap.builder(numEntries);
    for (int i = 0; i < numEntries; i++) {
        ShardId shardId = new ShardId(randomAsciiOfLength(32), randomAsciiOfLength(32), randomIntBetween(0, Integer.MAX_VALUE));
        ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, null, randomBoolean(), ShardRoutingState.UNASSIGNED);
        builder.put(shardRouting, randomAsciiOfLength(32));
    }
    return builder.build();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting)

Example 100 with ShardRouting

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

the class StartedShardsRoutingTests method testStartedShardsMatching.

public void testStartedShardsMatching() {
    AllocationService allocation = createAllocationService();
    logger.info("--> building initial cluster state");
    AllocationId allocationId = AllocationId.newRelocation(AllocationId.newInitializing());
    final IndexMetaData indexMetaData = IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(2).numberOfReplicas(0).putInSyncAllocationIds(1, Collections.singleton(allocationId.getId())).build();
    final Index index = indexMetaData.getIndex();
    ClusterState.Builder stateBuilder = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).metaData(MetaData.builder().put(indexMetaData, false));
    final ShardRouting initShard = TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", true, ShardRoutingState.INITIALIZING);
    final ShardRouting relocatingShard = TestShardRouting.newShardRouting(new ShardId(index, 1), "node1", "node2", true, ShardRoutingState.RELOCATING, allocationId);
    stateBuilder.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(initShard.shardId()).addShard(initShard).build()).addIndexShard(new IndexShardRoutingTable.Builder(relocatingShard.shardId()).addShard(relocatingShard).build())).build());
    ClusterState state = stateBuilder.build();
    logger.info("--> test starting of shard");
    ClusterState newState = allocation.applyStartedShards(state, Arrays.asList(initShard));
    assertThat("failed to start " + initShard + "\ncurrent routing table:" + newState.routingTable(), newState, not(equalTo(state)));
    assertTrue(initShard + "isn't started \ncurrent routing table:" + newState.routingTable(), newState.routingTable().index("test").shard(initShard.id()).allShardsStarted());
    state = newState;
    logger.info("--> testing starting of relocating shards");
    newState = allocation.applyStartedShards(state, Arrays.asList(relocatingShard.getTargetRelocatingShard()));
    assertThat("failed to start " + relocatingShard + "\ncurrent routing table:" + newState.routingTable(), newState, not(equalTo(state)));
    ShardRouting shardRouting = newState.routingTable().index("test").shard(relocatingShard.id()).getShards().get(0);
    assertThat(shardRouting.state(), equalTo(ShardRoutingState.STARTED));
    assertThat(shardRouting.currentNodeId(), equalTo("node2"));
    assertThat(shardRouting.relocatingNodeId(), nullValue());
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) AllocationId(org.elasticsearch.cluster.routing.AllocationId) Index(org.elasticsearch.index.Index) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)252 ClusterState (org.elasticsearch.cluster.ClusterState)119 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)71 ShardId (org.elasticsearch.index.shard.ShardId)60 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)59 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)56 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)52 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)51 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)46 MetaData (org.elasticsearch.cluster.metadata.MetaData)45 Index (org.elasticsearch.index.Index)36 Settings (org.elasticsearch.common.settings.Settings)32 ArrayList (java.util.ArrayList)29 HashSet (java.util.HashSet)29 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)29 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)28 IOException (java.io.IOException)27 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)26 HashMap (java.util.HashMap)25 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)25