From 8f6b609ef4773bc591425e3dc1c91396389b6557 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Fri, 17 Mar 2017 18:06:36 -0400 Subject: [PATCH] Stricter roll checking --- .../elasticsearch/index/translog/Translog.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/translog/Translog.java b/core/src/main/java/org/elasticsearch/index/translog/Translog.java index 8dbfd54a0fb4d..7fda44071b44e 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/core/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -420,16 +420,25 @@ public Location add(final Operation operation) throws IOException { out.seek(end); final ReleasablePagedBytesReference bytes = out.bytes(); final Location location; + final boolean shouldRollGeneration; try (ReleasableLock ignored = readLock.acquire()) { ensureOpen(); location = current.add(bytes, operation.seqNo()); + // check if we should roll under the read lock + shouldRollGeneration = + shouldRollGeneration() && rollingGeneration.compareAndSet(false, true); } - if (shouldRollGeneration() && rollingGeneration.compareAndSet(false, true)) { - // we have to check the condition again lest we could roll twice in a race - if (shouldRollGeneration()) { - try (ReleasableLock ignored = writeLock.acquire()) { + if (shouldRollGeneration) { + try (ReleasableLock ignored = writeLock.acquire()) { + /* + * We have to check the condition again lest we could roll twice if another + * thread committed the translog (which rolls the generation )between us + * releasing the read lock and acquiring the write lock. + */ + if (shouldRollGeneration()) { this.rollGeneration(); } + } finally { final boolean wasRolling = rollingGeneration.getAndSet(false); assert wasRolling; }