Skip to content

Commit 7491c62

Browse files
committed
Nit fixes
Signed-off-by: Shreyansh Ray <rayshrey@amazon.com>
1 parent 5013be9 commit 7491c62

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

server/src/internalClusterTest/java/org/opensearch/remotestore/CompositeDirectoryIT.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.opensearch.index.store.remote.file.CleanerDaemonThreadLeakFilter;
2727
import org.opensearch.indices.IndicesService;
2828
import org.opensearch.test.OpenSearchIntegTestCase;
29-
import org.opensearch.test.junit.annotations.TestLogging;
3029

3130
import java.util.Map;
3231

@@ -36,7 +35,7 @@
3635
@ThreadLeakFilters(filters = CleanerDaemonThreadLeakFilter.class)
3736
@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 1, numClientNodes = 0, supportsDedicatedMasters = false)
3837
// Uncomment the below line to enable trace level logs for this test for better debugging
39-
@TestLogging(reason = "Getting trace logs from composite directory package", value = "org.opensearch.index.store:TRACE")
38+
// @TestLogging(reason = "Getting trace logs from composite directory package", value = "org.opensearch.index.store:TRACE")
4039
public class CompositeDirectoryIT extends RemoteStoreBaseIntegTestCase {
4140

4241
/*

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ public static DataLocalityType getValueOf(final String localityType) {
646646
if (type != null) {
647647
return type;
648648
}
649-
throw new IllegalArgumentException("Unknown Locality Type constant [" + localityType + "].");
649+
throw new IllegalArgumentException("Unknown locality type constant [" + localityType + "].");
650650
}
651651
}
652652

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

+15-6
Original file line numberDiff line numberDiff line change
@@ -618,14 +618,23 @@ public synchronized IndexShard createShard(
618618
// TODO : Need to remove this check after support for hot indices is added in Composite Directory
619619
this.indexSettings.isStoreLocalityPartial()) {
620620
/*
621-
* Currently Composite Directory only supports local directory to be of type FSDirectory
622-
* The reason is that FileCache currently has it key type as Path
623-
* Composite Directory currently uses FSDirectory's getDirectory() method to fetch and use the Path for operating on FileCache
624-
* TODO : Refactor FileCache to have key in form of String instead of Path. Once that is done we can remove this assertion
621+
Currently Composite Directory only supports local directory to be of type FSDirectory
622+
The reason is that FileCache currently has it key type as Path
623+
Composite Directory currently uses FSDirectory's getDirectory() method to fetch and use the Path for operating on FileCache
624+
TODO : Refactor FileCache to have key in form of String instead of Path. Once that is done we can remove this assertion
625625
*/
626626
Directory localDirectory = directoryFactory.newDirectory(this.indexSettings, path);
627-
assert localDirectory instanceof FSDirectory : "For Composite Directory, local directory must be of type FSDirectory";
628-
assert fileCache != null : "File Cache not initialized on this Node, cannot create Composite Directory without FileCache";
627+
628+
if (localDirectory instanceof FSDirectory == false) throw new IllegalStateException(
629+
"For Composite Directory, local directory must be of type FSDirectory"
630+
);
631+
else if (fileCache == null) throw new IllegalStateException(
632+
"File Cache not initialized on this Node, cannot create Composite Directory without FileCache"
633+
);
634+
else if (remoteDirectory == null) throw new IllegalStateException(
635+
"Remote Directory must not be null for Composite Directory"
636+
);
637+
629638
directory = new CompositeDirectory((FSDirectory) localDirectory, (RemoteSegmentStoreDirectory) remoteDirectory, fileCache);
630639
} else {
631640
directory = directoryFactory.newDirectory(this.indexSettings, path);

server/src/main/java/org/opensearch/index/store/CompositeDirectory.java

+6-10
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String[] listAll() throws IOException {
101101

102102
/**
103103
* Removes an existing file in the directory.
104-
* Currently deleting only from local directory as files from remote should not be deleted due to availability reasons
104+
* Currently deleting only from local directory as files from remote should not be deleted as that is taken care by garbage collection logic of remote directory
105105
* @param name the name of an existing file.
106106
* @throws IOException in case of I/O error
107107
*/
@@ -181,6 +181,7 @@ public void rename(String source, String dest) throws IOException {
181181
localDirectory.rename(source, dest);
182182
fileCache.remove(localDirectory.getDirectory().resolve(source));
183183
cacheFile(dest);
184+
fileCache.decRef(localDirectory.getDirectory().resolve(dest));
184185
}
185186

186187
/**
@@ -243,10 +244,6 @@ public void close() throws IOException {
243244
*/
244245
public void afterSyncToRemote(Collection<String> files) throws IOException {
245246
logger.trace("afterSyncToRemote called for {}", files);
246-
if (remoteDirectory == null) {
247-
logger.trace("afterSyncToRemote called even though remote directory is not set");
248-
return;
249-
}
250247
for (String fileName : files) {
251248
/*
252249
Decrementing the refCount here for the path so that it becomes eligible for eviction
@@ -273,10 +270,9 @@ private String[] getRemoteFiles() throws IOException {
273270
remoteFiles = remoteDirectory.listAll();
274271
} catch (NullPointerException e) {
275272
/*
276-
There are two scenarios where the listAll() call on remote directory returns NullPointerException:
277-
- When remote directory is not set
278-
- When init() of remote directory has not yet been called
279-
Returning an empty list in the above scenarios
273+
We can encounter NPE when no data has been uploaded to remote store yet and as a result the metadata is empty
274+
Empty metadata means that there are no files currently in remote, hence returning an empty list in this scenario
275+
TODO : Catch the NPE in listAll of RemoteSegmentStoreDirectory itself instead of catching here
280276
*/
281277
remoteFiles = new String[0];
282278
}
@@ -285,7 +281,7 @@ There are two scenarios where the listAll() call on remote directory returns Nul
285281

286282
private void cacheFile(String name) throws IOException {
287283
Path filePath = localDirectory.getDirectory().resolve(name);
288-
// put will increase the refCount for the path, making sure it is not evicted, wil decrease the ref after it is uploaded to Remote
284+
// put will increase the refCount for the path, making sure it is not evicted, will decrease the ref after it is uploaded to Remote
289285
// so that it can be evicted after that
290286
// this is just a temporary solution, will pin the file once support for that is added in FileCache
291287
// TODO : Pin the above filePath in the file cache once pinning support is added so that it cannot be evicted unless it has been

server/src/main/java/org/opensearch/index/store/remote/filecache/FullFileCachedIndexInput.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
*/
2323
@ExperimentalApi
2424
public class FullFileCachedIndexInput implements CachedIndexInput {
25-
26-
private final IndexInput indexInput;
2725
private final FileCache fileCache;
2826
private final Path path;
2927
private final FileCachedIndexInput fileCachedIndexInput;
@@ -35,7 +33,6 @@ public class FullFileCachedIndexInput implements CachedIndexInput {
3533
public FullFileCachedIndexInput(FileCache fileCache, Path path, IndexInput indexInput) {
3634
this.fileCache = fileCache;
3735
this.path = path;
38-
this.indexInput = indexInput;
3936
fileCachedIndexInput = new FileCachedIndexInput(fileCache, path, indexInput);
4037
isClosed = new AtomicBoolean(false);
4138
}
@@ -54,7 +51,7 @@ public IndexInput getIndexInput() {
5451
*/
5552
@Override
5653
public long length() {
57-
return indexInput.length();
54+
return fileCachedIndexInput.length();
5855
}
5956

6057
/**
@@ -71,7 +68,7 @@ public boolean isClosed() {
7168
@Override
7269
public void close() throws Exception {
7370
if (!isClosed.getAndSet(true)) {
74-
indexInput.close();
71+
fileCachedIndexInput.close();
7572
}
7673
}
7774
}

server/src/test/java/org/opensearch/index/store/CompositeDirectoryTests.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.IOException;
2525
import java.nio.file.Path;
26+
import java.util.Arrays;
2627
import java.util.Collection;
2728
import java.util.List;
2829
import java.util.Map;
@@ -32,6 +33,7 @@
3233
import static org.mockito.ArgumentMatchers.eq;
3334
import static org.mockito.ArgumentMatchers.startsWith;
3435
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.times;
3537
import static org.mockito.Mockito.verify;
3638
import static org.mockito.Mockito.when;
3739

@@ -50,11 +52,16 @@ public void setup() throws IOException {
5052
}
5153

5254
public void testListAll() throws IOException {
55+
when(localDirectory.listAll()).thenReturn(new String[]{});
56+
String[] actualFileNames = compositeDirectory.listAll();
57+
String[] expectedFileNames = new String[] {};
58+
assertArrayEquals(expectedFileNames, actualFileNames);
59+
5360
populateMetadata();
5461
when(localDirectory.listAll()).thenReturn(new String[] { "_1.cfe", "_2.cfe", "_0.cfe_block_7", "_0.cfs_block_7" });
5562

56-
String[] actualFileNames = compositeDirectory.listAll();
57-
String[] expectedFileNames = new String[] { "_0.cfe", "_0.cfs", "_0.si", "_1.cfe", "_2.cfe", "segments_1" };
63+
actualFileNames = compositeDirectory.listAll();
64+
expectedFileNames = new String[] { "_0.cfe", "_0.cfs", "_0.si", "_1.cfe", "_2.cfe", "segments_1" };
5865
assertArrayEquals(expectedFileNames, actualFileNames);
5966
}
6067

@@ -169,4 +176,14 @@ public void testClose() throws IOException {
169176
verify(fileCache).remove(resolvedPath1);
170177
verify(fileCache).remove(resolvedPath2);
171178
}
179+
180+
public void testAfterSyncToRemote() throws IOException {
181+
Path basePath = mock(Path.class);
182+
Path resolvedPath = mock(Path.class);
183+
when(basePath.resolve(anyString())).thenReturn(resolvedPath);
184+
when(localDirectory.getDirectory()).thenReturn(basePath);
185+
Collection<String> files = Arrays.asList("_0.si", "_0.cfs");
186+
compositeDirectory.afterSyncToRemote(files);
187+
verify(fileCache, times(files.size())).decRef(resolvedPath);
188+
}
172189
}

0 commit comments

Comments
 (0)