Skip to content

Commit 12447ce

Browse files
Cache modification time of translog writer file (#95107)
Co-authored-by: Iraklis Psaroudakis <kingherc@gmail.com>
1 parent 3d6143b commit 12447ce

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

docs/changelog/95107.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 95107
2+
summary: Cache modification time of translog writer file
3+
area: Engine
4+
type: enhancement
5+
issues: []

server/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
8484

8585
private final DiskIoBufferPool diskIoBufferPool;
8686

87+
// package private for testing
88+
LastModifiedTimeCache lastModifiedTimeCache;
89+
90+
// package private for testing
91+
record LastModifiedTimeCache(long lastModifiedTime, long totalOffset, long syncedOffset) {}
92+
8793
private TranslogWriter(
8894
final ShardId shardId,
8995
final Checkpoint initialCheckpoint,
@@ -127,6 +133,7 @@ private TranslogWriter(
127133
this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null;
128134
this.tragedy = tragedy;
129135
this.operationListener = operationListener;
136+
this.lastModifiedTimeCache = new LastModifiedTimeCache(-1, -1, -1);
130137
}
131138

132139
public static TranslogWriter create(
@@ -642,4 +649,13 @@ public final void close() throws IOException {
642649
protected final boolean isClosed() {
643650
return closed.get();
644651
}
652+
653+
@Override
654+
public long getLastModifiedTime() throws IOException {
655+
if (lastModifiedTimeCache.totalOffset() != totalOffset || lastModifiedTimeCache.syncedOffset() != lastSyncedCheckpoint.offset) {
656+
long mtime = super.getLastModifiedTime();
657+
lastModifiedTimeCache = new LastModifiedTimeCache(mtime, totalOffset, lastSyncedCheckpoint.offset);
658+
}
659+
return lastModifiedTimeCache.lastModifiedTime();
660+
}
645661
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,40 @@ ChannelFactory getChannelFactory() {
15861586
}
15871587
}
15881588

1589+
public void testTranslogWriterLastModifiedTime() throws IOException {
1590+
Path tempDir = createTempDir();
1591+
try (Translog translog = create(tempDir)) {
1592+
long mtime = translog.getCurrent().getLastModifiedTime();
1593+
TranslogWriter.LastModifiedTimeCache mtimeCache = translog.getCurrent().lastModifiedTimeCache;
1594+
// no ops
1595+
long lastMtime = translog.getCurrent().getLastModifiedTime();
1596+
TranslogWriter.LastModifiedTimeCache lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
1597+
assertThat(lastMtime, equalTo(mtime));
1598+
assertEquals(lastMtimeCache, mtimeCache);
1599+
1600+
mtime = lastMtime;
1601+
mtimeCache = lastMtimeCache;
1602+
// add ops
1603+
int count = randomIntBetween(1, 100);
1604+
for (int i = 0; i < count; i++) {
1605+
translog.add(indexOp(randomAlphaOfLength(128), i, primaryTerm.get()));
1606+
}
1607+
lastMtime = translog.getCurrent().getLastModifiedTime();
1608+
lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
1609+
assertThat(lastMtime, greaterThanOrEqualTo(mtime));
1610+
assertThat(lastMtimeCache.totalOffset(), greaterThan(mtimeCache.totalOffset()));
1611+
1612+
mtime = lastMtime;
1613+
mtimeCache = lastMtimeCache;
1614+
// sync ops
1615+
translog.sync();
1616+
lastMtime = translog.getCurrent().getLastModifiedTime();
1617+
lastMtimeCache = translog.getCurrent().lastModifiedTimeCache;
1618+
assertThat(lastMtime, greaterThanOrEqualTo(mtime));
1619+
assertThat(lastMtimeCache.syncedOffset(), greaterThan(mtimeCache.syncedOffset()));
1620+
}
1621+
}
1622+
15891623
public void testTranslogOperationListener() throws IOException {
15901624
Path tempDir = createTempDir();
15911625
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT).build();

0 commit comments

Comments
 (0)