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