Apache9 commented on a change in pull request #581: HBASE-22888 Share some stuffs with the initial reader when new stream reader created URL: https://github.com/apache/hbase/pull/581#discussion_r329341747
########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePreadReader.java ########## @@ -0,0 +1,213 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.hadoop.hbase.io.hfile; + +import java.io.IOException; +import java.security.Key; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.io.crypto.Cipher; +import org.apache.hadoop.hbase.io.crypto.Encryption; +import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo; +import org.apache.hadoop.hbase.io.hfile.HFile.Reader; +import org.apache.hadoop.hbase.security.EncryptionUtil; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of {@link HFile.Reader} to deal with pread. + */ [email protected] +public class HFilePreadReader extends HFileReaderImpl { + private static final Logger LOG = LoggerFactory.getLogger(HFileReaderImpl.class); + + /** + * The iterator will track all blocks in load-on-open section, since we use the + * {@link org.apache.hadoop.hbase.io.ByteBuffAllocator} to manage the ByteBuffers in block now, + * so we must ensure that deallocate all ByteBuffers in the end. + */ + private final HFileBlock.BlockIterator blockIter; + + public HFilePreadReader(ReaderContext context, FixedFileTrailer trailer, + CacheConfig cacheConf, Configuration conf) throws IOException { + super(context, cacheConf, conf); + this.trailer = trailer; + this.compressAlgo = trailer.getCompressionCodec(); + // Comparator class name is stored in the trailer in version 2. + this.comparator = trailer.createComparator(); + checkFileVersion(); + this.hfileContext = createHFileContext(context, trailer, conf); + this.fsBlockReader = new HFileBlock.FSReaderImpl(context, hfileContext, + cacheConf.getByteBuffAllocator()); + // Initialize an block iterator, and parse load-on-open blocks in the following. + this.blockIter = fsBlockReader.blockRange(trailer.getLoadOnOpenDataOffset(), + fileSize - trailer.getTrailerSize()); + + dataBlockIndexReader = new HFileBlockIndex.CellBasedKeyBlockIndexReader(comparator, + trailer.getNumDataIndexLevels()); + metaBlockIndexReader = new HFileBlockIndex.ByteArrayKeyBlockIndexReader(1); + // Data index. We also read statistics about the block index written after + // the root level. + dataBlockIndexReader.readMultiLevelIndexRoot( + blockIter.nextBlockWithBlockType(BlockType.ROOT_INDEX), + trailer.getDataIndexCount()); + // Meta index. + metaBlockIndexReader.readRootIndex( + blockIter.nextBlockWithBlockType(BlockType.ROOT_INDEX), + trailer.getMetaIndexCount()); + loadFileInfo(); + // Store all other load-on-open blocks for further consumption. + HFileBlock b; + while ((b = blockIter.nextBlock()) != null) { + loadOnOpenBlocks.add(b); + } + // Prefetch file blocks upon open if requested + if (cacheConf.shouldPrefetchOnOpen()) { + PrefetchExecutor.request(path, new Runnable() { + @Override + public void run() { + long offset = 0; + long end = 0; + try { + end = getTrailer().getLoadOnOpenDataOffset(); + if (LOG.isTraceEnabled()) { + LOG.trace("Prefetch start " + getPathOffsetEndStr(path, offset, end)); + } + // Don't use BlockIterator here, because it's designed to read load-on-open section. + long onDiskSizeOfNextBlock = -1; + while (offset < end) { + if (Thread.interrupted()) { + break; + } + // Perhaps we got our block from cache? Unlikely as this may be, if it happens, then + // the internal-to-hfileblock thread local which holds the overread that gets the + // next header, will not have happened...so, pass in the onDiskSize gotten from the + // cached block. This 'optimization' triggers extremely rarely I'd say. + HFileBlock block = readBlock(offset, onDiskSizeOfNextBlock, /* cacheBlock= */true, + /* pread= */true, false, false, null, null); + try { + onDiskSizeOfNextBlock = block.getNextBlockOnDiskSize(); + offset += block.getOnDiskSizeWithHeader(); + } finally { + // Ideally here the readBlock won't find the block in cache. We call this + // readBlock so that block data is read from FS and cached in BC. we must call + // returnBlock here to decrease the reference count of block. + block.release(); + } + } + } catch (IOException e) { + // IOExceptions are probably due to region closes (relocation, etc.) + if (LOG.isTraceEnabled()) { + LOG.trace("Prefetch " + getPathOffsetEndStr(path, offset, end), e); + } + } catch (NullPointerException e) { + LOG.warn("Stream moved/closed or prefetch cancelled?" + + getPathOffsetEndStr(path, offset, end), e); + } catch (Exception e) { + // Other exceptions are interesting + LOG.warn("Prefetch " + getPathOffsetEndStr(path, offset, end), e); + } finally { + PrefetchExecutor.complete(path); + } + } + }); + } + } + + @Override + public FileInfo loadFileInfo() throws IOException { Review comment: So this method will be removed from the reader interface. The FileInfo should be loaded when opening an HFile. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
