Skip to content

Commit 4af5a2f

Browse files
authored
Fix TranslogTests#testStats (#85828) (#85857)
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 60d8398 commit 4af5a2f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,17 +446,23 @@ public void testFindEarliestLastModifiedAge() throws IOException {
446446
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, readers, w), equalTo(LongStream.of(periods).max().orElse(0L)));
447447
}
448448

449-
public void testStats() throws IOException {
449+
private void waitForPositiveAge() throws Exception {
450+
final var lastModifiedTime = translog.getCurrent().getLastModifiedTime();
451+
assertBusy(() -> assertThat(System.currentTimeMillis(), greaterThan(lastModifiedTime)));
452+
}
453+
454+
public void testStats() throws Exception {
450455
// self control cleaning for test
451456
final long firstOperationPosition = translog.getFirstOperationPosition();
452457
{
453458
final TranslogStats stats = stats();
454459
assertThat(stats.estimatedNumberOfOperations(), equalTo(0));
455460
}
456461
assertThat((int) firstOperationPosition, greaterThan(CodecUtil.headerLength(TranslogHeader.TRANSLOG_CODEC)));
457-
translog.add(new Translog.Index("1", 0, primaryTerm.get(), new byte[] { 1 }));
458462

463+
translog.add(new Translog.Index("1", 0, primaryTerm.get(), new byte[] { 1 }));
459464
{
465+
waitForPositiveAge();
460466
final TranslogStats stats = stats();
461467
assertThat(stats.estimatedNumberOfOperations(), equalTo(1));
462468
assertThat(stats.getTranslogSizeInBytes(), equalTo(157L));
@@ -467,6 +473,7 @@ public void testStats() throws IOException {
467473

468474
translog.add(new Translog.Delete("2", 1, primaryTerm.get()));
469475
{
476+
waitForPositiveAge();
470477
final TranslogStats stats = stats();
471478
assertThat(stats.estimatedNumberOfOperations(), equalTo(2));
472479
assertThat(stats.getTranslogSizeInBytes(), equalTo(193L));
@@ -477,6 +484,7 @@ public void testStats() throws IOException {
477484

478485
translog.add(new Translog.Delete("3", 2, primaryTerm.get()));
479486
{
487+
waitForPositiveAge();
480488
final TranslogStats stats = stats();
481489
assertThat(stats.estimatedNumberOfOperations(), equalTo(3));
482490
assertThat(stats.getTranslogSizeInBytes(), equalTo(229L));
@@ -487,6 +495,7 @@ public void testStats() throws IOException {
487495

488496
translog.add(new Translog.NoOp(3, 1, randomAlphaOfLength(16)));
489497
{
498+
waitForPositiveAge();
490499
final TranslogStats stats = stats();
491500
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
492501
assertThat(stats.getTranslogSizeInBytes(), equalTo(271L));
@@ -497,6 +506,7 @@ public void testStats() throws IOException {
497506

498507
translog.rollGeneration();
499508
{
509+
waitForPositiveAge();
500510
final TranslogStats stats = stats();
501511
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
502512
assertThat(stats.getTranslogSizeInBytes(), equalTo(326L));
@@ -532,13 +542,13 @@ public void testStats() throws IOException {
532542
translog.getDeletionPolicy().setLocalCheckpointOfSafeCommit(randomLongBetween(3, Long.MAX_VALUE));
533543
translog.trimUnreferencedReaders();
534544
{
535-
long lastModifiedAge = System.currentTimeMillis() - translog.getCurrent().getLastModifiedTime();
545+
waitForPositiveAge();
536546
final TranslogStats stats = stats();
537547
assertThat(stats.estimatedNumberOfOperations(), equalTo(0));
538548
assertThat(stats.getTranslogSizeInBytes(), equalTo(firstOperationPosition));
539549
assertThat(stats.getUncommittedOperations(), equalTo(0));
540550
assertThat(stats.getUncommittedSizeInBytes(), equalTo(firstOperationPosition));
541-
assertThat(stats.getEarliestLastModifiedAge(), greaterThanOrEqualTo(lastModifiedAge));
551+
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
542552
}
543553
}
544554

0 commit comments

Comments
 (0)