View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.snapshot;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FSDataOutputStream;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.*;
31  import org.apache.hadoop.hbase.client.*;
32  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
34  import org.apache.hadoop.hbase.regionserver.BloomType;
35  import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.FSTableDescriptors;
38  import org.apache.hadoop.hbase.util.FSUtils;
39  import org.junit.Assert;
40  
41  public class MobSnapshotTestingUtils {
42  
43    /**
44     * Create the Mob Table.
45     */
46    public static void createMobTable(final HBaseTestingUtility util,
47        final TableName tableName, int regionReplication,
48        final byte[]... families) throws IOException, InterruptedException {
49      HTableDescriptor htd = new HTableDescriptor(tableName);
50      htd.setRegionReplication(regionReplication);
51      for (byte[] family : families) {
52        HColumnDescriptor hcd = new HColumnDescriptor(family);
53        hcd.setMobEnabled(true);
54        hcd.setMobThreshold(0L);
55        htd.addFamily(hcd);
56      }
57      byte[][] splitKeys = SnapshotTestingUtils.getSplitKeys();
58      util.getHBaseAdmin().createTable(htd, splitKeys);
59      SnapshotTestingUtils.waitForTableToBeOnline(util, tableName);
60      assertEquals((splitKeys.length + 1) * regionReplication, util
61          .getHBaseAdmin().getTableRegions(tableName).size());
62    }
63  
64    /**
65     * Create a Mob table.
66     *
67     * @param util
68     * @param tableName
69     * @param families
70     * @return An HTable instance for the created table.
71     * @throws IOException
72     */
73    public static Table createMobTable(final HBaseTestingUtility util,
74        final TableName tableName, final byte[]... families) throws IOException {
75      HTableDescriptor htd = new HTableDescriptor(tableName);
76      for (byte[] family : families) {
77        HColumnDescriptor hcd = new HColumnDescriptor(family);
78        // Disable blooms (they are on by default as of 0.95) but we disable them
79        // here because
80        // tests have hard coded counts of what to expect in block cache, etc.,
81        // and blooms being
82        // on is interfering.
83        hcd.setBloomFilterType(BloomType.NONE);
84        hcd.setMobEnabled(true);
85        hcd.setMobThreshold(0L);
86        htd.addFamily(hcd);
87      }
88      util.getHBaseAdmin().createTable(htd);
89      // HBaseAdmin only waits for regions to appear in hbase:meta we should wait
90      // until they are assigned
91      util.waitUntilAllRegionsAssigned(htd.getTableName());
92      return ConnectionFactory.createConnection(util.getConfiguration()).getTable(htd.getTableName());
93    }
94  
95    /**
96     * Return the number of rows in the given table.
97     */
98    public static int countMobRows(final Table table) throws IOException {
99      Scan scan = new Scan();
100     ResultScanner results = table.getScanner(scan);
101     int count = 0;
102     for (Result res : results) {
103       count++;
104       List<Cell> cells = res.listCells();
105       for (Cell cell : cells) {
106         // Verify the value
107         Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
108       }
109     }
110     results.close();
111     return count;
112   }
113 
114   /**
115    * Return the number of rows in the given table.
116    */
117   public static int countMobRows(final Table table, final byte[]... families)
118       throws IOException {
119     Scan scan = new Scan();
120     for (byte[] family : families) {
121       scan.addFamily(family);
122     }
123     ResultScanner results = table.getScanner(scan);
124     int count = 0;
125     for (Result res : results) {
126       count++;
127       List<Cell> cells = res.listCells();
128       for (Cell cell : cells) {
129         // Verify the value
130         Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
131       }
132     }
133     results.close();
134     return count;
135   }
136 
137   public static void verifyMobRowCount(final HBaseTestingUtility util,
138       final TableName tableName, long expectedRows) throws IOException {
139 
140     Table table = ConnectionFactory.createConnection(util.getConfiguration()).getTable(tableName);
141     try {
142       assertEquals(expectedRows, countMobRows(table));
143     } finally {
144       table.close();
145     }
146   }
147 
148   // ==========================================================================
149   // Snapshot Mock
150   // ==========================================================================
151   public static class SnapshotMock extends SnapshotTestingUtils.SnapshotMock {
152     public SnapshotMock(final Configuration conf, final FileSystem fs, final Path rootDir) {
153       super(conf, fs, rootDir);
154     }
155 
156     @Override
157     public HTableDescriptor createHtd(final String tableName) {
158       HTableDescriptor htd = new HTableDescriptor(tableName);
159       HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY);
160       hcd.setMobEnabled(true);
161       hcd.setMobThreshold(0L);
162       htd.addFamily(hcd);
163       return htd;
164     }
165   }
166 }