1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.CellScanner;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.testclassification.LargeTests;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.mapreduce.TestTableSnapshotInputFormat;
35 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
36 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.util.FSUtils;
39 import org.junit.After;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44 @Category(LargeTests.class)
45 public class TestTableSnapshotScanner {
46
47 private static final Log LOG = LogFactory.getLog(TestTableSnapshotInputFormat.class);
48 private final HBaseTestingUtility UTIL = new HBaseTestingUtility();
49 private static final int NUM_REGION_SERVERS = 2;
50 private static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
51 public static byte[] bbb = Bytes.toBytes("bbb");
52 public static byte[] yyy = Bytes.toBytes("yyy");
53
54 private FileSystem fs;
55 private Path rootDir;
56
57 public void setupCluster() throws Exception {
58 setupConf(UTIL.getConfiguration());
59 UTIL.startMiniCluster(NUM_REGION_SERVERS, true);
60 rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
61 fs = rootDir.getFileSystem(UTIL.getConfiguration());
62 }
63
64 public void tearDownCluster() throws Exception {
65 UTIL.shutdownMiniCluster();
66 }
67
68 private static void setupConf(Configuration conf) {
69
70 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
71 }
72
73 @After
74 public void tearDown() throws Exception {
75 }
76
77 public static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
78 String snapshotName, int numRegions)
79 throws Exception {
80 try {
81 util.deleteTable(tableName);
82 } catch(Exception ex) {
83
84 }
85
86 if (numRegions > 1) {
87 util.createTable(tableName, FAMILIES, 1, bbb, yyy, numRegions);
88 } else {
89 util.createTable(tableName, FAMILIES);
90 }
91 Admin admin = util.getHBaseAdmin();
92
93
94 HTable table = new HTable(util.getConfiguration(), tableName);
95 util.loadTable(table, FAMILIES);
96
97 Path rootDir = FSUtils.getRootDir(util.getConfiguration());
98 FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
99
100 SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
101 Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
102
103
104 byte[] value = Bytes.toBytes("after_snapshot_value");
105 util.loadTable(table, FAMILIES, value);
106
107
108 admin.flush(tableName);
109 table.close();
110 }
111
112 @Test
113 public void testNoDuplicateResultsWhenSplitting() throws Exception {
114 setupCluster();
115 TableName tableName = TableName.valueOf("testNoDuplicateResultsWhenSplitting");
116 String snapshotName = "testSnapshotBug";
117 try {
118 if (UTIL.getHBaseAdmin().tableExists(tableName)) {
119 UTIL.deleteTable(tableName);
120 }
121
122 UTIL.createTable(tableName, FAMILIES);
123 Admin admin = UTIL.getHBaseAdmin();
124
125
126 Table table = UTIL.getConnection().getTable(tableName);
127 UTIL.loadTable(table, FAMILIES);
128
129
130 admin.split(tableName, Bytes.toBytes("eee"));
131 TestTableSnapshotInputFormat.blockUntilSplitFinished(UTIL, tableName, 2);
132
133 Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration());
134 FileSystem fs = rootDir.getFileSystem(UTIL.getConfiguration());
135
136 SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
137 Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
138
139
140 byte[] value = Bytes.toBytes("after_snapshot_value");
141 UTIL.loadTable(table, FAMILIES, value);
142
143
144 admin.flush(tableName);
145 table.close();
146
147 Path restoreDir = UTIL.getDataTestDirOnTestFS(snapshotName);
148 Scan scan = new Scan().withStartRow(bbb).withStopRow(yyy);
149
150 TableSnapshotScanner scanner =
151 new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir, snapshotName, scan);
152
153 verifyScanner(scanner, bbb, yyy);
154 scanner.close();
155 } finally {
156 UTIL.getHBaseAdmin().deleteSnapshot(snapshotName);
157 UTIL.deleteTable(tableName);
158 tearDownCluster();
159 }
160 }
161
162 @Test
163 public void testWithSingleRegion() throws Exception {
164 testScanner(UTIL, "testWithSingleRegion", 1, false);
165 }
166
167 @Test
168 public void testWithMultiRegion() throws Exception {
169 testScanner(UTIL, "testWithMultiRegion", 10, false);
170 }
171
172 @Test
173 public void testWithOfflineHBaseMultiRegion() throws Exception {
174 testScanner(UTIL, "testWithMultiRegion", 20, true);
175 }
176
177 private void testScanner(HBaseTestingUtility util, String snapshotName, int numRegions,
178 boolean shutdownCluster) throws Exception {
179 setupCluster();
180 TableName tableName = TableName.valueOf("testScanner");
181 try {
182 createTableAndSnapshot(util, tableName, snapshotName, numRegions);
183
184 if (shutdownCluster) {
185 util.shutdownMiniHBaseCluster();
186 }
187
188 Path restoreDir = util.getDataTestDirOnTestFS(snapshotName);
189 Scan scan = new Scan(bbb, yyy);
190
191 TableSnapshotScanner scanner = new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir,
192 snapshotName, scan);
193
194 verifyScanner(scanner, bbb, yyy);
195 scanner.close();
196 } finally {
197 if (!shutdownCluster) {
198 util.getHBaseAdmin().deleteSnapshot(snapshotName);
199 util.deleteTable(tableName);
200 tearDownCluster();
201 }
202 }
203 }
204
205 private void verifyScanner(ResultScanner scanner, byte[] startRow, byte[] stopRow)
206 throws IOException, InterruptedException {
207
208 HBaseTestingUtility.SeenRowTracker rowTracker =
209 new HBaseTestingUtility.SeenRowTracker(startRow, stopRow);
210
211 while (true) {
212 Result result = scanner.next();
213 if (result == null) {
214 break;
215 }
216 verifyRow(result);
217 rowTracker.addRow(result.getRow());
218 }
219
220
221 rowTracker.validate();
222 }
223
224 private static void verifyRow(Result result) throws IOException {
225 byte[] row = result.getRow();
226 CellScanner scanner = result.cellScanner();
227 while (scanner.advance()) {
228 Cell cell = scanner.current();
229
230
231 Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
232 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
233 }
234
235 for (int j = 0; j < FAMILIES.length; j++) {
236 byte[] actual = result.getValue(FAMILIES[j], FAMILIES[j]);
237 Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
238 + " ,actual:" + Bytes.toString(actual), row, actual);
239 }
240 }
241
242 }