Search in sources :

Example 51 with IndexShardRoutingTable

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

the class TransportReplicationActionTests method testNoRerouteOnStaleClusterState.

/**
     * When relocating a primary shard, there is a cluster state update at the end of relocation where the active primary is switched from
     * the relocation source to the relocation target. If relocation source receives and processes this cluster state
     * before the relocation target, there is a time span where relocation source believes active primary to be on
     * relocation target and relocation target believes active primary to be on relocation source. This results in replication
     * requests being sent back and forth.
     * <p>
     * This test checks that replication request is not routed back from relocation target to relocation source in case of
     * stale index routing table on relocation target.
     */
public void testNoRerouteOnStaleClusterState() throws InterruptedException, ExecutionException {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = state(index, true, ShardRoutingState.RELOCATING);
    String relocationTargetNode = state.getRoutingTable().shardRoutingTable(shardId).primaryShard().relocatingNodeId();
    state = ClusterState.builder(state).nodes(DiscoveryNodes.builder(state.nodes()).localNodeId(relocationTargetNode)).build();
    setState(clusterService, state);
    logger.debug("--> relocation ongoing state:\n{}", clusterService.state());
    Request request = new Request(shardId).timeout("1ms").routedBasedOnClusterVersion(clusterService.state().version() + 1);
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(null, request, listener);
    reroutePhase.run();
    assertListenerThrows("cluster state too old didn't cause a timeout", listener, UnavailableShardsException.class);
    assertTrue(request.isRetrySet.compareAndSet(true, false));
    request = new Request(shardId).routedBasedOnClusterVersion(clusterService.state().version() + 1);
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(null, request, listener);
    reroutePhase.run();
    assertFalse("cluster state too old didn't cause a retry", listener.isDone());
    assertTrue(request.isRetrySet.get());
    // finish relocation
    ShardRouting relocationTarget = clusterService.state().getRoutingTable().shardRoutingTable(shardId).shardsWithState(ShardRoutingState.INITIALIZING).get(0);
    AllocationService allocationService = ESAllocationTestCase.createAllocationService();
    ClusterState updatedState = allocationService.applyStartedShards(state, Collections.singletonList(relocationTarget));
    setState(clusterService, updatedState);
    logger.debug("--> relocation complete state:\n{}", clusterService.state());
    IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().index(index).shard(shardId.id());
    final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
    final List<CapturingTransport.CapturedRequest> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear().get(primaryNodeId);
    assertThat(capturedRequests, notNullValue());
    assertThat(capturedRequests.size(), equalTo(1));
    assertThat(capturedRequests.get(0).action, equalTo("testAction[p]"));
    assertIndexShardCounter(0);
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 52 with IndexShardRoutingTable

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

the class TransportReplicationActionTests method testNotStartedPrimary.

public void testNotStartedPrimary() throws InterruptedException, ExecutionException {
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    // no replicas in oder to skip the replication part
    setState(clusterService, state(index, true, randomBoolean() ? ShardRoutingState.INITIALIZING : ShardRoutingState.UNASSIGNED));
    ReplicationTask task = maybeTask();
    logger.debug("--> using initial state:\n{}", clusterService.state());
    Request request = new Request(shardId).timeout("1ms");
    PlainActionFuture<TestResponse> listener = new PlainActionFuture<>();
    TestAction.ReroutePhase reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    assertListenerThrows("unassigned primary didn't cause a timeout", listener, UnavailableShardsException.class);
    assertPhase(task, "failed");
    assertTrue(request.isRetrySet.get());
    request = new Request(shardId);
    listener = new PlainActionFuture<>();
    reroutePhase = action.new ReroutePhase(task, request, listener);
    reroutePhase.run();
    assertFalse("unassigned primary didn't cause a retry", listener.isDone());
    assertPhase(task, "waiting_for_retry");
    assertTrue(request.isRetrySet.get());
    setState(clusterService, state(index, true, ShardRoutingState.STARTED));
    logger.debug("--> primary assigned state:\n{}", clusterService.state());
    final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().index(index).shard(shardId.id());
    final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
    final List<CapturingTransport.CapturedRequest> capturedRequests = transport.getCapturedRequestsByTargetNodeAndClear().get(primaryNodeId);
    assertThat(capturedRequests, notNullValue());
    assertThat(capturedRequests.size(), equalTo(1));
    assertThat(capturedRequests.get(0).action, equalTo("testAction[p]"));
    assertIndexShardCounter(0);
}
Also used : IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TransportRequest(org.elasticsearch.transport.TransportRequest) CloseIndexRequest(org.elasticsearch.action.admin.indices.close.CloseIndexRequest) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) ShardId(org.elasticsearch.index.shard.ShardId) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture)

