Search in sources :

Example 1 with ImmutableOpenIntMap

use of org.elasticsearch.common.collect.ImmutableOpenIntMap in project elasticsearch by elastic.

the class ClusterStateHealthTests method generateClusterStates.

private List<ClusterState> generateClusterStates(final ClusterState originalClusterState, final String indexName, final int numberOfReplicas, final boolean withPrimaryAllocationFailures) {
    // generate random node ids
    final Set<String> nodeIds = new HashSet<>();
    final int numNodes = randomIntBetween(numberOfReplicas + 1, 10);
    for (int i = 0; i < numNodes; i++) {
        nodeIds.add(randomAsciiOfLength(8));
    }
    final List<ClusterState> clusterStates = new ArrayList<>();
    clusterStates.add(originalClusterState);
    ClusterState clusterState = originalClusterState;
    // initialize primaries
    RoutingTable routingTable = originalClusterState.routingTable();
    IndexRoutingTable indexRoutingTable = routingTable.index(indexName);
    IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary()) {
                newIndexRoutingTable.addShard(shardRouting.initialize(randomFrom(nodeIds), null, shardRouting.getExpectedShardSize()));
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    clusterStates.add(clusterState);
    // some primaries started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    ImmutableOpenIntMap.Builder<Set<String>> allocationIds = ImmutableOpenIntMap.<Set<String>>builder();
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() && randomBoolean()) {
                final ShardRouting newShardRouting = shardRouting.moveToStarted();
                allocationIds.fPut(newShardRouting.getId(), Sets.newHashSet(newShardRouting.allocationId().getId()));
                newIndexRoutingTable.addShard(newShardRouting);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    IndexMetaData.Builder idxMetaBuilder = IndexMetaData.builder(clusterState.metaData().index(indexName));
    for (final IntObjectCursor<Set<String>> entry : allocationIds.build()) {
        idxMetaBuilder.putInSyncAllocationIds(entry.key, entry.value);
    }
    MetaData.Builder metaDataBuilder = MetaData.builder(clusterState.metaData()).put(idxMetaBuilder);
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaDataBuilder).build();
    clusterStates.add(clusterState);
    if (withPrimaryAllocationFailures) {
        boolean alreadyFailedPrimary = false;
        // some primaries failed to allocate
        indexRoutingTable = routingTable.index(indexName);
        newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
        for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
            final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
            for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
                if (shardRouting.primary() && (shardRouting.started() == false || alreadyFailedPrimary == false)) {
                    newIndexRoutingTable.addShard(shardRouting.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, "unlucky shard")));
                    alreadyFailedPrimary = true;
                } else {
                    newIndexRoutingTable.addShard(shardRouting);
                }
            }
        }
        routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
        clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
        return clusterStates;
    }
    // all primaries started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    allocationIds = ImmutableOpenIntMap.<Set<String>>builder();
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() && shardRouting.started() == false) {
                final ShardRouting newShardRouting = shardRouting.moveToStarted();
                allocationIds.fPut(newShardRouting.getId(), Sets.newHashSet(newShardRouting.allocationId().getId()));
                newIndexRoutingTable.addShard(newShardRouting);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    idxMetaBuilder = IndexMetaData.builder(clusterState.metaData().index(indexName));
    for (final IntObjectCursor<Set<String>> entry : allocationIds.build()) {
        idxMetaBuilder.putInSyncAllocationIds(entry.key, entry.value);
    }
    metaDataBuilder = MetaData.builder(clusterState.metaData()).put(idxMetaBuilder);
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).metaData(metaDataBuilder).build();
    clusterStates.add(clusterState);
    // initialize replicas
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        final String primaryNodeId = shardRoutingTable.primaryShard().currentNodeId();
        Set<String> allocatedNodes = new HashSet<>();
        allocatedNodes.add(primaryNodeId);
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false) {
                // give the replica a different node id than the primary
                String replicaNodeId = randomFrom(Sets.difference(nodeIds, allocatedNodes));
                newIndexRoutingTable.addShard(shardRouting.initialize(replicaNodeId, null, shardRouting.getExpectedShardSize()));
                allocatedNodes.add(replicaNodeId);
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    // some replicas started
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false && randomBoolean()) {
                newIndexRoutingTable.addShard(shardRouting.moveToStarted());
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    // all replicas started
    boolean replicaStateChanged = false;
    indexRoutingTable = routingTable.index(indexName);
    newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
    for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
        final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
        for (final ShardRouting shardRouting : shardRoutingTable.getShards()) {
            if (shardRouting.primary() == false && shardRouting.started() == false) {
                newIndexRoutingTable.addShard(shardRouting.moveToStarted());
                replicaStateChanged = true;
            } else {
                newIndexRoutingTable.addShard(shardRouting);
            }
        }
    }
    // all of the replicas may have moved to started in the previous phase already
    if (replicaStateChanged) {
        routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
        clusterStates.add(ClusterState.builder(clusterState).routingTable(routingTable).build());
    }
    return clusterStates;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) Set(java.util.Set) HashSet(java.util.HashSet) UnassignedInfo(org.elasticsearch.cluster.routing.UnassignedInfo) ArrayList(java.util.ArrayList) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) HashSet(java.util.HashSet)

