use of org.opensearch.cluster.routing.IndexRoutingTable in project OpenSearch by opensearch-project.
the class ClusterStateTests method testToXContent.
public void testToXContent() throws IOException {
final ClusterState clusterState = buildClusterState();
IndexRoutingTable index = clusterState.getRoutingTable().getIndicesRouting().get("index");
String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId();
String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, new ToXContent.MapParams(singletonMap(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API)));
builder.endObject();
assertEquals("{\n" + " \"cluster_uuid\" : \"clusterUUID\",\n" + " \"version\" : 0,\n" + " \"state_uuid\" : \"stateUUID\",\n" + " \"master_node\" : \"masterNodeId\",\n" + " \"blocks\" : {\n" + " \"global\" : {\n" + " \"1\" : {\n" + " \"description\" : \"description\",\n" + " \"retryable\" : true,\n" + " \"disable_state_persistence\" : true,\n" + " \"levels\" : [\n" + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" + " \"metadata_write\"\n" + " ]\n" + " }\n" + " },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"2\" : {\n" + " \"description\" : \"description2\",\n" + " \"retryable\" : false,\n" + " \"levels\" : [\n" + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" + " \"metadata_write\"\n" + " ]\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"nodes\" : {\n" + " \"nodeId1\" : {\n" + " \"name\" : \"\",\n" + " \"ephemeral_id\" : \"" + ephemeralId + "\",\n" + " \"transport_address\" : \"127.0.0.1:111\",\n" + " \"attributes\" : { }\n" + " }\n" + " },\n" + " \"metadata\" : {\n" + " \"cluster_uuid\" : \"clusterUUID\",\n" + " \"cluster_uuid_committed\" : false,\n" + " \"cluster_coordination\" : {\n" + " \"term\" : 1,\n" + " \"last_committed_config\" : [\n" + " \"commitedConfigurationNodeId\"\n" + " ],\n" + " \"last_accepted_config\" : [\n" + " \"acceptedConfigurationNodeId\"\n" + " ],\n" + " \"voting_config_exclusions\" : [\n" + " {\n" + " \"node_id\" : \"exlucdedNodeId\",\n" + " \"node_name\" : \"excludedNodeName\"\n" + " }\n" + " ]\n" + " },\n" + " \"templates\" : {\n" + " \"template\" : {\n" + " \"order\" : 0,\n" + " \"index_patterns\" : [\n" + " \"pattern1\",\n" + " \"pattern2\"\n" + " ],\n" + " \"settings\" : {\n" + " \"index\" : {\n" + " \"version\" : {\n" + " \"created\" : \"" + Version.CURRENT.id + "\"\n" + " }\n" + " }\n" + " },\n" + " \"mappings\" : {\n" + " \"type\" : {\n" + " \"key1\" : { }\n" + " }\n" + " },\n" + " \"aliases\" : { }\n" + " }\n" + " },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"version\" : 1,\n" + " \"mapping_version\" : 1,\n" + " \"settings_version\" : 1,\n" + " \"aliases_version\" : 1,\n" + " \"routing_num_shards\" : 1,\n" + " \"state\" : \"open\",\n" + " \"settings\" : {\n" + " \"index\" : {\n" + " \"number_of_shards\" : \"1\",\n" + " \"number_of_replicas\" : \"2\",\n" + " \"version\" : {\n" + " \"created\" : \"" + Version.CURRENT.id + "\"\n" + " }\n" + " }\n" + " },\n" + " \"mappings\" : {\n" + " \"type\" : {\n" + " \"type1\" : {\n" + " \"key\" : \"value\"\n" + " }\n" + " }\n" + " },\n" + " \"aliases\" : [\n" + " \"alias\"\n" + " ],\n" + " \"primary_terms\" : {\n" + " \"0\" : 1\n" + " },\n" + " \"in_sync_allocations\" : {\n" + " \"0\" : [\n" + " \"allocationId\"\n" + " ]\n" + " },\n" + " \"rollover_info\" : {\n" + " \"rolloveAlias\" : {\n" + " \"met_conditions\" : { },\n" + " \"time\" : 1\n" + " }\n" + " },\n" + " \"system\" : false\n" + " }\n" + " },\n" + " \"index-graveyard\" : {\n" + " \"tombstones\" : [ ]\n" + " }\n" + " },\n" + " \"routing_table\" : {\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"shards\" : {\n" + " \"1\" : [\n" + " {\n" + " \"state\" : \"STARTED\",\n" + " \"primary\" : true,\n" + " \"node\" : \"nodeId2\",\n" + " \"relocating_node\" : null,\n" + " \"shard\" : 1,\n" + " \"index\" : \"index\",\n" + " \"allocation_id\" : {\n" + " \"id\" : \"" + allocationId + "\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"routing_nodes\" : {\n" + " \"unassigned\" : [ ],\n" + " \"nodes\" : {\n" + " \"nodeId2\" : [\n" + " {\n" + " \"state\" : \"STARTED\",\n" + " \"primary\" : true,\n" + " \"node\" : \"nodeId2\",\n" + " \"relocating_node\" : null,\n" + " \"shard\" : 1,\n" + " \"index\" : \"index\",\n" + " \"allocation_id\" : {\n" + " \"id\" : \"" + allocationId + "\"\n" + " }\n" + " }\n" + " ],\n" + " \"nodeId1\" : [ ]\n" + " }\n" + " }\n" + "}", Strings.toString(builder));
}
use of org.opensearch.cluster.routing.IndexRoutingTable in project OpenSearch by opensearch-project.
the class ClusterStateTests method testToXContent_FlatSettingTrue_ReduceMappingFalse.
public void testToXContent_FlatSettingTrue_ReduceMappingFalse() throws IOException {
Map<String, String> mapParams = new HashMap<String, String>() {
{
put("flat_settings", "true");
put("reduce_mappings", "false");
put(Metadata.CONTEXT_MODE_PARAM, Metadata.CONTEXT_MODE_API);
}
};
final ClusterState clusterState = buildClusterState();
IndexRoutingTable index = clusterState.getRoutingTable().getIndicesRouting().get("index");
String ephemeralId = clusterState.getNodes().get("nodeId1").getEphemeralId();
String allocationId = index.getShards().get(1).getAllAllocationIds().iterator().next();
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
builder.startObject();
clusterState.toXContent(builder, new ToXContent.MapParams(mapParams));
builder.endObject();
assertEquals("{\n" + " \"cluster_uuid\" : \"clusterUUID\",\n" + " \"version\" : 0,\n" + " \"state_uuid\" : \"stateUUID\",\n" + " \"master_node\" : \"masterNodeId\",\n" + " \"blocks\" : {\n" + " \"global\" : {\n" + " \"1\" : {\n" + " \"description\" : \"description\",\n" + " \"retryable\" : true,\n" + " \"disable_state_persistence\" : true,\n" + " \"levels\" : [\n" + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" + " \"metadata_write\"\n" + " ]\n" + " }\n" + " },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"2\" : {\n" + " \"description\" : \"description2\",\n" + " \"retryable\" : false,\n" + " \"levels\" : [\n" + " \"read\",\n" + " \"write\",\n" + " \"metadata_read\",\n" + " \"metadata_write\"\n" + " ]\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"nodes\" : {\n" + " \"nodeId1\" : {\n" + " \"name\" : \"\",\n" + " \"ephemeral_id\" : \"" + ephemeralId + "\",\n" + " \"transport_address\" : \"127.0.0.1:111\",\n" + " \"attributes\" : { }\n" + " }\n" + " },\n" + " \"metadata\" : {\n" + " \"cluster_uuid\" : \"clusterUUID\",\n" + " \"cluster_uuid_committed\" : false,\n" + " \"cluster_coordination\" : {\n" + " \"term\" : 1,\n" + " \"last_committed_config\" : [\n" + " \"commitedConfigurationNodeId\"\n" + " ],\n" + " \"last_accepted_config\" : [\n" + " \"acceptedConfigurationNodeId\"\n" + " ],\n" + " \"voting_config_exclusions\" : [\n" + " {\n" + " \"node_id\" : \"exlucdedNodeId\",\n" + " \"node_name\" : \"excludedNodeName\"\n" + " }\n" + " ]\n" + " },\n" + " \"templates\" : {\n" + " \"template\" : {\n" + " \"order\" : 0,\n" + " \"index_patterns\" : [\n" + " \"pattern1\",\n" + " \"pattern2\"\n" + " ],\n" + " \"settings\" : {\n" + " \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" + " },\n" + " \"mappings\" : {\n" + " \"type\" : {\n" + " \"key1\" : { }\n" + " }\n" + " },\n" + " \"aliases\" : { }\n" + " }\n" + " },\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"version\" : 1,\n" + " \"mapping_version\" : 1,\n" + " \"settings_version\" : 1,\n" + " \"aliases_version\" : 1,\n" + " \"routing_num_shards\" : 1,\n" + " \"state\" : \"open\",\n" + " \"settings\" : {\n" + " \"index.number_of_replicas\" : \"2\",\n" + " \"index.number_of_shards\" : \"1\",\n" + " \"index.version.created\" : \"" + Version.CURRENT.id + "\"\n" + " },\n" + " \"mappings\" : {\n" + " \"type\" : {\n" + " \"type1\" : {\n" + " \"key\" : \"value\"\n" + " }\n" + " }\n" + " },\n" + " \"aliases\" : [\n" + " \"alias\"\n" + " ],\n" + " \"primary_terms\" : {\n" + " \"0\" : 1\n" + " },\n" + " \"in_sync_allocations\" : {\n" + " \"0\" : [\n" + " \"allocationId\"\n" + " ]\n" + " },\n" + " \"rollover_info\" : {\n" + " \"rolloveAlias\" : {\n" + " \"met_conditions\" : { },\n" + " \"time\" : 1\n" + " }\n" + " },\n" + " \"system\" : false\n" + " }\n" + " },\n" + " \"index-graveyard\" : {\n" + " \"tombstones\" : [ ]\n" + " }\n" + " },\n" + " \"routing_table\" : {\n" + " \"indices\" : {\n" + " \"index\" : {\n" + " \"shards\" : {\n" + " \"1\" : [\n" + " {\n" + " \"state\" : \"STARTED\",\n" + " \"primary\" : true,\n" + " \"node\" : \"nodeId2\",\n" + " \"relocating_node\" : null,\n" + " \"shard\" : 1,\n" + " \"index\" : \"index\",\n" + " \"allocation_id\" : {\n" + " \"id\" : \"" + allocationId + "\"\n" + " }\n" + " }\n" + " ]\n" + " }\n" + " }\n" + " }\n" + " },\n" + " \"routing_nodes\" : {\n" + " \"unassigned\" : [ ],\n" + " \"nodes\" : {\n" + " \"nodeId2\" : [\n" + " {\n" + " \"state\" : \"STARTED\",\n" + " \"primary\" : true,\n" + " \"node\" : \"nodeId2\",\n" + " \"relocating_node\" : null,\n" + " \"shard\" : 1,\n" + " \"index\" : \"index\",\n" + " \"allocation_id\" : {\n" + " \"id\" : \"" + allocationId + "\"\n" + " }\n" + " }\n" + " ],\n" + " \"nodeId1\" : [ ]\n" + " }\n" + " }\n" + "}", Strings.toString(builder));
}
use of org.opensearch.cluster.routing.IndexRoutingTable in project OpenSearch by opensearch-project.
the class ActiveShardCountTests method startLessThanWaitOnShards.
private ClusterState startLessThanWaitOnShards(final ClusterState clusterState, final String indexName, final int numShardsToStart) {
RoutingTable routingTable = clusterState.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;
assert shardRoutingTable.getSize() > 2;
int numToStart = numShardsToStart;
// want less than half, and primary is already started
for (ShardRouting shardRouting : shardRoutingTable.getShards()) {
if (shardRouting.primary()) {
assertTrue(shardRouting.active());
} else {
if (numToStart > 0) {
shardRouting = shardRouting.initialize(randomAlphaOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted();
numToStart--;
}
}
newIndexRoutingTable.addShard(shardRouting);
}
}
routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
return ClusterState.builder(clusterState).routingTable(routingTable).build();
}
use of org.opensearch.cluster.routing.IndexRoutingTable in project OpenSearch by opensearch-project.
the class ActiveShardCountTests method startAllShards.
private ClusterState startAllShards(final ClusterState clusterState, final String indexName) {
RoutingTable routingTable = clusterState.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 (ShardRouting shardRouting : shardRoutingTable.getShards()) {
if (shardRouting.primary()) {
assertTrue(shardRouting.active());
} else {
if (shardRouting.active() == false) {
shardRouting = shardRouting.initialize(randomAlphaOfLength(8), null, shardRouting.getExpectedShardSize()).moveToStarted();
}
}
newIndexRoutingTable.addShard(shardRouting);
}
}
routingTable = RoutingTable.builder(routingTable).add(newIndexRoutingTable).build();
return ClusterState.builder(clusterState).routingTable(routingTable).build();
}
use of org.opensearch.cluster.routing.IndexRoutingTable 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));
}
}
Aggregations