Skip to content

Commit a6347a8

Browse files
committed
Fix SearchableSnapshotDirectoryStatsTests (#58912)
Similar to #58847 but in a different tests. The failure never reproduced locally but occurs from time to time on CI.
1 parent 5783b63 commit a6347a8

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,27 @@ public void testCachedBytesReadsAndWrites() {
112112

113113
final long cachedBytesWriteCount = TestUtils.numberOfRanges(length, rangeSize.getBytes());
114114

115-
assertThat(inputStats.getCachedBytesWritten(), notNullValue());
116-
assertThat(inputStats.getCachedBytesWritten().total(), equalTo(length));
117-
assertThat(inputStats.getCachedBytesWritten().count(), equalTo(cachedBytesWriteCount));
118-
assertThat(inputStats.getCachedBytesWritten().min(), greaterThan(0L));
119-
assertThat(
120-
inputStats.getCachedBytesWritten().max(),
121-
(length < rangeSize.getBytes()) ? equalTo(length) : equalTo(rangeSize.getBytes())
122-
);
123-
assertThat(
124-
inputStats.getCachedBytesWritten().totalNanoseconds(),
125-
allOf(
126-
// each read takes at least FAKE_CLOCK_ADVANCE_NANOS time
127-
greaterThanOrEqualTo(FAKE_CLOCK_ADVANCE_NANOS * cachedBytesWriteCount),
128-
129-
// worst case: we start all reads before finishing any of them
130-
lessThanOrEqualTo(FAKE_CLOCK_ADVANCE_NANOS * cachedBytesWriteCount * cachedBytesWriteCount)
131-
)
132-
);
115+
// cache writes are executed in a different thread pool and can take some time to be processed
116+
assertBusy(() -> {
117+
assertThat(inputStats.getCachedBytesWritten(), notNullValue());
118+
assertThat(inputStats.getCachedBytesWritten().total(), equalTo(length));
119+
assertThat(inputStats.getCachedBytesWritten().count(), equalTo(cachedBytesWriteCount));
120+
assertThat(inputStats.getCachedBytesWritten().min(), greaterThan(0L));
121+
assertThat(
122+
inputStats.getCachedBytesWritten().max(),
123+
(length < rangeSize.getBytes()) ? equalTo(length) : equalTo(rangeSize.getBytes())
124+
);
125+
assertThat(
126+
inputStats.getCachedBytesWritten().totalNanoseconds(),
127+
allOf(
128+
// each read takes at least FAKE_CLOCK_ADVANCE_NANOS time
129+
greaterThanOrEqualTo(FAKE_CLOCK_ADVANCE_NANOS * cachedBytesWriteCount),
130+
131+
// worst case: we start all reads before finishing any of them
132+
lessThanOrEqualTo(FAKE_CLOCK_ADVANCE_NANOS * cachedBytesWriteCount * cachedBytesWriteCount)
133+
)
134+
);
135+
});
133136

134137
assertThat(inputStats.getCachedBytesRead(), notNullValue());
135138
assertThat(inputStats.getCachedBytesRead().total(), greaterThanOrEqualTo(length));
@@ -146,7 +149,7 @@ public void testCachedBytesReadsAndWrites() {
146149
assertCounter(inputStats.getOptimizedBytesRead(), 0L, 0L, 0L, 0L);
147150
assertThat(inputStats.getOptimizedBytesRead().totalNanoseconds(), equalTo(0L));
148151

149-
} catch (IOException e) {
152+
} catch (Exception e) {
150153
throw new AssertionError(e);
151154
}
152155
});
@@ -337,15 +340,17 @@ public void testReadBytesContiguously() {
337340
}
338341
}
339342

340-
// cache file has been written in a single chunk
341-
assertCounter(inputStats.getCachedBytesWritten(), input.length(), 1L, input.length(), input.length());
342-
assertThat(inputStats.getCachedBytesWritten().totalNanoseconds(), equalTo(FAKE_CLOCK_ADVANCE_NANOS));
343+
// cache file has been written in a single chunk in a different thread pool and can take some time to be processed
344+
assertBusy(() -> {
345+
assertCounter(inputStats.getCachedBytesWritten(), input.length(), 1L, input.length(), input.length());
346+
assertThat(inputStats.getCachedBytesWritten().totalNanoseconds(), equalTo(FAKE_CLOCK_ADVANCE_NANOS));
347+
});
343348

344349
assertCounter(inputStats.getNonContiguousReads(), 0L, 0L, 0L, 0L);
345350
assertCounter(inputStats.getDirectBytesRead(), 0L, 0L, 0L, 0L);
346351
assertThat(inputStats.getDirectBytesRead().totalNanoseconds(), equalTo(0L));
347352

348-
} catch (IOException e) {
353+
} catch (Exception e) {
349354
throw new AssertionError(e);
350355
}
351356
});

0 commit comments

Comments
 (0)