-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Clone index input in multipart upload flow #19072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gbbafna
merged 8 commits into
opensearch-project:main
from
sachinpkale:index-input-clone
Aug 19, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ee8717d
Clone index input in multipart upload flow
sachinpkale cf27be8
Fix spotless error
sachinpkale 18138d7
Fix for segments_N file
sachinpkale 0464761
Fix spotless error
sachinpkale b04e72a
Fix test
sachinpkale b1069f8
Make sure to close IndexInput on exception
sachinpkale 93b9237
Fix spotless errors
sachinpkale c4cf7f4
Address PR comments
sachinpkale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -397,68 +397,82 @@ private void uploadBlob( | |
| assert ioContext != IOContext.READONCE : "Remote upload will fail with IoContext.READONCE"; | ||
| long expectedChecksum = calculateChecksumOfChecksum(from, src); | ||
| long contentLength; | ||
| try (IndexInput indexInput = from.openInput(src, ioContext)) { | ||
| IndexInput indexInput = from.openInput(src, ioContext); | ||
| try { | ||
| contentLength = indexInput.length(); | ||
| } | ||
| boolean remoteIntegrityEnabled = false; | ||
| if (getBlobContainer() instanceof AsyncMultiStreamBlobContainer) { | ||
| remoteIntegrityEnabled = ((AsyncMultiStreamBlobContainer) getBlobContainer()).remoteIntegrityCheckSupported(); | ||
| } | ||
| lowPriorityUpload = lowPriorityUpload || contentLength > ByteSizeUnit.GB.toBytes(15); | ||
| RemoteTransferContainer.OffsetRangeInputStreamSupplier offsetRangeInputStreamSupplier; | ||
| if (lowPriorityUpload) { | ||
| offsetRangeInputStreamSupplier = (size, position) -> lowPriorityUploadRateLimiter.apply( | ||
| new OffsetRangeIndexInputStream(from.openInput(src, ioContext), size, position) | ||
| ); | ||
| } else { | ||
| offsetRangeInputStreamSupplier = (size, position) -> uploadRateLimiter.apply( | ||
| new OffsetRangeIndexInputStream(from.openInput(src, ioContext), size, position) | ||
| ); | ||
| } | ||
| RemoteTransferContainer remoteTransferContainer = new RemoteTransferContainer( | ||
| src, | ||
| remoteFileName, | ||
| contentLength, | ||
| true, | ||
| lowPriorityUpload ? WritePriority.LOW : WritePriority.NORMAL, | ||
| offsetRangeInputStreamSupplier, | ||
| expectedChecksum, | ||
| remoteIntegrityEnabled | ||
| ); | ||
| ActionListener<Void> completionListener = ActionListener.wrap(resp -> { | ||
| try { | ||
| postUploadRunner.run(); | ||
| listener.onResponse(null); | ||
| } catch (Exception e) { | ||
| logger.error(() -> new ParameterizedMessage("Exception in segment postUpload for file [{}]", src), e); | ||
| listener.onFailure(e); | ||
| } | ||
| }, ex -> { | ||
| logger.error(() -> new ParameterizedMessage("Failed to upload blob {}", src), ex); | ||
| IOException corruptIndexException = ExceptionsHelper.unwrapCorruption(ex); | ||
| if (corruptIndexException != null) { | ||
| listener.onFailure(corruptIndexException); | ||
| return; | ||
| } | ||
| Throwable throwable = ExceptionsHelper.unwrap(ex, CorruptFileException.class); | ||
| if (throwable != null) { | ||
| CorruptFileException corruptFileException = (CorruptFileException) throwable; | ||
| listener.onFailure(new CorruptIndexException(corruptFileException.getMessage(), corruptFileException.getFileName())); | ||
| return; | ||
| boolean remoteIntegrityEnabled = false; | ||
| if (getBlobContainer() instanceof AsyncMultiStreamBlobContainer) { | ||
| remoteIntegrityEnabled = ((AsyncMultiStreamBlobContainer) getBlobContainer()).remoteIntegrityCheckSupported(); | ||
| } | ||
| listener.onFailure(ex); | ||
| }); | ||
| lowPriorityUpload = lowPriorityUpload || contentLength > ByteSizeUnit.GB.toBytes(15); | ||
| RemoteTransferContainer.OffsetRangeInputStreamSupplier offsetRangeInputStreamSupplier; | ||
|
|
||
| completionListener = ActionListener.runBefore(completionListener, () -> { | ||
| try { | ||
| remoteTransferContainer.close(); | ||
| } catch (Exception e) { | ||
| logger.warn("Error occurred while closing streams", e); | ||
| if (lowPriorityUpload) { | ||
| offsetRangeInputStreamSupplier = (size, position) -> lowPriorityUploadRateLimiter.apply( | ||
| new OffsetRangeIndexInputStream(indexInput.clone(), size, position) | ||
| ); | ||
| } else { | ||
| offsetRangeInputStreamSupplier = (size, position) -> uploadRateLimiter.apply( | ||
| new OffsetRangeIndexInputStream(indexInput.clone(), size, position) | ||
| ); | ||
| } | ||
| }); | ||
| RemoteTransferContainer remoteTransferContainer = new RemoteTransferContainer( | ||
| src, | ||
| remoteFileName, | ||
| contentLength, | ||
| true, | ||
| lowPriorityUpload ? WritePriority.LOW : WritePriority.NORMAL, | ||
| offsetRangeInputStreamSupplier, | ||
| expectedChecksum, | ||
| remoteIntegrityEnabled | ||
| ); | ||
| ActionListener<Void> completionListener = ActionListener.wrap(resp -> { | ||
| try { | ||
| postUploadRunner.run(); | ||
| listener.onResponse(null); | ||
| } catch (Exception e) { | ||
| logger.error(() -> new ParameterizedMessage("Exception in segment postUpload for file [{}]", src), e); | ||
| listener.onFailure(e); | ||
| } | ||
| }, ex -> { | ||
| logger.error(() -> new ParameterizedMessage("Failed to upload blob {}", src), ex); | ||
| IOException corruptIndexException = ExceptionsHelper.unwrapCorruption(ex); | ||
| if (corruptIndexException != null) { | ||
| listener.onFailure(corruptIndexException); | ||
| return; | ||
| } | ||
| Throwable throwable = ExceptionsHelper.unwrap(ex, CorruptFileException.class); | ||
| if (throwable != null) { | ||
| CorruptFileException corruptFileException = (CorruptFileException) throwable; | ||
| listener.onFailure(new CorruptIndexException(corruptFileException.getMessage(), corruptFileException.getFileName())); | ||
| return; | ||
| } | ||
| listener.onFailure(ex); | ||
| }); | ||
|
|
||
| WriteContext writeContext = remoteTransferContainer.createWriteContext(); | ||
| ((AsyncMultiStreamBlobContainer) blobContainer).asyncBlobUpload(writeContext, completionListener); | ||
| completionListener = ActionListener.runBefore(completionListener, () -> { | ||
| try { | ||
| remoteTransferContainer.close(); | ||
| } catch (Exception e) { | ||
| logger.warn("Error occurred while closing streams", e); | ||
| } | ||
| }); | ||
|
|
||
| completionListener = ActionListener.runAfter(completionListener, () -> { | ||
| try { | ||
| indexInput.close(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good to add a debug/trace log line for places where the close is happening to check in future if any potential leak happens. |
||
| } catch (IOException e) { | ||
| logger.warn("Error occurred while closing index input", e); | ||
| } | ||
| }); | ||
|
|
||
| WriteContext writeContext = remoteTransferContainer.createWriteContext(); | ||
| ((AsyncMultiStreamBlobContainer) blobContainer).asyncBlobUpload(writeContext, completionListener); | ||
| } catch (Exception e) { | ||
| logger.warn("Exception while calling asyncBlobUpload, closing IndexInput to avoid leak"); | ||
| indexInput.close(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| private long calculateChecksumOfChecksum(Directory directory, String file) throws IOException { | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.