1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.io.IOException;
23 import java.util.Arrays;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.CategoryBasedTimeout;
30 import org.apache.hadoop.hbase.Cell;
31 import org.apache.hadoop.hbase.CellUtil;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.client.Result;
37 import org.apache.hadoop.hbase.client.ResultScanner;
38 import org.apache.hadoop.hbase.client.Scan;
39 import org.apache.hadoop.hbase.client.Table;
40 import org.apache.hadoop.hbase.mapreduce.SyncTable.SyncMapper.Counter;
41 import org.apache.hadoop.hbase.testclassification.LargeTests;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.mapreduce.Counters;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.BeforeClass;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50 import org.junit.rules.TestRule;
51
52 import com.google.common.base.Throwables;
53
54
55
56
57 @Category(LargeTests.class)
58 public class TestSyncTable {
59 @Rule public final TestRule timeout = CategoryBasedTimeout.builder().
60 withTimeout(this.getClass()).withLookingForStuckThread(true).build();
61 private static final Log LOG = LogFactory.getLog(TestSyncTable.class);
62
63 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
64
65 @BeforeClass
66 public static void beforeClass() throws Exception {
67 TEST_UTIL.setJobWithoutMRCluster();
68 TEST_UTIL.startMiniCluster(3);
69 }
70
71 @AfterClass
72 public static void afterClass() throws Exception {
73 TEST_UTIL.cleanupDataTestDirOnTestFS();
74 TEST_UTIL.shutdownMiniCluster();
75 }
76
77 private static byte[][] generateSplits(int numRows, int numRegions) {
78 byte[][] splitRows = new byte[numRegions-1][];
79 for (int i = 1; i < numRegions; i++) {
80 splitRows[i-1] = Bytes.toBytes(numRows * i / numRegions);
81 }
82 return splitRows;
83 }
84
85 @Test
86 public void testSyncTable() throws Exception {
87 final TableName sourceTableName = TableName.valueOf("testSourceTable");
88 final TableName targetTableName = TableName.valueOf("testTargetTable");
89 Path testDir = TEST_UTIL.getDataTestDirOnTestFS("testSyncTable");
90
91 writeTestData(sourceTableName, targetTableName);
92 hashSourceTable(sourceTableName, testDir);
93 Counters syncCounters = syncTables(sourceTableName, targetTableName, testDir);
94 assertEqualTables(90, sourceTableName, targetTableName);
95
96 assertEquals(60, syncCounters.findCounter(Counter.ROWSWITHDIFFS).getValue());
97 assertEquals(10, syncCounters.findCounter(Counter.SOURCEMISSINGROWS).getValue());
98 assertEquals(10, syncCounters.findCounter(Counter.TARGETMISSINGROWS).getValue());
99 assertEquals(50, syncCounters.findCounter(Counter.SOURCEMISSINGCELLS).getValue());
100 assertEquals(50, syncCounters.findCounter(Counter.TARGETMISSINGCELLS).getValue());
101 assertEquals(20, syncCounters.findCounter(Counter.DIFFERENTCELLVALUES).getValue());
102
103 TEST_UTIL.deleteTable(sourceTableName);
104 TEST_UTIL.deleteTable(targetTableName);
105 }
106
107 @Test
108 public void testSyncTableDoDeletesFalse() throws Exception {
109 final TableName sourceTableName = TableName.valueOf("testSyncTableDoDeletesFalse_source");
110 final TableName targetTableName = TableName.valueOf("testSyncTableDoDeletesFalse_target");
111 Path testDir = TEST_UTIL.getDataTestDirOnTestFS("testSyncTableDoDeletesFalse");
112
113 writeTestData(sourceTableName, targetTableName);
114 hashSourceTable(sourceTableName, testDir);
115 Counters syncCounters = syncTables(sourceTableName, targetTableName,
116 testDir, "--doDeletes=false");
117 assertTargetDoDeletesFalse(100, sourceTableName, targetTableName);
118
119 assertEquals(60, syncCounters.findCounter(Counter.ROWSWITHDIFFS).getValue());
120 assertEquals(10, syncCounters.findCounter(Counter.SOURCEMISSINGROWS).getValue());
121 assertEquals(10, syncCounters.findCounter(Counter.TARGETMISSINGROWS).getValue());
122 assertEquals(50, syncCounters.findCounter(Counter.SOURCEMISSINGCELLS).getValue());
123 assertEquals(50, syncCounters.findCounter(Counter.TARGETMISSINGCELLS).getValue());
124 assertEquals(20, syncCounters.findCounter(Counter.DIFFERENTCELLVALUES).getValue());
125
126 TEST_UTIL.deleteTable(sourceTableName);
127 TEST_UTIL.deleteTable(targetTableName);
128 }
129
130 @Test
131 public void testSyncTableDoPutsFalse() throws Exception {
132 final TableName sourceTableName = TableName.valueOf("testSyncTableDoPutsFalse_source");
133 final TableName targetTableName = TableName.valueOf("testSyncTableDoPutsFalse_target");
134 Path testDir = TEST_UTIL.getDataTestDirOnTestFS("testSyncTableDoPutsFalse");
135
136 writeTestData(sourceTableName, targetTableName);
137 hashSourceTable(sourceTableName, testDir);
138 Counters syncCounters = syncTables(sourceTableName, targetTableName,
139 testDir, "--doPuts=false");
140 assertTargetDoPutsFalse(70, sourceTableName, targetTableName);
141
142 assertEquals(60, syncCounters.findCounter(Counter.ROWSWITHDIFFS).getValue());
143 assertEquals(10, syncCounters.findCounter(Counter.SOURCEMISSINGROWS).getValue());
144 assertEquals(10, syncCounters.findCounter(Counter.TARGETMISSINGROWS).getValue());
145 assertEquals(50, syncCounters.findCounter(Counter.SOURCEMISSINGCELLS).getValue());
146 assertEquals(50, syncCounters.findCounter(Counter.TARGETMISSINGCELLS).getValue());
147 assertEquals(20, syncCounters.findCounter(Counter.DIFFERENTCELLVALUES).getValue());
148
149 TEST_UTIL.deleteTable(sourceTableName);
150 TEST_UTIL.deleteTable(targetTableName);
151 }
152
153 private void assertEqualTables(int expectedRows, TableName sourceTableName,
154 TableName targetTableName) throws Exception {
155 Table sourceTable = TEST_UTIL.getConnection().getTable(sourceTableName);
156 Table targetTable = TEST_UTIL.getConnection().getTable(targetTableName);
157
158 ResultScanner sourceScanner = sourceTable.getScanner(new Scan());
159 ResultScanner targetScanner = targetTable.getScanner(new Scan());
160
161 for (int i = 0; i < expectedRows; i++) {
162 Result sourceRow = sourceScanner.next();
163 Result targetRow = targetScanner.next();
164
165 LOG.debug("SOURCE row: " + (sourceRow == null ? "null" : Bytes.toInt(sourceRow.getRow()))
166 + " cells:" + sourceRow);
167 LOG.debug("TARGET row: " + (targetRow == null ? "null" : Bytes.toInt(targetRow.getRow()))
168 + " cells:" + targetRow);
169
170 if (sourceRow == null) {
171 Assert.fail("Expected " + expectedRows
172 + " source rows but only found " + i);
173 }
174 if (targetRow == null) {
175 Assert.fail("Expected " + expectedRows
176 + " target rows but only found " + i);
177 }
178 Cell[] sourceCells = sourceRow.rawCells();
179 Cell[] targetCells = targetRow.rawCells();
180 if (sourceCells.length != targetCells.length) {
181 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
182 LOG.debug("Target cells: " + Arrays.toString(targetCells));
183 Assert.fail("Row " + Bytes.toInt(sourceRow.getRow())
184 + " has " + sourceCells.length
185 + " cells in source table but " + targetCells.length
186 + " cells in target table");
187 }
188 for (int j = 0; j < sourceCells.length; j++) {
189 Cell sourceCell = sourceCells[j];
190 Cell targetCell = targetCells[j];
191 try {
192 if (!CellUtil.matchingRow(sourceCell, targetCell)) {
193 Assert.fail("Rows don't match");
194 }
195 if (!CellUtil.matchingFamily(sourceCell, targetCell)) {
196 Assert.fail("Families don't match");
197 }
198 if (!CellUtil.matchingQualifier(sourceCell, targetCell)) {
199 Assert.fail("Qualifiers don't match");
200 }
201 if (!CellUtil.matchingTimestamp(sourceCell, targetCell)) {
202 Assert.fail("Timestamps don't match");
203 }
204 if (!CellUtil.matchingValue(sourceCell, targetCell)) {
205 Assert.fail("Values don't match");
206 }
207 } catch (Throwable t) {
208 LOG.debug("Source cell: " + sourceCell + " target cell: " + targetCell);
209 Throwables.propagate(t);
210 }
211 }
212 }
213 Result sourceRow = sourceScanner.next();
214 if (sourceRow != null) {
215 Assert.fail("Source table has more than " + expectedRows
216 + " rows. Next row: " + Bytes.toInt(sourceRow.getRow()));
217 }
218 Result targetRow = targetScanner.next();
219 if (targetRow != null) {
220 Assert.fail("Target table has more than " + expectedRows
221 + " rows. Next row: " + Bytes.toInt(targetRow.getRow()));
222 }
223 sourceScanner.close();
224 targetScanner.close();
225 sourceTable.close();
226 targetTable.close();
227 }
228
229 private void assertTargetDoDeletesFalse(int expectedRows, TableName
230 sourceTableName,
231 TableName targetTableName)
232 throws Exception {
233 Table sourceTable = TEST_UTIL.getConnection().getTable(sourceTableName);
234 Table targetTable = TEST_UTIL.getConnection().getTable(targetTableName);
235
236 ResultScanner sourceScanner = sourceTable.getScanner(new Scan());
237 ResultScanner targetScanner = targetTable.getScanner(new Scan());
238 Result targetRow = targetScanner.next();
239 Result sourceRow = sourceScanner.next();
240 int rowsCount = 0;
241 while (targetRow!=null) {
242 rowsCount++;
243
244
245 if (Bytes.toInt(sourceRow.getRow()) != Bytes.toInt(targetRow.getRow())) {
246 targetRow = targetScanner.next();
247 continue;
248 }
249
250 LOG.debug("SOURCE row: " + (sourceRow == null ? "null"
251 : Bytes.toInt(sourceRow.getRow()))
252 + " cells:" + sourceRow);
253 LOG.debug("TARGET row: " + (targetRow == null ? "null"
254 : Bytes.toInt(targetRow.getRow()))
255 + " cells:" + targetRow);
256
257 Cell[] sourceCells = sourceRow.rawCells();
258 Cell[] targetCells = targetRow.rawCells();
259 int targetRowKey = Bytes.toInt(targetRow.getRow());
260 if (targetRowKey >= 70 && targetRowKey < 80) {
261 if (sourceCells.length == targetCells.length) {
262 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
263 LOG.debug("Target cells: " + Arrays.toString(targetCells));
264 Assert.fail("Row " + targetRowKey + " should have more cells in "
265 + "target than in source");
266 }
267
268 } else {
269 if (sourceCells.length != targetCells.length) {
270 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
271 LOG.debug("Target cells: " + Arrays.toString(targetCells));
272 Assert.fail("Row " + Bytes.toInt(sourceRow.getRow())
273 + " has " + sourceCells.length
274 + " cells in source table but " + targetCells.length
275 + " cells in target table");
276 }
277 }
278 for (int j = 0; j < sourceCells.length; j++) {
279 Cell sourceCell = sourceCells[j];
280 Cell targetCell = targetCells[j];
281 try {
282 if (!CellUtil.matchingRow(sourceCell, targetCell)) {
283 Assert.fail("Rows don't match");
284 }
285 if (!CellUtil.matchingFamily(sourceCell, targetCell)) {
286 Assert.fail("Families don't match");
287 }
288 if (!CellUtil.matchingQualifier(sourceCell, targetCell)) {
289 Assert.fail("Qualifiers don't match");
290 }
291 if(targetRowKey < 80 && targetRowKey >= 90){
292 if (!CellUtil.matchingTimestamp(sourceCell, targetCell)) {
293 Assert.fail("Timestamps don't match");
294 }
295 }
296 if (!CellUtil.matchingValue(sourceCell, targetCell)) {
297 Assert.fail("Values don't match");
298 }
299 } catch (Throwable t) {
300 LOG.debug("Source cell: " + sourceCell + " target cell: "
301 + targetCell);
302 Throwables.propagate(t);
303 }
304 }
305 targetRow = targetScanner.next();
306 sourceRow = sourceScanner.next();
307 }
308 assertEquals("Target expected rows does not match.",expectedRows,
309 rowsCount);
310 sourceScanner.close();
311 targetScanner.close();
312 sourceTable.close();
313 targetTable.close();
314 }
315
316 private void assertTargetDoPutsFalse(int expectedRows, TableName
317 sourceTableName,
318 TableName targetTableName)
319 throws Exception {
320 Table sourceTable = TEST_UTIL.getConnection().getTable(sourceTableName);
321 Table targetTable = TEST_UTIL.getConnection().getTable(targetTableName);
322
323 ResultScanner sourceScanner = sourceTable.getScanner(new Scan());
324 ResultScanner targetScanner = targetTable.getScanner(new Scan());
325 Result targetRow = targetScanner.next();
326 Result sourceRow = sourceScanner.next();
327 int rowsCount = 0;
328
329 while (targetRow!=null) {
330
331
332 if (Bytes.toInt(sourceRow.getRow()) != Bytes.toInt(targetRow.getRow())) {
333 sourceRow = sourceScanner.next();
334 continue;
335 }
336
337 LOG.debug("SOURCE row: " + (sourceRow == null ?
338 "null" :
339 Bytes.toInt(sourceRow.getRow()))
340 + " cells:" + sourceRow);
341 LOG.debug("TARGET row: " + (targetRow == null ?
342 "null" :
343 Bytes.toInt(targetRow.getRow()))
344 + " cells:" + targetRow);
345
346 LOG.debug("rowsCount: " + rowsCount);
347
348 Cell[] sourceCells = sourceRow.rawCells();
349 Cell[] targetCells = targetRow.rawCells();
350 int targetRowKey = Bytes.toInt(targetRow.getRow());
351 if (targetRowKey >= 40 && targetRowKey < 60) {
352 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
353 LOG.debug("Target cells: " + Arrays.toString(targetCells));
354 Assert.fail("There shouldn't exist any rows between 40 and 60, since "
355 + "Puts are disabled and Deletes are enabled.");
356 } else if (targetRowKey >= 60 && targetRowKey < 70) {
357 if (sourceCells.length == targetCells.length) {
358 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
359 LOG.debug("Target cells: " + Arrays.toString(targetCells));
360 Assert.fail("Row " + Bytes.toInt(sourceRow.getRow())
361 + " shouldn't have same number of cells.");
362 }
363 } else if (targetRowKey >= 80 && targetRowKey < 90) {
364 LOG.debug("Source cells: " + Arrays.toString(sourceCells));
365 LOG.debug("Target cells: " + Arrays.toString(targetCells));
366 Assert.fail("There should be no rows between 80 and 90 on target, as "
367 + "these had different timestamps and should had been deleted.");
368 } else if (targetRowKey >= 90 && targetRowKey < 100) {
369 for (int j = 0; j < sourceCells.length; j++) {
370 Cell sourceCell = sourceCells[j];
371 Cell targetCell = targetCells[j];
372 if (CellUtil.matchingValue(sourceCell, targetCell)) {
373 Assert.fail("Cells values should not match for rows between "
374 + "90 and 100. Target row id: " + (Bytes.toInt(targetRow
375 .getRow())));
376 }
377 }
378 } else {
379 for (int j = 0; j < sourceCells.length; j++) {
380 Cell sourceCell = sourceCells[j];
381 Cell targetCell = targetCells[j];
382 try {
383 if (!CellUtil.matchingRow(sourceCell, targetCell)) {
384 Assert.fail("Rows don't match");
385 }
386 if (!CellUtil.matchingFamily(sourceCell, targetCell)) {
387 Assert.fail("Families don't match");
388 }
389 if (!CellUtil.matchingQualifier(sourceCell, targetCell)) {
390 Assert.fail("Qualifiers don't match");
391 }
392 if (!CellUtil.matchingTimestamp(sourceCell, targetCell)) {
393 Assert.fail("Timestamps don't match");
394 }
395 if (!CellUtil.matchingValue(sourceCell, targetCell)) {
396 Assert.fail("Values don't match");
397 }
398 } catch (Throwable t) {
399 LOG.debug(
400 "Source cell: " + sourceCell + " target cell: " + targetCell);
401 Throwables.propagate(t);
402 }
403 }
404 }
405 rowsCount++;
406 targetRow = targetScanner.next();
407 sourceRow = sourceScanner.next();
408 }
409 assertEquals("Target expected rows does not match.",expectedRows,
410 rowsCount);
411 sourceScanner.close();
412 targetScanner.close();
413 sourceTable.close();
414 targetTable.close();
415 }
416
417 private Counters syncTables(TableName sourceTableName, TableName targetTableName,
418 Path testDir, String... options) throws Exception {
419 SyncTable syncTable = new SyncTable(TEST_UTIL.getConfiguration());
420 String[] args = Arrays.copyOf(options, options.length+3);
421 args[options.length] = testDir.toString();
422 args[options.length+1] = sourceTableName.getNameAsString();
423 args[options.length+2] = targetTableName.getNameAsString();
424 int code = syncTable.run(args);
425 assertEquals("sync table job failed", 0, code);
426
427 LOG.info("Sync tables completed");
428 return syncTable.counters;
429 }
430
431 private void hashSourceTable(TableName sourceTableName, Path testDir)
432 throws Exception, IOException {
433 int numHashFiles = 3;
434 long batchSize = 100;
435 int scanBatch = 1;
436 HashTable hashTable = new HashTable(TEST_UTIL.getConfiguration());
437 int code = hashTable.run(new String[] {
438 "--batchsize=" + batchSize,
439 "--numhashfiles=" + numHashFiles,
440 "--scanbatch=" + scanBatch,
441 sourceTableName.getNameAsString(),
442 testDir.toString()});
443 assertEquals("hash table job failed", 0, code);
444
445 FileSystem fs = TEST_UTIL.getTestFileSystem();
446
447 HashTable.TableHash tableHash = HashTable.TableHash.read(fs.getConf(), testDir);
448 assertEquals(sourceTableName.getNameAsString(), tableHash.tableName);
449 assertEquals(batchSize, tableHash.batchSize);
450 assertEquals(numHashFiles, tableHash.numHashFiles);
451 assertEquals(numHashFiles - 1, tableHash.partitions.size());
452
453 LOG.info("Hash table completed");
454 }
455
456 private void writeTestData(TableName sourceTableName, TableName targetTableName)
457 throws Exception {
458 final byte[] family = Bytes.toBytes("family");
459 final byte[] column1 = Bytes.toBytes("c1");
460 final byte[] column2 = Bytes.toBytes("c2");
461 final byte[] value1 = Bytes.toBytes("val1");
462 final byte[] value2 = Bytes.toBytes("val2");
463 final byte[] value3 = Bytes.toBytes("val3");
464
465 int numRows = 100;
466 int sourceRegions = 10;
467 int targetRegions = 6;
468
469 Table sourceTable = TEST_UTIL.createTable(sourceTableName,
470 family, generateSplits(numRows, sourceRegions));
471
472 Table targetTable = TEST_UTIL.createTable(targetTableName,
473 family, generateSplits(numRows, targetRegions));
474
475 long timestamp = 1430764183454L;
476
477 int rowIndex = 0;
478
479 for (; rowIndex < 40; rowIndex++) {
480 Put sourcePut = new Put(Bytes.toBytes(rowIndex));
481 sourcePut.addColumn(family, column1, timestamp, value1);
482 sourcePut.addColumn(family, column2, timestamp, value2);
483 sourceTable.put(sourcePut);
484
485 Put targetPut = new Put(Bytes.toBytes(rowIndex));
486 targetPut.addColumn(family, column1, timestamp, value1);
487 targetPut.addColumn(family, column2, timestamp, value2);
488 targetTable.put(targetPut);
489 }
490
491
492
493
494 for (; rowIndex < 50; rowIndex++) {
495 Put put = new Put(Bytes.toBytes(rowIndex));
496 put.addColumn(family, column1, timestamp, value1);
497 put.addColumn(family, column2, timestamp, value2);
498 sourceTable.put(put);
499 }
500
501
502
503
504 for (; rowIndex < 60; rowIndex++) {
505 Put put = new Put(Bytes.toBytes(rowIndex));
506 put.addColumn(family, column1, timestamp, value1);
507 put.addColumn(family, column2, timestamp, value2);
508 targetTable.put(put);
509 }
510
511
512
513 for (; rowIndex < 70; rowIndex++) {
514 Put sourcePut = new Put(Bytes.toBytes(rowIndex));
515 sourcePut.addColumn(family, column1, timestamp, value1);
516 sourcePut.addColumn(family, column2, timestamp, value2);
517 sourceTable.put(sourcePut);
518
519 Put targetPut = new Put(Bytes.toBytes(rowIndex));
520 targetPut.addColumn(family, column1, timestamp, value1);
521 targetTable.put(targetPut);
522 }
523
524
525
526 for (; rowIndex < 80; rowIndex++) {
527 Put sourcePut = new Put(Bytes.toBytes(rowIndex));
528 sourcePut.addColumn(family, column1, timestamp, value1);
529 sourceTable.put(sourcePut);
530
531 Put targetPut = new Put(Bytes.toBytes(rowIndex));
532 targetPut.addColumn(family, column1, timestamp, value1);
533 targetPut.addColumn(family, column2, timestamp, value2);
534 targetTable.put(targetPut);
535 }
536
537
538
539
540 for (; rowIndex < 90; rowIndex++) {
541 Put sourcePut = new Put(Bytes.toBytes(rowIndex));
542 sourcePut.addColumn(family, column1, timestamp, column1);
543 sourcePut.addColumn(family, column2, timestamp, value2);
544 sourceTable.put(sourcePut);
545
546 Put targetPut = new Put(Bytes.toBytes(rowIndex));
547 targetPut.addColumn(family, column1, timestamp+1, column1);
548 targetPut.addColumn(family, column2, timestamp-1, value2);
549 targetTable.put(targetPut);
550 }
551
552
553
554 for (; rowIndex < numRows; rowIndex++) {
555 Put sourcePut = new Put(Bytes.toBytes(rowIndex));
556 sourcePut.addColumn(family, column1, timestamp, value1);
557 sourcePut.addColumn(family, column2, timestamp, value2);
558 sourceTable.put(sourcePut);
559
560 Put targetPut = new Put(Bytes.toBytes(rowIndex));
561 targetPut.addColumn(family, column1, timestamp, value3);
562 targetPut.addColumn(family, column2, timestamp, value3);
563 targetTable.put(targetPut);
564 }
565
566 sourceTable.close();
567 targetTable.close();
568 }
569
570
571 }