Skip to content

Commit

Permalink
Stricter roll checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontedor committed Mar 17, 2017
1 parent 421b336 commit 8f6b609
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions core/src/main/java/org/elasticsearch/index/translog/Translog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8f6b609

Please sign in to comment.