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.util;
020
021import static org.apache.hadoop.hbase.regionserver.HStoreFile.BULKLOAD_TIME_KEY;
022import static org.junit.Assert.assertArrayEquals;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.util.Arrays;
027import java.util.Locale;
028import java.util.Optional;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.hbase.ArrayBackedTag;
033import org.apache.hadoop.hbase.Cell;
034import org.apache.hadoop.hbase.KeyValue;
035import org.apache.hadoop.hbase.PrivateCellUtil;
036import org.apache.hadoop.hbase.Tag;
037import org.apache.hadoop.hbase.TagType;
038import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
039import org.apache.hadoop.hbase.client.Result;
040import org.apache.hadoop.hbase.client.ResultScanner;
041import org.apache.hadoop.hbase.client.Scan;
042import org.apache.hadoop.hbase.client.Table;
043import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
044import org.apache.hadoop.hbase.io.hfile.CacheConfig;
045import org.apache.hadoop.hbase.io.hfile.HFile;
046import org.apache.hadoop.hbase.io.hfile.HFileContext;
047import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
048import org.apache.hadoop.hbase.mob.MobUtils;
049
050/**
051 * Utility class for HFile-related testing.
052 */
053public class HFileTestUtil {
054
055  public static final String OPT_DATA_BLOCK_ENCODING_USAGE =
056    "Encoding algorithm (e.g. prefix "
057        + "compression) to use for data blocks in the test column family, "
058        + "one of " + Arrays.toString(DataBlockEncoding.values()) + ".";
059  public static final String OPT_DATA_BLOCK_ENCODING =
060      ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING.toLowerCase(Locale.ROOT);
061  /** Column family used by the test */
062  public static byte[] DEFAULT_COLUMN_FAMILY = Bytes.toBytes("test_cf");
063  /** Column families used by the test */
064  public static final byte[][] DEFAULT_COLUMN_FAMILIES = { DEFAULT_COLUMN_FAMILY };
065
066  /**
067   * Create an HFile with the given number of rows between a given
068   * start key and end key @ family:qualifier.  The value will be the key value.
069   * This file will not have tags.
070   */
071  public static void createHFile(
072      Configuration configuration,
073      FileSystem fs, Path path,
074      byte[] family, byte[] qualifier,
075      byte[] startKey, byte[] endKey, int numRows) throws IOException {
076      createHFile(configuration, fs, path, DataBlockEncoding.NONE, family, qualifier,
077        startKey, endKey, numRows, false);
078  }
079
080  /**
081   * Create an HFile with the given number of rows between a given
082   * start key and end key @ family:qualifier.  The value will be the key value.
083   * This file will use certain data block encoding algorithm.
084   */
085  public static void createHFileWithDataBlockEncoding(
086      Configuration configuration,
087      FileSystem fs, Path path, DataBlockEncoding encoding,
088      byte[] family, byte[] qualifier,
089      byte[] startKey, byte[] endKey, int numRows) throws IOException {
090      createHFile(configuration, fs, path, encoding, family, qualifier, startKey, endKey,
091        numRows, false);
092  }
093
094  /**
095   * Create an HFile with the given number of rows between a given
096   * start key and end key @ family:qualifier.  The value will be the key value.
097   * This cells will also have a tag whose value is the key.
098   */
099  public static void createHFileWithTags(
100      Configuration configuration,
101      FileSystem fs, Path path,
102      byte[] family, byte[] qualifier,
103      byte[] startKey, byte[] endKey, int numRows) throws IOException {
104      createHFile(configuration, fs, path, DataBlockEncoding.NONE, family, qualifier,
105        startKey, endKey, numRows, true);
106  }
107
108  /**
109   * Create an HFile with the given number of rows between a given
110   * start key and end key @ family:qualifier.
111   * If withTag is true, we add the rowKey as the tag value for
112   * tagtype MOB_TABLE_NAME_TAG_TYPE
113   */
114  public static void createHFile(
115      Configuration configuration,
116      FileSystem fs, Path path, DataBlockEncoding encoding,
117      byte[] family, byte[] qualifier,
118      byte[] startKey, byte[] endKey, int numRows, boolean withTag) throws IOException {
119    HFileContext meta = new HFileContextBuilder()
120        .withIncludesTags(withTag)
121        .withDataBlockEncoding(encoding)
122        .withColumnFamily(family)
123        .build();
124    HFile.Writer writer = HFile.getWriterFactory(configuration, new CacheConfig(configuration))
125        .withPath(fs, path)
126        .withFileContext(meta)
127        .create();
128    long now = EnvironmentEdgeManager.currentTime();
129    try {
130      // subtract 2 since iterateOnSplits doesn't include boundary keys
131      for (byte[] key : Bytes.iterateOnSplits(startKey, endKey, numRows - 2)) {
132        Cell kv = new KeyValue(key, family, qualifier, now, key);
133        if (withTag) {
134          // add a tag.  Arbitrarily chose mob tag since we have a helper already.
135          Tag tableNameTag = new ArrayBackedTag(TagType.MOB_TABLE_NAME_TAG_TYPE, key);
136          kv = MobUtils.createMobRefCell(kv, key, tableNameTag);
137
138          // verify that the kv has the tag.
139          Optional<Tag> tag = PrivateCellUtil.getTag(kv, TagType.MOB_TABLE_NAME_TAG_TYPE);
140          if (!tag.isPresent()) {
141            throw new IllegalStateException("Tag didn't stick to KV " + kv.toString());
142          }
143        }
144        writer.append(kv);
145      }
146    } finally {
147      writer.appendFileInfo(BULKLOAD_TIME_KEY,
148        Bytes.toBytes(EnvironmentEdgeManager.currentTime()));
149      writer.close();
150    }
151  }
152
153  /**
154   * This verifies that each cell has a tag that is equal to its rowkey name.  For this to work
155   * the hbase instance must have HConstants.RPC_CODEC_CONF_KEY set to
156   * KeyValueCodecWithTags.class.getCanonicalName());
157   * @param table table containing tagged cells
158   * @throws IOException if problems reading table
159   */
160  public static void verifyTags(Table table) throws IOException {
161    ResultScanner s = table.getScanner(new Scan());
162    for (Result r : s) {
163      for (Cell c : r.listCells()) {
164        Optional<Tag> tag = PrivateCellUtil.getTag(c, TagType.MOB_TABLE_NAME_TAG_TYPE);
165        if (!tag.isPresent()) {
166          fail(c.toString() + " has null tag");
167          continue;
168        }
169        Tag t = tag.get();
170        byte[] tval = Tag.cloneValue(t);
171        assertArrayEquals(c.toString() + " has tag" + Bytes.toString(tval),
172            r.getRow(), tval);
173      }
174    }
175  }
176}