-
Notifications
You must be signed in to change notification settings - Fork 70
/
ApiaryUnbufferedReadableByteChannel.java
338 lines (306 loc) · 11.5 KB
/
ApiaryUnbufferedReadableByteChannel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright 2022 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;
import static com.google.cloud.storage.Utils.ifNonNull;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpResponseException;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.retrying.ResultRetryAlgorithm;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.Storage.Objects;
import com.google.api.services.storage.Storage.Objects.Get;
import com.google.api.services.storage.model.StorageObject;
import com.google.cloud.storage.UnbufferedReadableByteChannelSession.UnbufferedReadableByteChannel;
import com.google.cloud.storage.spi.v1.StorageRpc;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.annotation.concurrent.Immutable;
class ApiaryUnbufferedReadableByteChannel implements UnbufferedReadableByteChannel {
private final ApiaryReadRequest apiaryReadRequest;
private final Storage storage;
private final SettableApiFuture<StorageObject> result;
private final HttpStorageOptions options;
private final ResultRetryAlgorithm<?> resultRetryAlgorithm;
private final Consumer<StorageObject> resolvedObjectCallback;
private long position;
private ScatteringByteChannel sbc;
private boolean open;
// returned X-Goog-Generation header value
private Long xGoogGeneration;
ApiaryUnbufferedReadableByteChannel(
ApiaryReadRequest apiaryReadRequest,
Storage storage,
SettableApiFuture<StorageObject> result,
HttpStorageOptions options,
ResultRetryAlgorithm<?> resultRetryAlgorithm,
Consumer<StorageObject> resolvedObjectCallback) {
this.apiaryReadRequest = apiaryReadRequest;
this.storage = storage;
this.result = result;
this.options = options;
this.resultRetryAlgorithm = resultRetryAlgorithm;
this.resolvedObjectCallback = resolvedObjectCallback;
this.open = true;
this.position =
apiaryReadRequest.getByteRangeSpec() != null
? apiaryReadRequest.getByteRangeSpec().beginOffset()
: 0;
}
@SuppressWarnings("UnnecessaryContinue")
@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
do {
if (sbc == null) {
sbc = Retrying.run(options, resultRetryAlgorithm, this::open, Function.identity());
}
try {
// According to the contract of Retrying#run it's possible for sbc to be null even after
// invocation. However, the function we provide is guaranteed to return non-null or throw
// an exception. So we suppress the warning from intellij here.
//noinspection ConstantConditions
long read = sbc.read(dsts, offset, length);
if (read == -1) {
open = false;
} else {
position += read;
}
return read;
} catch (Exception t) {
if (resultRetryAlgorithm.shouldRetry(t, null)) {
// if our retry algorithm COULD allow a retry, continue the loop and allow trying to
// open the stream again.
sbc = null;
continue;
} else if (t instanceof IOException) {
IOException ioE = (IOException) t;
if (resultRetryAlgorithm.shouldRetry(StorageException.translate(ioE), null)) {
sbc = null;
continue;
} else {
throw ioE;
}
} else {
throw new IOException(StorageException.coalesce(t));
}
}
} while (true);
}
@Override
public boolean isOpen() {
return open;
}
@Override
public void close() throws IOException {
open = false;
if (sbc != null) {
sbc.close();
}
}
private ScatteringByteChannel open() {
try {
Boolean b =
(Boolean) apiaryReadRequest.options.get(StorageRpc.Option.RETURN_RAW_INPUT_STREAM);
boolean returnRawInputStream = b != null ? b : true;
ApiaryReadRequest request = apiaryReadRequest.withNewBeginOffset(position);
Get get = createGetRequest(request, storage.objects(), xGoogGeneration, returnRawInputStream);
HttpResponse media = get.executeMedia();
InputStream content = media.getContent();
if (xGoogGeneration == null) {
HttpHeaders responseHeaders = media.getHeaders();
//noinspection unchecked
List<String> xGoogGenHeader = (List<String>) responseHeaders.get("x-goog-generation");
// TODO: wire in result metadata population
if (xGoogGenHeader != null && !xGoogGenHeader.isEmpty()) {
String s = xGoogGenHeader.get(0);
Long generation = Long.valueOf(s);
this.xGoogGeneration = generation;
resolvedObjectCallback.accept(
apiaryReadRequest.getObject().clone().setGeneration(generation));
}
}
ReadableByteChannel rbc = Channels.newChannel(content);
return StorageByteChannels.readable().asScatteringByteChannel(rbc);
} catch (HttpResponseException e) {
if (xGoogGeneration != null) {
int statusCode = e.getStatusCode();
if (statusCode == 404) {
throw new StorageException(404, "Failure while trying to resume download", e);
}
}
throw StorageException.translate(e);
} catch (IOException e) {
throw StorageException.translate(e);
} catch (Throwable t) {
throw StorageException.coalesce(t);
}
}
@VisibleForTesting
static Get createGetRequest(
ApiaryReadRequest apiaryReadRequest,
Objects objects,
Long xGoogGeneration,
boolean returnRawInputStream)
throws IOException {
StorageObject from = apiaryReadRequest.getObject();
Map<StorageRpc.Option, ?> options = apiaryReadRequest.getOptions();
Get get = objects.get(from.getBucket(), from.getName());
if (from.getGeneration() != null) {
get.setGeneration(from.getGeneration());
} else if (xGoogGeneration != null) {
get.setGeneration(xGoogGeneration);
}
ifNonNull(
options.get(StorageRpc.Option.IF_GENERATION_MATCH),
ApiaryUnbufferedReadableByteChannel::cast,
get::setIfGenerationMatch);
ifNonNull(
options.get(StorageRpc.Option.IF_GENERATION_NOT_MATCH),
ApiaryUnbufferedReadableByteChannel::cast,
get::setIfGenerationNotMatch);
ifNonNull(
options.get(StorageRpc.Option.IF_METAGENERATION_MATCH),
ApiaryUnbufferedReadableByteChannel::cast,
get::setIfMetagenerationMatch);
ifNonNull(
options.get(StorageRpc.Option.IF_METAGENERATION_NOT_MATCH),
ApiaryUnbufferedReadableByteChannel::cast,
get::setIfMetagenerationNotMatch);
ifNonNull(
options.get(StorageRpc.Option.USER_PROJECT),
ApiaryUnbufferedReadableByteChannel::cast,
get::setUserProject);
HttpHeaders headers = get.getRequestHeaders();
ifNonNull(
options.get(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY),
ApiaryUnbufferedReadableByteChannel::cast,
(String key) -> {
BaseEncoding base64 = BaseEncoding.base64();
HashFunction hashFunction = Hashing.sha256();
headers.set("x-goog-encryption-algorithm", "AES256");
headers.set("x-goog-encryption-key", key);
headers.set(
"x-goog-encryption-key-sha256",
base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
});
get.setReturnRawInputStream(returnRawInputStream);
String range = apiaryReadRequest.getByteRangeSpec().getHttpRangeHeader();
if (range != null) {
get.getRequestHeaders().setRange(range);
}
get.getMediaHttpDownloader().setDirectDownloadEnabled(true);
return get;
}
@SuppressWarnings("unchecked")
private static <T> T cast(Object o) {
return (T) o;
}
@Immutable
static final class ApiaryReadRequest implements Serializable {
private static final long serialVersionUID = -4059435314115374448L;
private static final Gson gson = new Gson();
private transient StorageObject object;
private final Map<StorageRpc.Option, ?> options;
private final ByteRangeSpec byteRangeSpec;
private volatile String objectJson;
ApiaryReadRequest(
StorageObject object, Map<StorageRpc.Option, ?> options, ByteRangeSpec byteRangeSpec) {
this.object = object;
this.options = options;
this.byteRangeSpec = byteRangeSpec;
}
StorageObject getObject() {
return object;
}
Map<StorageRpc.Option, ?> getOptions() {
return options;
}
ByteRangeSpec getByteRangeSpec() {
return byteRangeSpec;
}
ApiaryReadRequest withNewBeginOffset(long beginOffset) {
if (beginOffset > 0 && beginOffset != byteRangeSpec.beginOffset()) {
return new ApiaryReadRequest(
object, options, byteRangeSpec.withNewBeginOffset(beginOffset));
} else {
return this;
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ApiaryReadRequest)) {
return false;
}
ApiaryReadRequest that = (ApiaryReadRequest) o;
return java.util.Objects.equals(object, that.object)
&& java.util.Objects.equals(options, that.options)
&& java.util.Objects.equals(byteRangeSpec, that.byteRangeSpec);
}
@Override
public int hashCode() {
return java.util.Objects.hash(object, options, byteRangeSpec);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("byteRangeSpec", byteRangeSpec)
.add("options", options)
.add("object", getObjectJson())
.toString();
}
private String getObjectJson() {
if (objectJson == null) {
synchronized (this) {
if (objectJson == null) {
objectJson = gson.toJson(object);
}
}
}
return objectJson;
}
private void writeObject(ObjectOutputStream out) throws IOException {
String ignore = getObjectJson();
out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
JsonReader jsonReader = gson.newJsonReader(new StringReader(this.objectJson));
this.object = gson.fromJson(jsonReader, StorageObject.class);
}
}
}