1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce;
20
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.hadoop.fs.FileSystem;
23 import org.apache.hadoop.fs.Path;
24 import org.apache.hadoop.hbase.Cell;
25 import org.apache.hadoop.hbase.CellScanner;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.HBaseAdmin;
29 import org.apache.hadoop.hbase.client.HTable;
30 import org.apache.hadoop.hbase.client.Result;
31 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
33 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.apache.hadoop.hbase.util.FSUtils;
36 import org.junit.Assert;
37 import org.junit.Test;
38
39 import java.io.IOException;
40 import java.util.Arrays;
41
42 public abstract class TableSnapshotInputFormatTestBase {
43
44 protected final HBaseTestingUtility UTIL = new HBaseTestingUtility();
45 protected static final int NUM_REGION_SERVERS = 2;
46 protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
47
48 protected FileSystem fs;
49 protected Path rootDir;
50
51 public void setupCluster() throws Exception {
52 setupConf(UTIL.getConfiguration());
53 UTIL.startMiniCluster(NUM_REGION_SERVERS);
54 rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
55 fs = rootDir.getFileSystem(UTIL.getConfiguration());
56 }
57
58 public void tearDownCluster() throws Exception {
59 UTIL.shutdownMiniCluster();
60 }
61
62 private static void setupConf(Configuration conf) {
63
64 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
65 }
66
67 protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
68 int numRegions, int expectedNumSplits) throws Exception;
69
70 protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
71 String snapshotName, Path tableDir, int numRegions, int expectedNumSplits,
72 boolean shutdownCluster) throws Exception;
73
74 protected abstract byte[] getStartRow();
75
76 protected abstract byte[] getEndRow();
77
78 @Test
79 public void testWithMockedMapReduceSingleRegion() throws Exception {
80 testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1);
81 }
82
83 @Test
84 public void testWithMockedMapReduceMultiRegion() throws Exception {
85 testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 8);
86 }
87
88 @Test
89 public void testWithMapReduceSingleRegion() throws Exception {
90 testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, false);
91 }
92
93 @Test
94 public void testWithMapReduceMultiRegion() throws Exception {
95 testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 8, false);
96 }
97
98 @Test
99
100 public void testWithMapReduceAndOfflineHBaseMultiRegion() throws Exception {
101 testWithMapReduce(UTIL, "testWithMapReduceAndOfflineHBaseMultiRegion", 10, 8, true);
102 }
103
104 protected void testWithMapReduce(HBaseTestingUtility util, String snapshotName,
105 int numRegions, int expectedNumSplits, boolean shutdownCluster) throws Exception {
106 setupCluster();
107 util.startMiniMapReduceCluster();
108 try {
109 Path tableDir = util.getDataTestDirOnTestFS(snapshotName);
110 TableName tableName = TableName.valueOf("testWithMapReduce");
111 testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions,
112 expectedNumSplits, shutdownCluster);
113 } finally {
114 util.shutdownMiniMapReduceCluster();
115 tearDownCluster();
116 }
117 }
118
119 protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result)
120 throws IOException {
121 byte[] row = key.get();
122 CellScanner scanner = result.cellScanner();
123 while (scanner.advance()) {
124 Cell cell = scanner.current();
125
126
127 Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
128 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
129 }
130
131 for (int j = 0; j < FAMILIES.length; j++) {
132 byte[] actual = result.getValue(FAMILIES[j], null);
133 Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
134 + " ,actual:" + Bytes.toString(actual), row, actual);
135 }
136 }
137
138 protected static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
139 String snapshotName, byte[] startRow, byte[] endRow, int numRegions)
140 throws Exception {
141 try {
142 util.deleteTable(tableName);
143 } catch(Exception ex) {
144
145 }
146
147 if (numRegions > 1) {
148 util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions);
149 } else {
150 util.createTable(tableName, FAMILIES);
151 }
152 HBaseAdmin admin = util.getHBaseAdmin();
153
154
155 HTable table = new HTable(util.getConfiguration(), tableName);
156 util.loadTable(table, FAMILIES);
157
158 Path rootDir = FSUtils.getRootDir(util.getConfiguration());
159 FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
160
161 SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
162 Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
163
164
165 byte[] value = Bytes.toBytes("after_snapshot_value");
166 util.loadTable(table, FAMILIES, value);
167
168
169 admin.flush(tableName.toString());
170 table.close();
171 }
172
173 }