1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.backup;
20
21 import static org.junit.Assert.assertTrue;
22
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.BackupAdmin;
29 import org.apache.hadoop.hbase.client.Connection;
30 import org.apache.hadoop.hbase.client.ConnectionFactory;
31 import org.apache.hadoop.hbase.client.HTable;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.testclassification.LargeTests;
34 import org.apache.hadoop.hbase.util.Bytes;
35 import org.hamcrest.CoreMatchers;
36 import org.junit.Assert;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 import com.google.common.collect.Lists;
41
42 @Category(LargeTests.class)
43 public class TestIncrementalBackupNoDataLoss extends TestBackupBase {
44 private static final Log LOG = LogFactory.getLog(TestIncrementalBackupNoDataLoss.class);
45
46
47 @Test
48 public void TestIncBackupRestore() throws Exception {
49
50
51 LOG.info("create full backup image for all tables");
52 List<TableName> tables = Lists.newArrayList(table1, table2);
53 String backupIdFull = fullTableBackup(tables);
54 assertTrue(checkSucceeded(backupIdFull));
55 Connection conn = ConnectionFactory.createConnection(conf1);
56
57 HTable t1 = (HTable) conn.getTable(table1);
58 Put p1;
59 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
60 p1 = new Put(Bytes.toBytes("row-t1" + i));
61 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
62 t1.put(p1);
63 }
64
65 Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
66 t1.close();
67
68 HTable t2 = (HTable) conn.getTable(table2);
69 Put p2;
70 for (int i = 0; i < 5; i++) {
71 p2 = new Put(Bytes.toBytes("row-t2" + i));
72 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
73 t2.put(p2);
74 }
75
76 Assert.assertThat(TEST_UTIL.countRows(t2), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
77 t2.close();
78
79
80
81 tables = Lists.newArrayList(table1);
82 String backupIdInc1 = incrementalTableBackup(tables);
83 assertTrue(checkSucceeded(backupIdInc1));
84
85
86
87 tables = Lists.newArrayList(table2);
88 String backupIdInc2 = incrementalTableBackup(tables);
89 assertTrue(checkSucceeded(backupIdInc2));
90
91 TableName[] tablesRestoreInc1 = new TableName[] { table1 };
92 TableName[] tablesMapInc1 = new TableName[] { table1_restore };
93
94 if (TEST_UTIL.getHBaseAdmin().tableExists(table1_restore)) {
95 TEST_UTIL.deleteTable(table1_restore);
96 }
97 if (TEST_UTIL.getHBaseAdmin().tableExists(table2_restore)) {
98 TEST_UTIL.deleteTable(table2_restore);
99 }
100
101 BackupAdmin client = getBackupAdmin();
102 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdInc1, false, tablesRestoreInc1,
103 tablesMapInc1, false));
104
105 HTable hTable = (HTable) conn.getTable(table1_restore);
106 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
107 hTable.close();
108
109
110
111 TableName[] tablesRestoreInc2 = new TableName[] { table2 };
112 TableName[] tablesMapInc2 = new TableName[] { table2_restore };
113
114 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdInc2, false, tablesRestoreInc2,
115 tablesMapInc2, false));
116
117 hTable = (HTable) conn.getTable(table2_restore);
118 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
119 hTable.close();
120
121 conn.close();
122 }
123
124 }