Search in sources :

Example 21 with AllocationId

use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.

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 = startShardsAndReroute(allocation, state, 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 = startShardsAndReroute(allocation, state, 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.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) AllocationId(org.opensearch.cluster.routing.AllocationId) Index(org.opensearch.index.Index) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 22 with AllocationId

use of org.opensearch.cluster.routing.AllocationId in project fesen-httpclient by codelibs.

the class HttpSyncedFlushAction method parseShardRouting.

protected ShardRouting parseShardRouting(final XContentParser parser) {
    final ConstructingObjectParser<ShardRouting, Void> objectParser = new ConstructingObjectParser<>("routing", true, a -> {
        try (final ByteArrayStreamOutput out = new ByteArrayStreamOutput()) {
            int i = 0;
            final ShardRoutingState state = ShardRoutingState.valueOf((String) a[i++]);
            final boolean primary = (boolean) a[i++];
            final String currentNodeId = (String) a[i++];
            final String relocatingNodeId = (String) a[i++];
            final int shardIdValue = (int) a[i++];
            final String index = (String) a[i++];
            final long expectedShardSize = (long) a[i++];
            // cannot know from the info returned at REST
            final String uuid = "";
            final ShardId shardId = new ShardId(new Index(index, uuid), shardIdValue);
            final UnassignedInfo unassignedInfo = (UnassignedInfo) a[i++];
            final AllocationId allocationId = (AllocationId) a[i++];
            final RecoverySource recoverySource = (RecoverySource) a[i++];
            out.writeOptionalString(currentNodeId);
            out.writeOptionalString(relocatingNodeId);
            out.writeBoolean(primary);
            out.writeByte(state.value());
            if (state == ShardRoutingState.UNASSIGNED || state == ShardRoutingState.INITIALIZING) {
                recoverySource.writeTo(out);
            }
            out.writeOptionalWriteable(unassignedInfo);
            out.writeOptionalWriteable(allocationId);
            if (state == ShardRoutingState.RELOCATING || state == ShardRoutingState.INITIALIZING) {
                out.writeLong(expectedShardSize);
            }
            return new ShardRouting(shardId, out.toStreamInput());
        } catch (final IOException e) {
            throw new UncheckedIOException(e);
        }
    });
    objectParser.declareString(constructorArg(), STATE_FIELD);
    objectParser.declareBoolean(constructorArg(), PRIMARY_FIELD);
    objectParser.declareString(constructorArg(), NODE_FIELD);
    objectParser.declareString(constructorArg(), RELOCATING_NODE_FIELD);
    objectParser.declareInt(constructorArg(), SHARD_FIELD);
    objectParser.declareString(constructorArg(), INDEX_FIELD);
    objectParser.declareLong(constructorArg(), EXPECTED_SHARD_SIZE_IN_BYTES_FIELD);
    objectParser.declareObject(optionalConstructorArg(), (p, c) -> {
        try {
            return getUnassignedInfo(p);
        } catch (final Exception e) {
            throw new OpenSearchException("Failed to create SyncedFlushResponse.", e);
        }
    }, UNASSIGNED_INFO_FIELD);
    objectParser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> AllocationId.fromXContent(p), ALLOCATION_ID_FIELD);
    objectParser.declareObject(ConstructingObjectParser.optionalConstructorArg(), (p, c) -> getRecoverySource(p), RECOVERY_SOURCE_FIELD);
    return objectParser.apply(parser, null);
}
Also used : ByteArrayStreamOutput(org.codelibs.fesen.client.io.stream.ByteArrayStreamOutput) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) AllocationId(org.opensearch.cluster.routing.AllocationId) Index(org.opensearch.index.Index) UncheckedIOException(java.io.UncheckedIOException) ConstructingObjectParser(org.opensearch.common.xcontent.ConstructingObjectParser) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) OpenSearchException(org.opensearch.OpenSearchException) ParseException(java.text.ParseException) ParsingException(org.opensearch.common.ParsingException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) RecoverySource(org.opensearch.cluster.routing.RecoverySource) ShardId(org.opensearch.index.shard.ShardId) OpenSearchException(org.opensearch.OpenSearchException) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 23 with AllocationId

use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.

the class StartedShardsRoutingTests method testRelocatingPrimariesWithInitializingReplicas.

