|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import com.google.api.core.ApiFuture; |
| 20 | +import com.google.api.core.ApiFutures; |
| 21 | +import com.google.api.core.BetaApi; |
| 22 | +import com.google.api.core.InternalApi; |
| 23 | +import com.google.api.core.SettableApiFuture; |
| 24 | +import com.google.cloud.storage.Conversions.Decoder; |
| 25 | +import com.google.cloud.storage.Storage.BlobWriteOption; |
| 26 | +import com.google.cloud.storage.UnifiedOpts.ObjectTargetOpt; |
| 27 | +import com.google.cloud.storage.UnifiedOpts.Opts; |
| 28 | +import com.google.common.annotations.VisibleForTesting; |
| 29 | +import com.google.common.collect.ImmutableList; |
| 30 | +import com.google.common.util.concurrent.MoreExecutors; |
| 31 | +import com.google.storage.v2.WriteObjectResponse; |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.ObjectInputStream; |
| 34 | +import java.io.ObjectOutputStream; |
| 35 | +import java.nio.ByteBuffer; |
| 36 | +import java.nio.channels.WritableByteChannel; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | +import java.time.Clock; |
| 41 | +import java.time.Duration; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Collection; |
| 44 | +import java.util.stream.Collector; |
| 45 | +import javax.annotation.concurrent.Immutable; |
| 46 | +import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
| 47 | + |
| 48 | +/** |
| 49 | + * There are scenarios in which disk space is more plentiful than memory space. This new {@link |
| 50 | + * BlobWriteSessionConfig} allows augmenting an instance of storage to produce {@link |
| 51 | + * BlobWriteSession}s which will buffer to disk rather than holding things in memory. |
| 52 | + * |
| 53 | + * <p>Once the file on disk is closed, the entire file will then be uploaded to GCS. |
| 54 | + * |
| 55 | + * @see Storage#blobWriteSession(BlobInfo, BlobWriteOption...) |
| 56 | + * @see GrpcStorageOptions.Builder#setBlobWriteSessionConfig(BlobWriteSessionConfig) |
| 57 | + * @see BlobWriteSessionConfigs#bufferToDiskThenUpload(Path) |
| 58 | + * @see BlobWriteSessionConfigs#bufferToDiskThenUpload(Collection) |
| 59 | + */ |
| 60 | +@Immutable |
| 61 | +@BetaApi |
| 62 | +public final class BufferToDiskThenUpload extends BlobWriteSessionConfig { |
| 63 | + private static final long serialVersionUID = 9059242302276891867L; |
| 64 | + |
| 65 | + /** |
| 66 | + * non-final because of {@link java.io.Serializable}, however this field is effectively final as |
| 67 | + * it is immutable and there is not reference mutator method. |
| 68 | + */ |
| 69 | + @MonotonicNonNull private transient ImmutableList<Path> paths; |
| 70 | + |
| 71 | + private final boolean includeLoggingSink; |
| 72 | + |
| 73 | + /** Used for {@link java.io.Serializable} */ |
| 74 | + @MonotonicNonNull private volatile ArrayList<String> absolutePaths; |
| 75 | + |
| 76 | + @InternalApi |
| 77 | + BufferToDiskThenUpload(ImmutableList<Path> paths, boolean includeLoggingSink) throws IOException { |
| 78 | + this.paths = paths; |
| 79 | + this.includeLoggingSink = includeLoggingSink; |
| 80 | + } |
| 81 | + |
| 82 | + @VisibleForTesting |
| 83 | + @InternalApi |
| 84 | + BufferToDiskThenUpload withIncludeLoggingSink() throws IOException { |
| 85 | + return new BufferToDiskThenUpload(paths, true); |
| 86 | + } |
| 87 | + |
| 88 | + @InternalApi |
| 89 | + @Override |
| 90 | + WriterFactory createFactory(Clock clock) throws IOException { |
| 91 | + Duration window = Duration.ofMinutes(10); |
| 92 | + RecoveryFileManager recoveryFileManager = |
| 93 | + RecoveryFileManager.of(paths, getRecoverVolumeSinkFactory(clock, window)); |
| 94 | + ThroughputSink gcs = ThroughputSink.windowed(ThroughputMovingWindow.of(window), clock); |
| 95 | + gcs = includeLoggingSink ? ThroughputSink.tee(ThroughputSink.logged("gcs", clock), gcs) : gcs; |
| 96 | + return new Factory(recoveryFileManager, clock, gcs); |
| 97 | + } |
| 98 | + |
| 99 | + private RecoveryFileManager.RecoverVolumeSinkFactory getRecoverVolumeSinkFactory( |
| 100 | + Clock clock, Duration window) { |
| 101 | + return path -> { |
| 102 | + ThroughputSink windowed = ThroughputSink.windowed(ThroughputMovingWindow.of(window), clock); |
| 103 | + if (includeLoggingSink) { |
| 104 | + return ThroughputSink.tee( |
| 105 | + ThroughputSink.logged(path.toAbsolutePath().toString(), clock), windowed); |
| 106 | + } else { |
| 107 | + return windowed; |
| 108 | + } |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + private void writeObject(ObjectOutputStream out) throws IOException { |
| 113 | + if (absolutePaths == null) { |
| 114 | + synchronized (this) { |
| 115 | + if (absolutePaths == null) { |
| 116 | + absolutePaths = |
| 117 | + paths.stream() |
| 118 | + .map(Path::toAbsolutePath) |
| 119 | + .map(Path::toString) |
| 120 | + .collect( |
| 121 | + Collector.of( |
| 122 | + ArrayList::new, |
| 123 | + ArrayList::add, |
| 124 | + (left, right) -> { |
| 125 | + left.addAll(right); |
| 126 | + return left; |
| 127 | + })); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + out.defaultWriteObject(); |
| 132 | + } |
| 133 | + |
| 134 | + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
| 135 | + in.defaultReadObject(); |
| 136 | + this.paths = absolutePaths.stream().map(Paths::get).collect(ImmutableList.toImmutableList()); |
| 137 | + } |
| 138 | + |
| 139 | + private static final class Factory implements WriterFactory { |
| 140 | + |
| 141 | + private final RecoveryFileManager recoveryFileManager; |
| 142 | + private final Clock clock; |
| 143 | + private final ThroughputSink gcs; |
| 144 | + |
| 145 | + private Factory(RecoveryFileManager recoveryFileManager, Clock clock, ThroughputSink gcs) { |
| 146 | + this.recoveryFileManager = recoveryFileManager; |
| 147 | + this.clock = clock; |
| 148 | + this.gcs = gcs; |
| 149 | + } |
| 150 | + |
| 151 | + @InternalApi |
| 152 | + @Override |
| 153 | + public WritableByteChannelSession<?, BlobInfo> writeSession( |
| 154 | + StorageInternal storage, |
| 155 | + BlobInfo info, |
| 156 | + Opts<ObjectTargetOpt> opts, |
| 157 | + Decoder<WriteObjectResponse, BlobInfo> d) { |
| 158 | + return new Factory.WriteToFileThenUpload( |
| 159 | + storage, info, opts, recoveryFileManager.newRecoveryFile(info)); |
| 160 | + } |
| 161 | + |
| 162 | + private final class WriteToFileThenUpload |
| 163 | + implements WritableByteChannelSession<WritableByteChannel, BlobInfo> { |
| 164 | + |
| 165 | + private final StorageInternal storage; |
| 166 | + private final BlobInfo info; |
| 167 | + private final Opts<ObjectTargetOpt> opts; |
| 168 | + private final RecoveryFile rf; |
| 169 | + private final SettableApiFuture<BlobInfo> result; |
| 170 | + |
| 171 | + private WriteToFileThenUpload( |
| 172 | + StorageInternal storage, BlobInfo info, Opts<ObjectTargetOpt> opts, RecoveryFile rf) { |
| 173 | + this.info = info; |
| 174 | + this.opts = opts; |
| 175 | + this.rf = rf; |
| 176 | + this.storage = storage; |
| 177 | + this.result = SettableApiFuture.create(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public ApiFuture<WritableByteChannel> openAsync() { |
| 182 | + try { |
| 183 | + ApiFuture<WritableByteChannel> f = ApiFutures.immediateFuture(rf.writer()); |
| 184 | + return ApiFutures.transform( |
| 185 | + f, Factory.WriteToFileThenUpload.Flusher::new, MoreExecutors.directExecutor()); |
| 186 | + } catch (IOException e) { |
| 187 | + throw StorageException.coalesce(e); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public ApiFuture<BlobInfo> getResult() { |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | + private final class Flusher implements WritableByteChannel { |
| 197 | + |
| 198 | + private final WritableByteChannel delegate; |
| 199 | + |
| 200 | + private Flusher(WritableByteChannel delegate) { |
| 201 | + this.delegate = delegate; |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int write(ByteBuffer src) throws IOException { |
| 206 | + return delegate.write(src); |
| 207 | + } |
| 208 | + |
| 209 | + @Override |
| 210 | + public boolean isOpen() { |
| 211 | + return delegate.isOpen(); |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public void close() throws IOException { |
| 216 | + delegate.close(); |
| 217 | + try (RecoveryFile rf = Factory.WriteToFileThenUpload.this.rf) { |
| 218 | + Path path = rf.getPath(); |
| 219 | + long size = Files.size(path); |
| 220 | + ThroughputSink.computeThroughput( |
| 221 | + clock, |
| 222 | + gcs, |
| 223 | + size, |
| 224 | + () -> { |
| 225 | + BlobInfo blob = storage.internalCreateFrom(path, info, opts); |
| 226 | + result.set(blob); |
| 227 | + }); |
| 228 | + } catch (StorageException | IOException e) { |
| 229 | + result.setException(e); |
| 230 | + throw e; |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments