Skip to content

add completed to ResumableFileDownload ser/des #5254

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
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static byte[] toJson(ResumableFileDownload download) {
"s3ObjectLastModified");
}
marshallDownloadFileRequest(download.downloadFileRequest(), jsonGenerator);
TransferManagerJsonMarshaller.LIST.marshall(download.completedParts(), jsonGenerator, "completedParts");
jsonGenerator.writeEndObject();

return jsonGenerator.getBytes();
Expand Down Expand Up @@ -138,7 +139,9 @@ private static ResumableFileDownload fromNodes(Map<String, JsonNode> downloadNod
builder.s3ObjectLastModified(instantUnmarshaller.unmarshall(downloadNodes.get("s3ObjectLastModified")));
}
builder.downloadFileRequest(parseDownloadFileRequest(downloadNodes.get("downloadFileRequest")));

if (downloadNodes.get("completedParts") != null) {
builder.completedParts(TransferManagerJsonUnmarshaller.LIST_INT.unmarshall(downloadNodes.get("completedParts")));
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.math.BigDecimal;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.SdkField;
Expand Down Expand Up @@ -99,6 +101,21 @@ public Map<String, Object> unmarshall(String content, SdkField<?> field) {
}
};

TransferManagerJsonUnmarshaller<List<Integer>> LIST_INT = new TransferManagerJsonUnmarshaller<List<Integer>>() {
@Override
public List<Integer> unmarshall(JsonNode jsonContent, SdkField<?> field) {
if (jsonContent == null) {
return null;
}
return jsonContent.asArray().stream().map(INTEGER::unmarshall).collect(Collectors.toList());
}

@Override
public List<Integer> unmarshall(String content, SdkField<?> field) {
return unmarshall(JsonNode.parser().parse(content), field);
}
};

