|
102 | 102 | import java.util.concurrent.atomic.AtomicInteger; |
103 | 103 | import java.util.concurrent.atomic.AtomicLong; |
104 | 104 | import java.util.concurrent.atomic.AtomicReference; |
| 105 | +import java.util.function.BiConsumer; |
| 106 | +import java.util.function.BiFunction; |
105 | 107 | import java.util.stream.Collectors; |
106 | 108 | import java.util.stream.LongStream; |
107 | 109 |
|
@@ -2271,23 +2273,43 @@ public void testPrepareCommitAndCommit() throws IOException { |
2271 | 2273 | } |
2272 | 2274 |
|
2273 | 2275 | public void testCommitWithOpenView() throws IOException { |
2274 | | - final int operations = randomIntBetween(1, 1 << 16); |
| 2276 | + final int operations = randomIntBetween(1, 4096); |
2275 | 2277 | long seqNo = 0; |
2276 | | - long last; |
| 2278 | + long lastCommittedGeneration = -1; |
2277 | 2279 | for (int i = 0; i < operations; i++) { |
2278 | 2280 | translog.add(new Translog.NoOp(seqNo++, 0, "test")); |
2279 | 2281 | 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 { |
2281 | 2302 | final long generation = translog.currentFileGeneration(); |
2282 | 2303 | 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)); |
2285 | 2307 | 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++) { |
2288 | 2310 | assertFileDeleted(translog, g); |
2289 | 2311 | } |
2290 | | - for (long g = last; i < translog.currentFileGeneration(); g++) { |
| 2312 | + for (long g = lastCommittedGeneration; g < translog.currentFileGeneration(); g++) { |
2291 | 2313 | assertFileIsPresent(translog, g); |
2292 | 2314 | } |
2293 | 2315 | } |
|
0 commit comments