001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.io.hfile;
020
021import java.io.ByteArrayInputStream;
022import java.io.Closeable;
023import java.io.DataInput;
024import java.io.DataInputStream;
025import java.io.DataOutputStream;
026import java.io.IOException;
027import java.io.SequenceInputStream;
028import java.net.InetSocketAddress;
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.Comparator;
032import java.util.List;
033import java.util.Map;
034import java.util.Optional;
035import java.util.Set;
036import java.util.SortedMap;
037import java.util.TreeMap;
038import java.util.concurrent.atomic.LongAdder;
039
040import org.apache.commons.io.IOUtils;
041import org.apache.hadoop.conf.Configuration;
042import org.apache.hadoop.fs.FSDataInputStream;
043import org.apache.hadoop.fs.FSDataOutputStream;
044import org.apache.hadoop.fs.FileStatus;
045import org.apache.hadoop.fs.FileSystem;
046import org.apache.hadoop.fs.Path;
047import org.apache.hadoop.fs.PathFilter;
048import org.apache.hadoop.hbase.Cell;
049import org.apache.hadoop.hbase.CellComparator;
050import org.apache.hadoop.hbase.HConstants;
051import org.apache.yetus.audience.InterfaceAudience;
052import org.slf4j.Logger;
053import org.slf4j.LoggerFactory;
054import org.apache.hadoop.hbase.fs.HFileSystem;
055import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper;
056import org.apache.hadoop.hbase.io.MetricsIO;
057import org.apache.hadoop.hbase.io.MetricsIOWrapperImpl;
058import org.apache.hadoop.hbase.io.compress.Compression;
059import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
060import org.apache.hadoop.hbase.protobuf.ProtobufMagic;
061import org.apache.hadoop.hbase.regionserver.CellSink;
062import org.apache.hadoop.hbase.regionserver.ShipperListener;
063import org.apache.hbase.thirdparty.com.google.protobuf.UnsafeByteOperations;
064import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
065import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
066import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.BytesBytesPair;
067import org.apache.hadoop.hbase.shaded.protobuf.generated.HFileProtos;
068import org.apache.hadoop.hbase.util.BloomFilterWriter;
069import org.apache.hadoop.hbase.util.Bytes;
070import org.apache.hadoop.hbase.util.FSUtils;
071import org.apache.hadoop.io.Writable;
072
073import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
074import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
075
076/**
077 * File format for hbase.
078 * A file of sorted key/value pairs. Both keys and values are byte arrays.
079 * <p>
080 * The memory footprint of a HFile includes the following (below is taken from the
081 * <a
082 * href=https://issues.apache.org/jira/browse/HADOOP-3315>TFile</a> documentation
083 * but applies also to HFile):
084 * <ul>
085 * <li>Some constant overhead of reading or writing a compressed block.
086 * <ul>
087 * <li>Each compressed block requires one compression/decompression codec for
088 * I/O.
089 * <li>Temporary space to buffer the key.
090 * <li>Temporary space to buffer the value.
091 * </ul>
092 * <li>HFile index, which is proportional to the total number of Data Blocks.
093 * The total amount of memory needed to hold the index can be estimated as
094 * (56+AvgKeySize)*NumBlocks.
095 * </ul>
096 * Suggestions on performance optimization.
097 * <ul>
098 * <li>Minimum block size. We recommend a setting of minimum block size between
099 * 8KB to 1MB for general usage. Larger block size is preferred if files are
100 * primarily for sequential access. However, it would lead to inefficient random
101 * access (because there are more data to decompress). Smaller blocks are good
102 * for random access, but require more memory to hold the block index, and may
103 * be slower to create (because we must flush the compressor stream at the
104 * conclusion of each data block, which leads to an FS I/O flush). Further, due
105 * to the internal caching in Compression codec, the smallest possible block
106 * size would be around 20KB-30KB.
107 * <li>The current implementation does not offer true multi-threading for
108 * reading. The implementation uses FSDataInputStream seek()+read(), which is
109 * shown to be much faster than positioned-read call in single thread mode.
110 * However, it also means that if multiple threads attempt to access the same
111 * HFile (using multiple scanners) simultaneously, the actual I/O is carried out
112 * sequentially even if they access different DFS blocks (Reexamine! pread seems
113 * to be 10% faster than seek+read in my testing -- stack).
114 * <li>Compression codec. Use "none" if the data is not very compressable (by
115 * compressable, I mean a compression ratio at least 2:1). Generally, use "lzo"
116 * as the starting point for experimenting. "gz" overs slightly better
117 * compression ratio over "lzo" but requires 4x CPU to compress and 2x CPU to
118 * decompress, comparing to "lzo".
119 * </ul>
120 *
121 * For more on the background behind HFile, see <a
122 * href=https://issues.apache.org/jira/browse/HBASE-61>HBASE-61</a>.
123 * <p>
124 * File is made of data blocks followed by meta data blocks (if any), a fileinfo
125 * block, data block index, meta data block index, and a fixed size trailer
126 * which records the offsets at which file changes content type.
127 * <pre>&lt;data blocks&gt;&lt;meta blocks&gt;&lt;fileinfo&gt;&lt;
128 * data index&gt;&lt;meta index&gt;&lt;trailer&gt;</pre>
129 * Each block has a bit of magic at its start.  Block are comprised of
130 * key/values.  In data blocks, they are both byte arrays.  Metadata blocks are
131 * a String key and a byte array value.  An empty file looks like this:
132 * <pre>&lt;fileinfo&gt;&lt;trailer&gt;</pre>.  That is, there are not data nor meta
133 * blocks present.
134 * <p>
135 * TODO: Do scanners need to be able to take a start and end row?
136 * TODO: Should BlockIndex know the name of its file?  Should it have a Path
137 * that points at its file say for the case where an index lives apart from
138 * an HFile instance?
139 */
140@InterfaceAudience.Private
141public class HFile {
142  // LOG is being used in HFileBlock and CheckSumUtil
143  static final Logger LOG = LoggerFactory.getLogger(HFile.class);
144
145  /**
146   * Maximum length of key in HFile.
147   */
148  public final static int MAXIMUM_KEY_LENGTH = Integer.MAX_VALUE;
149
150  /**
151   * Default compression: none.
152   */
153  public final static Compression.Algorithm DEFAULT_COMPRESSION_ALGORITHM =
154    Compression.Algorithm.NONE;
155
156  /** Minimum supported HFile format version */
157  public static final int MIN_FORMAT_VERSION = 2;
158
159  /** Maximum supported HFile format version
160   */
161  public static final int MAX_FORMAT_VERSION = 3;
162
163  /**
164   * Minimum HFile format version with support for persisting cell tags
165   */
166  public static final int MIN_FORMAT_VERSION_WITH_TAGS = 3;
167
168  /** Default compression name: none. */
169  public final static String DEFAULT_COMPRESSION =
170    DEFAULT_COMPRESSION_ALGORITHM.getName();
171
172  /** Meta data block name for bloom filter bits. */
173  public static final String BLOOM_FILTER_DATA_KEY = "BLOOM_FILTER_DATA";
174
175  /**
176   * We assume that HFile path ends with
177   * ROOT_DIR/TABLE_NAME/REGION_NAME/CF_NAME/HFILE, so it has at least this
178   * many levels of nesting. This is needed for identifying table and CF name
179   * from an HFile path.
180   */
181  public final static int MIN_NUM_HFILE_PATH_LEVELS = 5;
182
183  /**
184   * The number of bytes per checksum.
185   */
186  public static final int DEFAULT_BYTES_PER_CHECKSUM = 16 * 1024;
187
188  // For measuring number of checksum failures
189  static final LongAdder CHECKSUM_FAILURES = new LongAdder();
190
191  // For tests. Gets incremented when we read a block whether from HDFS or from Cache.
192  public static final LongAdder DATABLOCK_READ_COUNT = new LongAdder();
193
194  /** Static instance for the metrics so that HFileReaders access the same instance */
195  static final MetricsIO metrics = new MetricsIO(new MetricsIOWrapperImpl());
196
197  /**
198   * Number of checksum verification failures. It also
199   * clears the counter.
200   */
201  public static final long getAndResetChecksumFailuresCount() {
202    return CHECKSUM_FAILURES.sumThenReset();
203  }
204
205  /**
206   * Number of checksum verification failures. It also
207   * clears the counter.
208   */
209  public static final long getChecksumFailuresCount() {
210    return CHECKSUM_FAILURES.sum();
211  }
212
213  public static final void updateReadLatency(long latencyMillis, boolean pread) {
214    if (pread) {
215      metrics.updateFsPreadTime(latencyMillis);
216    } else {
217      metrics.updateFsReadTime(latencyMillis);
218    }
219  }
220
221  public static final void updateWriteLatency(long latencyMillis) {
222    metrics.updateFsWriteTime(latencyMillis);
223  }
224
225  /** API required to write an {@link HFile} */
226  public interface Writer extends Closeable, CellSink, ShipperListener {
227    /** Max memstore (mvcc) timestamp in FileInfo */
228    public static final byte [] MAX_MEMSTORE_TS_KEY = Bytes.toBytes("MAX_MEMSTORE_TS_KEY");
229
230    /** Add an element to the file info map. */
231    void appendFileInfo(byte[] key, byte[] value) throws IOException;
232
233    /** @return the path to this {@link HFile} */
234    Path getPath();
235
236    /**
237     * Adds an inline block writer such as a multi-level block index writer or
238     * a compound Bloom filter writer.
239     */
240    void addInlineBlockWriter(InlineBlockWriter bloomWriter);
241
242    // The below three methods take Writables.  We'd like to undo Writables but undoing the below would be pretty
243    // painful.  Could take a byte [] or a Message but we want to be backward compatible around hfiles so would need
244    // to map between Message and Writable or byte [] and current Writable serialization.  This would be a bit of work
245    // to little gain.  Thats my thinking at moment.  St.Ack 20121129
246
247    void appendMetaBlock(String bloomFilterMetaKey, Writable metaWriter);
248
249    /**
250     * Store general Bloom filter in the file. This does not deal with Bloom filter
251     * internals but is necessary, since Bloom filters are stored differently
252     * in HFile version 1 and version 2.
253     */
254    void addGeneralBloomFilter(BloomFilterWriter bfw);
255
256    /**
257     * Store delete family Bloom filter in the file, which is only supported in
258     * HFile V2.
259     */
260    void addDeleteFamilyBloomFilter(BloomFilterWriter bfw) throws IOException;
261
262    /**
263     * Return the file context for the HFile this writer belongs to
264     */
265    HFileContext getFileContext();
266  }
267
268  /**
269   * This variety of ways to construct writers is used throughout the code, and
270   * we want to be able to swap writer implementations.
271   */
272  public static class WriterFactory {
273    protected final Configuration conf;
274    protected final CacheConfig cacheConf;
275    protected FileSystem fs;
276    protected Path path;
277    protected FSDataOutputStream ostream;
278    protected CellComparator comparator = CellComparator.getInstance();
279    protected InetSocketAddress[] favoredNodes;
280    private HFileContext fileContext;
281    protected boolean shouldDropBehind = false;
282
283    WriterFactory(Configuration conf, CacheConfig cacheConf) {
284      this.conf = conf;
285      this.cacheConf = cacheConf;
286    }
287
288    public WriterFactory withPath(FileSystem fs, Path path) {
289      Preconditions.checkNotNull(fs);
290      Preconditions.checkNotNull(path);
291      this.fs = fs;
292      this.path = path;
293      return this;
294    }
295
296    public WriterFactory withOutputStream(FSDataOutputStream ostream) {
297      Preconditions.checkNotNull(ostream);
298      this.ostream = ostream;
299      return this;
300    }
301
302    public WriterFactory withComparator(CellComparator comparator) {
303      Preconditions.checkNotNull(comparator);
304      this.comparator = comparator;
305      return this;
306    }
307
308    public WriterFactory withFavoredNodes(InetSocketAddress[] favoredNodes) {
309      // Deliberately not checking for null here.
310      this.favoredNodes = favoredNodes;
311      return this;
312    }
313
314    public WriterFactory withFileContext(HFileContext fileContext) {
315      this.fileContext = fileContext;
316      return this;
317    }
318
319    public WriterFactory withShouldDropCacheBehind(boolean shouldDropBehind) {
320      this.shouldDropBehind = shouldDropBehind;
321      return this;
322    }
323
324
325    public Writer create() throws IOException {
326      if ((path != null ? 1 : 0) + (ostream != null ? 1 : 0) != 1) {
327        throw new AssertionError("Please specify exactly one of " +
328            "filesystem/path or path");
329      }
330      if (path != null) {
331        ostream = HFileWriterImpl.createOutputStream(conf, fs, path, favoredNodes);
332        try {
333          ostream.setDropBehind(shouldDropBehind && cacheConf.shouldDropBehindCompaction());
334        } catch (UnsupportedOperationException uoe) {
335          LOG.trace("Unable to set drop behind on {}", path, uoe);
336          LOG.debug("Unable to set drop behind on {}", path.getName());
337        }
338      }
339      return new HFileWriterImpl(conf, cacheConf, path, ostream, comparator, fileContext);
340    }
341  }
342
343  /** The configuration key for HFile version to use for new files */
344  public static final String FORMAT_VERSION_KEY = "hfile.format.version";
345
346  public static int getFormatVersion(Configuration conf) {
347    int version = conf.getInt(FORMAT_VERSION_KEY, MAX_FORMAT_VERSION);
348    checkFormatVersion(version);
349    return version;
350  }
351
352  /**
353   * Returns the factory to be used to create {@link HFile} writers.
354   * Disables block cache access for all writers created through the
355   * returned factory.
356   */
357  public static final WriterFactory getWriterFactoryNoCache(Configuration
358       conf) {
359    return HFile.getWriterFactory(conf, CacheConfig.DISABLED);
360  }
361
362  /**
363   * Returns the factory to be used to create {@link HFile} writers
364   */
365  public static final WriterFactory getWriterFactory(Configuration conf,
366      CacheConfig cacheConf) {
367    int version = getFormatVersion(conf);
368    switch (version) {
369    case 2:
370      throw new IllegalArgumentException("This should never happen. " +
371        "Did you change hfile.format.version to read v2? This version of the software writes v3" +
372        " hfiles only (but it can read v2 files without having to update hfile.format.version " +
373        "in hbase-site.xml)");
374    case 3:
375      return new HFile.WriterFactory(conf, cacheConf);
376    default:
377      throw new IllegalArgumentException("Cannot create writer for HFile " +
378          "format version " + version);
379    }
380  }
381
382  /**
383   * An abstraction used by the block index.
384   * Implementations will check cache for any asked-for block and return cached block if found.
385   * Otherwise, after reading from fs, will try and put block into cache before returning.
386   */
387  public interface CachingBlockReader {
388    /**
389     * Read in a file block.
390     * @param offset offset to read.
391     * @param onDiskBlockSize size of the block
392     * @param cacheBlock
393     * @param pread
394     * @param isCompaction is this block being read as part of a compaction
395     * @param expectedBlockType the block type we are expecting to read with this read operation,
396     *  or null to read whatever block type is available and avoid checking (that might reduce
397     *  caching efficiency of encoded data blocks)
398     * @param expectedDataBlockEncoding the data block encoding the caller is expecting data blocks
399     *  to be in, or null to not perform this check and return the block irrespective of the
400     *  encoding. This check only applies to data blocks and can be set to null when the caller is
401     *  expecting to read a non-data block and has set expectedBlockType accordingly.
402     * @return Block wrapped in a ByteBuffer.
403     * @throws IOException
404     */
405    HFileBlock readBlock(long offset, long onDiskBlockSize,
406        boolean cacheBlock, final boolean pread, final boolean isCompaction,
407        final boolean updateCacheMetrics, BlockType expectedBlockType,
408        DataBlockEncoding expectedDataBlockEncoding)
409        throws IOException;
410  }
411
412  /** An interface used by clients to open and iterate an {@link HFile}. */
413  public interface Reader extends Closeable, CachingBlockReader {
414    /**
415     * Returns this reader's "name". Usually the last component of the path.
416     * Needs to be constant as the file is being moved to support caching on
417     * write.
418     */
419    String getName();
420
421    CellComparator getComparator();
422
423    HFileScanner getScanner(boolean cacheBlocks, final boolean pread, final boolean isCompaction);
424
425    HFileBlock getMetaBlock(String metaBlockName, boolean cacheBlock) throws IOException;
426
427    Map<byte[], byte[]> loadFileInfo() throws IOException;
428
429    Optional<Cell> getLastKey();
430
431    Optional<Cell> midKey() throws IOException;
432
433    long length();
434
435    long getEntries();
436
437    Optional<Cell> getFirstKey();
438
439    long indexSize();
440
441    Optional<byte[]> getFirstRowKey();
442
443    Optional<byte[]> getLastRowKey();
444
445    FixedFileTrailer getTrailer();
446
447    HFileBlockIndex.BlockIndexReader getDataBlockIndexReader();
448
449    HFileScanner getScanner(boolean cacheBlocks, boolean pread);
450
451    Compression.Algorithm getCompressionAlgorithm();
452
453    /**
454     * Retrieves general Bloom filter metadata as appropriate for each
455     * {@link HFile} version.
456     * Knows nothing about how that metadata is structured.
457     */
458    DataInput getGeneralBloomFilterMetadata() throws IOException;
459
460    /**
461     * Retrieves delete family Bloom filter metadata as appropriate for each
462     * {@link HFile}  version.
463     * Knows nothing about how that metadata is structured.
464     */
465    DataInput getDeleteBloomFilterMetadata() throws IOException;
466
467    Path getPath();
468
469    /** Close method with optional evictOnClose */
470    void close(boolean evictOnClose) throws IOException;
471
472    DataBlockEncoding getDataBlockEncoding();
473
474    boolean hasMVCCInfo();
475
476    /**
477     * Return the file context of the HFile this reader belongs to
478     */
479    HFileContext getFileContext();
480
481    boolean isPrimaryReplicaReader();
482
483    boolean shouldIncludeMemStoreTS();
484
485    boolean isDecodeMemStoreTS();
486
487    DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction);
488
489    @VisibleForTesting
490    HFileBlock.FSReader getUncachedBlockReader();
491
492    @VisibleForTesting
493    boolean prefetchComplete();
494
495    /**
496     * To close the stream's socket. Note: This can be concurrently called from multiple threads and
497     * implementation should take care of thread safety.
498     */
499    void unbufferStream();
500  }
501
502  /**
503   * Method returns the reader given the specified arguments.
504   * TODO This is a bad abstraction.  See HBASE-6635.
505   *
506   * @param path hfile's path
507   * @param fsdis stream of path's file
508   * @param size max size of the trailer.
509   * @param cacheConf Cache configuation values, cannot be null.
510   * @param hfs
511   * @param primaryReplicaReader true if this is a reader for primary replica
512   * @return an appropriate instance of HFileReader
513   * @throws IOException If file is invalid, will throw CorruptHFileException flavored IOException
514   */
515  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="SF_SWITCH_FALLTHROUGH",
516      justification="Intentional")
517  private static Reader openReader(Path path, FSDataInputStreamWrapper fsdis, long size,
518      CacheConfig cacheConf, HFileSystem hfs, boolean primaryReplicaReader, Configuration conf)
519      throws IOException {
520    FixedFileTrailer trailer = null;
521    try {
522      boolean isHBaseChecksum = fsdis.shouldUseHBaseChecksum();
523      assert !isHBaseChecksum; // Initially we must read with FS checksum.
524      trailer = FixedFileTrailer.readFromStream(fsdis.getStream(isHBaseChecksum), size);
525      switch (trailer.getMajorVersion()) {
526        case 2:
527          LOG.debug("Opening HFile v2 with v3 reader");
528          // Fall through. FindBugs: SF_SWITCH_FALLTHROUGH
529        case 3:
530          return new HFileReaderImpl(path, trailer, fsdis, size, cacheConf, hfs,
531              primaryReplicaReader, conf);
532        default:
533          throw new IllegalArgumentException("Invalid HFile version " + trailer.getMajorVersion());
534      }
535    } catch (Throwable t) {
536      IOUtils.closeQuietly(fsdis);
537      throw new CorruptHFileException("Problem reading HFile Trailer from file " + path, t);
538    } finally {
539      fsdis.unbuffer();
540    }
541  }
542
543  /**
544   * The sockets and the file descriptors held by the method parameter
545   * {@code FSDataInputStreamWrapper} passed will be freed after its usage so caller needs to ensure
546   * that no other threads have access to the same passed reference.
547   * @param fs A file system
548   * @param path Path to HFile
549   * @param fsdis a stream of path's file
550   * @param size max size of the trailer.
551   * @param cacheConf Cache configuration for hfile's contents
552   * @param primaryReplicaReader true if this is a reader for primary replica
553   * @param conf Configuration
554   * @return A version specific Hfile Reader
555   * @throws IOException If file is invalid, will throw CorruptHFileException flavored IOException
556   */
557  public static Reader createReader(FileSystem fs, Path path, FSDataInputStreamWrapper fsdis,
558      long size, CacheConfig cacheConf, boolean primaryReplicaReader, Configuration conf)
559      throws IOException {
560    HFileSystem hfs = null;
561
562    // If the fs is not an instance of HFileSystem, then create an
563    // instance of HFileSystem that wraps over the specified fs.
564    // In this case, we will not be able to avoid checksumming inside
565    // the filesystem.
566    if (!(fs instanceof HFileSystem)) {
567      hfs = new HFileSystem(fs);
568    } else {
569      hfs = (HFileSystem) fs;
570    }
571    return openReader(path, fsdis, size, cacheConf, hfs, primaryReplicaReader, conf);
572  }
573
574  /**
575  * Creates reader with cache configuration disabled
576  * @param fs filesystem
577  * @param path Path to file to read
578  * @return an active Reader instance
579  * @throws IOException Will throw a CorruptHFileException
580  * (DoNotRetryIOException subtype) if hfile is corrupt/invalid.
581  */
582  public static Reader createReader(FileSystem fs, Path path, Configuration conf)
583      throws IOException {
584    // The primaryReplicaReader is mainly used for constructing block cache key, so if we do not use
585    // block cache then it is OK to set it as any value. We use true here.
586    return createReader(fs, path, CacheConfig.DISABLED, true, conf);
587  }
588
589  /**
590   * @param fs filesystem
591   * @param path Path to file to read
592   * @param cacheConf This must not be null. @see
593   *          {@link org.apache.hadoop.hbase.io.hfile.CacheConfig#CacheConfig(Configuration)}
594   * @param primaryReplicaReader true if this is a reader for primary replica
595   * @return an active Reader instance
596   * @throws IOException Will throw a CorruptHFileException (DoNotRetryIOException subtype) if hfile
597   *           is corrupt/invalid.
598   */
599  public static Reader createReader(FileSystem fs, Path path, CacheConfig cacheConf,
600      boolean primaryReplicaReader, Configuration conf) throws IOException {
601    Preconditions.checkNotNull(cacheConf, "Cannot create Reader with null CacheConf");
602    FSDataInputStreamWrapper stream = new FSDataInputStreamWrapper(fs, path);
603    return openReader(path, stream, fs.getFileStatus(path).getLen(), cacheConf,
604      stream.getHfs(), primaryReplicaReader, conf);
605  }
606
607  /**
608   * This factory method is used only by unit tests. <br/>
609   * The sockets and the file descriptors held by the method parameter
610   * {@code FSDataInputStreamWrapper} passed will be freed after its usage so caller needs to ensure
611   * that no other threads have access to the same passed reference.
612   */
613  @VisibleForTesting
614  static Reader createReaderFromStream(Path path, FSDataInputStream fsdis, long size,
615      CacheConfig cacheConf, Configuration conf) throws IOException {
616    FSDataInputStreamWrapper wrapper = new FSDataInputStreamWrapper(fsdis);
617    return openReader(path, wrapper, size, cacheConf, null, true, conf);
618  }
619
620  /**
621   * Returns true if the specified file has a valid HFile Trailer.
622   * @param fs filesystem
623   * @param path Path to file to verify
624   * @return true if the file has a valid HFile Trailer, otherwise false
625   * @throws IOException if failed to read from the underlying stream
626   */
627  public static boolean isHFileFormat(final FileSystem fs, final Path path) throws IOException {
628    return isHFileFormat(fs, fs.getFileStatus(path));
629  }
630
631  /**
632   * Returns true if the specified file has a valid HFile Trailer.
633   * @param fs filesystem
634   * @param fileStatus the file to verify
635   * @return true if the file has a valid HFile Trailer, otherwise false
636   * @throws IOException if failed to read from the underlying stream
637   */
638  public static boolean isHFileFormat(final FileSystem fs, final FileStatus fileStatus)
639      throws IOException {
640    final Path path = fileStatus.getPath();
641    final long size = fileStatus.getLen();
642    try (FSDataInputStreamWrapper fsdis = new FSDataInputStreamWrapper(fs, path)) {
643      boolean isHBaseChecksum = fsdis.shouldUseHBaseChecksum();
644      assert !isHBaseChecksum; // Initially we must read with FS checksum.
645      FixedFileTrailer.readFromStream(fsdis.getStream(isHBaseChecksum), size);
646      return true;
647    } catch (IllegalArgumentException e) {
648      return false;
649    }
650  }
651
652  /**
653   * Metadata for this file. Conjured by the writer. Read in by the reader.
654   */
655  public static class FileInfo implements SortedMap<byte[], byte[]> {
656    static final String RESERVED_PREFIX = "hfile.";
657    static final byte[] RESERVED_PREFIX_BYTES = Bytes.toBytes(RESERVED_PREFIX);
658    static final byte [] LASTKEY = Bytes.toBytes(RESERVED_PREFIX + "LASTKEY");
659    static final byte [] AVG_KEY_LEN = Bytes.toBytes(RESERVED_PREFIX + "AVG_KEY_LEN");
660    static final byte [] AVG_VALUE_LEN = Bytes.toBytes(RESERVED_PREFIX + "AVG_VALUE_LEN");
661    static final byte [] CREATE_TIME_TS = Bytes.toBytes(RESERVED_PREFIX + "CREATE_TIME_TS");
662    static final byte [] COMPARATOR = Bytes.toBytes(RESERVED_PREFIX + "COMPARATOR");
663    static final byte [] TAGS_COMPRESSED = Bytes.toBytes(RESERVED_PREFIX + "TAGS_COMPRESSED");
664    public static final byte [] MAX_TAGS_LEN = Bytes.toBytes(RESERVED_PREFIX + "MAX_TAGS_LEN");
665    private final SortedMap<byte [], byte []> map = new TreeMap<>(Bytes.BYTES_COMPARATOR);
666
667    public FileInfo() {
668      super();
669    }
670
671    /**
672     * Append the given key/value pair to the file info, optionally checking the
673     * key prefix.
674     *
675     * @param k key to add
676     * @param v value to add
677     * @param checkPrefix whether to check that the provided key does not start
678     *          with the reserved prefix
679     * @return this file info object
680     * @throws IOException if the key or value is invalid
681     */
682    public FileInfo append(final byte[] k, final byte[] v,
683        final boolean checkPrefix) throws IOException {
684      if (k == null || v == null) {
685        throw new NullPointerException("Key nor value may be null");
686      }
687      if (checkPrefix && isReservedFileInfoKey(k)) {
688        throw new IOException("Keys with a " + FileInfo.RESERVED_PREFIX
689            + " are reserved");
690      }
691      put(k, v);
692      return this;
693    }
694
695    @Override
696    public void clear() {
697      this.map.clear();
698    }
699
700    @Override
701    public Comparator<? super byte[]> comparator() {
702      return map.comparator();
703    }
704
705    @Override
706    public boolean containsKey(Object key) {
707      return map.containsKey(key);
708    }
709
710    @Override
711    public boolean containsValue(Object value) {
712      return map.containsValue(value);
713    }
714
715    @Override
716    public Set<java.util.Map.Entry<byte[], byte[]>> entrySet() {
717      return map.entrySet();
718    }
719
720    @Override
721    public boolean equals(Object o) {
722      return map.equals(o);
723    }
724
725    @Override
726    public byte[] firstKey() {
727      return map.firstKey();
728    }
729
730    @Override
731    public byte[] get(Object key) {
732      return map.get(key);
733    }
734
735    @Override
736    public int hashCode() {
737      return map.hashCode();
738    }
739
740    @Override
741    public SortedMap<byte[], byte[]> headMap(byte[] toKey) {
742      return this.map.headMap(toKey);
743    }
744
745    @Override
746    public boolean isEmpty() {
747      return map.isEmpty();
748    }
749
750    @Override
751    public Set<byte[]> keySet() {
752      return map.keySet();
753    }
754
755    @Override
756    public byte[] lastKey() {
757      return map.lastKey();
758    }
759
760    @Override
761    public byte[] put(byte[] key, byte[] value) {
762      return this.map.put(key, value);
763    }
764
765    @Override
766    public void putAll(Map<? extends byte[], ? extends byte[]> m) {
767      this.map.putAll(m);
768    }
769
770    @Override
771    public byte[] remove(Object key) {
772      return this.map.remove(key);
773    }
774
775    @Override
776    public int size() {
777      return map.size();
778    }
779
780    @Override
781    public SortedMap<byte[], byte[]> subMap(byte[] fromKey, byte[] toKey) {
782      return this.map.subMap(fromKey, toKey);
783    }
784
785    @Override
786    public SortedMap<byte[], byte[]> tailMap(byte[] fromKey) {
787      return this.map.tailMap(fromKey);
788    }
789
790    @Override
791    public Collection<byte[]> values() {
792      return map.values();
793    }
794
795    /**
796     * Write out this instance on the passed in <code>out</code> stream.
797     * We write it as a protobuf.
798     * @param out
799     * @throws IOException
800     * @see #read(DataInputStream)
801     */
802    void write(final DataOutputStream out) throws IOException {
803      HFileProtos.FileInfoProto.Builder builder = HFileProtos.FileInfoProto.newBuilder();
804      for (Map.Entry<byte [], byte[]> e: this.map.entrySet()) {
805        HBaseProtos.BytesBytesPair.Builder bbpBuilder = HBaseProtos.BytesBytesPair.newBuilder();
806        bbpBuilder.setFirst(UnsafeByteOperations.unsafeWrap(e.getKey()));
807        bbpBuilder.setSecond(UnsafeByteOperations.unsafeWrap(e.getValue()));
808        builder.addMapEntry(bbpBuilder.build());
809      }
810      out.write(ProtobufMagic.PB_MAGIC);
811      builder.build().writeDelimitedTo(out);
812    }
813
814    /**
815     * Populate this instance with what we find on the passed in <code>in</code> stream.
816     * Can deserialize protobuf of old Writables format.
817     * @param in
818     * @throws IOException
819     * @see #write(DataOutputStream)
820     */
821    void read(final DataInputStream in) throws IOException {
822      // This code is tested over in TestHFileReaderV1 where we read an old hfile w/ this new code.
823      int pblen = ProtobufUtil.lengthOfPBMagic();
824      byte [] pbuf = new byte[pblen];
825      if (in.markSupported()) in.mark(pblen);
826      int read = in.read(pbuf);
827      if (read != pblen) throw new IOException("read=" + read + ", wanted=" + pblen);
828      if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
829        parsePB(HFileProtos.FileInfoProto.parseDelimitedFrom(in));
830      } else {
831        if (in.markSupported()) {
832          in.reset();
833          parseWritable(in);
834        } else {
835          // We cannot use BufferedInputStream, it consumes more than we read from the underlying IS
836          ByteArrayInputStream bais = new ByteArrayInputStream(pbuf);
837          SequenceInputStream sis = new SequenceInputStream(bais, in); // Concatenate input streams
838          // TODO: Am I leaking anything here wrapping the passed in stream?  We are not calling close on the wrapped
839          // streams but they should be let go after we leave this context?  I see that we keep a reference to the
840          // passed in inputstream but since we no longer have a reference to this after we leave, we should be ok.
841          parseWritable(new DataInputStream(sis));
842        }
843      }
844    }
845
846    /** Now parse the old Writable format.  It was a list of Map entries.  Each map entry was a key and a value of
847     * a byte [].  The old map format had a byte before each entry that held a code which was short for the key or
848     * value type.  We know it was a byte [] so in below we just read and dump it.
849     * @throws IOException
850     */
851    void parseWritable(final DataInputStream in) throws IOException {
852      // First clear the map.  Otherwise we will just accumulate entries every time this method is called.
853      this.map.clear();
854      // Read the number of entries in the map
855      int entries = in.readInt();
856      // Then read each key/value pair
857      for (int i = 0; i < entries; i++) {
858        byte [] key = Bytes.readByteArray(in);
859        // We used to read a byte that encoded the class type.  Read and ignore it because it is always byte [] in hfile
860        in.readByte();
861        byte [] value = Bytes.readByteArray(in);
862        this.map.put(key, value);
863      }
864    }
865
866    /**
867     * Fill our map with content of the pb we read off disk
868     * @param fip protobuf message to read
869     */
870    void parsePB(final HFileProtos.FileInfoProto fip) {
871      this.map.clear();
872      for (BytesBytesPair pair: fip.getMapEntryList()) {
873        this.map.put(pair.getFirst().toByteArray(), pair.getSecond().toByteArray());
874      }
875    }
876  }
877
878  /** Return true if the given file info key is reserved for internal use. */
879  public static boolean isReservedFileInfoKey(byte[] key) {
880    return Bytes.startsWith(key, FileInfo.RESERVED_PREFIX_BYTES);
881  }
882
883  /**
884   * Get names of supported compression algorithms. The names are acceptable by
885   * HFile.Writer.
886   *
887   * @return Array of strings, each represents a supported compression
888   *         algorithm. Currently, the following compression algorithms are
889   *         supported.
890   *         <ul>
891   *         <li>"none" - No compression.
892   *         <li>"gz" - GZIP compression.
893   *         </ul>
894   */
895  public static String[] getSupportedCompressionAlgorithms() {
896    return Compression.getSupportedAlgorithms();
897  }
898
899  // Utility methods.
900  /*
901   * @param l Long to convert to an int.
902   * @return <code>l</code> cast as an int.
903   */
904  static int longToInt(final long l) {
905    // Expecting the size() of a block not exceeding 4GB. Assuming the
906    // size() will wrap to negative integer if it exceeds 2GB (From tfile).
907    return (int)(l & 0x00000000ffffffffL);
908  }
909
910  /**
911   * Returns all HFiles belonging to the given region directory. Could return an
912   * empty list.
913   *
914   * @param fs  The file system reference.
915   * @param regionDir  The region directory to scan.
916   * @return The list of files found.
917   * @throws IOException When scanning the files fails.
918   */
919  static List<Path> getStoreFiles(FileSystem fs, Path regionDir)
920      throws IOException {
921    List<Path> regionHFiles = new ArrayList<>();
922    PathFilter dirFilter = new FSUtils.DirFilter(fs);
923    FileStatus[] familyDirs = fs.listStatus(regionDir, dirFilter);
924    for(FileStatus dir : familyDirs) {
925      FileStatus[] files = fs.listStatus(dir.getPath());
926      for (FileStatus file : files) {
927        if (!file.isDirectory() &&
928            (!file.getPath().toString().contains(HConstants.HREGION_OLDLOGDIR_NAME)) &&
929            (!file.getPath().toString().contains(HConstants.RECOVERED_EDITS_DIR))) {
930          regionHFiles.add(file.getPath());
931        }
932      }
933    }
934    return regionHFiles;
935  }
936
937  /**
938   * Checks the given {@link HFile} format version, and throws an exception if
939   * invalid. Note that if the version number comes from an input file and has
940   * not been verified, the caller needs to re-throw an {@link IOException} to
941   * indicate that this is not a software error, but corrupted input.
942   *
943   * @param version an HFile version
944   * @throws IllegalArgumentException if the version is invalid
945   */
946  public static void checkFormatVersion(int version)
947      throws IllegalArgumentException {
948    if (version < MIN_FORMAT_VERSION || version > MAX_FORMAT_VERSION) {
949      throw new IllegalArgumentException("Invalid HFile version: " + version
950          + " (expected to be " + "between " + MIN_FORMAT_VERSION + " and "
951          + MAX_FORMAT_VERSION + ")");
952    }
953  }
954
955
956  public static void checkHFileVersion(final Configuration c) {
957    int version = c.getInt(FORMAT_VERSION_KEY, MAX_FORMAT_VERSION);
958    if (version < MAX_FORMAT_VERSION || version > MAX_FORMAT_VERSION) {
959      throw new IllegalArgumentException("The setting for " + FORMAT_VERSION_KEY +
960        " (in your hbase-*.xml files) is " + version + " which does not match " +
961        MAX_FORMAT_VERSION +
962        "; are you running with a configuration from an older or newer hbase install (an " +
963        "incompatible hbase-default.xml or hbase-site.xml on your CLASSPATH)?");
964    }
965  }
966
967  public static void main(String[] args) throws Exception {
968    // delegate to preserve old behavior
969    HFilePrettyPrinter.main(args);
970  }
971}