Skip to content

Commit c63543b

Browse files
committed
Stronger preservation
1 parent 9120bf9 commit c63543b

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

core/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,19 +1348,19 @@ public static void writeOperationNoSize(BufferedChecksumStreamOutput out, Transl
13481348
}
13491349

13501350
/**
1351-
* Gets the minimum generation that could contain the sequence number, or the current generation if there is no generation with the
1352-
* specified sequence number between the minimum and maximum sequence numbers.
1351+
* Gets the minimum generation that could contain any sequence number after the specified sequence number, or the current generation if
1352+
* there is no generation that could any such sequence number.
13531353
*
13541354
* @param seqNo the sequence number
1355-
* @return the minimum generation for the sequence number, or the current generation
1355+
* @return the minimum generation for the sequence number
13561356
*/
13571357
public TranslogGeneration getMinGenerationForSeqNo(final long seqNo) {
13581358
try (ReleasableLock ignored = writeLock.acquire()) {
13591359
final long minTranslogFileGeneration = readers
13601360
.stream()
13611361
.filter(r -> {
13621362
final Checkpoint checkpoint = r.getCheckpoint();
1363-
return checkpoint.minSeqNo <= seqNo && seqNo <= checkpoint.maxSeqNo;
1363+
return seqNo <= checkpoint.maxSeqNo;
13641364
})
13651365
.mapToLong(TranslogReader::getGeneration)
13661366
.min()

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,22 +2201,30 @@ public void testMinGenerationForSeqNo() throws IOException {
22012201
}
22022202
}
22032203

2204-
final Set<Long> seenSeqNos = new HashSet<>();
2205-
for (final Map.Entry<Long, List<Long>> entry : generations.entrySet()) {
2206-
final long min = entry.getValue().stream().min(Long::compareTo).orElse(Long.MIN_VALUE);
2207-
final long max = entry.getValue().stream().max(Long::compareTo).orElse(Long.MAX_VALUE);
2208-
for (long seqNo = min; seqNo <= max; seqNo++) {
2209-
if (seenSeqNos.add(seqNo)) {
2210-
assertThat(
2211-
translog.getMinGenerationForSeqNo(seqNo).translogFileGeneration,
2212-
equalTo(entry.getKey()));
2213-
}
2214-
}
2204+
final Map<Long, Long> maxSeqNoByGeneration =
2205+
generations
2206+
.entrySet()
2207+
.stream()
2208+
.collect(Collectors.toMap(
2209+
Map.Entry::getKey,
2210+
e -> e.getValue().stream().max(Long::compareTo).orElse(Long.MAX_VALUE)));
2211+
2212+
for (long seqNo = 0; seqNo < operations; seqNo++) {
2213+
final long finalLongSeqNo = seqNo;
2214+
final Long operand =
2215+
maxSeqNoByGeneration
2216+
.entrySet()
2217+
.stream()
2218+
.filter(e -> finalLongSeqNo <= e.getValue())
2219+
.map(Map.Entry::getKey).min(Long::compareTo)
2220+
.orElse(Long.MIN_VALUE);
2221+
assertThat(translog.getMinGenerationForSeqNo(seqNo).translogFileGeneration, equalTo(operand));
22152222
}
22162223

2217-
assertThat(seenSeqNos, equalTo(new HashSet<>(seqNos)));
22182224
}
22192225

2226+
2227+
22202228
public void testSimpleCommit() throws IOException {
22212229
final int operations = randomIntBetween(1, 4096);
22222230
long seqNo = 0;

0 commit comments

Comments
 (0)