From 0bf1ed964e7b58ee92ff4d447cb044a70652c9b4 Mon Sep 17 00:00:00 2001 From: kimchy Date: Wed, 29 Jun 2011 14:51:32 +0300 Subject: [PATCH] fix wrong routing table versioning when deleting an index --- .../cluster/metadata/MetaDataDeleteIndexService.java | 12 ++++-------- .../metadata/MetaDataUpdateSettingsService.java | 4 +--- .../cluster/routing/IndexRoutingTable.java | 5 +++++ .../elasticsearch/cluster/routing/RoutingTable.java | 6 +----- .../elasticsearch/discovery/zen/ZenDiscovery.java | 3 ++- .../allocation/UpdateNumberOfReplicasTests.java | 5 ++--- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataDeleteIndexService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataDeleteIndexService.java index eac2f2096a6..dc1d7113ef7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataDeleteIndexService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataDeleteIndexService.java @@ -24,7 +24,6 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.action.index.NodeIndexDeletedAction; import org.elasticsearch.cluster.block.ClusterBlocks; -import org.elasticsearch.cluster.routing.IndexRoutingTable; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRouting; @@ -74,19 +73,16 @@ public class MetaDataDeleteIndexService extends AbstractComponent { @Override public ClusterState execute(ClusterState currentState) { final DeleteIndexListener listener = new DeleteIndexListener(request, userListener); try { - if (!currentState.metaData().hasIndex(request.index)) { + if (!currentState.metaData().hasConcreteIndex(request.index)) { listener.onFailure(new IndexMissingException(new Index(request.index))); return currentState; } logger.info("[{}] deleting index", request.index); - RoutingTable.Builder routingTableBuilder = new RoutingTable.Builder(); - for (IndexRoutingTable indexRoutingTable : currentState.routingTable().indicesRouting().values()) { - if (!indexRoutingTable.index().equals(request.index)) { - routingTableBuilder.add(indexRoutingTable); - } - } + RoutingTable.Builder routingTableBuilder = RoutingTable.builder().routingTable(currentState.routingTable()); + routingTableBuilder.remove(request.index); + MetaData newMetaData = newMetaDataBuilder() .metaData(currentState.metaData()) .remove(request.index) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java index 8beb8853967..3681fa87981 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/metadata/MetaDataUpdateSettingsService.java @@ -36,8 +36,6 @@ import org.elasticsearch.common.settings.Settings; import java.util.Map; import java.util.Set; -import static org.elasticsearch.cluster.routing.RoutingTable.*; - /** * @author kimchy (shay.banon) */ @@ -141,7 +139,7 @@ public class MetaDataUpdateSettingsService extends AbstractComponent implements @Override public ClusterState execute(ClusterState currentState) { try { String[] actualIndices = currentState.metaData().concreteIndices(indices); - RoutingTable.Builder routingTableBuilder = newRoutingTableBuilder().routingTable(currentState.routingTable()); + RoutingTable.Builder routingTableBuilder = RoutingTable.builder().routingTable(currentState.routingTable()); MetaData.Builder metaDataBuilder = MetaData.newMetaDataBuilder().metaData(currentState.metaData()); int updatedNumberOfReplicas = openSettings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, -1); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java index ddf2f56470d..1c83038ad41 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/IndexRoutingTable.java @@ -107,6 +107,11 @@ public class IndexRoutingTable implements Iterable { validation.addIndexFailure(index(), "Shard [" + indexShardRoutingTable.shardId().id() + "] routing table has wrong number of replicas, expected [" + indexMetaData.numberOfReplicas() + "], got [" + routingNumberOfReplicas + "]"); } + for (ShardRouting shardRouting : indexShardRoutingTable) { + if (!shardRouting.index().equals(index())) { + validation.addIndexFailure(index(), "shard routing has an index [" + shardRouting.index() + "] that is different than the routing table"); + } + } } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java index 873522a98b7..103b7fa4ea4 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/cluster/routing/RoutingTable.java @@ -46,7 +46,7 @@ import static org.elasticsearch.common.collect.Maps.*; @Immutable public class RoutingTable implements Iterable { - public static final RoutingTable EMPTY_ROUTING_TABLE = newRoutingTableBuilder().build(); + public static final RoutingTable EMPTY_ROUTING_TABLE = builder().build(); private final long version; @@ -204,10 +204,6 @@ public class RoutingTable implements Iterable { return new Builder(); } - public static Builder newRoutingTableBuilder() { - return new Builder(); - } - public static class Builder { private long version; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java index ce9d003b23c..18942609672 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java @@ -392,8 +392,9 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen clusterBlocks = ClusterBlocks.builder().blocks(clusterBlocks).addGlobalBlock(NO_MASTER_BLOCK).build(); // if this is a data node, clean the metadata and routing, since we want to recreate the indices and shards if (currentState.nodes().localNode().dataNode()) { + // TODO, what happens with versioning here? metaData = MetaData.newMetaDataBuilder().build(); - routingTable = RoutingTable.newRoutingTableBuilder().build(); + routingTable = RoutingTable.builder().build(); } masterFD.stop("no master elected since master left (reason = " + reason + ")"); asyncJoinCluster(); diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java index d26311ae06f..b3db39dd14c 100644 --- a/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/cluster/routing/allocation/UpdateNumberOfReplicasTests.java @@ -13,7 +13,6 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.*; import static org.elasticsearch.cluster.metadata.MetaData.*; import static org.elasticsearch.cluster.node.DiscoveryNodes.*; import static org.elasticsearch.cluster.routing.RoutingBuilders.*; -import static org.elasticsearch.cluster.routing.RoutingTable.*; import static org.elasticsearch.cluster.routing.ShardRoutingState.*; import static org.elasticsearch.cluster.routing.allocation.RoutingAllocationTests.*; import static org.elasticsearch.common.settings.ImmutableSettings.*; @@ -83,7 +82,7 @@ public class UpdateNumberOfReplicasTests { logger.info("add another replica"); routingNodes = clusterState.routingNodes(); prevRoutingTable = routingTable; - routingTable = newRoutingTableBuilder().routingTable(routingTable).updateNumberOfReplicas(2).build(); + routingTable = RoutingTable.builder().routingTable(routingTable).updateNumberOfReplicas(2).build(); metaData = MetaData.newMetaDataBuilder().metaData(clusterState.metaData()).updateNumberOfReplicas(2).build(); clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).metaData(metaData).build(); @@ -135,7 +134,7 @@ public class UpdateNumberOfReplicasTests { logger.info("now remove a replica"); routingNodes = clusterState.routingNodes(); prevRoutingTable = routingTable; - routingTable = newRoutingTableBuilder().routingTable(routingTable).updateNumberOfReplicas(1).build(); + routingTable = RoutingTable.builder().routingTable(routingTable).updateNumberOfReplicas(1).build(); metaData = MetaData.newMetaDataBuilder().metaData(clusterState.metaData()).updateNumberOfReplicas(1).build(); clusterState = newClusterStateBuilder().state(clusterState).routingTable(routingTable).metaData(metaData).build();