Example 2 with ImmutableOpenIntMap

use of org.elasticsearch.common.collect.ImmutableOpenIntMap in project elasticsearch by elastic.

the class IndicesShardStoresResponse method writeTo.

@Override
public void writeTo(StreamOutput out) throws IOException {
    super.writeTo(out);
    out.writeVInt(storeStatuses.size());
    for (ObjectObjectCursor<String, ImmutableOpenIntMap<List<StoreStatus>>> indexShards : storeStatuses) {
        out.writeString(indexShards.key);
        out.writeVInt(indexShards.value.size());
        for (IntObjectCursor<List<StoreStatus>> shardStatusesEntry : indexShards.value) {
            out.writeInt(shardStatusesEntry.key);
            out.writeVInt(shardStatusesEntry.value.size());
            for (StoreStatus storeStatus : shardStatusesEntry.value) {
                storeStatus.writeTo(out);
            }
        }
    }
    out.writeVInt(failures.size());
    for (ShardOperationFailedException failure : failures) {
        failure.writeTo(out);
    }
}
Also used : StoreStatus.readStoreStatus(org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus.readStoreStatus) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) ArrayList(java.util.ArrayList) List(java.util.List) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException)

Example 3 with ImmutableOpenIntMap

use of org.elasticsearch.common.collect.ImmutableOpenIntMap in project elasticsearch by elastic.

the class IndicesShardStoresResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    if (failures.size() > 0) {
        builder.startArray(Fields.FAILURES);
        for (ShardOperationFailedException failure : failures) {
            builder.startObject();
            failure.toXContent(builder, params);
            builder.endObject();
        }
        builder.endArray();
    }
    builder.startObject(Fields.INDICES);
    for (ObjectObjectCursor<String, ImmutableOpenIntMap<List<StoreStatus>>> indexShards : storeStatuses) {
        builder.startObject(indexShards.key);
        builder.startObject(Fields.SHARDS);
        for (IntObjectCursor<List<StoreStatus>> shardStatusesEntry : indexShards.value) {
            builder.startObject(String.valueOf(shardStatusesEntry.key));
            builder.startArray(Fields.STORES);
            for (StoreStatus storeStatus : shardStatusesEntry.value) {
                builder.startObject();
                storeStatus.toXContent(builder, params);
                builder.endObject();
            }
            builder.endArray();
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
    }
    builder.endObject();
    return builder;
}
Also used : StoreStatus.readStoreStatus(org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus.readStoreStatus) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) ArrayList(java.util.ArrayList) List(java.util.List) ShardOperationFailedException(org.elasticsearch.action.ShardOperationFailedException) DefaultShardOperationFailedException(org.elasticsearch.action.support.DefaultShardOperationFailedException)

Example 4 with ImmutableOpenIntMap

use of org.elasticsearch.common.collect.ImmutableOpenIntMap in project elasticsearch by elastic.

the class IndicesShardStoreResponseTests method testBasicSerialization.

