Skip to content

Commit

Permalink
Fix TranslogTests#testStats (#85828)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DaveCTurner committed Apr 13, 2022
1 parent 750bb1e commit 3b81473
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,12 @@ public void testFindEarliestLastModifiedAge() throws IOException {
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, readers, w), equalTo(LongStream.of(periods).max().orElse(0L)));
}

public void testStats() throws IOException {
private void waitForPositiveAge() throws Exception {
final long lastModifiedTime = translog.getCurrent().getLastModifiedTime();
assertBusy(() -> assertThat(System.currentTimeMillis(), greaterThan(lastModifiedTime)));
}

public void testStats() throws Exception {
// self control cleaning for test
translog.getDeletionPolicy().setRetentionSizeInBytes(1024 * 1024);
translog.getDeletionPolicy().setRetentionAgeInMillis(3600 * 1000);
Expand All @@ -433,6 +438,7 @@ public void testStats() throws IOException {
translog.add(new Translog.Index("test", "1", 0, primaryTerm.get(), new byte[] { 1 }));

{
waitForPositiveAge();
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(1));
assertThat(stats.getTranslogSizeInBytes(), equalTo(162L));
Expand All @@ -443,6 +449,7 @@ public void testStats() throws IOException {

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

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

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

translog.rollGeneration();
{
waitForPositiveAge();
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
assertThat(stats.getTranslogSizeInBytes(), equalTo(355L));
Expand Down Expand Up @@ -510,13 +520,13 @@ public void testStats() throws IOException {
translog.getDeletionPolicy().setLocalCheckpointOfSafeCommit(randomLongBetween(3, Long.MAX_VALUE));
translog.trimUnreferencedReaders();
{
long lastModifiedAge = System.currentTimeMillis() - translog.getCurrent().getLastModifiedTime();
waitForPositiveAge();
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
assertThat(stats.getTranslogSizeInBytes(), equalTo(355L));
assertThat(stats.getUncommittedOperations(), equalTo(0));
assertThat(stats.getUncommittedSizeInBytes(), equalTo(firstOperationPosition));
assertThat(stats.getEarliestLastModifiedAge(), greaterThanOrEqualTo(lastModifiedAge));
assertThat(stats.getEarliestLastModifiedAge(), greaterThan(0L));
}
}

Expand Down

0 comments on commit 3b81473

Please sign in to comment.