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);
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 HBaseAdmin 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.toString());
109 table.close();
110 }
111
112 @Test
113 public void testWithSingleRegion() throws Exception {
114 testScanner(UTIL, "testWithSingleRegion", 1, false);
115 }
116
117 @Test
118 public void testWithMultiRegion() throws Exception {
119 testScanner(UTIL, "testWithMultiRegion", 10, false);
120 }
121
122 @Test
123 public void testWithOfflineHBaseMultiRegion() throws Exception {
124 testScanner(UTIL, "testWithMultiRegion", 20, true);
125 }
126
127 private void testScanner(HBaseTestingUtility util, String snapshotName, int numRegions,
128 boolean shutdownCluster) throws Exception {
129 setupCluster();
130 TableName tableName = TableName.valueOf("testScanner");
131 try {
132 createTableAndSnapshot(util, tableName, snapshotName, numRegions);
133
134 if (shutdownCluster) {
135 util.shutdownMiniHBaseCluster();
136 }
137
138 Path restoreDir = util.getDataTestDirOnTestFS(snapshotName);
139 Scan scan = new Scan(bbb, yyy);
140
141 TableSnapshotScanner scanner = new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir,
142 snapshotName, scan);
143
144 verifyScanner(scanner, bbb, yyy);
145 scanner.close();
146 } finally {
147 if (!shutdownCluster) {
148 util.getHBaseAdmin().deleteSnapshot(snapshotName);
149 util.deleteTable(tableName);
150 tearDownCluster();
151 }
152 }
153 }
154
155 private void verifyScanner(ResultScanner scanner, byte[] startRow, byte[] stopRow)
156 throws IOException, InterruptedException {
157
158 HBaseTestingUtility.SeenRowTracker rowTracker =
159 new HBaseTestingUtility.SeenRowTracker(startRow, stopRow);
160
161 while (true) {
162 Result result = scanner.next();
163 if (result == null) {
164 break;
165 }
166 verifyRow(result);
167 rowTracker.addRow(result.getRow());
168 }
169
170
171 rowTracker.validate();
172 }
173
174 private static void verifyRow(Result result) throws IOException {
175 byte[] row = result.getRow();
176 CellScanner scanner = result.cellScanner();
177 while (scanner.advance()) {
178 Cell cell = scanner.current();
179
180
181 Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
182 cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
183 }
184
185 for (int j = 0; j < FAMILIES.length; j++) {
186 byte[] actual = result.getValue(FAMILIES[j], null);
187 Assert.assertArrayEquals("Row in snapshot does not match, expected:" + Bytes.toString(row)
188 + " ,actual:" + Bytes.toString(actual), row, actual);
189 }
190 }
191
192 }