Example 53 with IndexShardRoutingTable

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

the class ClusterState method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    EnumSet<Metric> metrics = Metric.parseString(params.param("metric", "_all"), true);
    if (metrics.contains(Metric.VERSION)) {
        builder.field("version", version);
        builder.field("state_uuid", stateUUID);
    }
    if (metrics.contains(Metric.MASTER_NODE)) {
        builder.field("master_node", nodes().getMasterNodeId());
    }
    if (metrics.contains(Metric.BLOCKS)) {
        builder.startObject("blocks");
        if (!blocks().global().isEmpty()) {
            builder.startObject("global");
            for (ClusterBlock block : blocks().global()) {
                block.toXContent(builder, params);
            }
            builder.endObject();
        }
        if (!blocks().indices().isEmpty()) {
            builder.startObject("indices");
            for (ObjectObjectCursor<String, Set<ClusterBlock>> entry : blocks().indices()) {
                builder.startObject(entry.key);
                for (ClusterBlock block : entry.value) {
                    block.toXContent(builder, params);
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
    }
    // nodes
    if (metrics.contains(Metric.NODES)) {
        builder.startObject("nodes");
        for (DiscoveryNode node : nodes) {
            node.toXContent(builder, params);
        }
        builder.endObject();
    }
    // meta data
    if (metrics.contains(Metric.METADATA)) {
        builder.startObject("metadata");
        builder.field("cluster_uuid", metaData().clusterUUID());
        builder.startObject("templates");
        for (ObjectCursor<IndexTemplateMetaData> cursor : metaData().templates().values()) {
            IndexTemplateMetaData templateMetaData = cursor.value;
            builder.startObject(templateMetaData.name());
            builder.field("index_patterns", templateMetaData.patterns());
            builder.field("order", templateMetaData.order());
            builder.startObject("settings");
            Settings settings = templateMetaData.settings();
            settings.toXContent(builder, params);
            builder.endObject();
            builder.startObject("mappings");
            for (ObjectObjectCursor<String, CompressedXContent> cursor1 : templateMetaData.mappings()) {
                Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor1.value.uncompressed()), false).v2();
                if (mapping.size() == 1 && mapping.containsKey(cursor1.key)) {
                    // the type name is the root value, reduce it
                    mapping = (Map<String, Object>) mapping.get(cursor1.key);
                }
                builder.field(cursor1.key);
                builder.map(mapping);
            }
            builder.endObject();
            builder.endObject();
        }
        builder.endObject();
        builder.startObject("indices");
        for (IndexMetaData indexMetaData : metaData()) {
            builder.startObject(indexMetaData.getIndex().getName());
            builder.field("state", indexMetaData.getState().toString().toLowerCase(Locale.ENGLISH));
            builder.startObject("settings");
            Settings settings = indexMetaData.getSettings();
            settings.toXContent(builder, params);
            builder.endObject();
            builder.startObject("mappings");
            for (ObjectObjectCursor<String, MappingMetaData> cursor : indexMetaData.getMappings()) {
                Map<String, Object> mapping = XContentHelper.convertToMap(new BytesArray(cursor.value.source().uncompressed()), false).v2();
                if (mapping.size() == 1 && mapping.containsKey(cursor.key)) {
                    // the type name is the root value, reduce it
                    mapping = (Map<String, Object>) mapping.get(cursor.key);
                }
                builder.field(cursor.key);
                builder.map(mapping);
            }
            builder.endObject();
            builder.startArray("aliases");
            for (ObjectCursor<String> cursor : indexMetaData.getAliases().keys()) {
                builder.value(cursor.value);
            }
            builder.endArray();
            builder.startObject(IndexMetaData.KEY_PRIMARY_TERMS);
            for (int shard = 0; shard < indexMetaData.getNumberOfShards(); shard++) {
                builder.field(Integer.toString(shard), indexMetaData.primaryTerm(shard));
            }
            builder.endObject();
            builder.startObject(IndexMetaData.KEY_IN_SYNC_ALLOCATIONS);
            for (IntObjectCursor<Set<String>> cursor : indexMetaData.getInSyncAllocationIds()) {
                builder.startArray(String.valueOf(cursor.key));
                for (String allocationId : cursor.value) {
                    builder.value(allocationId);
                }
                builder.endArray();
            }
            builder.endObject();
            // index metadata
            builder.endObject();
        }
        builder.endObject();
        for (ObjectObjectCursor<String, MetaData.Custom> cursor : metaData.customs()) {
            builder.startObject(cursor.key);
            cursor.value.toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
    }
    // routing table
    if (metrics.contains(Metric.ROUTING_TABLE)) {
        builder.startObject("routing_table");
        builder.startObject("indices");
        for (IndexRoutingTable indexRoutingTable : routingTable()) {
            builder.startObject(indexRoutingTable.getIndex().getName());
            builder.startObject("shards");
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id()));
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    shardRouting.toXContent(builder, params);
                }
                builder.endArray();
            }
            builder.endObject();
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
    }
    // routing nodes
    if (metrics.contains(Metric.ROUTING_NODES)) {
        builder.startObject("routing_nodes");
        builder.startArray("unassigned");
        for (ShardRouting shardRouting : getRoutingNodes().unassigned()) {
            shardRouting.toXContent(builder, params);
        }
        builder.endArray();
        builder.startObject("nodes");
        for (RoutingNode routingNode : getRoutingNodes()) {
            builder.startArray(routingNode.nodeId() == null ? "null" : routingNode.nodeId());
            for (ShardRouting shardRouting : routingNode) {
                shardRouting.toXContent(builder, params);
            }
            builder.endArray();
        }
        builder.endObject();
        builder.endObject();
    }
    if (metrics.contains(Metric.CUSTOMS)) {
        for (ObjectObjectCursor<String, Custom> cursor : customs) {
            builder.startObject(cursor.key);
            cursor.value.toXContent(builder, params);
            builder.endObject();
        }
    }
    return builder;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) EnumSet(java.util.EnumSet) Set(java.util.Set) ClusterBlock(org.elasticsearch.cluster.block.ClusterBlock) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Settings(org.elasticsearch.common.settings.Settings) BytesArray(org.elasticsearch.common.bytes.BytesArray) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 54 with IndexShardRoutingTable

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

