From 67badc6621b98249745711f70490cda37cf275ec Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Wed, 14 Aug 2019 12:14:47 -0400 Subject: [PATCH] close if failed to install reference --- .../elasticsearch/index/shard/IndexShard.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 202a362b09419..a71616be7d320 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/server/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -1606,9 +1606,8 @@ private void innerOpenEngineAndTranslog(LongSupplier globalCheckpointSupplier) t synchronized (engineMutex) { // we must create a new engine under mutex (see IndexShard#snapshotStoreMetadata). final Engine newEngine = engineFactory.newReadWriteEngine(config); - boolean success = false; - try { - synchronized (mutex) { + synchronized (mutex) { + try { verifyNotClosed(); assert currentEngineReference.get() == null : "engine is running"; onNewEngine(newEngine); @@ -1616,11 +1615,10 @@ private void innerOpenEngineAndTranslog(LongSupplier globalCheckpointSupplier) t // We set active because we are now writing operations to the engine; this way, // if we go idle after some time and become inactive, we still give sync'd flush a chance to run. active.set(true); - success = true; - } - } finally { - if (success == false) { - newEngine.close(); + } finally { + if (currentEngineReference.get() != newEngine) { + newEngine.close(); + } } } } @@ -3372,16 +3370,14 @@ public void close() throws IOException { IOUtils.close(super::close, newEngine); } }; - boolean success = false; - try { - synchronized (mutex) { + synchronized (mutex) { + try { verifyNotClosed(); IOUtils.close(currentEngineReference.getAndSet(readOnlyEngine)); - success = true; - } - } finally { - if (success == false) { - readOnlyEngine.close(); + } finally { + if (currentEngineReference.get() != readOnlyEngine) { + readOnlyEngine.close(); + } } } newEngineReference.set(engineFactory.newReadWriteEngine(newEngineConfig(replicationTracker)));