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 TestBackupAdmin extends TestBackupBase {
45 private static final Log LOG = LogFactory.getLog(TestBackupAdmin.class);
46
47 @Test
48 public void TestIncBackupRestoreWithAdminAPI() 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 BackupAdmin backupAdmin = null;
55 Connection conn = ConnectionFactory.createConnection(conf1);
56 admin = (HBaseAdmin) conn.getAdmin();
57 backupAdmin = admin.getBackupAdmin();
58 BackupRequest request = new BackupRequest();
59 request.setBackupType(BackupType.FULL).setTableList(tables).setTargetRootDir(BACKUP_ROOT_DIR);
60 String backupIdFull = backupAdmin.backupTables(request);
61
62 assertTrue(checkSucceeded(backupIdFull));
63
64
65 HTable t1 = (HTable) conn.getTable(table1);
66 Put p1;
67 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
68 p1 = new Put(Bytes.toBytes("row-t1" + i));
69 p1.addColumn(famName, qualName, Bytes.toBytes("val" + i));
70 t1.put(p1);
71 }
72
73 Assert.assertThat(TEST_UTIL.countRows(t1), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
74 t1.close();
75
76 HTable t2 = (HTable) conn.getTable(table2);
77 Put p2;
78 for (int i = 0; i < 5; i++) {
79 p2 = new Put(Bytes.toBytes("row-t2" + i));
80 p2.addColumn(famName, qualName, Bytes.toBytes("val" + i));
81 t2.put(p2);
82 }
83
84 Assert.assertThat(TEST_UTIL.countRows(t2), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
85 t2.close();
86
87
88 tables = Lists.newArrayList(table1, table2, table3);
89 request = new BackupRequest();
90 request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
91 .setTargetRootDir(BACKUP_ROOT_DIR);
92 String backupIdIncMultiple = backupAdmin.backupTables(request);
93 assertTrue(checkSucceeded(backupIdIncMultiple));
94
95
96 TableName[] tablesRestoreFull =
97 new TableName[] { table1, table2, table3, table4 };
98
99 TableName[] tablesMapFull =
100 new TableName[] { table1_restore, table2_restore, table3_restore, table4_restore };
101
102 RestoreRequest restoreRequest = new RestoreRequest();
103 restoreRequest.setBackupRootDir(BACKUP_ROOT_DIR).setBackupId(backupIdFull).
104 setCheck(false).setOverwrite(false).
105 setFromTables(tablesRestoreFull).setToTables(tablesMapFull);
106
107 backupAdmin.restore(restoreRequest);
108
109
110
111 assertTrue(admin.tableExists(table1_restore));
112 assertTrue(admin.tableExists(table2_restore));
113 assertTrue(admin.tableExists(table3_restore));
114 assertTrue(admin.tableExists(table4_restore));
115
116
117
118 HTable hTable = (HTable) conn.getTable(table1_restore);
119 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
120 hTable.close();
121
122 hTable = (HTable) conn.getTable(table2_restore);
123 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH));
124 hTable.close();
125
126 hTable = (HTable) conn.getTable(table3_restore);
127 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
128 hTable.close();
129
130 hTable = (HTable) conn.getTable(table4_restore);
131 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
132 hTable.close();
133
134
135 TableName[] tablesRestoreIncMultiple =
136 new TableName[] { table1, table2, table3 };
137 TableName[] tablesMapIncMultiple =
138 new TableName[] { table1_restore, table2_restore, table3_restore };
139
140 restoreRequest = new RestoreRequest();
141 restoreRequest.setBackupRootDir(BACKUP_ROOT_DIR).setBackupId(backupIdIncMultiple).
142 setCheck(false).setOverwrite(true).
143 setFromTables(tablesRestoreIncMultiple).setToTables(tablesMapIncMultiple);
144
145 backupAdmin.restore(restoreRequest);
146
147 hTable = (HTable) conn.getTable(table1_restore);
148 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH * 2));
149 hTable.close();
150
151 hTable = (HTable) conn.getTable(table2_restore);
152 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(NB_ROWS_IN_BATCH + 5));
153 hTable.close();
154
155 hTable = (HTable) conn.getTable(table3_restore);
156 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
157 hTable.close();
158
159
160
161 tables = toList(table4.getNameAsString());
162 request = new BackupRequest();
163 request.setBackupType(BackupType.INCREMENTAL).setTableList(tables)
164 .setTargetRootDir(BACKUP_ROOT_DIR);
165 String backupIdIncEmpty = admin.getBackupAdmin().backupTables(request);
166
167
168
169 TableName[] tablesRestoreIncEmpty = new TableName[] { table4 };
170 TableName[] tablesMapIncEmpty = new TableName[] { table4_restore };
171
172 restoreRequest = new RestoreRequest();
173 restoreRequest.setBackupRootDir(BACKUP_ROOT_DIR).setBackupId(backupIdIncEmpty).
174 setCheck(false).setOverwrite(true).
175 setFromTables(tablesRestoreIncEmpty).setToTables(tablesMapIncEmpty);
176
177 backupAdmin.restore(restoreRequest);
178
179 hTable = (HTable) conn.getTable(table4_restore);
180 Assert.assertThat(TEST_UTIL.countRows(hTable), CoreMatchers.equalTo(0));
181 hTable.close();
182 admin.close();
183 conn.close();
184 }
185
186 }