public void testBasicSerialization() throws Exception {
    ImmutableOpenMap.Builder<String, ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>>> indexStoreStatuses = ImmutableOpenMap.builder();
    List<IndicesShardStoresResponse.Failure> failures = new ArrayList<>();
    ImmutableOpenIntMap.Builder<List<IndicesShardStoresResponse.StoreStatus>> storeStatuses = ImmutableOpenIntMap.builder();
    DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
    List<IndicesShardStoresResponse.StoreStatus> storeStatusList = new ArrayList<>();
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node1, null, IndicesShardStoresResponse.StoreStatus.AllocationStatus.PRIMARY, null));
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node2, UUIDs.randomBase64UUID(), IndicesShardStoresResponse.StoreStatus.AllocationStatus.REPLICA, null));
    storeStatusList.add(new IndicesShardStoresResponse.StoreStatus(node1, UUIDs.randomBase64UUID(), IndicesShardStoresResponse.StoreStatus.AllocationStatus.UNUSED, new IOException("corrupted")));
    storeStatuses.put(0, storeStatusList);
    storeStatuses.put(1, storeStatusList);
    ImmutableOpenIntMap<List<IndicesShardStoresResponse.StoreStatus>> storesMap = storeStatuses.build();
    indexStoreStatuses.put("test", storesMap);
    indexStoreStatuses.put("test2", storesMap);
    failures.add(new IndicesShardStoresResponse.Failure("node1", "test", 3, new NodeDisconnectedException(node1, "")));
    IndicesShardStoresResponse storesResponse = new IndicesShardStoresResponse(indexStoreStatuses.build(), Collections.unmodifiableList(failures));
    XContentBuilder contentBuilder = XContentFactory.jsonBuilder();
    contentBuilder.startObject();
    storesResponse.toXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
    contentBuilder.endObject();
    BytesReference bytes = contentBuilder.bytes();
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, bytes)) {
        Map<String, Object> map = parser.map();
        List failureList = (List) map.get("failures");
        assertThat(failureList.size(), equalTo(1));
        HashMap failureMap = (HashMap) failureList.get(0);
        assertThat(failureMap.containsKey("index"), equalTo(true));
        assertThat(((String) failureMap.get("index")), equalTo("test"));
        assertThat(failureMap.containsKey("shard"), equalTo(true));
        assertThat(((int) failureMap.get("shard")), equalTo(3));
        assertThat(failureMap.containsKey("node"), equalTo(true));
        assertThat(((String) failureMap.get("node")), equalTo("node1"));
        Map<String, Object> indices = (Map<String, Object>) map.get("indices");
        for (String index : new String[] { "test", "test2" }) {
            assertThat(indices.containsKey(index), equalTo(true));
            Map<String, Object> shards = ((Map<String, Object>) ((Map<String, Object>) indices.get(index)).get("shards"));
            assertThat(shards.size(), equalTo(2));
            for (String shardId : shards.keySet()) {
                HashMap shardStoresStatus = (HashMap) shards.get(shardId);
                assertThat(shardStoresStatus.containsKey("stores"), equalTo(true));
                List stores = (ArrayList) shardStoresStatus.get("stores");
                assertThat(stores.size(), equalTo(storeStatusList.size()));
                for (int i = 0; i < stores.size(); i++) {
                    HashMap storeInfo = ((HashMap) stores.get(i));
                    IndicesShardStoresResponse.StoreStatus storeStatus = storeStatusList.get(i);
                    assertThat(((String) storeInfo.get("allocation_id")), equalTo((storeStatus.getAllocationId())));
                    assertThat(storeInfo.containsKey("allocation"), equalTo(true));
                    assertThat(((String) storeInfo.get("allocation")), equalTo(storeStatus.getAllocationStatus().value()));
                    assertThat(storeInfo.containsKey(storeStatus.getNode().getId()), equalTo(true));
                    if (storeStatus.getStoreException() != null) {
                        assertThat(storeInfo.containsKey("store_exception"), equalTo(true));
                    }
                }
            }
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) ArrayList(java.util.ArrayList) List(java.util.List) BytesReference(org.elasticsearch.common.bytes.BytesReference) NodeDisconnectedException(org.elasticsearch.transport.NodeDisconnectedException) IOException(java.io.IOException) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) ImmutableOpenIntMap(org.elasticsearch.common.collect.ImmutableOpenIntMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

ArrayList (java.util.ArrayList)4 ImmutableOpenIntMap (org.elasticsearch.common.collect.ImmutableOpenIntMap)4 List (java.util.List)3 ShardOperationFailedException (org.elasticsearch.action.ShardOperationFailedException)2 StoreStatus.readStoreStatus (org.elasticsearch.action.admin.indices.shards.IndicesShardStoresResponse.StoreStatus.readStoreStatus)2 DefaultShardOperationFailedException (org.elasticsearch.action.support.DefaultShardOperationFailedException)2 IOException (java.io.IOException)1 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ClusterState (org.elasticsearch.cluster.ClusterState)1 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)1 MetaData (org.elasticsearch.cluster.metadata.MetaData)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)1 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)1 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)1 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)1