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