Skip to content

Commit 84fd39f

Browse files
authored
Separate acquiring safe commit and last commit (#28271)
Previously we introduced a new parameter to `acquireIndexCommit` to allow acquire either a safe commit or a last commit. However with the new parameters, callers can provide a nonsense combination - flush first but acquire the safe commit. This commit separates acquireIndexCommit method into two different methods to avoid that problem. Moreover, this change should also improve the readability. Relates #28038
1 parent 3059862 commit 84fd39f

File tree

11 files changed

+51
-24
lines changed

11 files changed

+51
-24
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,17 @@ public void forceMerge(boolean flush) throws IOException {
868868
public abstract void forceMerge(boolean flush, int maxNumSegments, boolean onlyExpungeDeletes, boolean upgrade, boolean upgradeOnlyAncientSegments) throws EngineException, IOException;
869869

870870
/**
871-
* Snapshots the index and returns a handle to it. If needed will try and "commit" the
871+
* Snapshots the most recent index and returns a handle to it. If needed will try and "commit" the
872872
* lucene index to make sure we have a "fresh" copy of the files to snapshot.
873873
*
874-
* @param safeCommit indicates whether the engine should acquire the most recent safe commit, or the most recent commit.
875874
* @param flushFirst indicates whether the engine should flush before returning the snapshot
876875
*/
877-
public abstract IndexCommitRef acquireIndexCommit(boolean safeCommit, boolean flushFirst) throws EngineException;
876+
public abstract IndexCommitRef acquireLastIndexCommit(boolean flushFirst) throws EngineException;
877+
878+
/**
879+
* Snapshots the most recent safe index commit from the engine.
880+
*/
881+
public abstract IndexCommitRef acquireSafeIndexCommit() throws EngineException;
878882

879883
/**
880884
* fail engine due to some error. the engine will also be closed.

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,16 +1719,22 @@ public void forceMerge(final boolean flush, int maxNumSegments, boolean onlyExpu
17191719
}
17201720

17211721
@Override
1722-
public IndexCommitRef acquireIndexCommit(final boolean safeCommit, final boolean flushFirst) throws EngineException {
1722+
public IndexCommitRef acquireLastIndexCommit(final boolean flushFirst) throws EngineException {
17231723
// we have to flush outside of the readlock otherwise we might have a problem upgrading
17241724
// the to a write lock when we fail the engine in this operation
17251725
if (flushFirst) {
17261726
logger.trace("start flush for snapshot");
17271727
flush(false, true);
17281728
logger.trace("finish flush for snapshot");
17291729
}
1730-
final IndexCommit snapshotCommit = combinedDeletionPolicy.acquireIndexCommit(safeCommit);
1731-
return new Engine.IndexCommitRef(snapshotCommit, () -> combinedDeletionPolicy.releaseCommit(snapshotCommit));
1730+
final IndexCommit lastCommit = combinedDeletionPolicy.acquireIndexCommit(false);
1731+
return new Engine.IndexCommitRef(lastCommit, () -> combinedDeletionPolicy.releaseCommit(lastCommit));
1732+
}
1733+
1734+
@Override
1735+
public IndexCommitRef acquireSafeIndexCommit() throws EngineException {
1736+
final IndexCommit safeCommit = combinedDeletionPolicy.acquireIndexCommit(true);
1737+
return new Engine.IndexCommitRef(safeCommit, () -> combinedDeletionPolicy.releaseCommit(safeCommit));
17321738
}
17331739

17341740
private boolean failOnTragicEvent(AlreadyClosedException ex) {

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,22 +1077,34 @@ public org.apache.lucene.util.Version minimumCompatibleVersion() {
10771077
}
10781078

10791079
/**
1080-
* Creates a new {@link IndexCommit} snapshot form the currently running engine. All resources referenced by this
1080+
* Creates a new {@link IndexCommit} snapshot from the currently running engine. All resources referenced by this
10811081
* commit won't be freed until the commit / snapshot is closed.
10821082
*
1083-
* @param safeCommit <code>true</code> capture the most recent safe commit point; otherwise the most recent commit point.
10841083
* @param flushFirst <code>true</code> if the index should first be flushed to disk / a low level lucene commit should be executed
10851084
*/
1086-
public Engine.IndexCommitRef acquireIndexCommit(boolean safeCommit, boolean flushFirst) throws EngineException {
1087-
IndexShardState state = this.state; // one time volatile read
1085+
public Engine.IndexCommitRef acquireLastIndexCommit(boolean flushFirst) throws EngineException {
1086+
final IndexShardState state = this.state; // one time volatile read
10881087
// we allow snapshot on closed index shard, since we want to do one after we close the shard and before we close the engine
10891088
if (state == IndexShardState.STARTED || state == IndexShardState.RELOCATED || state == IndexShardState.CLOSED) {
1090-
return getEngine().acquireIndexCommit(safeCommit, flushFirst);
1089+
return getEngine().acquireLastIndexCommit(flushFirst);
10911090
} else {
10921091
throw new IllegalIndexShardStateException(shardId, state, "snapshot is not allowed");
10931092
}
10941093
}
10951094

1095+
/**
1096+
* Snapshots the most recent safe index commit from the currently running engine.
1097+
* All index files referenced by this index commit won't be freed until the commit/snapshot is closed.
1098+
*/
1099+
public Engine.IndexCommitRef acquireSafeIndexCommit() throws EngineException {
1100+
final IndexShardState state = this.state; // one time volatile read
1101+
// we allow snapshot on closed index shard, since we want to do one after we close the shard and before we close the engine
1102+
if (state == IndexShardState.STARTED || state == IndexShardState.RELOCATED || state == IndexShardState.CLOSED) {
1103+
return getEngine().acquireSafeIndexCommit();
1104+
} else {
1105+
throw new IllegalIndexShardStateException(shardId, state, "snapshot is not allowed");
1106+
}
1107+
}
10961108

10971109
/**
10981110
* gets a {@link Store.MetadataSnapshot} for the current directory. This method is safe to call in all lifecycle of the index shard,
@@ -1121,7 +1133,7 @@ public Store.MetadataSnapshot snapshotStoreMetadata() throws IOException {
11211133
return store.getMetadata(null, true);
11221134
}
11231135
}
1124-
indexCommit = engine.acquireIndexCommit(false, false);
1136+
indexCommit = engine.acquireLastIndexCommit(false);
11251137
return store.getMetadata(indexCommit.getIndexCommit());
11261138
} finally {
11271139
store.decRef();

server/src/main/java/org/elasticsearch/index/shard/LocalShardSnapshot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class LocalShardSnapshot implements Closeable {
4848
store.incRef();
4949
boolean success = false;
5050
try {
51-
indexCommit = shard.acquireIndexCommit(false, true);
51+
indexCommit = shard.acquireLastIndexCommit(true);
5252
success = true;
5353
} finally {
5454
if (success == false) {

server/src/main/java/org/elasticsearch/index/store/Store.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ final void ensureOpen() {
238238
*
239239
* {@link #readMetadataSnapshot(Path, ShardId, NodeEnvironment.ShardLocker, Logger)} to read a meta data while locking
240240
* {@link IndexShard#snapshotStoreMetadata()} to safely read from an existing shard
241-
* {@link IndexShard#acquireIndexCommit(boolean, boolean)} to get an {@link IndexCommit} which is safe to use but has to be freed
241+
* {@link IndexShard#acquireLastIndexCommit(boolean)} to get an {@link IndexCommit} which is safe to use but has to be freed
242242
* @param commit the index commit to read the snapshot from or <code>null</code> if the latest snapshot should be read from the
243243
* directory
244244
* @throws CorruptIndexException if the lucene index is corrupted. This can be caused by a checksum mismatch or an
@@ -262,7 +262,7 @@ public MetadataSnapshot getMetadata(IndexCommit commit) throws IOException {
262262
*
263263
* {@link #readMetadataSnapshot(Path, ShardId, NodeEnvironment.ShardLocker, Logger)} to read a meta data while locking
264264
* {@link IndexShard#snapshotStoreMetadata()} to safely read from an existing shard
265-
* {@link IndexShard#acquireIndexCommit(boolean, boolean)} to get an {@link IndexCommit} which is safe to use but has to be freed
265+
* {@link IndexShard#acquireLastIndexCommit(boolean)} to get an {@link IndexCommit} which is safe to use but has to be freed
266266
*
267267
* @param commit the index commit to read the snapshot from or <code>null</code> if the latest snapshot should be read from the
268268
* directory

server/src/main/java/org/elasticsearch/indices/recovery/RecoverySourceHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public RecoveryResponse recoverToTarget() throws IOException {
159159
} else {
160160
final Engine.IndexCommitRef phase1Snapshot;
161161
try {
162-
phase1Snapshot = shard.acquireIndexCommit(true, false);
162+
phase1Snapshot = shard.acquireSafeIndexCommit();
163163
} catch (final Exception e) {
164164
throw new RecoveryEngineException(shard.shardId(), 1, "snapshot failed", e);
165165
}

server/src/main/java/org/elasticsearch/repositories/Repository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ SnapshotInfo finalizeSnapshot(SnapshotId snapshotId, List<IndexId> indices, long
176176
/**
177177
* Creates a snapshot of the shard based on the index commit point.
178178
* <p>
179-
* The index commit point can be obtained by using {@link org.elasticsearch.index.engine.Engine#acquireIndexCommit} method.
179+
* The index commit point can be obtained by using {@link org.elasticsearch.index.engine.Engine#acquireLastIndexCommit} method.
180180
* Repository implementations shouldn't release the snapshot index commit point. It is done by the method caller.
181181
* <p>
182182
* As snapshot process progresses, implementation of this method should update {@link IndexShardSnapshotStatus} object and check

server/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private void snapshot(final IndexShard indexShard, final Snapshot snapshot, fina
390390
final Repository repository = snapshotsService.getRepositoriesService().repository(snapshot.getRepository());
391391
try {
392392
// we flush first to make sure we get the latest writes snapshotted
393-
try (Engine.IndexCommitRef snapshotRef = indexShard.acquireIndexCommit(false, true)) {
393+
try (Engine.IndexCommitRef snapshotRef = indexShard.acquireLastIndexCommit(true)) {
394394
repository.snapshotShard(indexShard, snapshot.getSnapshotId(), indexId, snapshotRef.getIndexCommit(), snapshotStatus);
395395
if (logger.isDebugEnabled()) {
396396
final IndexShardSnapshotStatus.Copy lastSnapshotStatus = snapshotStatus.asCopy();

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ public void testConcurrentWritesAndCommits() throws Exception {
21212121
boolean doneIndexing;
21222122
do {
21232123
doneIndexing = doneLatch.await(sleepTime, TimeUnit.MILLISECONDS);
2124-
commits.add(engine.acquireIndexCommit(false, true));
2124+
commits.add(engine.acquireLastIndexCommit(true));
21252125
if (commits.size() > commitLimit) { // don't keep on piling up too many commits
21262126
IOUtils.close(commits.remove(randomIntBetween(0, commits.size()-1)));
21272127
// we increase the wait time to make sure we eventually if things are slow wait for threads to finish.
@@ -4355,19 +4355,24 @@ public void testAcquireIndexCommit() throws Exception {
43554355
}
43564356
final boolean flushFirst = randomBoolean();
43574357
final boolean safeCommit = randomBoolean();
4358-
Engine.IndexCommitRef commit = engine.acquireIndexCommit(safeCommit, flushFirst);
4358+
final Engine.IndexCommitRef snapshot;
4359+
if (safeCommit) {
4360+
snapshot = engine.acquireSafeIndexCommit();
4361+
} else {
4362+
snapshot = engine.acquireLastIndexCommit(flushFirst);
4363+
}
43594364
int moreDocs = between(1, 20);
43604365
for (int i = 0; i < moreDocs; i++) {
43614366
index(engine, numDocs + i);
43624367
}
43634368
globalCheckpoint.set(numDocs + moreDocs - 1);
43644369
engine.flush();
43654370
// check that we can still read the commit that we captured
4366-
try (IndexReader reader = DirectoryReader.open(commit.getIndexCommit())) {
4371+
try (IndexReader reader = DirectoryReader.open(snapshot.getIndexCommit())) {
43674372
assertThat(reader.numDocs(), equalTo(flushFirst && (safeCommit == false || inSync) ? numDocs : 0));
43684373
}
43694374
assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(2));
4370-
commit.close();
4375+
snapshot.close();
43714376
// check it's clean up
43724377
engine.flush(true, true);
43734378
assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(1));

server/src/test/java/org/elasticsearch/indices/recovery/RecoverySourceHandlerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public void testThrowExceptionOnPrimaryRelocatedBeforePhase1Started() throws IOE
397397
when(shard.seqNoStats()).thenReturn(mock(SeqNoStats.class));
398398
when(shard.segmentStats(anyBoolean())).thenReturn(mock(SegmentsStats.class));
399399
when(shard.state()).thenReturn(IndexShardState.RELOCATED);
400-
when(shard.acquireIndexCommit(anyBoolean(), anyBoolean())).thenReturn(mock(Engine.IndexCommitRef.class));
400+
when(shard.acquireSafeIndexCommit()).thenReturn(mock(Engine.IndexCommitRef.class));
401401
doAnswer(invocation -> {
402402
((ActionListener<Releasable>)invocation.getArguments()[0]).onResponse(() -> {});
403403
return null;

0 commit comments

Comments
 (0)