Skip to content
5 changes: 5 additions & 0 deletions docs/changelog/95107.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 95107
summary: Cache modification time of translog writer file
area: Engine
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {

private final DiskIoBufferPool diskIoBufferPool;

// package private for testing
LastModifiedTimeCache lastModifiedTimeCache;

// package private for testing
record LastModifiedTimeCache(long lastModifiedTime, long totalOffset, long syncedOffset) {}

private TranslogWriter(
final ShardId shardId,
final Checkpoint initialCheckpoint,
Expand Down Expand Up @@ -127,6 +133,7 @@ private TranslogWriter(
this.seenSequenceNumbers = Assertions.ENABLED ? new HashMap<>() : null;
this.tragedy = tragedy;
this.operationListener = operationListener;
this.lastModifiedTimeCache = new LastModifiedTimeCache(-1, -1, -1);
}

public static TranslogWriter create(
Expand Down Expand Up @@ -642,4 +649,13 @@ public final void close() throws IOException {
protected final boolean isClosed() {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,40 @@ ChannelFactory getChannelFactory() {
}
}

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 {
Path tempDir = createTempDir();
final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, org.elasticsearch.Version.CURRENT).build();
Expand Down