Search in sources :

Example 16 with UnassignedInfo

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

the class MetadataIndexStateServiceTests method assertIsClosed.

private static void assertIsClosed(final String indexName, final ClusterState clusterState) {
    final IndexMetadata indexMetadata = clusterState.metadata().indices().get(indexName);
    assertThat(indexMetadata.getState(), is(IndexMetadata.State.CLOSE));
    final Settings indexSettings = indexMetadata.getSettings();
    assertThat(indexSettings.hasValue(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey()), is(true));
    assertThat(indexSettings.getAsBoolean(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey(), false), is(true));
    assertThat(clusterState.blocks().hasIndexBlock(indexName, MetadataIndexStateService.INDEX_CLOSED_BLOCK), is(true));
    assertThat("Index " + indexName + " must have only 1 block with [id=" + MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID + "]", clusterState.blocks().indices().getOrDefault(indexName, emptySet()).stream().filter(clusterBlock -> clusterBlock.id() == MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID).count(), equalTo(1L));
    final IndexRoutingTable indexRoutingTable = clusterState.routingTable().index(indexName);
    assertThat(indexRoutingTable, notNullValue());
    for (IndexShardRoutingTable shardRoutingTable : indexRoutingTable) {
        assertThat(shardRoutingTable.shards().stream().allMatch(ShardRouting::unassigned), is(true));
        assertThat(shardRoutingTable.shards().stream().map(ShardRouting::unassignedInfo).map(UnassignedInfo::getReason).allMatch(info -> info == UnassignedInfo.Reason.INDEX_CLOSED), is(true));
    }
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) INDEX_CLOSED_BLOCK_ID(org.opensearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK_ID) INDEX_CLOSED_BLOCK(org.opensearch.cluster.metadata.MetadataIndexStateService.INDEX_CLOSED_BLOCK) SETTING_VERSION_CREATED(org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED) Version(org.opensearch.Version) Strings(org.opensearch.common.Strings) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexId(org.opensearch.repositories.IndexId) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) Locale(java.util.Locale) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) SnapshotInfoTests(org.opensearch.snapshots.SnapshotInfoTests) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) SETTING_NUMBER_OF_REPLICAS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) SnapshotId(org.opensearch.snapshots.SnapshotId) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Collection(java.util.Collection) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) Tuple(org.opensearch.common.collect.Tuple) List(java.util.List) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) CloseIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.close.CloseIndexClusterStateUpdateRequest) ClusterState(org.opensearch.cluster.ClusterState) LegacyESVersion(org.opensearch.LegacyESVersion) VersionUtils(org.opensearch.test.VersionUtils) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections.singletonMap(java.util.Collections.singletonMap) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Collections.emptyMap(java.util.Collections.emptyMap) DeleteDataStreamRequestTests(org.opensearch.action.admin.indices.datastream.DeleteDataStreamRequestTests) Collections.emptySet(java.util.Collections.emptySet) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Mockito.when(org.mockito.Mockito.when) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) Snapshot(org.opensearch.snapshots.Snapshot) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) SETTING_NUMBER_OF_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS) Collections(java.util.Collections) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Settings(org.opensearch.common.settings.Settings)

Example 17 with UnassignedInfo

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

the class RestoreInProgressAllocationDeciderTests method testCanAllocatePrimaryExistingInRestoreInProgress.

public void testCanAllocatePrimaryExistingInRestoreInProgress() {
    RecoverySource.SnapshotRecoverySource recoverySource = createSnapshotRecoverySource("_existing");
    ClusterState clusterState = createInitialClusterState();
    RoutingTable routingTable = RoutingTable.builder(clusterState.getRoutingTable()).addAsRestore(clusterState.getMetadata().index("test"), recoverySource).build();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    ShardRouting primary = clusterState.getRoutingTable().shardRoutingTable("test", 0).primaryShard();
    assertEquals(ShardRoutingState.UNASSIGNED, primary.state());
    assertEquals(RecoverySource.Type.SNAPSHOT, primary.recoverySource().getType());
    routingTable = clusterState.routingTable();
    final RestoreInProgress.State shardState;
    if (randomBoolean()) {
        shardState = randomFrom(RestoreInProgress.State.STARTED, RestoreInProgress.State.INIT);
    } else {
        shardState = RestoreInProgress.State.FAILURE;
        UnassignedInfo currentInfo = primary.unassignedInfo();
        UnassignedInfo newInfo = new UnassignedInfo(currentInfo.getReason(), currentInfo.getMessage(), new IOException("i/o failure"), currentInfo.getNumFailedAllocations(), currentInfo.getUnassignedTimeInNanos(), currentInfo.getUnassignedTimeInMillis(), currentInfo.isDelayed(), currentInfo.getLastAllocationStatus(), currentInfo.getFailedNodeIds());
        primary = primary.updateUnassigned(newInfo, primary.recoverySource());
        IndexRoutingTable indexRoutingTable = routingTable.index("test");
        IndexRoutingTable.Builder newIndexRoutingTable = IndexRoutingTable.builder(indexRoutingTable.getIndex());
        for (final ObjectCursor<IndexShardRoutingTable> shardEntry : indexRoutingTable.getShards().values()) {
            final IndexShardRoutingTable shardRoutingTable = shardEntry.value;
            for (ShardRouting shardRouting : shardRoutingTable.getShards()) {
                if (shardRouting.primary()) {
                    newIndexRoutingTable.addShard(primary);
                } else {
                    newIndexRoutingTable.addShard(shardRouting);
                }
            }
        }
        routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
    }
    ImmutableOpenMap.Builder<ShardId, RestoreInProgress.ShardRestoreStatus> shards = ImmutableOpenMap.builder();
    shards.put(primary.shardId(), new RestoreInProgress.ShardRestoreStatus(clusterState.getNodes().getLocalNodeId(), shardState));
    Snapshot snapshot = recoverySource.snapshot();
    RestoreInProgress.State restoreState = RestoreInProgress.State.STARTED;
    RestoreInProgress.Entry restore = new RestoreInProgress.Entry(recoverySource.restoreUUID(), snapshot, restoreState, singletonList("test"), shards.build());
    clusterState = ClusterState.builder(clusterState).putCustom(RestoreInProgress.TYPE, new RestoreInProgress.Builder().add(restore).build()).routingTable(routingTable).build();
    Decision decision = executeAllocation(clusterState, primary);
    if (shardState == RestoreInProgress.State.FAILURE) {
        assertEquals(Decision.Type.NO, decision.type());
        assertEquals("shard has failed to be restored from the snapshot [_repository:_existing/_uuid] because of " + "[restore_source[_repository/_existing], failure IOException[i/o failure]] - manually close or delete the index " + "[test] in order to retry to restore the snapshot again or use the reroute API to force the allocation of " + "an empty primary shard", decision.getExplanation());
    } else {
        assertEquals(Decision.Type.YES, decision.type());
        assertEquals("shard is currently being restored", decision.getExplanation());
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) IOException(java.io.IOException) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) RecoverySource(org.opensearch.cluster.routing.RecoverySource) ShardId(org.opensearch.index.shard.ShardId) Snapshot(org.opensearch.snapshots.Snapshot) RestoreInProgress(org.opensearch.cluster.RestoreInProgress) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 18 with UnassignedInfo

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

the class PriorityComparatorTests method testPreferNewIndices.

public void testPreferNewIndices() {
    RoutingNodes.UnassignedShards shards = new RoutingNodes.UnassignedShards(mock(RoutingNodes.class));
    List<ShardRouting> shardRoutings = Arrays.asList(TestShardRouting.newShardRouting("oldest", 0, null, null, randomBoolean(), ShardRoutingState.UNASSIGNED, new UnassignedInfo(randomFrom(UnassignedInfo.Reason.values()), "foobar")), TestShardRouting.newShardRouting("newest", 0, null, null, randomBoolean(), ShardRoutingState.UNASSIGNED, new UnassignedInfo(randomFrom(UnassignedInfo.Reason.values()), "foobar")));
    Collections.shuffle(shardRoutings, random());
    for (ShardRouting routing : shardRoutings) {
        shards.add(routing);
    }
    shards.sort(new PriorityComparator() {

        @Override
        protected IndexMetadata getMetadata(Index index) {
            Settings settings;
            if ("oldest".equals(index.getName())) {
                settings = buildSettings(10, 1);
            } else if ("newest".equals(index.getName())) {
                settings = buildSettings(100, 1);
            } else {
                settings = Settings.EMPTY;
            }
            return IndexMetadata.builder(index.getName()).settings(settings).build();
        }
    });
    RoutingNodes.UnassignedShards.UnassignedIterator iterator = shards.iterator();
    ShardRouting next = iterator.next();
    assertEquals("newest", next.getIndexName());
    next = iterator.next();
    assertEquals("oldest", next.getIndexName());
    assertFalse(iterator.hasNext());
}
Also used : RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) Index(org.opensearch.index.Index) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Settings(org.opensearch.common.settings.Settings)

Example 19 with UnassignedInfo

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

the class IndexModuleTests method createInitializedShardRouting.

private ShardRouting createInitializedShardRouting() {
    ShardRouting shard = ShardRouting.newUnassigned(new ShardId("test", "_na_", 0), true, RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, null));
    shard = shard.initialize("node1", null, -1);
    return shard;
}
Also used : ShardId(org.opensearch.index.shard.ShardId) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 20 with UnassignedInfo

use of org.opensearch.cluster.routing.UnassignedInfo 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)

Aggregations

UnassignedInfo (org.opensearch.cluster.routing.UnassignedInfo)57 ShardRouting (org.opensearch.cluster.routing.ShardRouting)36 ShardId (org.opensearch.index.shard.ShardId)32 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)27 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)25 Index (org.opensearch.index.Index)17 ShardRoutingState (org.opensearch.cluster.routing.ShardRoutingState)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ClusterState (org.opensearch.cluster.ClusterState)15 ClusterInfo (org.opensearch.cluster.ClusterInfo)14 Metadata (org.opensearch.cluster.metadata.Metadata)11 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)11 AllocateUnassignedDecision (org.opensearch.cluster.routing.allocation.AllocateUnassignedDecision)11 Settings (org.opensearch.common.settings.Settings)11 RoutingNode (org.opensearch.cluster.routing.RoutingNode)10 RoutingTable (org.opensearch.cluster.routing.RoutingTable)10 MoveDecision (org.opensearch.cluster.routing.allocation.MoveDecision)10 NodeAllocationResult (org.opensearch.cluster.routing.allocation.NodeAllocationResult)10 RoutingAllocation (org.opensearch.cluster.routing.allocation.RoutingAllocation)10 XContentParser (org.opensearch.common.xcontent.XContentParser)10