From afa62e90d9f8277e0426033cab188548bb99a2f1 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Wed, 19 Nov 2014 15:58:26 +0100 Subject: [PATCH] [CORE] Free pending search contexts if index is closed Today we hold on to search context reference if they are not cleaned up for a while until a reaper thread trashes them if they timed out. This commit removes all pending contexts once the index is closed to release resources and filehandles immediatly once the index is closed. --- .../index/shard/service/IndexShard.java | 7 +++---- .../elasticsearch/search/SearchService.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java b/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java index d1a63819956..37fba04be57 100644 --- a/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java +++ b/src/main/java/org/elasticsearch/index/shard/service/IndexShard.java @@ -51,10 +51,7 @@ import org.elasticsearch.index.refresh.RefreshStats; import org.elasticsearch.index.search.stats.SearchStats; import org.elasticsearch.index.search.stats.ShardSearchService; import org.elasticsearch.index.service.IndexService; -import org.elasticsearch.index.shard.DocsStats; -import org.elasticsearch.index.shard.IllegalIndexShardStateException; -import org.elasticsearch.index.shard.IndexShardComponent; -import org.elasticsearch.index.shard.IndexShardState; +import org.elasticsearch.index.shard.*; import org.elasticsearch.index.store.StoreStats; import org.elasticsearch.index.suggest.stats.ShardSuggestService; import org.elasticsearch.index.suggest.stats.SuggestStats; @@ -183,6 +180,8 @@ public interface IndexShard extends IndexShardComponent { void readAllowed(Mode mode) throws IllegalIndexShardStateException; + ShardId shardId(); + public enum Mode { READ, WRITE diff --git a/src/main/java/org/elasticsearch/search/SearchService.java b/src/main/java/org/elasticsearch/search/SearchService.java index be26d54808e..e5e343e2ced 100644 --- a/src/main/java/org/elasticsearch/search/SearchService.java +++ b/src/main/java/org/elasticsearch/search/SearchService.java @@ -51,6 +51,7 @@ import org.elasticsearch.common.util.concurrent.FutureUtils; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.index.Index; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.fielddata.FieldDataType; import org.elasticsearch.index.fielddata.IndexFieldData; @@ -145,6 +146,15 @@ public class SearchService extends AbstractLifecycleComponent { this.threadPool = threadPool; this.clusterService = clusterService; this.indicesService = indicesService; + indicesService.indicesLifecycle().addListener(new IndicesLifecycle.Listener() { + + @Override + public void afterIndexDeleted(Index index) { + // once an index is closed we can just clean up all the pending search context information + // to release memory and let references to the filesystem go etc. + freeAllContextForIndex(index); + } + }); this.indicesWarmer = indicesWarmer; this.scriptService = scriptService; this.pageCacheRecycler = pageCacheRecycler; @@ -563,6 +573,16 @@ public class SearchService extends AbstractLifecycleComponent { return context; } + private void freeAllContextForIndex(Index index) { + assert index != null; + for (SearchContext ctx : activeContexts.values()) { + if (index.equals(ctx.indexShard().shardId().index())) { + freeContext(ctx.id()); + } + } + } + + public boolean freeContext(long id) { final SearchContext context = activeContexts.remove(id); if (context != null) {