Skip to content

Commit c416354

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

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
@@ -460,7 +460,7 @@ private void uploadNewSegments(
460460
batchUploadListener.onFailure(ex);
461461
});
462462
statsListener.beforeUpload(src);
463-
remoteDirectory.copyFrom(storeDirectory, src, IOContext.READONCE, aggregatedListener, isLowPriorityUpload());
463+
remoteDirectory.copyFrom(storeDirectory, src, IOContext.DEFAULT, aggregatedListener, isLowPriorityUpload());
464464
}
465465
}
466466

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

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ private void uploadBlob(
357357
ActionListener<Void> listener,
358358
boolean lowPriorityUpload
359359
) throws Exception {
360+
assert ioContext != IOContext.READONCE : "Remote upload will fail with IoContext.READONCE";
360361
long expectedChecksum = calculateChecksumOfChecksum(from, src);
361362
long contentLength;
362363
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
@@ -94,7 +94,7 @@ public void testCopyFrom() throws IOException, InterruptedException {
9494
storeDirectory,
9595
filename,
9696
filename,
97-
IOContext.READ,
97+
IOContext.DEFAULT,
9898
() -> postUploadInvoked.set(true),
9999
new ActionListener<>() {
100100
@Override
@@ -132,7 +132,7 @@ public void testCopyFromWithException() throws IOException, InterruptedException
132132
storeDirectory,
133133
filename,
134134
filename,
135-
IOContext.READ,
135+
IOContext.DEFAULT,
136136
() -> postUploadInvoked.set(true),
137137
new ActionListener<>() {
138138
@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)