Skip to content

Commit

Permalink
Fix another possible class cast exception scenario
Browse files Browse the repository at this point in the history
Signed-off-by: Ashish Singh <[email protected]>
  • Loading branch information
ashking94 committed Oct 25, 2023
1 parent b95ebfa commit 310277c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ public boolean transferSnapshot(TransferSnapshot transferSnapshot, TranslogTrans
throw ex;
}
} catch (InterruptedException ex) {
exceptionList.forEach(ex::addSuppressed);
Exception exception = new TranslogUploadFailedException("Failed to upload " + transferSnapshot, ex);
exceptionList.forEach(exception::addSuppressed);
Thread.currentThread().interrupt();
throw ex;
throw exception;
}
if (exceptionList.isEmpty()) {
TransferFileSnapshot tlogMetadata = prepareMetadata(transferSnapshot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -212,6 +213,62 @@ public void onUploadFailed(TransferSnapshot transferSnapshot, Exception ex) {
assertEquals("Timed out waiting for transfer of snapshot test-to-string to complete", exception.get().getMessage());
}

public void testTransferSnapshotOnThreadInterrupt() throws Exception {
SetOnce<Thread> uploadThread = new SetOnce<>();
doAnswer(invocationOnMock -> {
uploadThread.set(new Thread(() -> {
ActionListener<TransferFileSnapshot> listener = invocationOnMock.getArgument(2);
try {
Thread.sleep(31 * 1000);
} catch (InterruptedException ignore) {
List<TransferFileSnapshot> list = new ArrayList<>(invocationOnMock.getArgument(0));
listener.onFailure(new FileTransferException(list.get(0), ignore));
}
}));
uploadThread.get().start();
return null;
}).when(transferService).uploadBlobs(anySet(), anyMap(), any(ActionListener.class), any(WritePriority.class));
FileTransferTracker fileTransferTracker = new FileTransferTracker(
new ShardId("index", "indexUUid", 0),
remoteTranslogTransferTracker
);
TranslogTransferManager translogTransferManager = new TranslogTransferManager(
shardId,
transferService,
remoteBaseTransferPath,
fileTransferTracker,
remoteTranslogTransferTracker
);
SetOnce<Exception> exception = new SetOnce<>();

Thread thread = new Thread(() -> {
try {
translogTransferManager.transferSnapshot(createTransferSnapshot(), new TranslogTransferListener() {
@Override
public void onUploadComplete(TransferSnapshot transferSnapshot) {}

@Override
public void onUploadFailed(TransferSnapshot transferSnapshot, Exception ex) {
exception.set(ex);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
});
thread.start();

Thread.sleep(1000);
// Interrupt the thread
thread.interrupt();
assertBusy(() -> {
assertNotNull(exception.get());
assertTrue(exception.get() instanceof TranslogUploadFailedException);
assertEquals("Failed to upload test-to-string", exception.get().getMessage());
});
uploadThread.get().interrupt();
}

private TransferSnapshot createTransferSnapshot() {
return new TransferSnapshot() {
@Override
Expand Down

0 comments on commit 310277c

Please sign in to comment.