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));
}
}
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());
}
}
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());
}
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;
}
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);
}
Aggregations