Browse Source

Fix the earliest last modified age of translog stats (#64753)

Currently translog's `earliest_last_modified_age` field is always 0 in `_nodes/stats` response.
Howard 4 years ago
parent
commit
7f4ead91f3

+ 6 - 2
server/src/main/java/org/elasticsearch/index/translog/TranslogStats.java

@@ -80,8 +80,12 @@ public class TranslogStats implements Writeable, ToXContentFragment {
         this.translogSizeInBytes += translogStats.translogSizeInBytes;
         this.uncommittedOperations += translogStats.uncommittedOperations;
         this.uncommittedSizeInBytes += translogStats.uncommittedSizeInBytes;
-        this.earliestLastModifiedAge =
-            Math.min(this.earliestLastModifiedAge, translogStats.earliestLastModifiedAge);
+        if (this.earliestLastModifiedAge == 0) {
+            this.earliestLastModifiedAge = translogStats.earliestLastModifiedAge;
+        } else {
+            this.earliestLastModifiedAge =
+                Math.min(this.earliestLastModifiedAge, translogStats.earliestLastModifiedAge);
+        }
     }
 
     public long getTranslogSizeInBytes() {

+ 6 - 2
server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

@@ -516,14 +516,18 @@ public class TranslogTests extends ESTestCase {
 
     public void testTotalTests() {
         final TranslogStats total =
-            new TranslogStats(0, 0, 0, 0, 1);
+            new TranslogStats();
         final int n = randomIntBetween(0, 16);
         final List<TranslogStats> statsList = new ArrayList<>(n);
+        long earliestLastModifiedAge = Long.MAX_VALUE;
         for (int i = 0; i < n; i++) {
             final TranslogStats stats = new TranslogStats(randomIntBetween(1, 4096), randomIntBetween(1, 1 << 20),
                 randomIntBetween(1, 1 << 20), randomIntBetween(1, 4096), randomIntBetween(1, 1 << 20));
             statsList.add(stats);
             total.add(stats);
+            if (earliestLastModifiedAge > stats.getEarliestLastModifiedAge()) {
+                earliestLastModifiedAge = stats.getEarliestLastModifiedAge();
+            }
         }
 
         assertThat(
@@ -540,7 +544,7 @@ public class TranslogTests extends ESTestCase {
             equalTo(statsList.stream().mapToLong(TranslogStats::getUncommittedSizeInBytes).sum()));
         assertThat(
             total.getEarliestLastModifiedAge(),
-            equalTo(1L));
+            equalTo(earliestLastModifiedAge));
     }
 
     public void testNegativeNumberOfOperations() {