Skip to content

Commit 588f46d

Browse files
authored
Change IOContext from READONCE to DEFAULT to avoid WrongThreadException (#17502)
--------- Signed-off-by: Sachin Kale <sachinpkale@gmail.com>
1 parent cb869c0 commit 588f46d

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

server/src/main/java/org/opensearch/index/shard/RemoteStoreRefreshListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ private void uploadNewSegments(
459459
batchUploadListener.onFailure(ex);
460460
});
461461
statsListener.beforeUpload(src);
462-
remoteDirectory.copyFrom(storeDirectory, src, IOContext.READONCE, aggregatedListener, isLowPriorityUpload());
462+
remoteDirectory.copyFrom(storeDirectory, src, IOContext.DEFAULT, aggregatedListener, isLowPriorityUpload());
463463
}
464464
}
465465

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

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ private void uploadBlob(
383383
ActionListener<Void> listener,
384384
boolean lowPriorityUpload
385385
) throws Exception {
386+
assert ioContext != IOContext.READONCE : "Remote upload will fail with IoContext.READONCE";
386387
long expectedChecksum = calculateChecksumOfChecksum(from, src);
387388
long contentLength;
388389
try (IndexInput indexInput = from.openInput(src, ioContext)) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void testCopyFrom() throws IOException, InterruptedException {
9292
storeDirectory,
9393
filename,
9494
filename,
95-
IOContext.READONCE,
95+
IOContext.DEFAULT,
9696
() -> postUploadInvoked.set(true),
9797
new ActionListener<>() {
9898
@Override
@@ -130,7 +130,7 @@ public void testCopyFromWithException() throws IOException, InterruptedException
130130
storeDirectory,
131131
filename,
132132
filename,
133-
IOContext.READONCE,
133+
IOContext.DEFAULT,
134134
() -> postUploadInvoked.set(true),
135135
new ActionListener<>() {
136136
@Override

test/framework/src/main/java/org/opensearch/test/store/MockFSDirectoryFactory.java

+16
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636

3737
import org.apache.logging.log4j.Logger;
3838
import org.apache.lucene.index.CheckIndex;
39+
import org.apache.lucene.index.IndexFileNames;
3940
import org.apache.lucene.store.Directory;
41+
import org.apache.lucene.store.IOContext;
42+
import org.apache.lucene.store.IndexInput;
4043
import org.apache.lucene.store.LockObtainFailedException;
4144
import org.apache.lucene.tests.store.BaseDirectoryWrapper;
4245
import org.apache.lucene.tests.store.MockDirectoryWrapper;
@@ -203,6 +206,19 @@ public synchronized void crash() throws IOException {
203206
public Set<String> getPendingDeletions() throws IOException {
204207
return in.getPendingDeletions();
205208
}
209+
210+
// In remote store feature, the upload flow is async and IndexInput can be opened and closed
211+
// by different threads, so we always use IOContext.DEFAULT.
212+
// But MockDirectoryWrapper throws an exception if segments_N fil is opened with any IOContext other than READONCE.
213+
// Following change is temporary override to avoid the test failures. We should fix the multiple thread access
214+
// in remote store upload flow.
215+
@Override
216+
public synchronized IndexInput openInput(String name, IOContext context) throws IOException {
217+
if (name.startsWith(IndexFileNames.SEGMENTS)) {
218+
context = IOContext.READONCE;
219+
}
220+
return super.openInput(name, context);
221+
}
206222
}
207223

208224
static final class CloseableDirectory implements Closeable {

0 commit comments

Comments
 (0)