Skip to content

Commit aa9a2bc

Browse files
committed
Avoid accidental contiguous read (#55713)
If we choose to read from two random positions that are 1024 bytes apart then this counts as a contiguous read for stats purposes, failing this test. This commit ensures that we always perform a non-contiguous read.
1 parent 6d2a537 commit aa9a2bc

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/index/store/SearchableSnapshotDirectoryStatsTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,11 @@ public void testReadBytesNonContiguously() {
357357
long totalBytesRead = 0L;
358358
long minBytesRead = Long.MAX_VALUE;
359359
long maxBytesRead = Long.MIN_VALUE;
360+
long lastReadPosition = 0L;
361+
int iterations = between(1, 10);
360362

361-
for (long i = 1L; i <= randomLongBetween(1L, 10L); i++) {
362-
final long randomPosition = randomLongBetween(1L, input.length() - 1L);
363+
for (int i = 1; i <= iterations; i++) {
364+
final long randomPosition = randomValueOtherThan(lastReadPosition, () -> randomLongBetween(1L, input.length() - 1L));
363365
input.seek(randomPosition);
364366

365367
final byte[] readBuffer = new byte[512];
@@ -368,6 +370,7 @@ public void testReadBytesNonContiguously() {
368370

369371
// BufferedIndexInput tries to read as much bytes as possible
370372
final long bytesRead = Math.min(BufferedIndexInput.bufferSize(ioContext), input.length() - randomPosition);
373+
lastReadPosition = randomPosition + bytesRead;
371374
totalBytesRead += bytesRead;
372375
minBytesRead = (bytesRead < minBytesRead) ? bytesRead : minBytesRead;
373376
maxBytesRead = (bytesRead > maxBytesRead) ? bytesRead : maxBytesRead;

0 commit comments

Comments
 (0)