001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import java.io.IOException;
021import java.util.Optional;
022import org.apache.commons.lang3.mutable.MutableBoolean;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper;
026import org.apache.yetus.audience.InterfaceAudience;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Implementation of {@link HFile.Reader} to deal with pread.
032 */
033@InterfaceAudience.Private
034public class HFilePreadReader extends HFileReaderImpl {
035  private static final Logger LOG = LoggerFactory.getLogger(HFileReaderImpl.class);
036
037  public HFilePreadReader(ReaderContext context, HFileInfo fileInfo, CacheConfig cacheConf,
038    Configuration conf) throws IOException {
039    super(context, fileInfo, cacheConf, conf);
040    final MutableBoolean shouldCache = new MutableBoolean(true);
041
042    cacheConf.getBlockCache().ifPresent(cache -> {
043      Optional<Boolean> result = cache.shouldCacheFile(path.getName());
044      shouldCache.setValue(result.isPresent() ? result.get().booleanValue() : true);
045    });
046
047    // Prefetch file blocks upon open if requested
048    if (cacheConf.shouldPrefetchOnOpen() && cacheIfCompactionsOff() && shouldCache.booleanValue()) {
049      PrefetchExecutor.request(path, new Runnable() {
050        @Override
051        public void run() {
052          long offset = 0;
053          long end = 0;
054          HFile.Reader prefetchStreamReader = null;
055          try {
056            ReaderContext streamReaderContext = ReaderContextBuilder.newBuilder(context)
057              .withReaderType(ReaderContext.ReaderType.STREAM)
058              .withInputStreamWrapper(new FSDataInputStreamWrapper(context.getFileSystem(),
059                context.getInputStreamWrapper().getReaderPath()))
060              .build();
061            prefetchStreamReader =
062              new HFileStreamReader(streamReaderContext, fileInfo, cacheConf, conf);
063            end = getTrailer().getLoadOnOpenDataOffset();
064            if (LOG.isTraceEnabled()) {
065              LOG.trace("Prefetch start " + getPathOffsetEndStr(path, offset, end));
066            }
067            // Don't use BlockIterator here, because it's designed to read load-on-open section.
068            long onDiskSizeOfNextBlock = -1;
069            // if we are here, block cache is present anyways
070            BlockCache cache = cacheConf.getBlockCache().get();
071            boolean interrupted = false;
072            int blockCount = 0;
073            int dataBlockCount = 0;
074            while (offset < end) {
075              if (Thread.interrupted()) {
076                break;
077              }
078              // Some cache implementations can be persistent and resilient to restarts,
079              // so we check first if the block exists on its in-memory index, if so, we just
080              // update the offset and move on to the next block without actually going read all
081              // the way to the cache.
082              BlockCacheKey cacheKey = new BlockCacheKey(name, offset);
083              if (cache.isAlreadyCached(cacheKey).orElse(false)) {
084                // Right now, isAlreadyCached is only supported by BucketCache, which should
085                // always cache data blocks.
086                int size = cache.getBlockSize(cacheKey).orElse(0);
087                if (size > 0) {
088                  offset += size;
089                  LOG.debug("Found block of size {} for cache key {}. "
090                    + "Skipping prefetch, the block is already cached.", size, cacheKey);
091                  blockCount++;
092                  dataBlockCount++;
093                  continue;
094                } else {
095                  LOG.debug("Found block for cache key {}, but couldn't get its size. "
096                    + "Maybe the cache implementation doesn't support it? "
097                    + "We'll need to read the block from cache or file system. ", cacheKey);
098                }
099              } else {
100                LOG.debug("No entry in the backing map for cache key {}. ", cacheKey);
101              }
102              // Perhaps we got our block from cache? Unlikely as this may be, if it happens, then
103              // the internal-to-hfileblock thread local which holds the overread that gets the
104              // next header, will not have happened...so, pass in the onDiskSize gotten from the
105              // cached block. This 'optimization' triggers extremely rarely I'd say.
106              HFileBlock block = prefetchStreamReader.readBlock(offset, onDiskSizeOfNextBlock,
107                /* cacheBlock= */true, /* pread= */false, false, false, null, null, true);
108              try {
109                if (!cacheConf.isInMemory() && !cache.blockFitsIntoTheCache(block).orElse(true)) {
110                  LOG.warn(
111                    "Interrupting prefetch for file {} because block {} of size {} "
112                      + "doesn't fit in the available cache space.",
113                    path, cacheKey, block.getOnDiskSizeWithHeader());
114                  interrupted = true;
115                  break;
116                }
117                onDiskSizeOfNextBlock = block.getNextBlockOnDiskSize();
118                offset += block.getOnDiskSizeWithHeader();
119                blockCount++;
120                if (block.getBlockType().isData()) {
121                  dataBlockCount++;
122                }
123              } finally {
124                // Ideally here the readBlock won't find the block in cache. We call this
125                // readBlock so that block data is read from FS and cached in BC. we must call
126                // returnBlock here to decrease the reference count of block.
127                block.release();
128              }
129            }
130            if (!interrupted) {
131              cacheConf.getBlockCache().get().notifyFileCachingCompleted(path, blockCount,
132                dataBlockCount, offset);
133            }
134          } catch (IOException e) {
135            // IOExceptions are probably due to region closes (relocation, etc.)
136            if (LOG.isTraceEnabled()) {
137              LOG.trace("Prefetch " + getPathOffsetEndStr(path, offset, end), e);
138            }
139          } catch (Throwable e) {
140            // Other exceptions are interesting
141            LOG.warn("Prefetch " + getPathOffsetEndStr(path, offset, end), e);
142          } finally {
143            if (prefetchStreamReader != null) {
144              try {
145                prefetchStreamReader.close(false);
146              } catch (IOException e) {
147                LOG.warn("Close prefetch stream reader failed, path: " + path, e);
148              }
149            }
150            PrefetchExecutor.complete(path);
151          }
152        }
153      });
154    }
155  }
156
157  /*
158   * Get the region name for the given file path. A HFile is always kept under the <region>/<column
159   * family>/<hfile>. To find the region for a given hFile, just find the name of the grandparent
160   * directory.
161   */
162  private static String getRegionName(Path path) {
163    return path.getParent().getParent().getName();
164  }
165
166  private static String getPathOffsetEndStr(final Path path, final long offset, final long end) {
167    return "path=" + path.toString() + ", offset=" + offset + ", end=" + end;
168  }
169
170  public void close(boolean evictOnClose) throws IOException {
171    PrefetchExecutor.cancel(path);
172    // Deallocate blocks in load-on-open section
173    this.fileInfo.close();
174    // Deallocate data blocks
175    cacheConf.getBlockCache().ifPresent(cache -> {
176      if (evictOnClose) {
177        int numEvicted = cache.evictBlocksByHfileName(name);
178        if (LOG.isTraceEnabled()) {
179          LOG.trace("On close, file= {} evicted= {} block(s)", name, numEvicted);
180        }
181      }
182    });
183    fsBlockReader.closeStreams();
184  }
185}