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 static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.hbase.CategoryBasedTimeout;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.HDFSBlocksDistribution;
35 import org.apache.hadoop.hbase.HRegionInfo;
36 import org.apache.hadoop.hbase.client.HBaseAdmin;
37 import org.apache.hadoop.hbase.client.Table;
38 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
39 import org.apache.hadoop.hbase.testclassification.LargeTests;
40 import org.apache.hadoop.hbase.TableName;
41 import org.apache.hadoop.hbase.client.Result;
42 import org.apache.hadoop.hbase.client.Scan;
43 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
44 import org.apache.hadoop.hbase.mapreduce.TableSnapshotInputFormat.TableSnapshotRegionSplit;
45 import org.apache.hadoop.hbase.util.Bytes;
46 import org.apache.hadoop.hbase.util.FSUtils;
47 import org.apache.hadoop.io.NullWritable;
48 import org.apache.hadoop.mapreduce.InputSplit;
49 import org.apache.hadoop.mapreduce.Job;
50 import org.apache.hadoop.mapreduce.RecordReader;
51 import org.apache.hadoop.mapreduce.Reducer;
52 import org.apache.hadoop.mapreduce.TaskAttemptContext;
53 import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
54 import org.junit.After;
55 import org.junit.Assert;
56 import org.junit.Rule;
57 import org.junit.Test;
58 import org.junit.experimental.categories.Category;
59 import org.junit.rules.TestRule;
60
61 import com.google.common.collect.Lists;
62
63 @Category(LargeTests.class)
64 public class TestTableSnapshotInputFormat extends TableSnapshotInputFormatTestBase {
65 @Rule public final TestRule timeout = CategoryBasedTimeout.builder().
66 withTimeout(this.getClass()).withLookingForStuckThread(true).build();
67
68 private static final byte[] bbb = Bytes.toBytes("bbb");
69 private static final byte[] yyy = Bytes.toBytes("yyy");
70
71 @Override
72 protected byte[] getStartRow() {
73 return bbb;
74 }
75
76 @Override
77 protected byte[] getEndRow() {
78 return yyy;
79 }
80
81 @After
82 public void tearDown() throws Exception {
83 }
84
85 @Test
86 public void testGetBestLocations() throws IOException {
87 TableSnapshotInputFormatImpl tsif = new TableSnapshotInputFormatImpl();
88 Configuration conf = UTIL.getConfiguration();
89
90 HDFSBlocksDistribution blockDistribution = new HDFSBlocksDistribution();
91 Assert.assertEquals(Lists.newArrayList(), tsif.getBestLocations(conf, blockDistribution));
92
93 blockDistribution.addHostsAndBlockWeight(new String[] {"h1"}, 1);
94 Assert.assertEquals(Lists.newArrayList("h1"), tsif.getBestLocations(conf, blockDistribution));
95
96 blockDistribution.addHostsAndBlockWeight(new String[] {"h1"}, 1);
97 Assert.assertEquals(Lists.newArrayList("h1"), tsif.getBestLocations(conf, blockDistribution));
98
99 blockDistribution.addHostsAndBlockWeight(new String[] {"h2"}, 1);
100 Assert.assertEquals(Lists.newArrayList("h1"), tsif.getBestLocations(conf, blockDistribution));
101
102 blockDistribution = new HDFSBlocksDistribution();
103 blockDistribution.addHostsAndBlockWeight(new String[] {"h1"}, 10);
104 blockDistribution.addHostsAndBlockWeight(new String[] {"h2"}, 7);
105 blockDistribution.addHostsAndBlockWeight(new String[] {"h3"}, 5);
106 blockDistribution.addHostsAndBlockWeight(new String[] {"h4"}, 1);
107 Assert.assertEquals(Lists.newArrayList("h1"), tsif.getBestLocations(conf, blockDistribution));
108
109 blockDistribution.addHostsAndBlockWeight(new String[] {"h2"}, 2);
110 Assert.assertEquals(Lists.newArrayList("h1", "h2"),
111 tsif.getBestLocations(conf, blockDistribution));
112
113 blockDistribution.addHostsAndBlockWeight(new String[] {"h2"}, 3);
114 Assert.assertEquals(Lists.newArrayList("h2", "h1"),
115 tsif.getBestLocations(conf, blockDistribution));
116
117 blockDistribution.addHostsAndBlockWeight(new String[] {"h3"}, 6);
118 blockDistribution.addHostsAndBlockWeight(new String[] {"h4"}, 9);
119
120 Assert.assertEquals(Lists.newArrayList("h2", "h3", "h4", "h1"),
121 tsif.getBestLocations(conf, blockDistribution));
122 }
123
124 public static enum TestTableSnapshotCounters {
125 VALIDATION_ERROR
126 }
127
128 public static class TestTableSnapshotMapper
129 extends TableMapper<ImmutableBytesWritable, NullWritable> {
130 @Override
131 protected void map(ImmutableBytesWritable key, Result value,
132 Context context) throws IOException, InterruptedException {
133
134 verifyRowFromMap(key, value);
135 context.write(key, NullWritable.get());
136 }
137 }
138
139 public static class TestTableSnapshotReducer
140 extends Reducer<ImmutableBytesWritable, NullWritable, NullWritable, NullWritable> {
141 HBaseTestingUtility.SeenRowTracker rowTracker =
142 new HBaseTestingUtility.SeenRowTracker(bbb, yyy);
143 @Override
144 protected void reduce(ImmutableBytesWritable key, Iterable<NullWritable> values,
145 Context context) throws IOException, InterruptedException {
146 rowTracker.addRow(key.get());
147 }
148
149 @Override
150 protected void cleanup(Context context) throws IOException,
151 InterruptedException {
152 rowTracker.validate();
153 }
154 }
155
156 @Test
157 public void testInitTableSnapshotMapperJobConfig() throws Exception {
158 setupCluster();
159 TableName tableName = TableName.valueOf("testInitTableSnapshotMapperJobConfig");
160 String snapshotName = "foo";
161
162 try {
163 createTableAndSnapshot(UTIL, tableName, snapshotName, getStartRow(), getEndRow(), 1);
164 Job job = new Job(UTIL.getConfiguration());
165 Path tmpTableDir = UTIL.getRandomDir();
166
167 TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName,
168 new Scan(), TestTableSnapshotMapper.class, ImmutableBytesWritable.class,
169 NullWritable.class, job, false, tmpTableDir);
170
171
172
173 Assert.assertEquals(
174 "Snapshot job should be configured for default LruBlockCache.",
175 HConstants.HFILE_BLOCK_CACHE_SIZE_DEFAULT,
176 job.getConfiguration().getFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, -1), 0.01);
177 Assert.assertEquals(
178 "Snapshot job should not use BucketCache.",
179 0, job.getConfiguration().getFloat("hbase.bucketcache.size", -1), 0.01);
180 } finally {
181 UTIL.getHBaseAdmin().deleteSnapshot(snapshotName);
182 UTIL.deleteTable(tableName);
183 tearDownCluster();
184 }
185 }
186
187 @Override
188 public void testRestoreSnapshotDoesNotCreateBackRefLinksInit(TableName tableName,
189 String snapshotName, Path tmpTableDir) throws Exception {
190 Job job = new Job(UTIL.getConfiguration());
191 TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName,
192 new Scan(), TestTableSnapshotMapper.class, ImmutableBytesWritable.class,
193 NullWritable.class, job, false, tmpTableDir);
194 }
195
196 @Override
197 public void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
198 int numRegions, int expectedNumSplits) throws Exception {
199 setupCluster();
200 TableName tableName = TableName.valueOf("testWithMockedMapReduce");
201 try {
202 createTableAndSnapshot(
203 util, tableName, snapshotName, getStartRow(), getEndRow(), numRegions);
204
205 Job job = new Job(util.getConfiguration());
206 Path tmpTableDir = util.getRandomDir();
207 Scan scan = new Scan(getStartRow(), getEndRow());
208
209 TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName,
210 scan, TestTableSnapshotMapper.class, ImmutableBytesWritable.class,
211 NullWritable.class, job, false, tmpTableDir);
212
213 verifyWithMockedMapReduce(job, numRegions, expectedNumSplits, getStartRow(), getEndRow());
214
215 } finally {
216 util.getHBaseAdmin().deleteSnapshot(snapshotName);
217 util.deleteTable(tableName);
218 tearDownCluster();
219 }
220 }
221
222 public static void blockUntilSplitFinished(HBaseTestingUtility util, TableName tableName,
223 int expectedRegionSize) throws Exception {
224 for (int i = 0; i < 100; i++) {
225 List<HRegionInfo> hRegionInfoList = util.getHBaseAdmin().getTableRegions(tableName);
226 if (hRegionInfoList.size() >= expectedRegionSize) {
227 break;
228 }
229 Thread.sleep(1000);
230 }
231 }
232
233 @Test
234 public void testNoDuplicateResultsWhenSplitting() throws Exception {
235 setupCluster();
236 TableName tableName = TableName.valueOf("testNoDuplicateResultsWhenSplitting");
237 String snapshotName = "testSnapshotBug";
238 try {
239 if (UTIL.getHBaseAdmin().tableExists(tableName)) {
240 UTIL.deleteTable(tableName);
241 }
242
243 UTIL.createTable(tableName, FAMILIES);
244 HBaseAdmin admin = UTIL.getHBaseAdmin();
245
246
247 Table table = UTIL.getConnection().getTable(tableName);
248 UTIL.loadTable(table, FAMILIES);
249
250
251 admin.split(tableName, Bytes.toBytes("eee"));
252 blockUntilSplitFinished(UTIL, tableName, 2);
253
254 Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration());
255 FileSystem fs = rootDir.getFileSystem(UTIL.getConfiguration());
256
257 SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName, Arrays.asList(FAMILIES),
258 null, snapshotName, rootDir, fs, true);
259
260
261 byte[] value = Bytes.toBytes("after_snapshot_value");
262 UTIL.loadTable(table, FAMILIES, value);
263
264
265 admin.flush(tableName);
266 table.close();
267
268 Job job = new Job(UTIL.getConfiguration());
269 Path tmpTableDir = UTIL.getRandomDir();
270
271 Scan scan = new Scan().setStartRow(getStartRow()).setStopRow(getEndRow());
272
273 TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName, scan,
274 TestTableSnapshotMapper.class, ImmutableBytesWritable.class, NullWritable.class, job, false,
275 tmpTableDir);
276
277 verifyWithMockedMapReduce(job, 2, 2, getStartRow(), getEndRow());
278 } finally {
279 UTIL.getHBaseAdmin().deleteSnapshot(snapshotName);
280 UTIL.deleteTable(tableName);
281 tearDownCluster();
282 }
283 }
284
285 private void verifyWithMockedMapReduce(Job job, int numRegions, int expectedNumSplits,
286 byte[] startRow, byte[] stopRow)
287 throws IOException, InterruptedException {
288 TableSnapshotInputFormat tsif = new TableSnapshotInputFormat();
289 List<InputSplit> splits = tsif.getSplits(job);
290
291 Assert.assertEquals(expectedNumSplits, splits.size());
292
293 HBaseTestingUtility.SeenRowTracker rowTracker =
294 new HBaseTestingUtility.SeenRowTracker(startRow, stopRow);
295
296 for (int i = 0; i < splits.size(); i++) {
297
298 InputSplit split = splits.get(i);
299 Assert.assertTrue(split instanceof TableSnapshotRegionSplit);
300
301
302 TaskAttemptContext taskAttemptContext = mock(TaskAttemptContext.class);
303 when(taskAttemptContext.getConfiguration()).thenReturn(job.getConfiguration());
304 RecordReader<ImmutableBytesWritable, Result> rr =
305 tsif.createRecordReader(split, taskAttemptContext);
306 rr.initialize(split, taskAttemptContext);
307
308
309 while (rr.nextKeyValue()) {
310 byte[] row = rr.getCurrentKey().get();
311 verifyRowFromMap(rr.getCurrentKey(), rr.getCurrentValue());
312 rowTracker.addRow(row);
313 }
314
315 rr.close();
316 }
317
318
319 rowTracker.validate();
320 }
321
322 @Override
323 protected void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
324 String snapshotName, Path tableDir, int numRegions, int expectedNumSplits,
325 boolean shutdownCluster) throws Exception {
326 doTestWithMapReduce(util, tableName, snapshotName, getStartRow(), getEndRow(), tableDir,
327 numRegions, expectedNumSplits, shutdownCluster);
328 }
329
330
331 public static void doTestWithMapReduce(HBaseTestingUtility util, TableName tableName,
332 String snapshotName, byte[] startRow, byte[] endRow, Path tableDir, int numRegions,
333 int expectedNumSplits, boolean shutdownCluster) throws Exception {
334
335
336 createTableAndSnapshot(util, tableName, snapshotName, startRow, endRow, numRegions);
337
338 if (shutdownCluster) {
339 util.shutdownMiniHBaseCluster();
340 }
341
342 try {
343
344 Job job = new Job(util.getConfiguration());
345 Scan scan = new Scan(startRow, endRow);
346
347 job.setJarByClass(util.getClass());
348 TableMapReduceUtil.addDependencyJars(job.getConfiguration(),
349 TestTableSnapshotInputFormat.class);
350
351 TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName,
352 scan, TestTableSnapshotMapper.class, ImmutableBytesWritable.class,
353 NullWritable.class, job, true, tableDir);
354
355 job.setReducerClass(TestTableSnapshotInputFormat.TestTableSnapshotReducer.class);
356 job.setNumReduceTasks(1);
357 job.setOutputFormatClass(NullOutputFormat.class);
358
359 Assert.assertTrue(job.waitForCompletion(true));
360 } finally {
361 if (!shutdownCluster) {
362 util.getHBaseAdmin().deleteSnapshot(snapshotName);
363 util.deleteTable(tableName);
364 }
365 }
366 }
367 }