-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RemoteDirectory instance to Store as a secondary directory
Signed-off-by: Sachin Kale <kalsac@amazon.com>
- Loading branch information
Sachin Kale
committed
May 11, 2022
1 parent
bcfd328
commit ce5f6cd
Showing
9 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
server/src/main/java/org/opensearch/index/store/RemoteDirectoryFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.store; | ||
|
||
import org.apache.lucene.store.Directory; | ||
import org.opensearch.common.blobstore.BlobContainer; | ||
import org.opensearch.common.blobstore.BlobPath; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.shard.ShardPath; | ||
import org.opensearch.plugins.IndexStorePlugin; | ||
import org.opensearch.repositories.Repository; | ||
import org.opensearch.repositories.blobstore.BlobStoreRepository; | ||
|
||
import java.io.IOException; | ||
|
||
public class RemoteDirectoryFactory implements IndexStorePlugin.RemoteDirectoryFactory { | ||
|
||
@Override | ||
public Directory newDirectory(IndexSettings indexSettings, ShardPath path, Repository repository) throws IOException { | ||
assert repository instanceof BlobStoreRepository : "repository should be instance of BlobStoreRepository"; | ||
BlobPath blobPath = new BlobPath(); | ||
blobPath = blobPath.add(indexSettings.getIndex().getName()).add(String.valueOf(path.getShardId().getId())); | ||
BlobContainer blobContainer = ((BlobStoreRepository) repository).blobStore().blobContainer(blobPath); | ||
return new RemoteDirectory(blobContainer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
server/src/test/java/org/opensearch/index/store/RemoteDirectoryFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.index.store; | ||
|
||
import org.apache.lucene.store.Directory; | ||
import org.junit.Before; | ||
import org.mockito.ArgumentCaptor; | ||
import org.opensearch.common.blobstore.BlobContainer; | ||
import org.opensearch.common.blobstore.BlobPath; | ||
import org.opensearch.common.blobstore.BlobStore; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.index.IndexSettings; | ||
import org.opensearch.index.shard.ShardId; | ||
import org.opensearch.index.shard.ShardPath; | ||
import org.opensearch.repositories.blobstore.BlobStoreRepository; | ||
import org.opensearch.test.IndexSettingsModule; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class RemoteDirectoryFactoryTests extends OpenSearchTestCase { | ||
|
||
private RemoteDirectoryFactory remoteDirectoryFactory; | ||
|
||
@Before | ||
public void setup() { | ||
remoteDirectoryFactory = new RemoteDirectoryFactory(); | ||
} | ||
|
||
public void testNewDirectory() throws IOException { | ||
Settings settings = Settings.builder().build(); | ||
IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); | ||
Path tempDir = createTempDir().resolve(indexSettings.getUUID()).resolve("0"); | ||
ShardPath shardPath = new ShardPath(false, tempDir, tempDir, new ShardId(indexSettings.getIndex(), 0)); | ||
BlobStoreRepository repository = mock(BlobStoreRepository.class); | ||
BlobStore blobStore = mock(BlobStore.class); | ||
BlobContainer blobContainer = mock(BlobContainer.class); | ||
when(repository.blobStore()).thenReturn(blobStore); | ||
when(blobStore.blobContainer(any())).thenReturn(blobContainer); | ||
when(blobContainer.listBlobs()).thenReturn(Collections.emptyMap()); | ||
|
||
Directory directory = remoteDirectoryFactory.newDirectory(indexSettings, shardPath, repository); | ||
assertTrue(directory instanceof RemoteDirectory); | ||
ArgumentCaptor<BlobPath> blobPathCaptor = ArgumentCaptor.forClass(BlobPath.class); | ||
verify(blobStore).blobContainer(blobPathCaptor.capture()); | ||
BlobPath blobPath = blobPathCaptor.getValue(); | ||
assertEquals("foo/0/", blobPath.buildAsString()); | ||
|
||
directory.listAll(); | ||
verify(blobContainer).listBlobs(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters