Search in sources :

Example 31 with IndexRoutingTable

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

the class ShardStateActionTests method getRandomShardRouting.

private ShardRouting getRandomShardRouting(String index) {
    IndexRoutingTable indexRoutingTable = clusterService.state().routingTable().index(index);
    ShardsIterator shardsIterator = indexRoutingTable.randomAllActiveShardsIt();
    ShardRouting shardRouting = shardsIterator.nextOrNull();
    assert shardRouting != null;
    return shardRouting;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) ShardsIterator(org.elasticsearch.cluster.routing.ShardsIterator)

Example 32 with IndexRoutingTable

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

the class ClusterStateDiffIT method randomChangeToIndexRoutingTable.

/**
     * Randomly updates index routing table in the cluster state
     */
private IndexRoutingTable randomChangeToIndexRoutingTable(IndexRoutingTable original, String[] nodes) {
    IndexRoutingTable.Builder builder = IndexRoutingTable.builder(original.getIndex());
    for (ObjectCursor<IndexShardRoutingTable> indexShardRoutingTable : original.shards().values()) {
        Set<String> availableNodes = Sets.newHashSet(nodes);
        for (ShardRouting shardRouting : indexShardRoutingTable.value.shards()) {
            availableNodes.remove(shardRouting.currentNodeId());
            if (shardRouting.relocating()) {
                availableNodes.remove(shardRouting.relocatingNodeId());
            }
        }
        for (ShardRouting shardRouting : indexShardRoutingTable.value.shards()) {
            final ShardRouting updatedShardRouting = randomChange(shardRouting, availableNodes);
            availableNodes.remove(updatedShardRouting.currentNodeId());
            if (shardRouting.relocating()) {
                availableNodes.remove(updatedShardRouting.relocatingNodeId());
            }
            builder.addShard(updatedShardRouting);
        }
    }
    return builder.build();
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) TestShardRouting(org.elasticsearch.cluster.routing.TestShardRouting) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 33 with IndexRoutingTable

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

the class ClusterStateDiffIT method randomIndexRoutingTable.

/**
     * Randomly updates index routing table in the cluster state
     */
private IndexRoutingTable randomIndexRoutingTable(String index, String[] nodeIds) {
    IndexRoutingTable.Builder builder = IndexRoutingTable.builder(new Index(index, "_na_"));
    int shardCount = randomInt(10);
    for (int i = 0; i < shardCount; i++) {
        IndexShardRoutingTable.Builder indexShard = new IndexShardRoutingTable.Builder(new ShardId(index, "_na_", i));
        int replicaCount = randomIntBetween(1, 10);
        Set<String> availableNodeIds = Sets.newHashSet(nodeIds);
        for (int j = 0; j < replicaCount; j++) {
            UnassignedInfo unassignedInfo = null;
            if (randomInt(5) == 1) {
                unassignedInfo = new UnassignedInfo(randomReason(), randomAsciiOfLength(10));
            }
            if (availableNodeIds.isEmpty()) {
                break;
            }
            String nodeId = randomFrom(availableNodeIds);
            availableNodeIds.remove(nodeId);
            indexShard.addShard(TestShardRouting.newShardRouting(index, i, nodeId, null, j == 0, ShardRoutingState.fromValue((byte) randomIntBetween(2, 3)), unassignedInfo));
        }
        builder.addIndexShard(indexShard.build());
    }
    return builder.build();
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) AliasMetaData.newAliasMetaDataBuilder(org.elasticsearch.cluster.metadata.AliasMetaData.newAliasMetaDataBuilder) Index(org.elasticsearch.index.Index)

Example 34 with IndexRoutingTable

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

the class AckClusterUpdateSettingsIT method testClusterUpdateSettingsAcknowledgement.

public void testClusterUpdateSettingsAcknowledgement() {
    createIndex("test");
    ensureGreen();
    // now that the cluster is stable, remove timeout
    removePublishTimeout();
    NodesInfoResponse nodesInfo = client().admin().cluster().prepareNodesInfo().get();
    String excludedNodeId = null;
    for (NodeInfo nodeInfo : nodesInfo.getNodes()) {
        if (nodeInfo.getNode().isDataNode()) {
            excludedNodeId = nodeInfo.getNode().getId();
            break;
        }
    }
    assertNotNull(excludedNodeId);
    ClusterUpdateSettingsResponse clusterUpdateSettingsResponse = client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put("cluster.routing.allocation.exclude._id", excludedNodeId)).get();
    assertAcked(clusterUpdateSettingsResponse);
    assertThat(clusterUpdateSettingsResponse.getTransientSettings().get("cluster.routing.allocation.exclude._id"), equalTo(excludedNodeId));
    for (Client client : clients()) {
        ClusterState clusterState = getLocalClusterState(client);
        assertThat(clusterState.metaData().transientSettings().get("cluster.routing.allocation.exclude._id"), equalTo(excludedNodeId));
        for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
            for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
                for (ShardRouting shardRouting : indexShardRoutingTable) {
                    assert clusterState.nodes() != null;
                    if (shardRouting.unassigned() == false && clusterState.nodes().get(shardRouting.currentNodeId()).getId().equals(excludedNodeId)) {
                        //if the shard is still there it must be relocating and all nodes need to know, since the request was acknowledged
                        //reroute happens as part of the update settings and we made sure no throttling comes into the picture via settings
                        assertThat(shardRouting.relocating(), equalTo(true));
                    }
                }
            }
        }
    }
}
Also used : NodesInfoResponse(org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse) ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) NodeInfo(org.elasticsearch.action.admin.cluster.node.info.NodeInfo) ClusterUpdateSettingsResponse(org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse) Client(org.elasticsearch.client.Client) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 35 with IndexRoutingTable

use of org.elasticsearch.cluster.routing.IndexRoutingTable 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)

Aggregations

IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)35 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)29 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)21 ClusterState (org.elasticsearch.cluster.ClusterState)18 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)15 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)14 ShardId (org.elasticsearch.index.shard.ShardId)11 HashSet (java.util.HashSet)7 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)6 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)6 MetaData (org.elasticsearch.cluster.metadata.MetaData)5 Settings (org.elasticsearch.common.settings.Settings)5 ObjectIntHashMap (com.carrotsearch.hppc.ObjectIntHashMap)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 ClusterName (org.elasticsearch.cluster.ClusterName)3 TestShardRouting (org.elasticsearch.cluster.routing.TestShardRouting)3 UnassignedInfo (org.elasticsearch.cluster.routing.UnassignedInfo)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2