|
16 | 16 |
|
17 | 17 | package com.google.cloud.dataflow.sdk.io; |
18 | 18 |
|
| 19 | +import static com.google.common.base.Preconditions.checkState; |
| 20 | + |
19 | 21 | import com.google.cloud.dataflow.sdk.annotations.Experimental; |
20 | 22 | import com.google.cloud.dataflow.sdk.coders.AvroCoder; |
21 | 23 | import com.google.cloud.dataflow.sdk.options.PipelineOptions; |
|
38 | 40 | import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; |
39 | 41 | import org.apache.commons.compress.compressors.snappy.SnappyCompressorInputStream; |
40 | 42 | import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; |
| 43 | +import org.apache.commons.compress.utils.CountingInputStream; |
41 | 44 |
|
42 | 45 | import java.io.ByteArrayInputStream; |
| 46 | +import java.io.EOFException; |
43 | 47 | import java.io.IOException; |
44 | 48 | import java.io.InputStream; |
45 | 49 | import java.io.PushbackInputStream; |
46 | 50 | import java.nio.ByteBuffer; |
47 | 51 | import java.nio.channels.Channels; |
48 | 52 | import java.nio.channels.ReadableByteChannel; |
| 53 | +import java.nio.channels.SeekableByteChannel; |
| 54 | +import java.util.Arrays; |
49 | 55 | import java.util.Collection; |
50 | 56 | import java.util.zip.Inflater; |
51 | 57 | import java.util.zip.InflaterInputStream; |
52 | 58 |
|
| 59 | +import javax.annotation.concurrent.GuardedBy; |
| 60 | + |
53 | 61 | // CHECKSTYLE.OFF: JavadocStyle |
54 | 62 | /** |
55 | 63 | * A {@link FileBasedSource} for reading Avro files. |
@@ -437,35 +445,32 @@ public double getFractionOfBlockConsumed() { |
437 | 445 | * the total number of records in the block and the block's size in bytes, followed by the |
438 | 446 | * block's (optionally-encoded) records. Each block is terminated by a 16-bit sync marker. |
439 | 447 | * |
440 | | - * <p>Here, we consider the sync marker that precedes a block to be its offset, as this allows |
441 | | - * a reader that begins reading at that offset to detect the sync marker and the beginning of |
442 | | - * the block. |
443 | | - * |
444 | 448 | * @param <T> The type of records contained in the block. |
445 | 449 | */ |
446 | 450 | @Experimental(Experimental.Kind.SOURCE_SINK) |
447 | 451 | public static class AvroReader<T> extends BlockBasedReader<T> { |
448 | 452 | // The current block. |
449 | 453 | private AvroBlock<T> currentBlock; |
450 | 454 |
|
451 | | - // Offset of the block. |
| 455 | + // A lock used to synchronize block offsets for getRemainingParallelism |
| 456 | + private final Object progressLock = new Object(); |
| 457 | + |
| 458 | + // Offset of the current block. |
| 459 | + @GuardedBy("progressLock") |
452 | 460 | private long currentBlockOffset = 0; |
453 | 461 |
|
454 | 462 | // Size of the current block. |
| 463 | + @GuardedBy("progressLock") |
455 | 464 | private long currentBlockSizeBytes = 0; |
456 | 465 |
|
457 | | - // Current offset within the stream. |
458 | | - private long currentOffset = 0; |
459 | | - |
460 | 466 | // Stream used to read from the underlying file. |
461 | | - // A pushback stream is used to restore bytes buffered during seeking/decoding. |
| 467 | + // A pushback stream is used to restore bytes buffered during seeking. |
462 | 468 | private PushbackInputStream stream; |
| 469 | + // Counts the number of bytes read. Used only to tell how many bytes are taken up in |
| 470 | + // a block's variable-length header. |
| 471 | + private CountingInputStream countStream; |
463 | 472 |
|
464 | | - // Small buffer for reading encoded values from the stream. |
465 | | - // The maximum size of an encoded long is 10 bytes, and this buffer will be used to read two. |
466 | | - private final byte[] readBuffer = new byte[20]; |
467 | | - |
468 | | - // Decoder to decode binary-encoded values from the buffer. |
| 473 | + // Caches the Avro DirectBinaryDecoder used to decode binary-encoded values from the buffer. |
469 | 474 | private BinaryDecoder decoder; |
470 | 475 |
|
471 | 476 | /** |
@@ -480,51 +485,67 @@ public synchronized AvroSource<T> getCurrentSource() { |
480 | 485 | return (AvroSource<T>) super.getCurrentSource(); |
481 | 486 | } |
482 | 487 |
|
| 488 | + // Precondition: the stream is positioned after the sync marker in the current (about to be |
| 489 | + // previous) block. currentBlockSize equals the size of the current block, or zero if this |
| 490 | + // reader was just started. |
| 491 | + // |
| 492 | + // Postcondition: same as above, but for the new current (formerly next) block. |
483 | 493 | @Override |
484 | 494 | public boolean readNextBlock() throws IOException { |
485 | | - // The next block in the file is after the first sync marker that can be read starting from |
486 | | - // the current offset. First, we seek past the next sync marker, if it exists. After a sync |
487 | | - // marker is the start of a block. A block begins with the number of records contained in |
488 | | - // the block, encoded as a long, followed by the size of the block in bytes, encoded as a |
489 | | - // long. The currentOffset after this method should be last byte after this block, and the |
490 | | - // currentBlockOffset should be the start of the sync marker before this block. |
491 | | - |
492 | | - // Seek to the next sync marker, if one exists. |
493 | | - currentOffset += advancePastNextSyncMarker(stream, getCurrentSource().getSyncMarker()); |
494 | | - |
495 | | - // The offset of the current block includes its preceding sync marker. |
496 | | - currentBlockOffset = currentOffset - getCurrentSource().getSyncMarker().length; |
497 | | - |
498 | | - // Read a small buffer to parse the block header. |
499 | | - // We cannot use a BinaryDecoder to do this directly from the stream because a BinaryDecoder |
500 | | - // internally buffers data and we only want to read as many bytes from the stream as the size |
501 | | - // of the header. Though BinaryDecoder#InputStream returns an input stream that is aware of |
502 | | - // its internal buffering, we would have to re-wrap this input stream to seek for the next |
503 | | - // block in the file. |
504 | | - int read = stream.read(readBuffer); |
505 | | - // We reached the last sync marker in the file. |
506 | | - if (read <= 0) { |
| 495 | + long startOfNextBlock = currentBlockOffset + currentBlockSizeBytes; |
| 496 | + |
| 497 | + // Before reading the variable-sized block header, record the current number of bytes read. |
| 498 | + long preHeaderCount = countStream.getBytesRead(); |
| 499 | + decoder = DecoderFactory.get().directBinaryDecoder(countStream, decoder); |
| 500 | + long numRecords; |
| 501 | + try { |
| 502 | + numRecords = decoder.readLong(); |
| 503 | + } catch (EOFException e) { |
| 504 | + // Expected for the last block, at which the start position is the EOF. The way to detect |
| 505 | + // stream ending is to try reading from it. |
507 | 506 | return false; |
508 | 507 | } |
509 | | - decoder = DecoderFactory.get().binaryDecoder(readBuffer, decoder); |
510 | | - long numRecords = decoder.readLong(); |
511 | 508 | long blockSize = decoder.readLong(); |
512 | 509 |
|
513 | | - // The decoder buffers data internally, but since we know the size of the stream the |
514 | | - // decoder has constructed from the readBuffer, the number of bytes available in the |
515 | | - // input stream is equal to the number of unconsumed bytes. |
516 | | - int headerSize = readBuffer.length - decoder.inputStream().available(); |
517 | | - stream.unread(readBuffer, headerSize, read - headerSize); |
| 510 | + // Mark header size as the change in the number of bytes read. |
| 511 | + long headerSize = countStream.getBytesRead() - preHeaderCount; |
518 | 512 |
|
519 | 513 | // Create the current block by reading blockSize bytes. Block sizes permitted by the Avro |
520 | 514 | // specification are [32, 2^30], so this narrowing is ok. |
521 | 515 | byte[] data = new byte[(int) blockSize]; |
522 | | - stream.read(data); |
| 516 | + int read = stream.read(data); |
| 517 | + checkState(blockSize == read, "Only %s/%s bytes in the block were read", read, blockSize); |
523 | 518 | currentBlock = new AvroBlock<>(data, numRecords, getCurrentSource()); |
524 | | - currentBlockSizeBytes = blockSize; |
525 | 519 |
|
526 | | - // Update current offset with the number of bytes we read to get the next block. |
527 | | - currentOffset += headerSize + blockSize; |
| 520 | + // Read the end of this block, which MUST be a sync marker for correctness. |
| 521 | + byte[] syncMarker = getCurrentSource().getSyncMarker(); |
| 522 | + byte[] readSyncMarker = new byte[syncMarker.length]; |
| 523 | + long syncMarkerOffset = startOfNextBlock + headerSize + blockSize; |
| 524 | + long bytesRead = stream.read(readSyncMarker); |
| 525 | + checkState( |
| 526 | + bytesRead == syncMarker.length, |
| 527 | + "When trying to read a sync marker at position %s, only able to read %s/%s bytes", |
| 528 | + syncMarkerOffset, |
| 529 | + bytesRead, |
| 530 | + syncMarker.length); |
| 531 | + if (!Arrays.equals(syncMarker, readSyncMarker)) { |
| 532 | + throw new IllegalStateException( |
| 533 | + String.format( |
| 534 | + "Expected the bytes [%d,%d) in file %s to be a sync marker, but found %s", |
| 535 | + syncMarkerOffset, |
| 536 | + syncMarkerOffset + syncMarker.length, |
| 537 | + getCurrentSource().getFileOrPatternSpec(), |
| 538 | + Arrays.toString(readSyncMarker) |
| 539 | + )); |
| 540 | + } |
| 541 | + |
| 542 | + // Atomically update both the position and offset of the new block. |
| 543 | + synchronized (progressLock) { |
| 544 | + currentBlockOffset = startOfNextBlock; |
| 545 | + // Total block size includes the header, block content, and trailing sync marker. |
| 546 | + currentBlockSizeBytes = headerSize + blockSize + syncMarker.length; |
| 547 | + } |
| 548 | + |
528 | 549 | return true; |
529 | 550 | } |
530 | 551 |
|
@@ -535,32 +556,65 @@ public AvroBlock<T> getCurrentBlock() { |
535 | 556 |
|
536 | 557 | @Override |
537 | 558 | public long getCurrentBlockOffset() { |
538 | | - return currentBlockOffset; |
| 559 | + synchronized (progressLock) { |
| 560 | + return currentBlockOffset; |
| 561 | + } |
539 | 562 | } |
540 | 563 |
|
541 | 564 | @Override |
542 | 565 | public long getCurrentBlockSize() { |
543 | | - return currentBlockSizeBytes; |
| 566 | + synchronized (progressLock) { |
| 567 | + return currentBlockSizeBytes; |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + @Override |
| 572 | + public long getSplitPointsRemaining() { |
| 573 | + if (isDone()) { |
| 574 | + return 0; |
| 575 | + } |
| 576 | + synchronized (progressLock) { |
| 577 | + if (currentBlockOffset + currentBlockSizeBytes >= getCurrentSource().getEndOffset()) { |
| 578 | + // This block is known to be the last block in the range. |
| 579 | + return 1; |
| 580 | + } |
| 581 | + } |
| 582 | + return super.getSplitPointsRemaining(); |
544 | 583 | } |
545 | 584 |
|
546 | 585 | /** |
547 | 586 | * Creates a {@link PushbackInputStream} that has a large enough pushback buffer to be able |
548 | | - * to push back the syncBuffer and the readBuffer. |
| 587 | + * to push back the syncBuffer. |
549 | 588 | */ |
550 | 589 | private PushbackInputStream createStream(ReadableByteChannel channel) { |
551 | 590 | return new PushbackInputStream( |
552 | 591 | Channels.newInputStream(channel), |
553 | | - getCurrentSource().getSyncMarker().length + readBuffer.length); |
| 592 | + getCurrentSource().getSyncMarker().length); |
554 | 593 | } |
555 | 594 |
|
556 | | - /** |
557 | | - * Starts reading from the provided channel. Assumes that the channel is already seeked to |
558 | | - * the source's start offset. |
559 | | - */ |
| 595 | + // Postcondition: the stream is positioned at the beginning of the first block after the start |
| 596 | + // of the current source, and currentBlockOffset is that position. Additionally, |
| 597 | + // currentBlockSizeBytes will be set to 0 indicating that the previous block was empty. |
560 | 598 | @Override |
561 | 599 | protected void startReading(ReadableByteChannel channel) throws IOException { |
| 600 | + long startOffset = getCurrentSource().getStartOffset(); |
| 601 | + byte[] syncMarker = getCurrentSource().getSyncMarker(); |
| 602 | + long syncMarkerLength = syncMarker.length; |
| 603 | + |
| 604 | + if (startOffset != 0) { |
| 605 | + // Rewind order to find the sync marker ending the previous block. |
| 606 | + long position = Math.max(0, startOffset - syncMarkerLength); |
| 607 | + ((SeekableByteChannel) channel).position(position); |
| 608 | + startOffset = position; |
| 609 | + } |
| 610 | + |
| 611 | + // Satisfy the post condition. |
562 | 612 | stream = createStream(channel); |
563 | | - currentOffset = getCurrentSource().getStartOffset(); |
| 613 | + countStream = new CountingInputStream(stream); |
| 614 | + synchronized (progressLock) { |
| 615 | + currentBlockOffset = startOffset + advancePastNextSyncMarker(stream, syncMarker); |
| 616 | + currentBlockSizeBytes = 0; |
| 617 | + } |
564 | 618 | } |
565 | 619 |
|
566 | 620 | /** |
|