Skip to content

Commit

Permalink
refactor: Move TransferManagerConfig to own file and Add Parallel Upl…
Browse files Browse the repository at this point in the history
…oad/Download Configs to jobs (#1895)
  • Loading branch information
sydney-munro authored Feb 9, 2023
1 parent 233579d commit 0fa8d57
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;

public final class DownloadJob {

@NonNull private final List<DownloadResult> successResponses;
@NonNull private final List<DownloadResult> failedResponses;

@NonNull private final ParallelDownloadConfig parallelDownloadConfig;
private final boolean anyFailed;

private DownloadJob(
@NonNull List<DownloadResult> successResponses,
@NonNull List<DownloadResult> failedResponses) {
@NonNull List<DownloadResult> failedResponses,
@NonNull ParallelDownloadConfig parallelDownloadConfig) {
this.successResponses = successResponses;
this.failedResponses = failedResponses;
this.anyFailed = failedResponses.isEmpty();
this.anyFailed = !failedResponses.isEmpty();
this.parallelDownloadConfig = parallelDownloadConfig;
}

public List<DownloadResult> getSuccessResponses() {
Expand All @@ -50,30 +55,36 @@ public boolean isAnyFailed() {
return anyFailed;
}

public ParallelDownloadConfig getParallelDownloadConfig() {
return parallelDownloadConfig;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if (!(o instanceof DownloadJob)) {
return false;
}
DownloadJob that = (DownloadJob) o;
return anyFailed == that.anyFailed
&& successResponses.equals(that.successResponses)
&& failedResponses.equals(that.failedResponses);
&& failedResponses.equals(that.failedResponses)
&& parallelDownloadConfig.equals(that.parallelDownloadConfig);
}

@Override
public int hashCode() {
return Objects.hash(successResponses, failedResponses, anyFailed);
return Objects.hash(successResponses, failedResponses, parallelDownloadConfig, anyFailed);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("successResponses", successResponses)
.add("failedResponses", failedResponses)
.add("parallelDownloadConfig", parallelDownloadConfig)
.add("anyFailed", anyFailed)
.toString();
}
Expand All @@ -86,6 +97,7 @@ public static final class Builder {

private @NonNull List<DownloadResult> successResponses;
private @NonNull List<DownloadResult> failedResponses;
private @MonotonicNonNull ParallelDownloadConfig parallelDownloadConfig;

private Builder() {
this.successResponses = ImmutableList.of();
Expand All @@ -102,10 +114,17 @@ public Builder setFailedResponses(@NonNull List<DownloadResult> failedResponses)
return this;
}

public Builder setParallelDownloadConfig(
@NonNull ParallelDownloadConfig parallelDownloadConfig) {
this.parallelDownloadConfig = parallelDownloadConfig;
return this;
}

public DownloadJob build() {
checkNotNull(successResponses);
checkNotNull(failedResponses);
return new DownloadJob(successResponses, failedResponses);
checkNotNull(parallelDownloadConfig);
return new DownloadJob(successResponses, failedResponses, parallelDownloadConfig);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,103 +17,15 @@
package com.google.cloud.storage.transfermanager;

import com.google.cloud.storage.BlobInfo;
import com.google.common.base.MoreObjects;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull;

public interface TransferManager {

class TransferManagerConfig {
private final int maxWorkers;
private final int perWorkerBufferSize;
private final boolean allowChunking;

private TransferManagerConfig(int maxWorkers, int perWorkerBufferSize, boolean allowChunking) {
this.maxWorkers = maxWorkers;
this.perWorkerBufferSize = perWorkerBufferSize;
this.allowChunking = allowChunking;
}

public int getMaxWorkers() {
return maxWorkers;
}

public int getPerWorkerBufferSize() {
return perWorkerBufferSize;
}

public boolean isAllowChunking() {
return allowChunking;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TransferManagerConfig that = (TransferManagerConfig) o;
return maxWorkers == that.maxWorkers
&& perWorkerBufferSize == that.perWorkerBufferSize
&& allowChunking == that.allowChunking;
}

@Override
public int hashCode() {
return Objects.hash(maxWorkers, perWorkerBufferSize, allowChunking);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("maxWorkers", maxWorkers)
.add("perWorkerBufferSize", perWorkerBufferSize)
.add("allowChunking", allowChunking)
.toString();
}

public static Builder newBuilder() {
return new Builder();
}

static class Builder {

private int maxWorkers;
private int perWorkerBufferSize;
private boolean allowChunking;

private Builder() {
// TODO: add default values
// bufferSize tbd?
this.maxWorkers = 2 * Runtime.getRuntime().availableProcessors();
this.allowChunking = false;
}

public Builder setMaxWorkers(int maxWorkers) {
this.maxWorkers = maxWorkers;
return this;
}

public Builder setPerWorkerBufferSize(int perWorkerBufferSize) {
this.perWorkerBufferSize = perWorkerBufferSize;
return this;
}

public Builder setAllowChunking(boolean allowChunking) {
this.allowChunking = allowChunking;
return this;
}

public TransferManagerConfig build() {
return new TransferManagerConfig(maxWorkers, perWorkerBufferSize, allowChunking);
}
}
}

@NonNull
UploadJob uploadFiles(List<Path> files, ParallelUploadConfig opts);

@NonNull
DownloadJob downloadBlobs(List<BlobInfo> blobs, ParallelDownloadConfig opts);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2023 Google LLC
*
* Licensed 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 com.google.cloud.storage.transfermanager;

import com.google.common.base.MoreObjects;
import java.util.Objects;

public class TransferManagerConfig {
private final int maxWorkers;
private final int perWorkerBufferSize;
private final boolean allowChunking;

// Getting stuff in for implementation bits
// getService to get Storage instance
// private final StorageOptions storageOptions;

private TransferManagerConfig(int maxWorkers, int perWorkerBufferSize, boolean allowChunking) {
this.maxWorkers = maxWorkers;
this.perWorkerBufferSize = perWorkerBufferSize;
this.allowChunking = allowChunking;
}

public int getMaxWorkers() {
return maxWorkers;
}

public int getPerWorkerBufferSize() {
return perWorkerBufferSize;
}

public boolean isAllowChunking() {
return allowChunking;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TransferManagerConfig that = (TransferManagerConfig) o;
return maxWorkers == that.maxWorkers
&& perWorkerBufferSize == that.perWorkerBufferSize
&& allowChunking == that.allowChunking;
}

@Override
public int hashCode() {
return Objects.hash(maxWorkers, perWorkerBufferSize, allowChunking);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("maxWorkers", maxWorkers)
.add("perWorkerBufferSize", perWorkerBufferSize)
.add("allowChunking", allowChunking)
.toString();
}

public static Builder newBuilder() {
return new Builder();
}

static class Builder {

private int maxWorkers;
private int perWorkerBufferSize;
private boolean allowChunking;

private Builder() {
// TODO: add default values
// bufferSize tbd?
this.perWorkerBufferSize = 16 * 1024 * 1024;
this.maxWorkers = 2 * Runtime.getRuntime().availableProcessors();
this.allowChunking = false;
}

public Builder setMaxWorkers(int maxWorkers) {
this.maxWorkers = maxWorkers;
return this;
}

public Builder setPerWorkerBufferSize(int perWorkerBufferSize) {
this.perWorkerBufferSize = perWorkerBufferSize;
return this;
}

public Builder setAllowChunking(boolean allowChunking) {
this.allowChunking = allowChunking;
return this;
}

public TransferManagerConfig build() {
return new TransferManagerConfig(maxWorkers, perWorkerBufferSize, allowChunking);
}
}
}
Loading

0 comments on commit 0fa8d57

Please sign in to comment.