diff --git a/src/main/java/org/elasticsearch/index/indexing/ShardIndexingService.java b/src/main/java/org/elasticsearch/index/indexing/ShardIndexingService.java index 72a6ebfe801..a84010ea366 100644 --- a/src/main/java/org/elasticsearch/index/indexing/ShardIndexingService.java +++ b/src/main/java/org/elasticsearch/index/indexing/ShardIndexingService.java @@ -132,6 +132,9 @@ public class ShardIndexingService extends AbstractIndexShardComponent { } } + public void postCreate(Engine.Create create, Throwable ex) { + } + public Engine.Index preIndex(Engine.Index index) { totalStats.indexCurrent.inc(); typeStats(index.type()).indexCurrent.inc(); @@ -168,7 +171,7 @@ public class ShardIndexingService extends AbstractIndexShardComponent { } } - public void failedIndex(Engine.Index index) { + public void postIndex(Engine.Index index, Throwable ex) { totalStats.indexCurrent.dec(); typeStats(index.type()).indexCurrent.dec(); } @@ -208,7 +211,7 @@ public class ShardIndexingService extends AbstractIndexShardComponent { } } - public void failedDelete(Engine.Delete delete) { + public void postDelete(Engine.Delete delete, Throwable ex) { totalStats.deleteCurrent.dec(); typeStats(delete.type()).deleteCurrent.dec(); } 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 0d7be7b5556..bd20b7a1667 100644 --- a/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java +++ b/src/main/java/org/elasticsearch/index/shard/service/InternalIndexShard.java @@ -418,11 +418,16 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I public ParsedDocument create(Engine.Create create) throws ElasticsearchException { writeAllowed(create.origin()); create = indexingService.preCreate(create); - if (logger.isTraceEnabled()) { - logger.trace("index [{}][{}]{}", create.type(), create.id(), create.docs()); + try { + if (logger.isTraceEnabled()) { + logger.trace("index [{}][{}]{}", create.type(), create.id(), create.docs()); + } + engine.create(create); + create.endTime(System.nanoTime()); + } catch (Throwable ex) { + indexingService.postCreate(create, ex); + throw ex; } - engine.create(create); - create.endTime(System.nanoTime()); indexingService.postCreate(create); return create.parsedDoc(); } @@ -445,8 +450,8 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I } engine.index(index); index.endTime(System.nanoTime()); - } catch (RuntimeException ex) { - indexingService.failedIndex(index); + } catch (Throwable ex) { + indexingService.postIndex(index, ex); throw ex; } indexingService.postIndex(index); @@ -470,8 +475,8 @@ public class InternalIndexShard extends AbstractIndexShardComponent implements I } engine.delete(delete); delete.endTime(System.nanoTime()); - } catch (RuntimeException ex) { - indexingService.failedDelete(delete); + } catch (Throwable ex) { + indexingService.postDelete(delete, ex); throw ex; } indexingService.postDelete(delete);