Skip to content

Commit c6fce61

Browse files
committed
chore: ClientStuff -> BlobReadChannelContext
ClientStuff is no longer Serializable. Instead, BlobReadChannelV2State will store the instance of HttpStorageOptions and then reconstitute the BlobReadChannelContext at restore() time.
1 parent baaae2d commit c6fce61

File tree

4 files changed

+47
-52
lines changed

4 files changed

+47
-52
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/BlobReadChannelV2.java

+31-36
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.cloud.storage.spi.v1.StorageRpc;
2929
import com.google.common.base.MoreObjects;
3030
import java.io.IOException;
31-
import java.io.ObjectInputStream;
3231
import java.io.Serializable;
3332
import java.nio.ByteBuffer;
3433
import java.util.Map;
@@ -38,7 +37,7 @@ final class BlobReadChannelV2 implements StorageReadChannel {
3837

3938
private final StorageObject storageObject;
4039
private final Map<StorageRpc.Option, ?> opts;
41-
private final ClientStuff clientStuff;
40+
private final BlobReadChannelContext blobReadChannelContext;
4241

4342
private LazyReadChannel<StorageObject> lazyReadChannel;
4443
private StorageObject resolvedObject;
@@ -48,10 +47,12 @@ final class BlobReadChannelV2 implements StorageReadChannel {
4847
private BufferHandle bufferHandle;
4948

5049
BlobReadChannelV2(
51-
StorageObject storageObject, Map<StorageRpc.Option, ?> opts, ClientStuff clientStuff) {
50+
StorageObject storageObject,
51+
Map<StorageRpc.Option, ?> opts,
52+
BlobReadChannelContext blobReadChannelContext) {
5253
this.storageObject = storageObject;
5354
this.opts = opts;
54-
this.clientStuff = clientStuff;
55+
this.blobReadChannelContext = blobReadChannelContext;
5556
this.byteRangeSpec = ByteRangeSpec.nullRange();
5657
}
5758

@@ -116,7 +117,8 @@ public synchronized int read(ByteBuffer dst) throws IOException {
116117
@Override
117118
public RestorableState<ReadChannel> capture() {
118119
ApiaryReadRequest apiaryReadRequest = getApiaryReadRequest();
119-
return new BlobReadChannelV2State(apiaryReadRequest, clientStuff, chunkSize);
120+
return new BlobReadChannelV2State(
121+
apiaryReadRequest, blobReadChannelContext.getStorageOptions(), chunkSize);
120122
}
121123

122124
private void maybeResetChannel(boolean umallocBuffer) throws IOException {
@@ -147,7 +149,7 @@ private LazyReadChannel<StorageObject> newLazyReadChannel() {
147149
}
148150
return ResumableMedia.http()
149151
.read()
150-
.byteChannel(clientStuff)
152+
.byteChannel(blobReadChannelContext)
151153
.setCallback(this::setResolvedObject)
152154
.buffered(bufferHandle)
153155
.setApiaryReadRequest(getApiaryReadRequest())
@@ -170,21 +172,22 @@ static class BlobReadChannelV2State implements RestorableState<ReadChannel>, Ser
170172
private static final long serialVersionUID = -7595661593080505431L;
171173

172174
private final ApiaryReadRequest request;
173-
private final ClientStuff clientStuff;
175+
private final HttpStorageOptions options;
174176

175177
private final Integer chunkSize;
176178

177179
private BlobReadChannelV2State(
178-
ApiaryReadRequest request, ClientStuff clientStuff, Integer chunkSize) {
180+
ApiaryReadRequest request, HttpStorageOptions options, Integer chunkSize) {
179181
this.request = request;
180-
this.clientStuff = clientStuff;
182+
this.options = options;
181183
this.chunkSize = chunkSize;
182184
}
183185

184186
@Override
185187
public ReadChannel restore() {
186188
BlobReadChannelV2 channel =
187-
new BlobReadChannelV2(request.getObject(), request.getOptions(), clientStuff);
189+
new BlobReadChannelV2(
190+
request.getObject(), request.getOptions(), BlobReadChannelContext.from(options));
188191
channel.setByteRangeSpec(request.getByteRangeSpec());
189192
if (chunkSize != null) {
190193
channel.setChunkSize(chunkSize);
@@ -202,32 +205,31 @@ public boolean equals(Object o) {
202205
}
203206
BlobReadChannelV2State that = (BlobReadChannelV2State) o;
204207
return Objects.equals(request, that.request)
205-
&& Objects.equals(clientStuff, that.clientStuff)
208+
&& Objects.equals(options, that.options)
206209
&& Objects.equals(chunkSize, that.chunkSize);
207210
}
208211

209212
@Override
210213
public int hashCode() {
211-
return Objects.hash(request, clientStuff, chunkSize);
214+
return Objects.hash(request, options, chunkSize);
212215
}
213216

214217
@Override
215218
public String toString() {
216219
return MoreObjects.toStringHelper(this)
217-
.add("\nrequest", request)
218-
.add("\nclientStuff", clientStuff)
219-
.add("\nchunkSize", chunkSize)
220+
.add("request", request)
221+
.add("options", options)
222+
.add("chunkSize", chunkSize)
220223
.toString();
221224
}
222225
}
223226

224-
static final class ClientStuff implements Serializable {
225-
private static final long serialVersionUID = 4244938428650333730L;
227+
static final class BlobReadChannelContext {
226228
private final HttpStorageOptions storageOptions;
227229
private final HttpRetryAlgorithmManager retryAlgorithmManager;
228-
private transient Storage apiaryClient;
230+
private final Storage apiaryClient;
229231

230-
private ClientStuff(
232+
private BlobReadChannelContext(
231233
HttpStorageOptions storageOptions,
232234
Storage apiaryClient,
233235
HttpRetryAlgorithmManager retryAlgorithmManager) {
@@ -248,20 +250,16 @@ public Storage getApiaryClient() {
248250
return apiaryClient;
249251
}
250252

251-
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
252-
in.defaultReadObject();
253-
this.apiaryClient = storageOptions.getStorageRpcV1().getStorage();
254-
}
255-
256-
static ClientStuff from(HttpStorageOptions options) {
257-
return new ClientStuff(
253+
static BlobReadChannelContext from(HttpStorageOptions options) {
254+
return new BlobReadChannelContext(
258255
options, options.getStorageRpcV1().getStorage(), options.getRetryAlgorithmManager());
259256
}
260257

261-
static ClientStuff from(com.google.cloud.storage.Storage s) {
262-
if (s instanceof StorageImpl) {
263-
StorageImpl impl = (StorageImpl) s;
264-
return from(impl.getOptions());
258+
static BlobReadChannelContext from(com.google.cloud.storage.Storage s) {
259+
StorageOptions options = s.getOptions();
260+
if (options instanceof HttpStorageOptions) {
261+
HttpStorageOptions httpStorageOptions = (HttpStorageOptions) options;
262+
return from(httpStorageOptions);
265263
}
266264
throw new IllegalArgumentException("Only HttpStorageOptions based instance supported");
267265
}
@@ -271,10 +269,10 @@ public boolean equals(Object o) {
271269
if (this == o) {
272270
return true;
273271
}
274-
if (!(o instanceof ClientStuff)) {
272+
if (!(o instanceof BlobReadChannelContext)) {
275273
return false;
276274
}
277-
ClientStuff that = (ClientStuff) o;
275+
BlobReadChannelContext that = (BlobReadChannelContext) o;
278276
return Objects.equals(storageOptions, that.storageOptions)
279277
&& Objects.equals(retryAlgorithmManager, that.retryAlgorithmManager);
280278
}
@@ -286,10 +284,7 @@ public int hashCode() {
286284

287285
@Override
288286
public String toString() {
289-
return MoreObjects.toStringHelper(this)
290-
.add("\nstorageOptions", storageOptions)
291-
.add("\nretryAlgorithmManager", retryAlgorithmManager)
292-
.toString();
287+
return MoreObjects.toStringHelper(this).add("storageOptions", storageOptions).toString();
293288
}
294289
}
295290
}

google-cloud-storage/src/main/java/com/google/cloud/storage/HttpDownloadSessionBuilder.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.google.api.core.SettableApiFuture;
2323
import com.google.api.services.storage.model.StorageObject;
2424
import com.google.cloud.storage.ApiaryUnbufferedReadableByteChannel.ApiaryReadRequest;
25-
import com.google.cloud.storage.BlobReadChannelV2.ClientStuff;
25+
import com.google.cloud.storage.BlobReadChannelV2.BlobReadChannelContext;
2626
import com.google.cloud.storage.BufferedReadableByteChannelSession.BufferedReadableByteChannel;
2727
import com.google.cloud.storage.UnbufferedReadableByteChannelSession.UnbufferedReadableByteChannel;
2828
import java.nio.ByteBuffer;
@@ -42,20 +42,21 @@ public static HttpDownloadSessionBuilder create() {
4242
return INSTANCE;
4343
}
4444

45-
public ReadableByteChannelSessionBuilder byteChannel(ClientStuff clientStuff) {
46-
// TODO: refactor ClientStuff to push retry to a lower individual config
45+
public ReadableByteChannelSessionBuilder byteChannel(
46+
BlobReadChannelContext blobReadChannelContext) {
47+
// TODO: refactor BlobReadChannelContext to push retry to a lower individual config
4748
// similar to GapicWritableByteChannelSessionBuilder.ResumableUploadBuilder.withRetryConfig
48-
return new ReadableByteChannelSessionBuilder(clientStuff);
49+
return new ReadableByteChannelSessionBuilder(blobReadChannelContext);
4950
}
5051

5152
public static final class ReadableByteChannelSessionBuilder {
5253

53-
private final ClientStuff clientStuff;
54+
private final BlobReadChannelContext blobReadChannelContext;
5455
// private Hasher hasher; // TODO: wire in Hasher
5556
private Consumer<StorageObject> callback;
5657

57-
private ReadableByteChannelSessionBuilder(ClientStuff clientStuff) {
58-
this.clientStuff = clientStuff;
58+
private ReadableByteChannelSessionBuilder(BlobReadChannelContext blobReadChannelContext) {
59+
this.blobReadChannelContext = blobReadChannelContext;
5960
}
6061

6162
public ReadableByteChannelSessionBuilder setCallback(Consumer<StorageObject> callback) {
@@ -86,10 +87,10 @@ public UnbufferedReadableByteChannelSessionBuilder unbuffered() {
8687
return (request, resultFuture) ->
8788
new ApiaryUnbufferedReadableByteChannel(
8889
request,
89-
clientStuff.getApiaryClient(),
90+
blobReadChannelContext.getApiaryClient(),
9091
resultFuture,
91-
clientStuff.getStorageOptions(),
92-
clientStuff.getRetryAlgorithmManager().idempotent(),
92+
blobReadChannelContext.getStorageOptions(),
93+
blobReadChannelContext.getRetryAlgorithmManager().idempotent(),
9394
callback);
9495
}
9596

google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import com.google.cloud.ReadChannel;
3838
import com.google.cloud.WriteChannel;
3939
import com.google.cloud.storage.Acl.Entity;
40-
import com.google.cloud.storage.BlobReadChannelV2.ClientStuff;
40+
import com.google.cloud.storage.BlobReadChannelV2.BlobReadChannelContext;
4141
import com.google.cloud.storage.HmacKey.HmacKeyMetadata;
4242
import com.google.cloud.storage.PostPolicyV4.ConditionV4Type;
4343
import com.google.cloud.storage.PostPolicyV4.PostConditionsV4;
@@ -590,7 +590,7 @@ public ReadChannel reader(BlobId blob, BlobSourceOption... options) {
590590
Opts<ObjectSourceOpt> opts = Opts.unwrap(options).resolveFrom(blob);
591591
StorageObject storageObject = Conversions.apiary().blobId().encode(blob);
592592
ImmutableMap<StorageRpc.Option, ?> optionsMap = opts.getRpcOptions();
593-
return new BlobReadChannelV2(storageObject, optionsMap, ClientStuff.from(this));
593+
return new BlobReadChannelV2(storageObject, optionsMap, BlobReadChannelContext.from(this));
594594
}
595595

596596
@Override

google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.google.cloud.ReadChannel;
2828
import com.google.cloud.Restorable;
2929
import com.google.cloud.storage.Acl.Project.ProjectRole;
30-
import com.google.cloud.storage.BlobReadChannelV2.ClientStuff;
30+
import com.google.cloud.storage.BlobReadChannelV2.BlobReadChannelContext;
3131
import com.google.cloud.storage.Storage.BucketField;
3232
import com.google.cloud.storage.Storage.PredefinedAcl;
3333
import com.google.cloud.storage.UnifiedOpts.Opt;
@@ -192,6 +192,7 @@ protected Serializable[] serializableObjects() {
192192
}
193193

194194
@Override
195+
@SuppressWarnings("resource")
195196
protected Restorable<?>[] restorableObjects() {
196197
HttpStorageOptions options = HttpStorageOptions.newBuilder().setProjectId("p2").build();
197198
ResultRetryAlgorithm<?> algorithm =
@@ -201,9 +202,7 @@ protected Restorable<?>[] restorableObjects() {
201202
new BlobReadChannelV2(
202203
new StorageObject().setBucket("b").setName("n"),
203204
EMPTY_RPC_OPTIONS,
204-
ClientStuff.from(options));
205-
// avoid closing when you don't want partial writes to GCS upon failure
206-
@SuppressWarnings("resource")
205+
BlobReadChannelContext.from(options));
207206
BlobWriteChannel writer =
208207
new BlobWriteChannel(
209208
options, BlobInfo.newBuilder(BlobId.of("b", "n")).build(), "upload-id", algorithm);

0 commit comments

Comments
 (0)