Skip to content

Commit b826516

Browse files
authored
Increase store ref before snapshotting index commit (#84776)
Snapshotted commits should also hold a reference to the store, so they are always usable; otherwise, callers need to manage the store's references manually. This change applies only to InternalEngine as we already do this in ReadOnlyEngine.
1 parent 5272689 commit b826516

File tree

5 files changed

+55
-28
lines changed

5 files changed

+55
-28
lines changed

docs/changelog/84776.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 84776
2+
summary: Increase store ref before snapshotting index commit
3+
area: Engine
4+
type: bug
5+
issues: []

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,14 +2169,40 @@ public IndexCommitRef acquireLastIndexCommit(final boolean flushFirst) throws En
21692169
flush(false, true);
21702170
logger.trace("finish flush for snapshot");
21712171
}
2172-
final IndexCommit lastCommit = combinedDeletionPolicy.acquireIndexCommit(false);
2173-
return new Engine.IndexCommitRef(lastCommit, () -> releaseIndexCommit(lastCommit));
2172+
store.incRef();
2173+
boolean success = false;
2174+
try {
2175+
final IndexCommit lastCommit = combinedDeletionPolicy.acquireIndexCommit(false);
2176+
final IndexCommitRef commitRef = new IndexCommitRef(
2177+
lastCommit,
2178+
() -> IOUtils.close(() -> releaseIndexCommit(lastCommit), store::decRef)
2179+
);
2180+
success = true;
2181+
return commitRef;
2182+
} finally {
2183+
if (success == false) {
2184+
store.decRef();
2185+
}
2186+
}
21742187
}
21752188

21762189
@Override
21772190
public IndexCommitRef acquireSafeIndexCommit() throws EngineException {
2178-
final IndexCommit safeCommit = combinedDeletionPolicy.acquireIndexCommit(true);
2179-
return new Engine.IndexCommitRef(safeCommit, () -> releaseIndexCommit(safeCommit));
2191+
store.incRef();
2192+
boolean success = false;
2193+
try {
2194+
final IndexCommit safeCommit = combinedDeletionPolicy.acquireIndexCommit(true);
2195+
final IndexCommitRef commitRef = new IndexCommitRef(
2196+
safeCommit,
2197+
() -> IOUtils.close(() -> releaseIndexCommit(safeCommit), store::decRef)
2198+
);
2199+
success = true;
2200+
return commitRef;
2201+
} finally {
2202+
if (success == false) {
2203+
store.decRef();
2204+
}
2205+
}
21802206
}
21812207

21822208
private void releaseIndexCommit(IndexCommit snapshot) throws IOException {

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

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,16 @@
2222
import java.io.Closeable;
2323
import java.io.IOException;
2424
import java.util.Collection;
25-
import java.util.concurrent.atomic.AtomicBoolean;
2625

2726
final class LocalShardSnapshot implements Closeable {
2827
private final IndexShard shard;
2928
private final Store store;
3029
private final Engine.IndexCommitRef indexCommit;
31-
private final AtomicBoolean closed = new AtomicBoolean(false);
3230

3331
LocalShardSnapshot(IndexShard shard) {
3432
this.shard = shard;
35-
store = shard.store();
36-
store.incRef();
37-
boolean success = false;
38-
try {
39-
indexCommit = shard.acquireLastIndexCommit(true);
40-
success = true;
41-
} finally {
42-
if (success == false) {
43-
store.decRef();
44-
}
45-
}
33+
this.store = shard.store();
34+
this.indexCommit = shard.acquireLastIndexCommit(true);
4635
}
4736

4837
Index getIndex() {
@@ -110,13 +99,7 @@ public void close() throws IOException {
11099

111100
@Override
112101
public void close() throws IOException {
113-
if (closed.compareAndSet(false, true)) {
114-
try {
115-
indexCommit.close();
116-
} finally {
117-
store.decRef();
118-
}
119-
}
102+
indexCommit.close();
120103
}
121104

122105
IndexMetadata getIndexMetadata() {

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5664,6 +5664,7 @@ public void testAcquireIndexCommit() throws Exception {
56645664
final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED);
56655665
final Engine.IndexCommitRef snapshot;
56665666
final boolean closeSnapshotBeforeEngine = randomBoolean();
5667+
final int expectedDocs;
56675668
try (InternalEngine engine = createEngine(store, createTempDir(), globalCheckpoint::get)) {
56685669
int numDocs = between(1, 20);
56695670
for (int i = 0; i < numDocs; i++) {
@@ -5679,6 +5680,7 @@ public void testAcquireIndexCommit() throws Exception {
56795680
} else {
56805681
snapshot = engine.acquireLastIndexCommit(flushFirst);
56815682
}
5683+
expectedDocs = flushFirst && safeCommit == false ? numDocs : 0;
56825684
int moreDocs = between(1, 20);
56835685
for (int i = 0; i < moreDocs; i++) {
56845686
index(engine, numDocs + i);
@@ -5687,7 +5689,7 @@ public void testAcquireIndexCommit() throws Exception {
56875689
engine.flush();
56885690
// check that we can still read the commit that we captured
56895691
try (IndexReader reader = DirectoryReader.open(snapshot.getIndexCommit())) {
5690-
assertThat(reader.numDocs(), equalTo(flushFirst && safeCommit == false ? numDocs : 0));
5692+
assertThat(reader.numDocs(), equalTo(expectedDocs));
56915693
}
56925694
assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(2));
56935695

@@ -5699,8 +5701,17 @@ public void testAcquireIndexCommit() throws Exception {
56995701
}
57005702
}
57015703

5704+
if (randomBoolean()) {
5705+
IOUtils.close(store);
5706+
}
5707+
57025708
if (closeSnapshotBeforeEngine == false) {
5703-
snapshot.close(); // shouldn't throw AlreadyClosedException
5709+
// check that we can still read the commit that we captured
5710+
try (DirectoryReader reader = DirectoryReader.open(snapshot.getIndexCommit())) {
5711+
assertThat(reader.numDocs(), equalTo(expectedDocs));
5712+
} finally {
5713+
snapshot.close();
5714+
}
57045715
}
57055716
}
57065717

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/repository/CcrRestoreSourceServiceTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,10 @@ public void testGetSessionReader() throws IOException {
167167

168168
byte[] expectedBytes = new byte[(int) fileMetadata.length()];
169169
byte[] actualBytes = new byte[(int) fileMetadata.length()];
170-
Engine.IndexCommitRef indexCommitRef = indexShard1.acquireSafeIndexCommit();
171-
try (IndexInput indexInput = indexCommitRef.getIndexCommit().getDirectory().openInput(fileName, IOContext.READONCE)) {
170+
try (
171+
Engine.IndexCommitRef indexCommitRef = indexShard1.acquireSafeIndexCommit();
172+
IndexInput indexInput = indexCommitRef.getIndexCommit().getDirectory().openInput(fileName, IOContext.READONCE)
173+
) {
172174
indexInput.seek(0);
173175
indexInput.readBytes(expectedBytes, 0, (int) fileMetadata.length());
174176
}

0 commit comments

Comments
 (0)