Skip to content

Commit f3b3297

Browse files
authored
Sets the new engine before closing the previous one in IndexShard.resetEngine (#122255)
Current code closes the previous engine instance, creates the new engine and then updates the currentEngineReference: IOUtils.close(currentEngine); var newEngine = createEngine(engineConfig); currentEngineReference.set(newEngine); This leaves more room for callers of getEngineOrNull() to pick a closed instance. Instead we can create the new engine first and atomically update the reference to the new instance.
1 parent 49eccbe commit f3b3297

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4313,17 +4313,15 @@ public void resetEngine() {
43134313
assert waitForEngineOrClosedShardListeners.isDone();
43144314
try {
43154315
synchronized (engineMutex) {
4316-
final var currentEngine = getEngine();
4317-
currentEngine.prepareForEngineReset();
4318-
var engineConfig = newEngineConfig(replicationTracker);
43194316
verifyNotClosed();
4320-
IOUtils.close(currentEngine);
4321-
var newEngine = createEngine(engineConfig);
4322-
currentEngineReference.set(newEngine);
4317+
getEngine().prepareForEngineReset();
4318+
var newEngine = createEngine(newEngineConfig(replicationTracker));
4319+
IOUtils.close(currentEngineReference.getAndSet(newEngine));
43234320
onNewEngine(newEngine);
43244321
}
43254322
onSettingsChanged();
43264323
} catch (Exception e) {
4324+
// we want to fail the shard in the case prepareForEngineReset throws
43274325
failShard("unable to reset engine", e);
43284326
}
43294327
}

server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4563,11 +4563,9 @@ public void testResetEngine() throws Exception {
45634563
var newEngineCreated = new CountDownLatch(2);
45644564
var indexShard = newStartedShard(true, Settings.EMPTY, config -> {
45654565
try {
4566-
return new ReadOnlyEngine(config, null, null, true, Function.identity(), true, true) {
4566+
return new ReadOnlyEngine(config, null, new TranslogStats(), false, Function.identity(), true, true) {
45674567
@Override
4568-
public void prepareForEngineReset() throws IOException {
4569-
;
4570-
}
4568+
public void prepareForEngineReset() throws IOException {}
45714569
};
45724570
} finally {
45734571
newEngineCreated.countDown();

0 commit comments

Comments
 (0)