1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
66
67
68
69
70
71
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
79
80
81
82
83 hcd.setBloomFilterType(BloomType.NONE);
84 hcd.setMobEnabled(true);
85 hcd.setMobThreshold(0L);
86 htd.addFamily(hcd);
87 }
88 util.getHBaseAdmin().createTable(htd);
89
90
91 util.waitUntilAllRegionsAssigned(htd.getTableName());
92 return ConnectionFactory.createConnection(util.getConfiguration()).getTable(htd.getTableName());
93 }
94
95
96
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
107 Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
108 }
109 }
110 results.close();
111 return count;
112 }
113
114
115
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
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
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 }