Example #1
0
        private BlockReaderLocal(BlockReaderLocal.Builder builder)
        {
            this.replica    = builder.replica;
            this.dataIn     = replica.GetDataStream().GetChannel();
            this.dataPos    = builder.dataPos;
            this.checksumIn = replica.GetMetaStream().GetChannel();
            BlockMetadataHeader header = builder.replica.GetMetaHeader();

            this.checksum       = header.GetChecksum();
            this.verifyChecksum = builder.verifyChecksum && (this.checksum.GetChecksumType().
                                                             id != DataChecksum.ChecksumNull);
            this.filename           = builder.filename;
            this.block              = builder.block;
            this.bytesPerChecksum   = checksum.GetBytesPerChecksum();
            this.checksumSize       = checksum.GetChecksumSize();
            this.maxAllocatedChunks = (bytesPerChecksum == 0) ? 0 : ((builder.bufferSize + bytesPerChecksum
                                                                      - 1) / bytesPerChecksum);
            // Calculate the effective maximum readahead.
            // We can't do more readahead than there is space in the buffer.
            int maxReadaheadChunks = (bytesPerChecksum == 0) ? 0 : ((Math.Min(builder.bufferSize
                                                                              , builder.maxReadahead) + bytesPerChecksum - 1) / bytesPerChecksum);

            if (maxReadaheadChunks == 0)
            {
                this.zeroReadaheadRequested = true;
                maxReadaheadChunks          = 1;
            }
            else
            {
                this.zeroReadaheadRequested = false;
            }
            this.maxReadaheadLength = maxReadaheadChunks * bytesPerChecksum;
            this.storageType        = builder.storageType;
        }
Example #2
0
        /// <summary>Verifies the block's checksum.</summary>
        /// <remarks>Verifies the block's checksum. This is an I/O intensive operation.</remarks>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="Org.Apache.Hadoop.FS.ChecksumException"/>
        private static void VerifyChecksum(long length, FileInputStream metaIn, FileChannel
                                           blockChannel, string blockFileName)
        {
            // Verify the checksum from the block's meta file
            // Get the DataChecksum from the meta file header
            BlockMetadataHeader header = BlockMetadataHeader.ReadHeader(new DataInputStream(new
                                                                                            BufferedInputStream(metaIn, BlockMetadataHeader.GetHeaderSize())));
            FileChannel metaChannel = null;

            try
            {
                metaChannel = metaIn.GetChannel();
                if (metaChannel == null)
                {
                    throw new IOException("Block InputStream meta file has no FileChannel.");
                }
                DataChecksum checksum         = header.GetChecksum();
                int          bytesPerChecksum = checksum.GetBytesPerChecksum();
                int          checksumSize     = checksum.GetChecksumSize();
                int          numChunks        = (8 * 1024 * 1024) / bytesPerChecksum;
                ByteBuffer   blockBuf         = ByteBuffer.Allocate(numChunks * bytesPerChecksum);
                ByteBuffer   checksumBuf      = ByteBuffer.Allocate(numChunks * checksumSize);
                // Verify the checksum
                int bytesVerified = 0;
                while (bytesVerified < length)
                {
                    Preconditions.CheckState(bytesVerified % bytesPerChecksum == 0, "Unexpected partial chunk before EOF"
                                             );
                    System.Diagnostics.Debug.Assert(bytesVerified % bytesPerChecksum == 0);
                    int bytesRead = FillBuffer(blockChannel, blockBuf);
                    if (bytesRead == -1)
                    {
                        throw new IOException("checksum verification failed: premature EOF");
                    }
                    blockBuf.Flip();
                    // Number of read chunks, including partial chunk at end
                    int chunks = (bytesRead + bytesPerChecksum - 1) / bytesPerChecksum;
                    checksumBuf.Limit(chunks * checksumSize);
                    FillBuffer(metaChannel, checksumBuf);
                    checksumBuf.Flip();
                    checksum.VerifyChunkedSums(blockBuf, checksumBuf, blockFileName, bytesVerified);
                    // Success
                    bytesVerified += bytesRead;
                    blockBuf.Clear();
                    checksumBuf.Clear();
                }
            }
            finally
            {
                IOUtils.CloseQuietly(metaChannel);
            }
        }
 /// <summary>Convert the replica to a string for debugging purposes.</summary>
 /// <remarks>
 /// Convert the replica to a string for debugging purposes.
 /// Note that we can't take the lock here.
 /// </remarks>
 public override string ToString()
 {
     return(new StringBuilder().Append("ShortCircuitReplica{").Append("key=").Append(key
                                                                                     ).Append(", metaHeader.version=").Append(metaHeader.GetVersion()).Append(", metaHeader.checksum="
                                                                                                                                                              ).Append(metaHeader.GetChecksum()).Append(", ident=").Append("0x").Append(Sharpen.Extensions.ToHexString
                                                                                                                                                                                                                                            (Runtime.IdentityHashCode(this))).Append(", creationTimeMs=").Append(creationTimeMs
                                                                                                                                                                                                                                                                                                                 ).Append("}").ToString());
 }