default T unmarshall(JsonNode jsonContent, SdkField<?> field) {
return jsonContent != null && !jsonContent.isNull() ? unmarshall(jsonContent.text(), field) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalLong;
Expand Down Expand Up @@ -61,6 +63,7 @@ public final class ResumableFileDownload implements ResumableTransfer,
private final Instant s3ObjectLastModified;
private final Long totalSizeInBytes;
private final Instant fileLastModified;
private final List<Integer> completedParts;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the reason for List that we may download different parts in parallel in the future?

Copy link
Contributor Author

@L-Applin L-Applin May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, trying to prepare for when we will do parallel write to file as well


private ResumableFileDownload(DefaultBuilder builder) {
this.downloadFileRequest = Validate.paramNotNull(builder.downloadFileRequest, "downloadFileRequest");
Expand All @@ -69,6 +72,7 @@ private ResumableFileDownload(DefaultBuilder builder) {
this.s3ObjectLastModified = builder.s3ObjectLastModified;
this.totalSizeInBytes = Validate.isPositiveOrNull(builder.totalSizeInBytes, "totalSizeInBytes");
this.fileLastModified = builder.fileLastModified;
this.completedParts = Validate.getOrDefault(builder.completedParts, Collections::emptyList);
}

@Override
Expand All @@ -94,6 +98,9 @@ public boolean equals(Object o) {
if (!Objects.equals(fileLastModified, that.fileLastModified)) {
return false;
}
if (!Objects.equals(completedParts, that.completedParts)) {
return false;
}
return Objects.equals(totalSizeInBytes, that.totalSizeInBytes);
}

Expand All @@ -104,6 +111,7 @@ public int hashCode() {
result = 31 * result + (s3ObjectLastModified != null ? s3ObjectLastModified.hashCode() : 0);
result = 31 * result + (fileLastModified != null ? fileLastModified.hashCode() : 0);
result = 31 * result + (totalSizeInBytes != null ? totalSizeInBytes.hashCode() : 0);
result = 31 * result + (completedParts != null ? completedParts.hashCode() : 0);
return result;
}

Expand Down Expand Up @@ -149,6 +157,15 @@ public OptionalLong totalSizeInBytes() {
return totalSizeInBytes == null ? OptionalLong.empty() : OptionalLong.of(totalSizeInBytes);
}

/**
* The lists of parts that were successfully completed and saved to the file. Non-empty only for multipart downloads.
*
* @return part numbers of a multipart download that were completed saved to file.
*/
public List<Integer> completedParts() {
return Collections.unmodifiableList(completedParts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we wrap with umodifableList in the ctor instead of the getter? Also, we should probably create a new list. Example: https://github.com/aws/aws-sdk-java-v2/blob/master/core/sdk-core/src/main/java/software/amazon/awssdk/core/client/config/ClientOverrideConfiguration.java#L148

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will address in full resume PR

}

@Override
public String toString() {
return ToString.builder("ResumableFileDownload")
Expand All @@ -157,6 +174,7 @@ public String toString() {
.add("s3ObjectLastModified", s3ObjectLastModified)
.add("totalSizeInBytes", totalSizeInBytes)
.add("downloadFileRequest", downloadFileRequest)
.add("completedParts", completedParts)
.build();
}

Expand Down Expand Up @@ -318,6 +336,14 @@ default ResumableFileDownload.Builder downloadFileRequest(Consumer<DownloadFileR
* @return a reference to this object so that method calls can be chained together.
*/
Builder fileLastModified(Instant lastModified);

/**
* For multipart download, set the lists of parts that were successfully completed and saved to the file.
*
* @param completedParts the list of completed parts saved to file.
* @return a reference to this object so that method calls can be chained together.
*/
Builder completedParts(List<Integer> completedParts);
}

private static final class DefaultBuilder implements Builder {
Expand All @@ -327,6 +353,7 @@ private static final class DefaultBuilder implements Builder {
private Instant s3ObjectLastModified;
private Long totalSizeInBytes;
private Instant fileLastModified;
private List<Integer> completedParts;

private DefaultBuilder() {
}
Expand All @@ -337,6 +364,7 @@ private DefaultBuilder(ResumableFileDownload persistableFileDownload) {
this.totalSizeInBytes = persistableFileDownload.totalSizeInBytes;
this.fileLastModified = persistableFileDownload.fileLastModified;
this.s3ObjectLastModified = persistableFileDownload.s3ObjectLastModified;
this.completedParts = persistableFileDownload.completedParts;
}

@Override
Expand Down Expand Up @@ -369,6 +397,12 @@ public Builder fileLastModified(Instant fileLastModified) {
return this;
}

@Override
public Builder completedParts(List<Integer> completedParts) {
this.completedParts = Collections.unmodifiableList(completedParts);
return this;
}

@Override
public ResumableFileDownload build() {
return new ResumableFileDownload(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class ResumableFileDownloadSerializerTest {

@ParameterizedTest
@MethodSource("downloadObjects")
void serializeDeserialize_ShouldWorkForAllDownloads(ResumableFileDownload download) {
void serializeDeserialize_ShouldWorkForAllDownloads(ResumableFileDownload download) {
byte[] serializedDownload = ResumableFileDownloadSerializer.toJson(download);
ResumableFileDownload deserializedDownload = ResumableFileDownloadSerializer.fromJson(serializedDownload);

assertThat(deserializedDownload).isEqualTo(download);
}

@Test
void serializeDeserialize_fromStoredString_ShouldWork() {
void serializeDeserialize_fromStoredString_ShouldWork() {
ResumableFileDownload download =
ResumableFileDownload.builder()
.downloadFileRequest(d -> d.destination(Paths.get("test/request"))
Expand All @@ -95,12 +95,12 @@ void serializeDeserialize_fromStoredString_ShouldWork() {
}

@Test
void serializeDeserialize_DoesNotPersistConfiguration() {
void serializeDeserialize_DoesNotPersistConfiguration() {
ResumableFileDownload download =
ResumableFileDownload.builder()
.downloadFileRequest(d -> d.destination(PATH)
.getObjectRequest(GET_OBJECT_REQUESTS.get("STANDARD"))
.addTransferListener(LoggingTransferListener.create()))
.addTransferListener(LoggingTransferListener.create()))
.bytesTransferred(1000L)
.build();

Expand All @@ -113,7 +113,7 @@ void serializeDeserialize_DoesNotPersistConfiguration() {
}

@Test
void serializeDeserialize_DoesNotPersistRequestOverrideConfiguration() {
void serializeDeserialize_DoesNotPersistRequestOverrideConfiguration() {
GetObjectRequest requestWithOverride =
GetObjectRequest.builder()
.bucket("BUCKET")
Expand All @@ -139,6 +139,28 @@ void serializeDeserialize_DoesNotPersistRequestOverrideConfiguration() {
assertThat(deserializedDownload).isEqualTo(download.copy(d -> d.downloadFileRequest(fileRequestCopy)));
}

@Test
void serializeDeserialize_withCompletedParts_persistCompletedParts() {
ResumableFileDownload download =
ResumableFileDownload.builder()
.downloadFileRequest(d -> d.destination(Paths.get("test/request"))
.getObjectRequest(GET_OBJECT_REQUESTS.get("ALL_TYPES")))
.bytesTransferred(1000L)
.fileLastModified(parseIso8601Date("2022-03-08T10:15:30Z"))
.totalSizeInBytes(5000L)
.s3ObjectLastModified(parseIso8601Date("2022-03-10T08:21:00Z"))
.completedParts(Arrays.asList(1, 2, 3))
.build();
byte[] serializedDownload = ResumableFileDownloadSerializer.toJson(download);
assertThat(new String(serializedDownload, StandardCharsets.UTF_8))
.isEqualTo(SERIALIZED_DOWNLOAD_OBJECT_WITH_COMPLETED_PARTS);

ResumableFileDownload deserializedDownload = ResumableFileDownloadSerializer.fromJson(
SERIALIZED_DOWNLOAD_OBJECT_WITH_COMPLETED_PARTS.getBytes(StandardCharsets.UTF_8));
assertThat(deserializedDownload).isEqualTo(download);

}

public static Collection<ResumableFileDownload> downloadObjects() {
return Stream.of(differentDownloadSettings(),
differentGetObjects())
Expand All @@ -159,7 +181,8 @@ private static List<ResumableFileDownload> differentDownloadSettings() {
resumableFileDownload(1000L, null, null, null, request),
resumableFileDownload(1000L, null, DATE1, null, request),
resumableFileDownload(1000L, 2000L, DATE1, DATE2, request),
resumableFileDownload(Long.MAX_VALUE, Long.MAX_VALUE, DATE1, DATE2, request)
resumableFileDownload(Long.MAX_VALUE, Long.MAX_VALUE, DATE1, DATE2, request),
resumableFileDownload(1000L, 2000L, DATE1, DATE2, request, Arrays.asList(1, 2, 3))
);
}

Expand All @@ -182,6 +205,16 @@ private static ResumableFileDownload resumableFileDownload(Long bytesTransferred
}
return builder.build();
}
private static ResumableFileDownload resumableFileDownload(Long bytesTransferred,
Long totalSizeInBytes,
Instant fileLastModified,
Instant s3ObjectLastModified,
DownloadFileRequest request,
List<Integer> completedParts) {
ResumableFileDownload dl = resumableFileDownload(
bytesTransferred, totalSizeInBytes, fileLastModified, s3ObjectLastModified, request);
return dl.copy(b -> b.completedParts(completedParts));
}

private static DownloadFileRequest downloadRequest(Path path, GetObjectRequest request) {
return DownloadFileRequest.builder()
Expand All @@ -196,5 +229,16 @@ private static DownloadFileRequest downloadRequest(Path path, GetObjectRequest r
+ "\"getObjectRequest\":{\"Bucket\":\"BUCKET\","
+ "\"If-Modified-Since\":1577880630.000,\"Key\":\"KEY\","
+ "\"x-amz-request-payer\":\"requester\",\"partNumber\":1,"
+ "\"x-amz-checksum-mode\":\"ENABLED\"}}}";
+ "\"x-amz-checksum-mode\":\"ENABLED\"}},\"completedParts\":[]}";

private static final String SERIALIZED_DOWNLOAD_OBJECT_WITH_COMPLETED_PARTS =
"{\"bytesTransferred\":1000,"
+ "\"fileLastModified\":1646734530.000,"
+ "\"totalSizeInBytes\":5000,\"s3ObjectLastModified\":1646900460"
+ ".000,\"downloadFileRequest\":{\"destination\":\"test/request\","
+ "\"getObjectRequest\":{\"Bucket\":\"BUCKET\","
+ "\"If-Modified-Since\":1577880630.000,\"Key\":\"KEY\","
+ "\"x-amz-request-payer\":\"requester\",\"partNumber\":1,"
+ "\"x-amz-checksum-mode\":\"ENABLED\"}},\"completedParts\":[1,2,3]}";

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.internal.ArrayJsonNode;
import software.amazon.awssdk.protocols.jsoncore.internal.NullJsonNode;
import software.amazon.awssdk.protocols.jsoncore.internal.NumberJsonNode;
import software.amazon.awssdk.protocols.jsoncore.internal.StringJsonNode;
Expand Down Expand Up @@ -57,7 +59,12 @@ private static Stream<Arguments> unmarshallingValues() {
Arguments.of(new StringJsonNode(BinaryUtils.toBase64(SdkBytes.fromString("100", StandardCharsets.UTF_8)
.asByteArray())),
SdkBytes.fromString("100", StandardCharsets.UTF_8),
TransferManagerJsonUnmarshaller.SDK_BYTES)
TransferManagerJsonUnmarshaller.SDK_BYTES),
Arguments.of(new ArrayJsonNode(Arrays.asList(new NumberJsonNode("1"),
new NumberJsonNode("2"),
new NumberJsonNode("3"))),
Arrays.asList(1, 2, 3),
TransferManagerJsonUnmarshaller.LIST_INT)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Arrays;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.testutils.RandomTempFile;
import software.amazon.awssdk.transfer.s3.model.ResumableFileDownload;

class ResumableFileDownloadTest {

Expand Down Expand Up @@ -130,6 +130,7 @@ private static ResumableFileDownload resumableFileDownload() {
.fileLastModified(DATE1)
.s3ObjectLastModified(DATE2)
.totalSizeInBytes(2000L)
.completedParts(Arrays.asList(1, 2, 5))
.build();
}
}
Loading