Skip to content

Commit

Permalink
Fix TranslogTests#testStats (#85828) (#85857)
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 authored Apr 13, 2022
1 parent 60d8398 commit 4af5a2f
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,23 @@ 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 var lastModifiedTime = translog.getCurrent().getLastModifiedTime();
assertBusy(() -> assertThat(System.currentTimeMillis(), greaterThan(lastModifiedTime)));
}

public void testStats() throws Exception {
// self control cleaning for test
final long firstOperationPosition = translog.getFirstOperationPosition();
{
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(0));
}
assertThat((int) firstOperationPosition, greaterThan(CodecUtil.headerLength(TranslogHeader.TRANSLOG_CODEC)));
translog.add(new Translog.Index("1", 0, primaryTerm.get(), new byte[] { 1 }));

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

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

translog.add(new Translog.Delete("3", 2, primaryTerm.get()));
{
waitForPositiveAge();
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(3));
assertThat(stats.getTranslogSizeInBytes(), equalTo(229L));
Expand All @@ -487,6 +495,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(271L));
Expand All @@ -497,6 +506,7 @@ public void testStats() throws IOException {

translog.rollGeneration();
{
waitForPositiveAge();
final TranslogStats stats = stats();
assertThat(stats.estimatedNumberOfOperations(), equalTo(4));
assertThat(stats.getTranslogSizeInBytes(), equalTo(326L));
Expand Down Expand Up @@ -532,13 +542,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(0));
assertThat(stats.getTranslogSizeInBytes(), equalTo(firstOperationPosition));
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 4af5a2f

Please sign in to comment.