From bba7179eebdfe87bfd50e7149da6e27196cce59f Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 14 Apr 2011 21:15:55 +0300 Subject: [PATCH] automatically clean indices that no longer exists in the metadata, this can happen if we delete a closed index, or when a node joins a cluster, that no longer has those indices --- .../elasticsearch/env/NodeEnvironment.java | 6 ++++- .../indices/store/IndicesStore.java | 25 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/env/NodeEnvironment.java b/modules/elasticsearch/src/main/java/org/elasticsearch/env/NodeEnvironment.java index f65a7e2754c..6b7d4b66106 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/env/NodeEnvironment.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/env/NodeEnvironment.java @@ -105,8 +105,12 @@ public class NodeEnvironment extends AbstractComponent { return nodeFile; } + public File indicesLocation() { + return new File(nodeDataLocation(), "indices"); + } + public File indexLocation(Index index) { - return new File(new File(nodeDataLocation(), "indices"), index.name()); + return new File(indicesLocation(), index.name()); } public File shardLocation(ShardId shardId) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/store/IndicesStore.java b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/store/IndicesStore.java index 03440e7c27f..8d3ce201a03 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/indices/store/IndicesStore.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/indices/store/IndicesStore.java @@ -28,21 +28,28 @@ import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.ShardRoutingState; import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.env.NodeEnvironment; import org.elasticsearch.index.service.IndexService; import org.elasticsearch.indices.IndicesService; +import java.io.File; + /** * @author kimchy (shay.banon) */ public class IndicesStore extends AbstractComponent implements ClusterStateListener { + private final NodeEnvironment nodeEnv; + private final IndicesService indicesService; private final ClusterService clusterService; - @Inject public IndicesStore(Settings settings, IndicesService indicesService, ClusterService clusterService) { + @Inject public IndicesStore(Settings settings, NodeEnvironment nodeEnv, IndicesService indicesService, ClusterService clusterService) { super(settings); + this.nodeEnv = nodeEnv; this.indicesService = indicesService; this.clusterService = clusterService; clusterService.add(this); @@ -94,5 +101,21 @@ public class IndicesStore extends AbstractComponent implements ClusterStateListe } } } + + // do the reverse, and delete dangling indices that might remain on that node + // this can happen when deleting a closed index, or when a node joins and it has deleted indices + if (nodeEnv.hasNodeFile()) { + File[] files = nodeEnv.indicesLocation().listFiles(); + if (files != null) { + for (File file : files) { + // if we have the index on the metadata, don't delete it + if (event.state().metaData().hasIndex(file.getName())) { + continue; + } + logger.debug("[{}] deleting index that is no longer in the cluster meta_date", file.getName()); + FileSystemUtils.deleteRecursively(file); + } + } + } } }