-
Notifications
You must be signed in to change notification settings - Fork 658
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
SOLR-16886: Don't commit multi-part uploads that have been aborted #1773
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.solr.s3; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.internal.verification.VerificationModeFactory.times; | ||
|
||
import com.carrotsearch.randomizedtesting.generators.RandomStrings; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Consumer; | ||
import org.apache.solr.SolrTestCaseJ4; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; | ||
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
import software.amazon.awssdk.services.s3.model.UploadPartRequest; | ||
import software.amazon.awssdk.services.s3.model.UploadPartResponse; | ||
|
||
public class S3OutputStreamMockitoTest extends SolrTestCaseJ4 { | ||
|
||
private S3Client clientMock; | ||
|
||
private static byte[] largeBuffer; | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
assumeWorkingMockito(); | ||
String content = | ||
RandomStrings.randomAsciiAlphanumOfLength(random(), S3OutputStream.PART_SIZE + 1024); | ||
largeBuffer = content.getBytes(StandardCharsets.UTF_8); | ||
// pre-check -- ensure that our test string isn't too small | ||
assertTrue(largeBuffer.length > S3OutputStream.PART_SIZE); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDownClass() { | ||
largeBuffer = null; | ||
} | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
clientMock = mock(S3Client.class); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void testMultiPartUploadCompleted() throws IOException { | ||
when(clientMock.createMultipartUpload((Consumer<CreateMultipartUploadRequest.Builder>) any())) | ||
.thenReturn(CreateMultipartUploadResponse.builder().build()); | ||
when(clientMock.uploadPart((UploadPartRequest) any(), (RequestBody) any())) | ||
.thenReturn(UploadPartResponse.builder().build()); | ||
S3OutputStream stream = new S3OutputStream(clientMock, "key", "bucket"); | ||
stream.write(largeBuffer); | ||
verify(clientMock) | ||
.createMultipartUpload((Consumer<CreateMultipartUploadRequest.Builder>) any()); | ||
verify(clientMock).uploadPart((UploadPartRequest) any(), (RequestBody) any()); | ||
verify(clientMock, never()) | ||
.completeMultipartUpload((Consumer<CompleteMultipartUploadRequest.Builder>) any()); | ||
verify(clientMock, never()) | ||
.abortMultipartUpload((Consumer<AbortMultipartUploadRequest.Builder>) any()); | ||
|
||
stream.close(); | ||
verify(clientMock) | ||
.completeMultipartUpload((Consumer<CompleteMultipartUploadRequest.Builder>) any()); | ||
verify(clientMock, never()) | ||
.abortMultipartUpload((Consumer<AbortMultipartUploadRequest.Builder>) any()); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void testMultiPartUploadAborted() throws IOException { | ||
when(clientMock.createMultipartUpload((Consumer<CreateMultipartUploadRequest.Builder>) any())) | ||
.thenReturn(CreateMultipartUploadResponse.builder().build()); | ||
when(clientMock.uploadPart((UploadPartRequest) any(), (RequestBody) any())) | ||
.thenThrow(S3Exception.builder().message("fake exception").build()); | ||
S3OutputStream stream = new S3OutputStream(clientMock, "key", "bucket"); | ||
// first time it should throw the exception from S3Client | ||
org.apache.solr.s3.S3Exception solrS3Exception = | ||
assertThrows(org.apache.solr.s3.S3Exception.class, () -> stream.write(largeBuffer)); | ||
assertEquals(S3Exception.class, solrS3Exception.getCause().getClass()); | ||
assertEquals("fake exception", solrS3Exception.getCause().getMessage()); | ||
verify(clientMock).abortMultipartUpload((Consumer<AbortMultipartUploadRequest.Builder>) any()); | ||
|
||
// after that, the exception should be because the MPU is aborted | ||
solrS3Exception = | ||
assertThrows(org.apache.solr.s3.S3Exception.class, () -> stream.write(largeBuffer)); | ||
assertEquals(IllegalStateException.class, solrS3Exception.getCause().getClass()); | ||
assertTrue( | ||
"Unexpected exception message: " + solrS3Exception.getCause().getMessage(), | ||
solrS3Exception | ||
.getCause() | ||
.getMessage() | ||
.contains("Can't upload new parts on a MultipartUpload that was aborted")); | ||
|
||
verify(clientMock) | ||
.createMultipartUpload((Consumer<CreateMultipartUploadRequest.Builder>) any()); | ||
verify(clientMock).uploadPart((UploadPartRequest) any(), (RequestBody) any()); | ||
verify(clientMock, never()) | ||
.completeMultipartUpload((Consumer<CompleteMultipartUploadRequest.Builder>) any()); | ||
verify(clientMock, times(2)) | ||
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. why does it call abort twice? 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. The first time is after calling 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. Thanks for the explanation! |
||
.abortMultipartUpload((Consumer<AbortMultipartUploadRequest.Builder>) any()); | ||
|
||
stream.close(); | ||
verify(clientMock, never()) | ||
.completeMultipartUpload((Consumer<CompleteMultipartUploadRequest.Builder>) any()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this could just return like
complete()
below. Any reason to error here and not there? It'd make the logic a bit simpler inclose()
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's true that this isn't consistent with the logic added in
close
. I kind of like throwing the error instead of just ignoring things because it otherwise hides the fact that the component is being misused (an upload after an abort). The reality is that none of the error paths should happen with the current code. I'm more inclined to throw the ISE exception in thecompoete()
ifaborted
than going the other way around.Maybe it's simpler if I check for
aborted
inclose()
too and handled separately? let me push a change and seeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that makes sense to me.