public void testRelocatingPrimariesWithInitializingReplicas() {
    AllocationService allocation = createAllocationService();
    logger.info("--> building initial cluster state");
    AllocationId primaryId = AllocationId.newRelocation(AllocationId.newInitializing());
    AllocationId replicaId = AllocationId.newInitializing();
    boolean relocatingReplica = randomBoolean();
    if (relocatingReplica) {
        replicaId = AllocationId.newRelocation(replicaId);
    }
    final IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(1).putInSyncAllocationIds(0, relocatingReplica ? Sets.newHashSet(primaryId.getId(), replicaId.getId()) : Sets.newHashSet(primaryId.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")).add(newNode("node3")).add(newNode("node4"))).metadata(Metadata.builder().put(indexMetadata, false));
    final ShardRouting relocatingPrimary = TestShardRouting.newShardRouting(new ShardId(index, 0), "node1", "node2", true, ShardRoutingState.RELOCATING, primaryId);
    final ShardRouting replica = TestShardRouting.newShardRouting(new ShardId(index, 0), "node3", relocatingReplica ? "node4" : null, false, relocatingReplica ? ShardRoutingState.RELOCATING : ShardRoutingState.INITIALIZING, replicaId);
    stateBuilder.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(new IndexShardRoutingTable.Builder(relocatingPrimary.shardId()).addShard(relocatingPrimary).addShard(replica).build())).build());
    ClusterState state = stateBuilder.build();
    logger.info("--> test starting of relocating primary shard with initializing / relocating replica");
    ClusterState newState = startShardsAndReroute(allocation, state, relocatingPrimary.getTargetRelocatingShard());
    assertNotEquals(newState, state);
    assertTrue(newState.routingTable().index("test").allPrimaryShardsActive());
    ShardRouting startedReplica = newState.routingTable().index("test").shard(0).replicaShards().get(0);
    if (relocatingReplica) {
        assertTrue(startedReplica.relocating());
        assertEquals(replica.currentNodeId(), startedReplica.currentNodeId());
        assertEquals(replica.relocatingNodeId(), startedReplica.relocatingNodeId());
        assertEquals(replica.allocationId().getId(), startedReplica.allocationId().getId());
        assertNotEquals(replica.allocationId().getRelocationId(), startedReplica.allocationId().getRelocationId());
    } else {
        assertTrue(startedReplica.initializing());
        assertEquals(replica.currentNodeId(), startedReplica.currentNodeId());
        assertNotEquals(replica.allocationId().getId(), startedReplica.allocationId().getId());
    }
    logger.info("--> test starting of relocating primary shard together with initializing / relocating replica");
    List<ShardRouting> startedShards = new ArrayList<>();
    startedShards.add(relocatingPrimary.getTargetRelocatingShard());
    startedShards.add(relocatingReplica ? replica.getTargetRelocatingShard() : replica);
    Collections.shuffle(startedShards, random());
    newState = startShardsAndReroute(allocation, state, startedShards);
    assertNotEquals(newState, state);
    assertTrue(newState.routingTable().index("test").shard(0).allShardsStarted());
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) AllocationId(org.opensearch.cluster.routing.AllocationId) ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 24 with AllocationId

use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.

the class RemoveCorruptedShardDataCommand method newAllocationId.

private void newAllocationId(ShardPath shardPath, Terminal terminal) throws IOException {
    final Path shardStatePath = shardPath.getShardStatePath();
    final ShardStateMetadata shardStateMetadata = ShardStateMetadata.FORMAT.loadLatestState(logger, namedXContentRegistry, shardStatePath);
    if (shardStateMetadata == null) {
        throw new OpenSearchException("No shard state meta data at " + shardStatePath);
    }
    final AllocationId newAllocationId = AllocationId.newInitializing();
    terminal.println("Changing allocation id " + shardStateMetadata.allocationId.getId() + " to " + newAllocationId.getId());
    final ShardStateMetadata newShardStateMetadata = new ShardStateMetadata(shardStateMetadata.primary, shardStateMetadata.indexUUID, newAllocationId);
    ShardStateMetadata.FORMAT.writeAndCleanup(newShardStateMetadata, shardStatePath);
    terminal.println("");
    terminal.println("You should run the following command to allocate this shard:");
    printRerouteCommand(shardPath, terminal, true);
}
Also used : Path(java.nio.file.Path) AllocationId(org.opensearch.cluster.routing.AllocationId) OpenSearchException(org.opensearch.OpenSearchException)

Example 25 with AllocationId

use of org.opensearch.cluster.routing.AllocationId in project OpenSearch by opensearch-project.

the class ClusterStateCreationUtils method state.

/**
 * Creates cluster state with and index that has one shard and #(replicaStates) replicas
 *
 * @param index              name of the index
 * @param activePrimaryLocal if active primary should coincide with the local node in the cluster state
 * @param primaryState       state of primary
 * @param replicaStates      states of the replicas. length of this array determines also the number of replicas
 */
