Skip to content

Commit 5f7baf9

Browse files
author
Sachin Kale
committed
Open the index to be restored as part of restore flow
Signed-off-by: Sachin Kale <[email protected]>
1 parent e5809d0 commit 5f7baf9

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

server/src/main/java/org/opensearch/index/IndexService.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,7 @@ public synchronized IndexShard createShard(
514514
this.indexSettings,
515515
path
516516
);
517-
remoteStore = new Store(
518-
shardId,
519-
this.indexSettings,
520-
remoteDirectory,
521-
lock,
522-
new StoreCloseListener(shardId, () -> eventListener.onStoreClosed(shardId))
523-
);
517+
remoteStore = new Store(shardId, this.indexSettings, remoteDirectory, lock, Store.OnClose.EMPTY);
524518
}
525519

526520
Directory directory = directoryFactory.newDirectory(this.indexSettings, path);

server/src/main/java/org/opensearch/snapshots/RestoreService.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,35 @@ public ClusterState execute(ClusterState currentState) {
227227
logger.warn("Remote store restore is not supported for non-existent index. Skipping: {}", index);
228228
continue;
229229
}
230-
if (currentIndexMetadata.getState() != IndexMetadata.State.CLOSE) {
231-
throw new IllegalStateException(
232-
"cannot restore index ["
233-
+ index
234-
+ "] because an open index "
235-
+ "with same name already exists in the cluster. Close the existing index"
236-
);
237-
}
238230
if (currentIndexMetadata.getSettings().getAsBoolean(SETTING_REMOTE_STORE, false)) {
239-
IndexId indexId = new IndexId(index, currentIndexMetadata.getIndexUUID());
231+
if (currentIndexMetadata.getState() != IndexMetadata.State.CLOSE) {
232+
throw new IllegalStateException(
233+
"cannot restore index ["
234+
+ index
235+
+ "] because an open index "
236+
+ "with same name already exists in the cluster. Close the existing index"
237+
);
238+
}
239+
IndexMetadata updatedIndexMetadata = IndexMetadata.builder(currentIndexMetadata)
240+
.state(IndexMetadata.State.OPEN)
241+
.version(1 + currentIndexMetadata.getVersion())
242+
.mappingVersion(1 + currentIndexMetadata.getMappingVersion())
243+
.settingsVersion(1 + currentIndexMetadata.getSettingsVersion())
244+
.aliasesVersion(1 + currentIndexMetadata.getAliasesVersion())
245+
.build();
246+
247+
IndexId indexId = new IndexId(index, updatedIndexMetadata.getIndexUUID());
240248

241249
RemoteStoreRecoverySource recoverySource = new RemoteStoreRecoverySource(
242250
restoreUUID,
243-
currentIndexMetadata.getCreationVersion(),
251+
updatedIndexMetadata.getCreationVersion(),
244252
indexId
245253
);
246-
rtBuilder.addAsRemoteStoreRestore(currentIndexMetadata, recoverySource);
254+
rtBuilder.addAsRemoteStoreRestore(updatedIndexMetadata, recoverySource);
255+
blocks.updateBlocks(updatedIndexMetadata);
256+
mdBuilder.put(updatedIndexMetadata, true);
247257
indicesToBeRestored.add(index);
248-
totalShards += currentIndexMetadata.getNumberOfShards();
258+
totalShards += updatedIndexMetadata.getNumberOfShards();
249259
} else {
250260
logger.warn("Remote store is not enabled for index: {}", index);
251261
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.apache.lucene.store.Directory;
4545
import org.apache.lucene.store.FilterDirectory;
4646
import org.apache.lucene.store.IOContext;
47+
import org.apache.lucene.tests.mockfile.ExtrasFS;
4748
import org.apache.lucene.tests.store.BaseDirectoryWrapper;
4849
import org.apache.lucene.util.BytesRef;
4950
import org.apache.lucene.util.Constants;
@@ -2691,8 +2692,10 @@ public void testRestoreShardFromRemoteStore() throws IOException {
26912692
Directory remoteDirectory = ((FilterDirectory) ((FilterDirectory) target.remoteStore().directory()).getDelegate()).getDelegate();
26922693
((BaseDirectoryWrapper) remoteDirectory).setCheckIndexOnClose(false);
26932694

2695+
// extra0 file is added as a part of https://lucene.apache.org/core/7_2_1/test-framework/org/apache/lucene/mockfile/ExtrasFS.html
2696+
// Safe to remove without impacting the test
26942697
for (String file : remoteDirectory.listAll()) {
2695-
if (file.equals("extra0")) {
2698+
if (ExtrasFS.isExtra(file)) {
26962699
remoteDirectory.deleteFile(file);
26972700
}
26982701
}

0 commit comments

Comments
 (0)