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