Skip to content

Commit

Permalink
Add test for rolling and committing
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontedor committed Mar 17, 2017
1 parent 70ade35 commit d9fbc42
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/src/main/java/org/elasticsearch/index/translog/Translog.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -1388,26 +1389,30 @@ public long prepareCommit() throws IOException {
try (ReleasableLock ignored = writeLock.acquire()) {
ensureOpen();
if (currentCommittingGeneration != NOT_SET_GENERATION) {
throw new IllegalStateException("already committing a translog with generation: " +
final String message = String.format(
Locale.ROOT,
"already committing a translog with generation [%d]",
currentCommittingGeneration);
throw new IllegalStateException(message);
}
currentCommittingGeneration = current.getGeneration();
rollGeneration();
}
return 0L;
return 0;
}

@Override
public long commit() throws IOException {
try (ReleasableLock lock = writeLock.acquire()) {
try (ReleasableLock ignored = writeLock.acquire()) {
ensureOpen();
if (currentCommittingGeneration == NOT_SET_GENERATION) {
prepareCommit();
}
assert currentCommittingGeneration != NOT_SET_GENERATION;
assert readers.stream().filter(r -> r.getGeneration() == currentCommittingGeneration).findFirst().isPresent()
: "reader list doesn't contain committing generation [" + currentCommittingGeneration + "]";
lastCommittedTranslogFileGeneration = current.getGeneration(); // this is important - otherwise old files will not be cleaned up
assert readers.stream().anyMatch(r -> r.getGeneration() == currentCommittingGeneration)
: "readers missing committing generation [" + currentCommittingGeneration + "]";
// set the last committed generation otherwise old files will not be cleaned up
lastCommittedTranslogFileGeneration = current.getGeneration();
currentCommittingGeneration = NOT_SET_GENERATION;
trimUnreferencedReaders();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2149,4 +2149,40 @@ public void testGenerationThreshold() throws IOException {
}
}

public void testRollGenerationBetweenPrepareCommitAndCommit() throws IOException {
final long generation = translog.currentFileGeneration();
int seqNo = 0;

final int operationsBefore = randomIntBetween(1, 256);
for (int i = 0; i < operationsBefore; i++) {
translog.add(new Translog.NoOp(seqNo++, 0, "test"));
}

translog.prepareCommit();
assertThat(translog.currentFileGeneration(), equalTo(generation + 1));
for (long g = generation; g <= generation + 1; g++) {
assertFileIsPresent(translog, g);
}

final int operationsBetween = randomIntBetween(1, 256);
for (int i = 0; i < operationsBetween; i++) {
translog.add(new Translog.NoOp(seqNo++, 0, "test"));
}

try (ReleasableLock ignored = translog.writeLock.acquire()) {
translog.rollGeneration();
}
assertThat(translog.currentFileGeneration(), equalTo(generation + 2));
for (long g = generation; g <= generation + 2; g++) {
assertFileIsPresent(translog, g);
}

translog.commit();

for (long g = generation; g < generation + 2; g++) {
assertFileDeleted(translog, g);
}
assertFileIsPresent(translog, generation + 2);
}

}

0 comments on commit d9fbc42

Please sign in to comment.