Skip to content

Commit

Permalink
Revert "Fix RoutingTable Lookup by Index (elastic#75530)"
Browse files Browse the repository at this point in the history
This reverts commit 1169828.
  • Loading branch information
original-brownbear committed Jul 21, 2021
1 parent 175d007 commit 5e44147
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,16 @@ public boolean hasIndex(String index) {
}

public boolean hasIndex(Index index) {
IndexRoutingTable indexRouting = index(index);
return indexRouting != null;
IndexRoutingTable indexRouting = index(index.getName());
return indexRouting != null && indexRouting.getIndex().equals(index);
}

public IndexRoutingTable index(String index) {
return indicesRouting.get(index);
}

public IndexRoutingTable index(Index index) {
IndexRoutingTable indexRouting = index(index.getName());
return indexRouting != null && indexRouting.getIndex().equals(index) ? indexRouting : null;
return indicesRouting.get(index.getName());
}

public ImmutableOpenMap<String, IndexRoutingTable> indicesRouting() {
Expand Down Expand Up @@ -135,8 +134,8 @@ public IndexShardRoutingTable shardRoutingTable(String index, int shardId) {
* @throws ShardNotFoundException if provided shard id is unknown
*/
public IndexShardRoutingTable shardRoutingTable(ShardId shardId) {
IndexRoutingTable indexRouting = index(shardId.getIndex());
if (indexRouting == null) {
IndexRoutingTable indexRouting = index(shardId.getIndexName());
if (indexRouting == null || indexRouting.getIndex().equals(shardId.getIndex()) == false) {
throw new IndexNotFoundException(shardId.getIndex());
}
IndexShardRoutingTable shard = indexRouting.shard(shardId.id());
Expand All @@ -148,7 +147,7 @@ public IndexShardRoutingTable shardRoutingTable(ShardId shardId) {

@Nullable
public ShardRouting getByAllocationId(ShardId shardId, String allocationId) {
final IndexRoutingTable indexRoutingTable = index(shardId.getIndex());
final IndexRoutingTable indexRoutingTable = index(shardId.getIndexName());
if (indexRoutingTable == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.elasticsearch.cluster.node.DiscoveryNodes.Builder;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.IndexNotFoundException;
Expand Down Expand Up @@ -128,7 +127,6 @@ public void testHasIndex() {

public void testIndex() {
assertThat(clusterState.routingTable().index(TEST_INDEX_1).getIndex().getName(), is(TEST_INDEX_1));
assertThat(clusterState.routingTable().index(new Index(TEST_INDEX_1, UUIDs.randomBase64UUID())), is(nullValue()));
assertThat(clusterState.routingTable().index("foobar"), is(nullValue()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -169,7 +170,10 @@ public void testManagerSkipsIndicesWithRedStatus() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(manager.getUpgradeStatus(markShardsUnavailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY));
final ClusterState.Builder clusterStateBuilder = createClusterState();
markShardsUnavailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UNHEALTHY));
}

/**
Expand All @@ -180,7 +184,10 @@ public void testManagerSkipsIndicesWithOutdatedFormat() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState(5)), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE));
final ClusterState.Builder clusterStateBuilder = createClusterState(5);
markShardsAvailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_UPGRADE));
}

/**
Expand All @@ -190,7 +197,10 @@ public void testManagerSkipsIndicesWithUpToDateMappings() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(manager.getUpgradeStatus(markShardsAvailable(createClusterState()), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE));
final ClusterState.Builder clusterStateBuilder = createClusterState();
markShardsAvailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.UP_TO_DATE));
}

/**
Expand All @@ -200,10 +210,10 @@ public void testManagerProcessesIndicesWithOutdatedMappings() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(
manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0")))), DESCRIPTOR),
equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)
);
final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0")));
markShardsAvailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE));
}

/**
Expand All @@ -213,10 +223,10 @@ public void testManagerProcessesIndicesWithNullVersionMetadata() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(
manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(null)))), DESCRIPTOR),
equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)
);
final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(null)));
markShardsAvailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE));
}

/**
Expand All @@ -226,7 +236,10 @@ public void testManagerSubmitsPutRequest() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

manager.clusterChanged(event(markShardsAvailable(createClusterState(Strings.toString(getMappings("1.0.0"))))));
final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings("1.0.0")));
markShardsAvailable(clusterStateBuilder);

manager.clusterChanged(event(clusterStateBuilder));

verify(client, times(1)).execute(any(PutMappingAction.class), any(PutMappingRequest.class), any());
}
Expand All @@ -238,10 +251,10 @@ public void testCanHandleIntegerMetaVersion() {
SystemIndices systemIndices = new SystemIndices(Map.of("MyIndex", FEATURE));
SystemIndexManager manager = new SystemIndexManager(systemIndices, client);

assertThat(
manager.getUpgradeStatus(markShardsAvailable(createClusterState(Strings.toString(getMappings(3)))), DESCRIPTOR),
equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE)
);
final ClusterState.Builder clusterStateBuilder = createClusterState(Strings.toString(getMappings(3)));
markShardsAvailable(clusterStateBuilder);

assertThat(manager.getUpgradeStatus(clusterStateBuilder.build(), DESCRIPTOR), equalTo(UpgradeStatus.NEEDS_MAPPINGS_UPDATE));
}

private static ClusterState.Builder createClusterState() {
Expand Down Expand Up @@ -273,16 +286,12 @@ private static ClusterState.Builder createClusterState(String mappings, int form
return ClusterState.builder(state()).metadata(metadataBuilder.build());
}

private ClusterState markShardsAvailable(ClusterState.Builder clusterStateBuilder) {
final ClusterState cs = clusterStateBuilder.build();
return ClusterState.builder(cs)
.routingTable(buildIndexRoutingTable(cs.metadata().index(DESCRIPTOR.getPrimaryIndex()).getIndex()))
.build();
private void markShardsAvailable(ClusterState.Builder clusterStateBuilder) {
clusterStateBuilder.routingTable(buildIndexRoutingTable(DESCRIPTOR.getPrimaryIndex()));
}

private ClusterState markShardsUnavailable(ClusterState.Builder clusterStateBuilder) {
final ClusterState cs = clusterStateBuilder.build();
final RoutingTable routingTable = buildIndexRoutingTable(cs.metadata().index(DESCRIPTOR.getPrimaryIndex()).getIndex());
private void markShardsUnavailable(ClusterState.Builder clusterStateBuilder) {
final RoutingTable routingTable = buildIndexRoutingTable(DESCRIPTOR.getPrimaryIndex());

Index prevIndex = routingTable.index(DESCRIPTOR.getPrimaryIndex()).getIndex();

Expand All @@ -304,7 +313,7 @@ private ClusterState markShardsUnavailable(ClusterState.Builder clusterStateBuil
)
.build();

return ClusterState.builder(cs).routingTable(unavailableRoutingTable).build();
clusterStateBuilder.routingTable(unavailableRoutingTable);
}

private static ClusterState state() {
Expand Down Expand Up @@ -342,7 +351,8 @@ private static IndexMetadata.Builder getIndexMetadata(
return indexMetadata;
}

private static RoutingTable buildIndexRoutingTable(Index index) {
private static RoutingTable buildIndexRoutingTable(String indexName) {
Index index = new Index(indexName, UUID.randomUUID().toString());
ShardRouting shardRouting = ShardRouting.newUnassigned(
new ShardId(index, 0),
true,
Expand All @@ -356,8 +366,8 @@ private static RoutingTable buildIndexRoutingTable(Index index) {
return RoutingTable.builder().add(IndexRoutingTable.builder(index).addIndexShard(table).build()).build();
}

private ClusterChangedEvent event(ClusterState clusterState) {
return new ClusterChangedEvent("test-event", clusterState, EMPTY_CLUSTER_STATE);
private ClusterChangedEvent event(ClusterState.Builder clusterStateBuilder) {
return new ClusterChangedEvent("test-event", clusterStateBuilder.build(), EMPTY_CLUSTER_STATE);
}

private static Settings getSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS;
Expand Down Expand Up @@ -267,7 +268,7 @@ private ClusterState getClusterStateWithSecurityIndex() {
metadata = SecurityTestUtils.addAliasToMetadata(metadata, securityIndexName);
}

Index index = metadata.index(securityIndexName).getIndex();
Index index = new Index(securityIndexName, UUID.randomUUID().toString());
ShardRouting shardRouting = ShardRouting.newUnassigned(new ShardId(index, 0), true,
RecoverySource.ExistingStoreRecoverySource.INSTANCE, new UnassignedInfo(Reason.INDEX_CREATED, ""));
IndexShardRoutingTable table = new IndexShardRoutingTable.Builder(new ShardId(index, 0))
Expand Down
Loading

0 comments on commit 5e44147

Please sign in to comment.