Skip to content

Commit 1817095

Browse files
committed
Stronger test
1 parent f5d3973 commit 1817095

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
import java.util.concurrent.atomic.AtomicInteger;
103103
import java.util.concurrent.atomic.AtomicLong;
104104
import java.util.concurrent.atomic.AtomicReference;
105+
import java.util.function.BiConsumer;
106+
import java.util.function.BiFunction;
105107
import java.util.stream.Collectors;
106108
import java.util.stream.LongStream;
107109

@@ -2271,23 +2273,43 @@ public void testPrepareCommitAndCommit() throws IOException {
22712273
}
22722274

22732275
public void testCommitWithOpenView() throws IOException {
2274-
final int operations = randomIntBetween(1, 1 << 16);
2276+
final int operations = randomIntBetween(1, 4096);
22752277
long seqNo = 0;
2276-
long last;
2278+
long lastCommittedGeneration = -1;
22772279
for (int i = 0; i < operations; i++) {
22782280
translog.add(new Translog.NoOp(seqNo++, 0, "test"));
22792281
if (rarely()) {
2280-
try (Translog.View ignored = translog.newView()) {
2282+
if (randomBoolean()) {
2283+
try (Translog.View ignored = translog.newView()) {
2284+
final long viewGeneration = lastCommittedGeneration;
2285+
translog.prepareCommit();
2286+
final long committedGeneration = randomIntBetween(
2287+
Math.max(1, Math.toIntExact(lastCommittedGeneration)),
2288+
Math.toIntExact(translog.currentFileGeneration()));
2289+
translog.commit(committedGeneration);
2290+
lastCommittedGeneration = committedGeneration;
2291+
// with an open view, committing should preserve generations back to the last committed generation
2292+
for (long g = 1; g < Math.min(lastCommittedGeneration, viewGeneration); g++) {
2293+
assertFileDeleted(translog, g);
2294+
}
2295+
// the view generation could be -1 if no commit has been performed
2296+
final long max = Math.max(1, Math.min(lastCommittedGeneration, viewGeneration));
2297+
for (long g = max; g < translog.currentFileGeneration(); g++) {
2298+
assertFileIsPresent(translog, g);
2299+
}
2300+
}
2301+
} else {
22812302
final long generation = translog.currentFileGeneration();
22822303
translog.prepareCommit();
2283-
final long committedGeneration =
2284-
randomIntBetween(1, Math.toIntExact(generation));
2304+
final long committedGeneration = randomIntBetween(
2305+
Math.max(1, Math.toIntExact(lastCommittedGeneration)),
2306+
Math.toIntExact(generation));
22852307
translog.commit(committedGeneration);
2286-
last = committedGeneration;
2287-
for (long g = 0; i < last; g++) {
2308+
lastCommittedGeneration = committedGeneration;
2309+
for (long g = 1; g < lastCommittedGeneration; g++) {
22882310
assertFileDeleted(translog, g);
22892311
}
2290-
for (long g = last; i < translog.currentFileGeneration(); g++) {
2312+
for (long g = lastCommittedGeneration; g < translog.currentFileGeneration(); g++) {
22912313
assertFileIsPresent(translog, g);
22922314
}
22932315
}

0 commit comments

Comments
 (0)