the class DiskThresholdDecider method getExpectedShardSize.

/**
     * Returns the expected shard size for the given shard or the default value provided if not enough information are available
     * to estimate the shards size.
     */
public static long getExpectedShardSize(ShardRouting shard, RoutingAllocation allocation, long defaultValue) {
    final IndexMetaData metaData = allocation.metaData().getIndexSafe(shard.index());
    final ClusterInfo info = allocation.clusterInfo();
    if (metaData.getMergeSourceIndex() != null && shard.active() == false && shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) {
        // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in
        // the worst case
        long targetShardSize = 0;
        final Index mergeSourceIndex = metaData.getMergeSourceIndex();
        final IndexMetaData sourceIndexMeta = allocation.metaData().getIndexSafe(mergeSourceIndex);
        final Set<ShardId> shardIds = IndexMetaData.selectShrinkShards(shard.id(), sourceIndexMeta, metaData.getNumberOfShards());
        for (IndexShardRoutingTable shardRoutingTable : allocation.routingTable().index(mergeSourceIndex.getName())) {
            if (shardIds.contains(shardRoutingTable.shardId())) {
                targetShardSize += info.getShardSize(shardRoutingTable.primaryShard(), 0);
            }
        }
        return targetShardSize == 0 ? defaultValue : targetShardSize;
    } else {
        return info.getShardSize(shard, defaultValue);
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ClusterInfo(org.elasticsearch.cluster.ClusterInfo) Index(org.elasticsearch.index.Index) 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