From 764a519627cd3a79b713082caf200eb9ce3018d1 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 5 Sep 2013 09:44:41 +0200 Subject: [PATCH] Return empty completion stats if engine throws an exception If the index shard is not started yet the underlying engine can throw an exception since not everything is initialized. This can happen if a shard is just about starting up or recovering / initalizing and stats are requested. Closes #3619 --- .../shard/service/InternalIndexShard.java | 43 ++++++++++--------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java b/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java index 31594797cb7..9e66799ba0b 100644 --- a/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java +++ b/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java @@ -82,7 +82,6 @@ import org.elasticsearch.search.suggest.completion.Completion090PostingsFormat; import org.elasticsearch.search.suggest.completion.CompletionStats; import org.elasticsearch.threadpool.ThreadPool; -import java.io.IOException; import java.io.PrintStream; import java.nio.channels.ClosedByInterruptException; import java.util.concurrent.ScheduledFuture; @@ -457,16 +456,16 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I @Override public DocsStats docStats() { - Engine.Searcher searcher = null; try { - searcher = engine.searcher(); - return new DocsStats(searcher.reader().numDocs(), searcher.reader().numDeletedDocs()); - } catch (Exception e) { - return new DocsStats(); - } finally { - if (searcher != null) { + final Engine.Searcher searcher = engine.searcher(); + try { + return new DocsStats(searcher.reader().numDocs(), searcher.reader().numDeletedDocs()); + } finally { searcher.release(); } + } catch (Throwable e) { + logger.debug("Can not docStats completion stats from engine shard state [{}]", e, state); + return new DocsStats(); } } @@ -489,7 +488,8 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I public StoreStats storeStats() { try { return store.stats(); - } catch (IOException e) { + } catch (Throwable e) { + logger.debug("Can not build store stats from engine shard state [{}]", e, state); return new StoreStats(); } } @@ -532,19 +532,22 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I @Override public CompletionStats completionStats(String... fields) { CompletionStats completionStats = new CompletionStats(); - - Engine.Searcher searcher = engine().searcher(); - - try { - PostingsFormat postingsFormat = this.codecService.postingsFormatService().get(Completion090PostingsFormat.CODEC_NAME).get(); - if (postingsFormat instanceof Completion090PostingsFormat) { - Completion090PostingsFormat completionPostingsFormat = (Completion090PostingsFormat) postingsFormat; - completionStats.add(completionPostingsFormat.completionStats(searcher().reader(), fields)); + try{ + final Engine.Searcher searcher = engine.searcher(); + try { + PostingsFormat postingsFormat = this.codecService.postingsFormatService().get(Completion090PostingsFormat.CODEC_NAME).get(); + if (postingsFormat instanceof Completion090PostingsFormat) { + Completion090PostingsFormat completionPostingsFormat = (Completion090PostingsFormat) postingsFormat; + completionStats.add(completionPostingsFormat.completionStats(searcher().reader(), fields)); + } + } finally { + searcher.release(); } - } finally { - searcher.release(); + } catch (Throwable e) { + logger.debug("Can not build completion stats from engine shard state [{}]", e, state); + // if we are called during engine stop / start or before start we can run into Exceptions + // like the engine is already closed or no saercher is present at this point. } - return completionStats; }