Skip to content

Commit c064b50

Browse files
authored
Encapsulate Translog in Engine (#31220)
This removes the abstract `getTranslog` method in `Engine`, instead leaving it to the abstract implementations of the other methods that use the translog. This allows future Engines not to have a Translog, as instead they must implement the methods that use the translog pieces to return necessary values.
1 parent 99e0458 commit c064b50

File tree

4 files changed

+62
-40
lines changed

4 files changed

+62
-40
lines changed

server/src/main/java/org/elasticsearch/index/engine/Engine.java

+8-30
Original file line numberDiff line numberDiff line change
@@ -565,18 +565,10 @@ public enum SearcherScope {
565565
EXTERNAL, INTERNAL
566566
}
567567

568-
/**
569-
* Returns the translog associated with this engine.
570-
* Prefer to keep the translog package-private, so that an engine can control all accesses to the translog.
571-
*/
572-
abstract Translog getTranslog();
573-
574568
/**
575569
* Checks if the underlying storage sync is required.
576570
*/
577-
public boolean isTranslogSyncNeeded() {
578-
return getTranslog().syncNeeded();
579-
}
571+
public abstract boolean isTranslogSyncNeeded();
580572

581573
/**
582574
* Ensures that all locations in the given stream have been written to the underlying storage.
@@ -585,35 +577,25 @@ public boolean isTranslogSyncNeeded() {
585577

586578
public abstract void syncTranslog() throws IOException;
587579

588-
public Closeable acquireTranslogRetentionLock() {
589-
return getTranslog().acquireRetentionLock();
590-
}
580+
public abstract Closeable acquireTranslogRetentionLock();
591581

592582
/**
593583
* Creates a new translog snapshot from this engine for reading translog operations whose seq# at least the provided seq#.
594584
* The caller has to close the returned snapshot after finishing the reading.
595585
*/
596-
public Translog.Snapshot newTranslogSnapshotFromMinSeqNo(long minSeqNo) throws IOException {
597-
return getTranslog().newSnapshotFromMinSeqNo(minSeqNo);
598-
}
586+
public abstract Translog.Snapshot newTranslogSnapshotFromMinSeqNo(long minSeqNo) throws IOException;
599587

600588
/**
601589
* Returns the estimated number of translog operations in this engine whose seq# at least the provided seq#.
602590
*/
603-
public int estimateTranslogOperationsFromMinSeq(long minSeqNo) {
604-
return getTranslog().estimateTotalOperationsFromMinSeq(minSeqNo);
605-
}
591+
public abstract int estimateTranslogOperationsFromMinSeq(long minSeqNo);
606592

607-
public TranslogStats getTranslogStats() {
608-
return getTranslog().stats();
609-
}
593+
public abstract TranslogStats getTranslogStats();
610594

611595
/**
612596
* Returns the last location that the translog of this engine has written into.
613597
*/
614-
public Translog.Location getTranslogLastWriteLocation() {
615-
return getTranslog().getLastWriteLocation();
616-
}
598+
public abstract Translog.Location getTranslogLastWriteLocation();
617599

618600
protected final void ensureOpen(Exception suppressed) {
619601
if (isClosed.get()) {
@@ -661,9 +643,7 @@ public CommitStats commitStats() {
661643
/**
662644
* Returns the latest global checkpoint value that has been persisted in the underlying storage (i.e. translog's checkpoint)
663645
*/
664-
public long getLastSyncedGlobalCheckpoint() {
665-
return getTranslog().getLastSyncedGlobalCheckpoint();
666-
}
646+
public abstract long getLastSyncedGlobalCheckpoint();
667647

668648
/**
669649
* Global stats on segments.
@@ -935,9 +915,7 @@ public final boolean refreshNeeded() {
935915
*
936916
* @return {@code true} if the current generation should be rolled to a new generation
937917
*/
938-
public boolean shouldRollTranslogGeneration() {
939-
return getTranslog().shouldRollGeneration();
940-
}
918+
public abstract boolean shouldRollTranslogGeneration();
941919

942920
/**
943921
* Rolls the translog generation and cleans unneeded.

server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@
7373
import org.elasticsearch.index.translog.TranslogConfig;
7474
import org.elasticsearch.index.translog.TranslogCorruptedException;
7575
import org.elasticsearch.index.translog.TranslogDeletionPolicy;
76+
import org.elasticsearch.index.translog.TranslogStats;
7677
import org.elasticsearch.threadpool.ThreadPool;
7778

79+
import java.io.Closeable;
7880
import java.io.IOException;
7981
import java.util.Arrays;
8082
import java.util.Collection;
@@ -422,12 +424,17 @@ private Translog openTranslog(EngineConfig engineConfig, TranslogDeletionPolicy
422424
return new Translog(translogConfig, translogUUID, translogDeletionPolicy, globalCheckpointSupplier, engineConfig.getPrimaryTermSupplier());
423425
}
424426

425-
@Override
427+
// Package private for testing purposes only
426428
Translog getTranslog() {
427429
ensureOpen();
428430
return translog;
429431
}
430432

433+
@Override
434+
public boolean isTranslogSyncNeeded() {
435+
return getTranslog().syncNeeded();
436+
}
437+
431438
@Override
432439
public boolean ensureTranslogSynced(Stream<Translog.Location> locations) throws IOException {
433440
final boolean synced = translog.ensureSynced(locations);
@@ -443,6 +450,31 @@ public void syncTranslog() throws IOException {
443450
revisitIndexDeletionPolicyOnTranslogSynced();
444451
}
445452

453+
@Override
454+
public Closeable acquireTranslogRetentionLock() {
455+
return getTranslog().acquireRetentionLock();
456+
}
457+
458+
@Override
459+
public Translog.Snapshot newTranslogSnapshotFromMinSeqNo(long minSeqNo) throws IOException {
460+
return getTranslog().newSnapshotFromMinSeqNo(minSeqNo);
461+
}
462+
463+
@Override
464+
public int estimateTranslogOperationsFromMinSeq(long minSeqNo) {
465+
return getTranslog().estimateTotalOperationsFromMinSeq(minSeqNo);
466+
}
467+
468+
@Override
469+
public TranslogStats getTranslogStats() {
470+
return getTranslog().stats();
471+
}
472+
473+
@Override
474+
public Translog.Location getTranslogLastWriteLocation() {
475+
return getTranslog().getLastWriteLocation();
476+
}
477+
446478
private void revisitIndexDeletionPolicyOnTranslogSynced() throws IOException {
447479
if (combinedDeletionPolicy.hasUnreferencedCommits()) {
448480
indexWriter.deleteUnusedFiles();
@@ -1570,6 +1602,11 @@ public void trimUnreferencedTranslogFiles() throws EngineException {
15701602
}
15711603
}
15721604

1605+
@Override
1606+
public boolean shouldRollTranslogGeneration() {
1607+
return getTranslog().shouldRollGeneration();
1608+
}
1609+
15731610
@Override
15741611
public void trimOperationsFromTranslog(long belowTerm, long aboveSeqNo) throws EngineException {
15751612
try (ReleasableLock lock = readLock.acquire()) {
@@ -2191,6 +2228,11 @@ LocalCheckpointTracker getLocalCheckpointTracker() {
21912228
return localCheckpointTracker;
21922229
}
21932230

2231+
@Override
2232+
public long getLastSyncedGlobalCheckpoint() {
2233+
return getTranslog().getLastSyncedGlobalCheckpoint();
2234+
}
2235+
21942236
@Override
21952237
public long getLocalCheckpoint() {
21962238
return localCheckpointTracker.getCheckpoint();

server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ protected void commitIndexWriter(IndexWriter writer, Translog translog, String s
731731
super.commitIndexWriter(writer, translog, syncId);
732732
}
733733
};
734-
assertThat(recoveringEngine.getTranslog().stats().getUncommittedOperations(), equalTo(docs));
734+
assertThat(getTranslog(recoveringEngine).stats().getUncommittedOperations(), equalTo(docs));
735735
recoveringEngine.recoverFromTranslog();
736736
assertTrue(committed.get());
737737
} finally {
@@ -758,7 +758,7 @@ public void testTranslogRecoveryWithMultipleGenerations() throws IOException {
758758
final ParsedDocument doc = testParsedDocument(id, null, testDocumentWithTextField(), SOURCE, null);
759759
initialEngine.index(indexForDoc(doc));
760760
if (rarely()) {
761-
initialEngine.getTranslog().rollGeneration();
761+
getTranslog(initialEngine).rollGeneration();
762762
} else if (rarely()) {
763763
initialEngine.flush();
764764
}
@@ -3983,14 +3983,14 @@ public void testFillUpSequenceIdGapsOnRecovery() throws IOException {
39833983
assertEquals(checkpointOnReplica, replicaEngine.getLocalCheckpoint());
39843984
trimUnsafeCommits(copy(replicaEngine.config(), globalCheckpoint::get));
39853985
recoveringEngine = new InternalEngine(copy(replicaEngine.config(), globalCheckpoint::get));
3986-
assertEquals(numDocsOnReplica, recoveringEngine.getTranslog().stats().getUncommittedOperations());
3986+
assertEquals(numDocsOnReplica, getTranslog(recoveringEngine).stats().getUncommittedOperations());
39873987
recoveringEngine.recoverFromTranslog();
39883988
assertEquals(maxSeqIDOnReplica, recoveringEngine.getSeqNoStats(-1).getMaxSeqNo());
39893989
assertEquals(checkpointOnReplica, recoveringEngine.getLocalCheckpoint());
39903990
assertEquals((maxSeqIDOnReplica + 1) - numDocsOnReplica, recoveringEngine.fillSeqNoGaps(2));
39913991

39923992
// now snapshot the tlog and ensure the primary term is updated
3993-
try (Translog.Snapshot snapshot = recoveringEngine.getTranslog().newSnapshot()) {
3993+
try (Translog.Snapshot snapshot = getTranslog(recoveringEngine).newSnapshot()) {
39943994
assertTrue((maxSeqIDOnReplica + 1) - numDocsOnReplica <= snapshot.totalOperations());
39953995
Translog.Operation operation;
39963996
while ((operation = snapshot.next()) != null) {
@@ -4005,7 +4005,7 @@ public void testFillUpSequenceIdGapsOnRecovery() throws IOException {
40054005
assertEquals(maxSeqIDOnReplica, recoveringEngine.getLocalCheckpoint());
40064006
if ((flushed = randomBoolean())) {
40074007
globalCheckpoint.set(recoveringEngine.getSeqNoStats(-1).getMaxSeqNo());
4008-
recoveringEngine.getTranslog().sync();
4008+
getTranslog(recoveringEngine).sync();
40094009
recoveringEngine.flush(true, true);
40104010
}
40114011
}
@@ -4018,7 +4018,7 @@ public void testFillUpSequenceIdGapsOnRecovery() throws IOException {
40184018
trimUnsafeCommits(replicaEngine.config());
40194019
recoveringEngine = new InternalEngine(copy(replicaEngine.config(), globalCheckpoint::get));
40204020
if (flushed) {
4021-
assertThat(recoveringEngine.getTranslog().stats().getUncommittedOperations(), equalTo(0));
4021+
assertThat(recoveringEngine.getTranslogStats().getUncommittedOperations(), equalTo(0));
40224022
}
40234023
recoveringEngine.recoverFromTranslog();
40244024
assertEquals(maxSeqIDOnReplica, recoveringEngine.getSeqNoStats(-1).getMaxSeqNo());
@@ -4221,7 +4221,7 @@ protected void commitIndexWriter(IndexWriter writer, Translog translog, String s
42214221
engine.index(indexForDoc(testParsedDocument(Integer.toString(docId), null, document, B_1, null)));
42224222
if (frequently()) {
42234223
globalCheckpoint.set(randomLongBetween(globalCheckpoint.get(), engine.getLocalCheckpoint()));
4224-
engine.getTranslog().sync();
4224+
engine.syncTranslog();
42254225
}
42264226
if (frequently()) {
42274227
final long lastSyncedGlobalCheckpoint = Translog.readGlobalCheckpoint(translogPath, translogUUID);
@@ -4243,7 +4243,7 @@ protected void commitIndexWriter(IndexWriter writer, Translog translog, String s
42434243
}
42444244
// Make sure we keep all translog operations after the local checkpoint of the safe commit.
42454245
long localCheckpointFromSafeCommit = Long.parseLong(safeCommit.getUserData().get(SequenceNumbers.LOCAL_CHECKPOINT_KEY));
4246-
try (Translog.Snapshot snapshot = engine.getTranslog().newSnapshot()) {
4246+
try (Translog.Snapshot snapshot = getTranslog(engine).newSnapshot()) {
42474247
assertThat(snapshot, SnapshotMatchers.containsSeqNoRange(localCheckpointFromSafeCommit + 1, docId));
42484248
}
42494249
}

test/framework/src/main/java/org/elasticsearch/index/engine/EngineTestCase.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,8 @@ protected Engine.Delete replicaDeleteForDoc(String id, long version, long seqNo,
507507
* Exposes a translog associated with the given engine for testing purpose.
508508
*/
509509
public static Translog getTranslog(Engine engine) {
510-
return engine.getTranslog();
510+
assert engine instanceof InternalEngine : "only InternalEngines have translogs, got: " + engine.getClass();
511+
InternalEngine internalEngine = (InternalEngine) engine;
512+
return internalEngine.getTranslog();
511513
}
512514
}

0 commit comments

Comments
 (0)