76
76
import java .io .Closeable ;
77
77
import java .io .EOFException ;
78
78
import java .io .IOException ;
79
+ import java .io .UncheckedIOException ;
79
80
import java .nio .ByteBuffer ;
80
81
import java .nio .channels .FileChannel ;
81
82
import java .nio .charset .Charset ;
88
89
import java .util .Arrays ;
89
90
import java .util .Collection ;
90
91
import java .util .Collections ;
92
+ import java .util .Comparator ;
91
93
import java .util .HashMap ;
92
94
import java .util .HashSet ;
93
95
import java .util .Iterator ;
104
106
import java .util .concurrent .atomic .AtomicInteger ;
105
107
import java .util .concurrent .atomic .AtomicLong ;
106
108
import java .util .stream .Collectors ;
109
+ import java .util .stream .IntStream ;
107
110
import java .util .stream .LongStream ;
111
+ import java .util .stream .Stream ;
108
112
109
113
import static com .carrotsearch .randomizedtesting .RandomizedTest .randomLongBetween ;
110
114
import static org .elasticsearch .common .util .BigArrays .NON_RECYCLING_INSTANCE ;
@@ -891,7 +895,7 @@ protected void doRun() throws Exception {
891
895
// these are what we expect the snapshot to return (and potentially some more).
892
896
Set <Translog .Operation > expectedOps = new HashSet <>(writtenOps .keySet ());
893
897
expectedOps .removeIf (op -> op .seqNo () <= committedLocalCheckpointAtView );
894
- try (Translog .Snapshot snapshot = translog .newSnapshotFromMinSeqNo (committedLocalCheckpointAtView + 1L )) {
898
+ try (Translog .Snapshot snapshot = translog .newSnapshotFrom (committedLocalCheckpointAtView + 1L )) {
895
899
Translog .Operation op ;
896
900
while ((op = snapshot .next ()) != null ) {
897
901
expectedOps .remove (op );
@@ -2484,7 +2488,7 @@ public void testMinSeqNoBasedAPI() throws IOException {
2484
2488
}
2485
2489
assertThat (translog .estimateTotalOperationsFromMinSeq (seqNo ), equalTo (expectedSnapshotOps ));
2486
2490
int readFromSnapshot = 0 ;
2487
- try (Translog .Snapshot snapshot = translog .newSnapshotFromMinSeqNo (seqNo )) {
2491
+ try (Translog .Snapshot snapshot = translog .newSnapshotFrom (seqNo )) {
2488
2492
assertThat (snapshot .totalOperations (), equalTo (expectedSnapshotOps ));
2489
2493
Translog .Operation op ;
2490
2494
while ((op = snapshot .next ()) != null ) {
@@ -2500,6 +2504,38 @@ public void testMinSeqNoBasedAPI() throws IOException {
2500
2504
}
2501
2505
}
2502
2506
2507
+ public void testGetSnapshotBetween () throws IOException {
2508
+ final int numOperations = randomIntBetween (2 , 8196 );
2509
+ final List <Integer > sequenceNumbers = IntStream .range (0 , numOperations ).boxed ().collect (Collectors .toList ());
2510
+ Collections .shuffle (sequenceNumbers , random ());
2511
+ for (Integer sequenceNumber : sequenceNumbers ) {
2512
+ translog .add (new Translog .NoOp (sequenceNumber , 0 , "test" ));
2513
+ if (rarely ()) {
2514
+ translog .rollGeneration ();
2515
+ }
2516
+ }
2517
+ translog .rollGeneration ();
2518
+
2519
+ final int iters = randomIntBetween (8 , 32 );
2520
+ for (int iter = 0 ; iter < iters ; iter ++) {
2521
+ int min = randomIntBetween (0 , numOperations - 1 );
2522
+ int max = randomIntBetween (min , numOperations );
2523
+ try (Translog .Snapshot snapshot = translog .getSnapshotBetween (min , max )) {
2524
+ final List <Translog .Operation > operations = new ArrayList <>();
2525
+ for (Translog .Operation operation = snapshot .next (); operation != null ; operation = snapshot .next ()) {
2526
+ if (operation .seqNo () >= min && operation .seqNo () <= max ) {
2527
+ operations .add (operation );
2528
+ }
2529
+ }
2530
+ operations .sort (Comparator .comparingLong (Translog .Operation ::seqNo ));
2531
+ Iterator <Translog .Operation > iterator = operations .iterator ();
2532
+ for (long expectedSeqNo = min ; expectedSeqNo < max ; expectedSeqNo ++) {
2533
+ assertThat (iterator .next ().seqNo (), equalTo (expectedSeqNo ));
2534
+ }
2535
+ }
2536
+ }
2537
+ }
2538
+
2503
2539
public void testSimpleCommit () throws IOException {
2504
2540
final int operations = randomIntBetween (1 , 4096 );
2505
2541
long seqNo = 0 ;
0 commit comments