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.HBaseAdmin;
32 import org.apache.hadoop.hbase.client.HTable;
33 import org.apache.hadoop.hbase.client.Put;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.hamcrest.CoreMatchers;
37 import org.junit.Assert;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 import com.google.common.collect.Lists;
42
43 @Category(LargeTests.class)
44 public class TestIncrementalBackup extends TestBackupBase {
45 private static final Log LOG = LogFactory.getLog(TestIncrementalBackup.class);
46
47 @Test
48 public void TestIncBackupRestore() throws Exception {
49
50 LOG.info("create full backup image for all tables");
51
52 List<TableName> tables = Lists.newArrayList(table1, table2, table3, table4);
53 HBaseAdmin admin = null;
54 Connection conn = ConnectionFactory.createConnection(conf1);
55 admin = (HBaseAdmin) conn.getAdmin();
56
57 BackupRequest request = new BackupRequest();
58 request.setBackupType(BackupType.FULL).setTableList(tables).setTargetRootDir(BACKUP_ROOT_DIR);
59 String backupIdFull = admin.getBackupAdmin().backupTables(request);
60
61 assertTrue(checkSucceeded(backupIdFull));
62
63
64 HTable t1 = (HTable) conn.getTable(table1);
65 Put p1;
66 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
67 p1 = new Put(Bytes.toBytes("row-t1" + i));
68 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
69 t1.put(p1);
70 }
71
72 Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
73 t1.close();
74
75 HTable t2 = (HTable) conn.getTable(table2);
76 Put p2;
77 for (int i = 0; i < 5; i++) {
78 p2 = new Put(Bytes.toBytes("row-t2" + i));
79 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
80 t2.put(p2);
81 }
82
83 Assert.assertThat(TEST_UTIL.countRows(t2), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
84 t2.close();
85
86
87 tables = Lists.newArrayList(table1, table2, table3);
88 request = new BackupRequest();
89 request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
90 .setTargetRootDir(BACKUP_ROOT_DIR);
91 String backupIdIncMultiple = admin.getBackupAdmin().backupTables(request);
92 assertTrue(checkSucceeded(backupIdIncMultiple));
93
94
95 TableName[] tablesRestoreFull =
96 new TableName[] { table1, table2, table3, table4 };
97
98 TableName[] tablesMapFull =
99 new TableName[] { table1_restore, table2_restore, table3_restore, table4_restore };
100
101 BackupAdmin client = getBackupAdmin();
102 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdFull, false,
103 tablesRestoreFull,
104 tablesMapFull, false));
105
106
107 HBaseAdmin hAdmin = TEST_UTIL.getHBaseAdmin();
108 assertTrue(hAdmin.tableExists(table1_restore));
109 assertTrue(hAdmin.tableExists(table2_restore));
110 assertTrue(hAdmin.tableExists(table3_restore));
111 assertTrue(hAdmin.tableExists(table4_restore));
112
113 hAdmin.close();
114
115
116 HTable hTable = (HTable) conn.getTable(table1_restore);
117 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
118 hTable.close();
119
120 hTable = (HTable) conn.getTable(table2_restore);
121 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
122 hTable.close();
123
124 hTable = (HTable) conn.getTable(table3_restore);
125 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
126 hTable.close();
127
128 hTable = (HTable) conn.getTable(table4_restore);
129 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
130 hTable.close();
131
132
133 TableName[] tablesRestoreIncMultiple =
134 new TableName[] { table1, table2, table3 };
135 TableName[] tablesMapIncMultiple =
136 new TableName[] { table1_restore, table2_restore, table3_restore };
137 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncMultiple, false,
138 tablesRestoreIncMultiple, tablesMapIncMultiple, true));
139
140 hTable = (HTable) conn.getTable(table1_restore);
141 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
142 hTable.close();
143
144 hTable = (HTable) conn.getTable(table2_restore);
145 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
146 hTable.close();
147
148 hTable = (HTable) conn.getTable(table3_restore);
149 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
150 hTable.close();
151
152
153
154 tables = toList(table4.getNameAsString());
155 request = new BackupRequest();
156 request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
157 .setTargetRootDir(BACKUP_ROOT_DIR);
158 String backupIdIncEmpty = admin.getBackupAdmin().backupTables(request);
159
160
161
162 TableName[] tablesRestoreIncEmpty = new TableName[] { table4 };
163 TableName[] tablesMapIncEmpty = new TableName[] { table4_restore };
164
165 client.restore(createRestoreRequest(BACKUP_ROOT_DIR, backupIdIncEmpty, false,
166 tablesRestoreIncEmpty,
167 tablesMapIncEmpty, true));
168
169 hTable = (HTable) conn.getTable(table4_restore);
170 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
171 hTable.close();
172 admin.close();
173 conn.close();
174 }
175
176 }