Browse Source

Cache modification time of translog writer file (#95107)

Co-authored-by: Iraklis Psaroudakis <kingherc@gmail.com>
loupipalien 2 years ago
parent
commit
12447cec61

+ 5 - 0
docs/changelog/95107.yaml

@@ -0,0 +1,5 @@
+pr: 95107
+summary: Cache modification time of translog writer file
+area: Engine
+type: enhancement
+issues: []

+ 16 - 0
server/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java

@@ -84,6 +84,12 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
 
 
     private final DiskIoBufferPool diskIoBufferPool;
     private final DiskIoBufferPool diskIoBufferPool;
 
 
+    // package private for testing
+    LastModifiedTimeCache lastModifiedTimeCache;
+
+    // package private for testing
+    record LastModifiedTimeCache(long lastModifiedTime, long totalOffset, long syncedOffset) {}
+
     private TranslogWriter(
     private TranslogWriter(
         final ShardId shardId,
         final ShardId shardId,
         final Checkpoint initialCheckpoint,
         final Checkpoint initialCheckpoint,
@@ -127,6 +133,7 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
         this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null;
         this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null;
         this.tragedy = tragedy;
         this.tragedy = tragedy;
         this.operationListener = operationListener;
         this.operationListener = operationListener;
+        this.lastModifiedTimeCache = new LastModifiedTimeCache(-1, -1, -1);
     }
     }
 
 
     public static TranslogWriter create(
     public static TranslogWriter create(
@@ -642,4 +649,13 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
     protected final boolean isClosed() {
     protected final boolean isClosed() {
         return closed.get();
         return closed.get();
     }
     }
+
+    @Override
+    public long getLastModifiedTime() throws IOException {
+        if (lastModifiedTimeCache.totalOffset() != totalOffset || lastModifiedTimeCache.syncedOffset() != lastSyncedCheckpoint.offset) {
+            long mtime = super.getLastModifiedTime();
+            lastModifiedTimeCache = new LastModifiedTimeCache(mtime, totalOffset, lastSyncedCheckpoint.offset);
+        }
+        return lastModifiedTimeCache.lastModifiedTime();
+    }
 }
 }

+ 34 - 0
server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java

@@ -1586,6 +1586,40 @@ public class TranslogTests extends ESTestCase {
         }
         }
     }
     }
 
 
+    public void testTranslogWriterLastModifiedTime() throws IOException {
+        Path tempDir = createTempDir();
+        try (Translog translog = create(tempDir)) {
+            long mtime = translog.getCurrent().getLastModifiedTime();
+            TranslogWriter.LastModifiedTimeCache mtimeCache = translog.getCurrent().lastModifiedTimeCache;
+            // no ops
+            long lastMtime = translog.getCurrent().getLastModifiedTime();
+            TranslogWriter.LastModifiedTimeCache lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
+            assertThat(lastMtime, equalTo(mtime));
+            assertEquals(lastMtimeCache, mtimeCache);
+
+            mtime = lastMtime;
+            mtimeCache = lastMtimeCache;
+            // add ops
+            int count = randomIntBetween(1, 100);
+            for (int i = 0; i < count; i++) {
+                translog.add(indexOp(randomAlphaOfLength(128), i, primaryTerm.get()));
+            }
+            lastMtime = translog.getCurrent().getLastModifiedTime();
+            lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
+            assertThat(lastMtime, greaterThanOrEqualTo(mtime));
+            assertThat(lastMtimeCache.totalOffset(), greaterThan(mtimeCache.totalOffset()));
+
+            mtime = lastMtime;
+            mtimeCache = lastMtimeCache;
+            // sync ops
+            translog.sync();
+            lastMtime = translog.getCurrent().getLastModifiedTime();
+            lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
+            assertThat(lastMtime, greaterThanOrEqualTo(mtime));
+            assertThat(lastMtimeCache.syncedOffset(), greaterThan(mtimeCache.syncedOffset()));
+        }
+    }
+
     public void testTranslogOperationListener() throws IOException {
     public void testTranslogOperationListener() throws IOException {
         Path tempDir = createTempDir();
         Path tempDir = createTempDir();
         final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT).build();
         final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT).build();