public static ClusterState state(String index, boolean activePrimaryLocal, ShardRoutingState primaryState, ShardRoutingState... replicaStates) {
    final int numberOfReplicas = replicaStates.length;
    int numberOfNodes = numberOfReplicas + 1;
    if (primaryState == ShardRoutingState.RELOCATING) {
        numberOfNodes++;
    }
    for (ShardRoutingState state : replicaStates) {
        if (state == ShardRoutingState.RELOCATING) {
            numberOfNodes++;
        }
    }
    // we need a non-local master to test shard failures
    numberOfNodes = Math.max(2, numberOfNodes);
    final ShardId shardId = new ShardId(index, "_na_", 0);
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    Set<String> unassignedNodes = new HashSet<>();
    for (int i = 0; i < numberOfNodes + 1; i++) {
        final DiscoveryNode node = newNode(i);
        discoBuilder = discoBuilder.add(node);
        unassignedNodes.add(node.getId());
    }
    discoBuilder.localNodeId(newNode(0).getId());
    // we need a non-local master to test shard failures
    discoBuilder.masterNodeId(newNode(1).getId());
    final int primaryTerm = 1 + randomInt(200);
    IndexMetadata indexMetadata = IndexMetadata.builder(index).settings(Settings.builder().put(SETTING_VERSION_CREATED, Version.CURRENT).put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, numberOfReplicas).put(SETTING_CREATION_DATE, System.currentTimeMillis())).primaryTerm(0, primaryTerm).build();
    IndexShardRoutingTable.Builder indexShardRoutingBuilder = new IndexShardRoutingTable.Builder(shardId);
    String primaryNode = null;
    String relocatingNode = null;
    UnassignedInfo unassignedInfo = null;
    if (primaryState != ShardRoutingState.UNASSIGNED) {
        if (activePrimaryLocal) {
            primaryNode = newNode(0).getId();
            unassignedNodes.remove(primaryNode);
        } else {
            Set<String> unassignedNodesExecludingPrimary = new HashSet<>(unassignedNodes);
            unassignedNodesExecludingPrimary.remove(newNode(0).getId());
            primaryNode = selectAndRemove(unassignedNodesExecludingPrimary);
            unassignedNodes.remove(primaryNode);
        }
        if (primaryState == ShardRoutingState.RELOCATING) {
            relocatingNode = selectAndRemove(unassignedNodes);
        } else if (primaryState == ShardRoutingState.INITIALIZING) {
            unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null);
        }
    } else {
        unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null);
    }
    indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(index, 0, primaryNode, relocatingNode, true, primaryState, unassignedInfo));
    for (ShardRoutingState replicaState : replicaStates) {
        String replicaNode = null;
        relocatingNode = null;
        unassignedInfo = null;
        if (replicaState != ShardRoutingState.UNASSIGNED) {
            assert primaryNode != null : "a replica is assigned but the primary isn't";
            replicaNode = selectAndRemove(unassignedNodes);
            if (replicaState == ShardRoutingState.RELOCATING) {
                relocatingNode = selectAndRemove(unassignedNodes);
            }
        } else {
            unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null);
        }
        indexShardRoutingBuilder.addShard(TestShardRouting.newShardRouting(index, shardId.id(), replicaNode, relocatingNode, false, replicaState, unassignedInfo));
    }
    final IndexShardRoutingTable indexShardRoutingTable = indexShardRoutingBuilder.build();
    IndexMetadata.Builder indexMetadataBuilder = new IndexMetadata.Builder(indexMetadata);
    indexMetadataBuilder.putInSyncAllocationIds(0, indexShardRoutingTable.activeShards().stream().map(ShardRouting::allocationId).map(AllocationId::getId).collect(Collectors.toSet()));
    ClusterState.Builder state = ClusterState.builder(new ClusterName("test"));
    state.nodes(discoBuilder);
    state.metadata(Metadata.builder().put(indexMetadataBuilder.build(), false).generateClusterUuidIfNeeded());
    state.routingTable(RoutingTable.builder().add(IndexRoutingTable.builder(indexMetadata.getIndex()).addIndexShard(indexShardRoutingTable)).build());
    return state.build();
}
Also used : IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Builder(org.opensearch.cluster.routing.RoutingTable.Builder) AllocationId(org.opensearch.cluster.routing.AllocationId) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) ShardId(org.opensearch.index.shard.ShardId) ClusterName(org.opensearch.cluster.ClusterName) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Aggregations

AllocationId (org.opensearch.cluster.routing.AllocationId)47 ShardId (org.opensearch.index.shard.ShardId)29 AtomicLong (java.util.concurrent.atomic.AtomicLong)21 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)17 HashSet (java.util.HashSet)16 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)16 CyclicBarrier (java.util.concurrent.CyclicBarrier)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 ShardRouting (org.opensearch.cluster.routing.ShardRouting)16 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)14 ArrayList (java.util.ArrayList)13 Set (java.util.Set)13 Matchers.containsString (org.hamcrest.Matchers.containsString)13 Matchers.hasToString (org.hamcrest.Matchers.hasToString)13 ShardRoutingState (org.opensearch.cluster.routing.ShardRoutingState)13 Settings (org.opensearch.common.settings.Settings)13 IndexSettings (org.opensearch.index.IndexSettings)13 IOException (java.io.IOException)12 Collections (java.util.Collections)12 Collections.emptySet (java.util.Collections.emptySet)12