Skip to content

Commit

Permalink
chore: implement Serializable for BufferToDiskThenUpload
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWhitehead committed Jul 31, 2023
1 parent 94a1c64 commit 203ee70
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@
import com.google.common.util.concurrent.MoreExecutors;
import com.google.storage.v2.WriteObjectResponse;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Clock;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collector;
import javax.annotation.concurrent.Immutable;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/**
* There are scenarios in which disk space is more plentiful than memory space. This new {@link
Expand All @@ -54,10 +60,19 @@
@Immutable
@BetaApi
public final class BufferToDiskThenUpload extends BlobWriteSessionConfig {
private static final long serialVersionUID = 9059242302276891867L;

/**
* non-final because of {@link java.io.Serializable}, however this field is effectively final as
* it is immutable and there is not reference mutator method.
*/
@MonotonicNonNull private transient ImmutableList<Path> paths;

private final ImmutableList<Path> paths;
private final boolean includeLoggingSink;

/** Used for {@link java.io.Serializable} */
@MonotonicNonNull private volatile ArrayList<String> absolutePaths;

@InternalApi
BufferToDiskThenUpload(ImmutableList<Path> paths, boolean includeLoggingSink) throws IOException {
this.paths = paths;
Expand Down Expand Up @@ -94,6 +109,33 @@ private RecoveryFileManager.RecoverVolumeSinkFactory getRecoverVolumeSinkFactory
};
}

private void writeObject(ObjectOutputStream out) throws IOException {
if (absolutePaths == null) {
synchronized (this) {
if (absolutePaths == null) {
absolutePaths =
paths.stream()
.map(Path::toAbsolutePath)
.map(Path::toString)
.collect(
Collector.of(
ArrayList::new,
ArrayList::add,
(left, right) -> {
left.addAll(right);
return left;
}));
}
}
}
out.defaultWriteObject();
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
this.paths = absolutePaths.stream().map(Paths::get).collect(ImmutableList.toImmutableList());
}

private static final class Factory implements WriterFactory {

private final RecoveryFileManager recoveryFileManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,47 @@ protected Serializable[] serializableObjects() {
.add(UnifiedOpts.md5MatchExtractor())
.build();

return new Serializable[] {
ACL_DOMAIN,
ACL_GROUP,
ACL_PROJECT_,
ACL_USER,
ACL_RAW,
ACL,
BLOB_INFO,
BLOB,
BUCKET_INFO,
BUCKET,
ORIGIN,
CORS,
PAGE_RESULT,
BLOB_LIST_OPTIONS,
BLOB_SOURCE_OPTIONS,
BLOB_TARGET_OPTIONS,
BUCKET_LIST_OPTIONS,
BUCKET_SOURCE_OPTIONS,
BUCKET_TARGET_OPTIONS,
STORAGE_EXCEPTION,
optionsDefault1,
optionsDefault2,
optionsHttp1,
optionsHttp2,
optionsGrpc1,
optionsGrpc2,
serializableOpts
};
try {
GrpcStorageOptions grpcStorageOptionsBufferToTemp =
StorageOptions.grpc()
.setCredentials(NoCredentials.getInstance())
.setProjectId("project1")
.setBlobWriteSessionConfig(BlobWriteSessionConfigs.bufferToTempDirThenUpload())
.build();

return new Serializable[] {
ACL_DOMAIN,
ACL_GROUP,
ACL_PROJECT_,
ACL_USER,
ACL_RAW,
ACL,
BLOB_INFO,
BLOB,
BUCKET_INFO,
BUCKET,
ORIGIN,
CORS,
PAGE_RESULT,
BLOB_LIST_OPTIONS,
BLOB_SOURCE_OPTIONS,
BLOB_TARGET_OPTIONS,
BUCKET_LIST_OPTIONS,
BUCKET_SOURCE_OPTIONS,
BUCKET_TARGET_OPTIONS,
STORAGE_EXCEPTION,
optionsDefault1,
optionsDefault2,
optionsHttp1,
optionsHttp2,
optionsGrpc1,
optionsGrpc2,
serializableOpts,
grpcStorageOptionsBufferToTemp
};
} catch (IOException ioe) {
throw new AssertionError(ioe);
}
}

@Override
Expand Down

0 comments on commit 203ee70

Please sign in to comment.