|
49 | 49 | import java.nio.channels.ScatteringByteChannel; |
50 | 50 | import java.util.List; |
51 | 51 | import java.util.Locale; |
| 52 | +import java.util.OptionalLong; |
52 | 53 | import java.util.concurrent.ArrayBlockingQueue; |
53 | 54 | import java.util.concurrent.CancellationException; |
54 | 55 | import java.util.concurrent.ExecutionException; |
@@ -91,7 +92,15 @@ final class GapicUnbufferedReadableByteChannel |
91 | 92 | this.result = result; |
92 | 93 | this.read = read; |
93 | 94 | this.req = req; |
94 | | - this.hasher = hasher; |
| 95 | + this.hasher = |
| 96 | + (req.getReadOffset() == 0 && !(hasher instanceof Hasher.NoOpHasher)) |
| 97 | + ? new CumulativeHasher( |
| 98 | + hasher, |
| 99 | + 0, |
| 100 | + req.getReadLimit() <= 0 |
| 101 | + ? OptionalLong.empty() |
| 102 | + : OptionalLong.of(req.getReadLimit())) |
| 103 | + : hasher; |
95 | 104 | this.fetchOffset = new AtomicLong(req.getReadOffset()); |
96 | 105 | this.blobOffset = req.getReadOffset(); |
97 | 106 | this.retrier = retrier; |
@@ -154,7 +163,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { |
154 | 163 | if (take instanceof IOException) { |
155 | 164 | IOException ioe = (IOException) take; |
156 | 165 | if (alg.shouldRetry(ioe, null)) { |
157 | | - readObjectObserver = null; |
| 166 | + cancelAndDrainCurrentObserver(); |
158 | 167 | continue; |
159 | 168 | } else { |
160 | 169 | ioe.addSuppressed(new AsyncStorageTaskException()); |
@@ -165,7 +174,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { |
165 | 174 | Throwable throwable = (Throwable) take; |
166 | 175 | BaseServiceException coalesce = StorageException.coalesce(throwable); |
167 | 176 | if (alg.shouldRetry(coalesce, null)) { |
168 | | - readObjectObserver = null; |
| 177 | + cancelAndDrainCurrentObserver(); |
169 | 178 | continue; |
170 | 179 | } else { |
171 | 180 | close(); |
@@ -174,6 +183,7 @@ public long read(ByteBuffer[] dsts, int offset, int length) throws IOException { |
174 | 183 | } |
175 | 184 | if (take == EOF_MARKER) { |
176 | 185 | complete = true; |
| 186 | + validateCumulativeChecksum(); |
177 | 187 | break; |
178 | 188 | } |
179 | 189 |
|
@@ -240,7 +250,9 @@ private void drainQueue() throws IOException { |
240 | 250 | while (queue.nonEmpty()) { |
241 | 251 | try { |
242 | 252 | java.lang.Object queueValue = queue.poll(); |
243 | | - if (queueValue instanceof ReadObjectResponse) { |
| 253 | + if (queueValue instanceof java.io.Closeable) { |
| 254 | + ((java.io.Closeable) queueValue).close(); |
| 255 | + } else if (queueValue instanceof ReadObjectResponse) { |
244 | 256 | ReadObjectResponse resp = (ReadObjectResponse) queueValue; |
245 | 257 | ResponseContentLifecycleHandle<ReadObjectResponse> handle = |
246 | 258 | read.getResponseContentLifecycleManager().get(resp); |
@@ -273,6 +285,19 @@ private void drainQueue() throws IOException { |
273 | 285 | } |
274 | 286 | } |
275 | 287 |
|
| 288 | + private void cancelAndDrainCurrentObserver() { |
| 289 | + if (readObjectObserver != null) { |
| 290 | + readObjectObserver.cancel(); |
| 291 | + try { |
| 292 | + drainQueue(); |
| 293 | + } catch (IOException e) { |
| 294 | + // drainQueue() in this context can be ignored because we are resetting the |
| 295 | + // stream. |
| 296 | + } |
| 297 | + readObjectObserver = null; |
| 298 | + } |
| 299 | + } |
| 300 | + |
276 | 301 | ApiFuture<Object> getResult() { |
277 | 302 | return result; |
278 | 303 | } |
@@ -311,14 +336,27 @@ private IOException createError(String message) throws IOException { |
311 | 336 | return new IOException(message, cause); |
312 | 337 | } |
313 | 338 |
|
| 339 | + private void validateCumulativeChecksum() throws IOException { |
| 340 | + if (hasher instanceof CumulativeHasher) { |
| 341 | + CumulativeHasher cumulativeHasher = (CumulativeHasher) hasher; |
| 342 | + try { |
| 343 | + cumulativeHasher.validateCumulativeChecksum(metadata); |
| 344 | + } catch (UncheckedCumulativeChecksumMismatchException exception) { |
| 345 | + throw new IOException(StorageException.coalesce(exception)); |
| 346 | + } |
| 347 | + } |
| 348 | + } |
| 349 | + |
314 | 350 | private final class ReadObjectObserver extends StateCheckingResponseObserver<ReadObjectResponse> { |
315 | 351 |
|
316 | 352 | private final SettableApiFuture<Void> open = SettableApiFuture.create(); |
317 | 353 | private final SettableApiFuture<Throwable> cancellation = SettableApiFuture.create(); |
318 | 354 |
|
319 | 355 | private volatile StreamController controller; |
| 356 | + private volatile boolean cancelled = false; |
320 | 357 |
|
321 | 358 | void cancel() { |
| 359 | + cancelled = true; |
322 | 360 | controller.cancel(); |
323 | 361 | } |
324 | 362 |
|
@@ -331,10 +369,13 @@ protected void onStartImpl(StreamController controller) { |
331 | 369 |
|
332 | 370 | @Override |
333 | 371 | protected void onResponseImpl(ReadObjectResponse response) { |
334 | | - controller.request(1); |
335 | | - open.set(null); |
336 | 372 | try (ResponseContentLifecycleHandle<ReadObjectResponse> handle = |
337 | 373 | read.getResponseContentLifecycleManager().get(response)) { |
| 374 | + if (cancelled) { |
| 375 | + return; |
| 376 | + } |
| 377 | + controller.request(1); |
| 378 | + open.set(null); |
338 | 379 | ChecksummedData checksummedData = response.getChecksummedData(); |
339 | 380 | ByteString content = checksummedData.getContent(); |
340 | 381 | int contentSize = content.size(); |
@@ -348,6 +389,8 @@ protected void onResponseImpl(ReadObjectResponse response) { |
348 | 389 | queue.offer(e); |
349 | 390 | return; |
350 | 391 | } |
| 392 | + } else if (hasher instanceof CumulativeHasher) { |
| 393 | + hasher.validateUnchecked(null, content); |
351 | 394 | } |
352 | 395 | if (response.hasMetadata()) { |
353 | 396 | Object respMetadata = response.getMetadata(); |
@@ -380,6 +423,12 @@ protected void onResponseImpl(ReadObjectResponse response) { |
380 | 423 |
|
381 | 424 | @Override |
382 | 425 | protected void onErrorImpl(Throwable t) { |
| 426 | + if (t instanceof CancellationException) { |
| 427 | + cancellation.set(t); |
| 428 | + } |
| 429 | + if (cancelled) { |
| 430 | + return; |
| 431 | + } |
383 | 432 | if (t instanceof OutOfRangeException) { |
384 | 433 | try { |
385 | 434 | queue.offer(EOF_MARKER); |
@@ -389,17 +438,15 @@ protected void onErrorImpl(Throwable t) { |
389 | 438 | throw Code.ABORTED.toStatus().withCause(e).asRuntimeException(); |
390 | 439 | } |
391 | 440 | } |
392 | | - if (t instanceof CancellationException) { |
393 | | - cancellation.set(t); |
394 | | - } |
395 | 441 | if (!open.isDone()) { |
396 | 442 | open.setException(t); |
397 | | - } |
398 | | - try { |
399 | | - queue.offer(t); |
400 | | - } catch (InterruptedException e) { |
401 | | - Thread.currentThread().interrupt(); |
402 | | - throw Code.ABORTED.toStatus().withCause(e).asRuntimeException(); |
| 443 | + } else { |
| 444 | + try { |
| 445 | + queue.offer(t); |
| 446 | + } catch (InterruptedException e) { |
| 447 | + Thread.currentThread().interrupt(); |
| 448 | + throw Code.ABORTED.toStatus().withCause(e).asRuntimeException(); |
| 449 | + } |
403 | 450 | } |
404 | 451 | } |
405 | 452 |
|
|