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.snapshot;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import org.apache.commons.lang3.StringUtils;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.CellUtil;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.client.Result;
034import org.apache.hadoop.hbase.client.ResultScanner;
035import org.apache.hadoop.hbase.client.Scan;
036import org.apache.hadoop.hbase.client.Table;
037import org.apache.hadoop.hbase.client.TableDescriptor;
038import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
039import org.apache.hadoop.hbase.regionserver.BloomType;
040import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.junit.Assert;
043
044public class MobSnapshotTestingUtils {
045
046  /**
047   * Create the Mob Table.
048   */
049  public static void createMobTable(final HBaseTestingUtil util, final TableName tableName,
050    int regionReplication, final byte[]... families) throws IOException, InterruptedException {
051    createMobTable(util, tableName, SnapshotTestingUtils.getSplitKeys(), regionReplication,
052      StoreFileTrackerFactory.Trackers.DEFAULT.name(), families);
053  }
054
055  public static void createMobTable(final HBaseTestingUtil util, final TableName tableName,
056    int regionReplication, String storeFileTracker, final byte[]... families)
057    throws IOException, InterruptedException {
058    createMobTable(util, tableName, SnapshotTestingUtils.getSplitKeys(), regionReplication,
059      storeFileTracker, families);
060  }
061
062  public static void createPreSplitMobTable(final HBaseTestingUtil util, final TableName tableName,
063    int nRegions, final byte[]... families) throws IOException, InterruptedException {
064    createMobTable(util, tableName, SnapshotTestingUtils.getSplitKeys(nRegions), 1, families);
065  }
066
067  public static void createMobTable(final HBaseTestingUtil util, final TableName tableName,
068    final byte[][] splitKeys, int regionReplication, final byte[]... families)
069    throws IOException, InterruptedException {
070    createMobTable(util, tableName, splitKeys, regionReplication,
071      StoreFileTrackerFactory.Trackers.DEFAULT.name(), families);
072  }
073
074  public static void createMobTable(final HBaseTestingUtil util, final TableName tableName,
075    final byte[][] splitKeys, int regionReplication, String storeFileTracker,
076    final byte[]... families) throws IOException, InterruptedException {
077    createMobTable(util, tableName, splitKeys, regionReplication, storeFileTracker, null, families);
078  }
079
080  public static void createMobTable(HBaseTestingUtil util, TableName tableName, byte[][] splitKeys,
081    int regionReplication, String storeFileTracker, String cpClassName, byte[]... families)
082    throws IOException, InterruptedException {
083    TableDescriptorBuilder builder =
084      TableDescriptorBuilder.newBuilder(tableName).setRegionReplication(regionReplication);
085    for (byte[] family : families) {
086      builder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).setMobEnabled(true)
087        .setMobThreshold(0L).build());
088    }
089    if (!StringUtils.isBlank(cpClassName)) {
090      builder.setCoprocessor(cpClassName);
091    }
092    builder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, storeFileTracker);
093    util.getAdmin().createTable(builder.build(), splitKeys);
094    SnapshotTestingUtils.waitForTableToBeOnline(util, tableName);
095    assertEquals((splitKeys.length + 1) * regionReplication,
096      util.getAdmin().getRegions(tableName).size());
097  }
098
099  /**
100   * Create a Mob table.
101   * @return An Table instance for the created table.
102   */
103  public static Table createMobTable(final HBaseTestingUtil util, final TableName tableName,
104    final byte[]... families) throws IOException {
105    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
106    for (byte[] family : families) {
107      // Disable blooms (they are on by default as of 0.95) but we disable them
108      // here because
109      // tests have hard coded counts of what to expect in block cache, etc.,
110      // and blooms being
111      // on is interfering.
112      builder.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family)
113        .setBloomFilterType(BloomType.NONE).setMobEnabled(true).setMobThreshold(0L).build());
114    }
115    util.getAdmin().createTable(builder.build());
116    // HBaseAdmin only waits for regions to appear in hbase:meta we should wait
117    // until they are assigned
118    util.waitUntilAllRegionsAssigned(tableName);
119    return ConnectionFactory.createConnection(util.getConfiguration()).getTable(tableName);
120  }
121
122  /**
123   * Return the number of rows in the given table.
124   */
125  public static int countMobRows(final Table table, final byte[]... families) throws IOException {
126    Scan scan = new Scan();
127    for (byte[] family : families) {
128      scan.addFamily(family);
129    }
130    try (ResultScanner results = table.getScanner(scan)) {
131      int count = 0;
132      for (Result res; (res = results.next()) != null;) {
133        count++;
134        for (Cell cell : res.listCells()) {
135          // Verify the value
136          Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
137        }
138      }
139      return count;
140    }
141  }
142
143  public static void verifyMobRowCount(final HBaseTestingUtil util, final TableName tableName,
144    long expectedRows) throws IOException {
145
146    Table table = ConnectionFactory.createConnection(util.getConfiguration()).getTable(tableName);
147    try {
148      assertEquals(expectedRows, countMobRows(table));
149    } finally {
150      table.close();
151    }
152  }
153
154  // ==========================================================================
155  // Snapshot Mock
156  // ==========================================================================
157  public static class SnapshotMock extends SnapshotTestingUtils.SnapshotMock {
158    public SnapshotMock(final Configuration conf, final FileSystem fs, final Path rootDir) {
159      super(conf, fs, rootDir);
160    }
161
162    @Override
163    public TableDescriptor createHtd(final String tableName) {
164      return TableDescriptorBuilder
165        .newBuilder(TableName.valueOf(tableName)).setColumnFamily(ColumnFamilyDescriptorBuilder
166          .newBuilder(Bytes.toBytes(TEST_FAMILY)).setMobEnabled(true).setMobThreshold(0L).build())
167        .build();
168    }
169  }
170}