Skip to content

Commit 3b81473

Browse files
committed
Fix TranslogTests#testStats (#85828)
Today these tests assume that the last-modified time on the translog file is at least one millisecond before the time at which stats are computed, but this isn't always true. With this commit we add a (bounded) wait for the clock to advance beyond the file's last-modified time to ensure that we observe a nonzero age. Closes #85717
1 parent 750bb1e commit 3b81473

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,12 @@ public void testFindEarliestLastModifiedAge() throws IOException {
420420
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, readers, w), equalTo(LongStream.of(periods).max().orElse(0L)));
421421
}
422422

423-
public void testStats() throws IOException {
423+
private void waitForPositiveAge() throws Exception {
424+
final long lastModifiedTime = translog.getCurrent().getLastModifiedTime();
425+
assertBusy(() -> assertThat(System.currentTimeMillis(), greaterThan(lastModifiedTime)));
426+
}
427+
428+
public void testStats() throws Exception {
424429
// self control cleaning for test
425430
translog.getDeletionPolicy().setRetentionSizeInBytes(1024 * 1024);
426431
translog.getDeletionPolicy().setRetentionAgeInMillis(3600 * 1000);
@@ -433,6 +438,7 @@ public void testStats() throws IOException {
433438
translog.add(new Translog.Index("test", "1", 0, primaryTerm.get(), new byte[] { 1 }));
434439

435440
{
441+
waitForPositiveAge();
436442
final TranslogStats stats = stats();
437443
assertThat(stats.estimatedNumberOfOperations(), equalTo(1));
438444
assertThat(stats.getTranslogSizeInBytes(), equalTo(162L));
@@ -443,6 +449,7 @@ public void testStats() throws IOException {
443449

444450
translog.add(new Translog.Delete("test", "2", 1, primaryTerm.get(), newUid("2")));
445451
{
452+
waitForPositiveAge();
446453
final TranslogStats stats = stats();
447454
assertThat(stats.estimatedNumberOfOperations(), equalTo(2));
448455
assertThat(stats.getTranslogSizeInBytes(), equalTo(210L));
@@ -453,6 +460,7 @@ public void testStats() throws IOException {
453460

454461
translog.add(new Translog.Delete("test", "3", 2, primaryTerm.get(), newUid("3")));
455462
{
463+
waitForPositiveAge();
456464
final TranslogStats stats = stats();
457465
assertThat(stats.estimatedNumberOfOperations(), equalTo(3));
458466
assertThat(stats.getTranslogSizeInBytes(), equalTo(258L));
@@ -463,6 +471,7 @@ public void testStats() throws IOException {
463471

464472
translog.add(new Translog.NoOp(3, 1, randomAlphaOfLength(16)));
465473
{
474+
waitForPositiveAge();
466475
final TranslogStats stats = stats();
467476
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
468477
assertThat(stats.getTranslogSizeInBytes(), equalTo(300L));
@@ -473,6 +482,7 @@ public void testStats() throws IOException {
473482

474483
translog.rollGeneration();
475484
{
485+
waitForPositiveAge();
476486
final TranslogStats stats = stats();
477487
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
478488
assertThat(stats.getTranslogSizeInBytes(), equalTo(355L));
@@ -510,13 +520,13 @@ public void testStats() throws IOException {
510520
translog.getDeletionPolicy().setLocalCheckpointOfSafeCommit(randomLongBetween(3, Long.MAX_VALUE));
511521
translog.trimUnreferencedReaders();
512522
{
513-
long lastModifiedAge = System.currentTimeMillis() - translog.getCurrent().getLastModifiedTime();
523+
waitForPositiveAge();
514524
final TranslogStats stats = stats();
515525
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
516526
assertThat(stats.getTranslogSizeInBytes(), equalTo(355L));
517527
assertThat(stats.getUncommittedOperations(), equalTo(0));
518528
assertThat(stats.getUncommittedSizeInBytes(), equalTo(firstOperationPosition));
519-
assertThat(stats.getEarliestLastModifiedAge(), greaterThanOrEqualTo(lastModifiedAge));
529+
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
520530
}
521531
}
522532

0 commit comments

Comments
 (0)