1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.testclassification.LargeTests;
29 import org.apache.hadoop.hbase.NamespaceDescriptor;
30 import org.apache.hadoop.hbase.NamespaceNotFoundException;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
33 import org.apache.hadoop.hbase.snapshot.SnapshotDoesNotExistException;
34 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43
44
45
46 @Category(LargeTests.class)
47 public class TestCloneSnapshotFromClient {
48 final Log LOG = LogFactory.getLog(getClass());
49
50 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51
52 private final byte[] FAMILY = Bytes.toBytes("cf");
53
54 private byte[] emptySnapshot;
55 private byte[] snapshotName0;
56 private byte[] snapshotName1;
57 private byte[] snapshotName2;
58 private int snapshot0Rows;
59 private int snapshot1Rows;
60 private TableName tableName;
61 private Admin admin;
62
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
66 TEST_UTIL.getConfiguration().setBoolean("hbase.online.schema.update.enable", true);
67 TEST_UTIL.getConfiguration().setInt("hbase.hstore.compactionThreshold", 10);
68 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
69 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
70 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
71 TEST_UTIL.getConfiguration().setBoolean(
72 "hbase.master.enabletable.roundrobin", true);
73 TEST_UTIL.getConfiguration().setInt(HConstants.REGION_SERVER_HIGH_PRIORITY_HANDLER_COUNT, 40);
74 TEST_UTIL.startMiniCluster(3);
75 }
76
77 @AfterClass
78 public static void tearDownAfterClass() throws Exception {
79 TEST_UTIL.shutdownMiniCluster();
80 }
81
82
83
84
85
86
87 @Before
88 public void setup() throws Exception {
89 this.admin = TEST_UTIL.getHBaseAdmin();
90
91 long tid = System.currentTimeMillis();
92 tableName = TableName.valueOf("testtb-" + tid);
93 emptySnapshot = Bytes.toBytes("emptySnaptb-" + tid);
94 snapshotName0 = Bytes.toBytes("snaptb0-" + tid);
95 snapshotName1 = Bytes.toBytes("snaptb1-" + tid);
96 snapshotName2 = Bytes.toBytes("snaptb2-" + tid);
97
98
99 SnapshotTestingUtils.createTable(TEST_UTIL, tableName, getNumReplicas(), FAMILY);
100 admin.disableTable(tableName);
101
102
103 admin.snapshot(emptySnapshot, tableName);
104
105
106 admin.enableTable(tableName);
107 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY);
108 try (Table table = TEST_UTIL.getConnection().getTable(tableName)){
109 snapshot0Rows = TEST_UTIL.countRows(table);
110 }
111 admin.disableTable(tableName);
112
113
114 admin.snapshot(snapshotName0, tableName);
115
116
117 admin.enableTable(tableName);
118 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, FAMILY);
119 try (Table table = TEST_UTIL.getConnection().getTable(tableName)){
120 snapshot1Rows = TEST_UTIL.countRows(table);
121 }
122 admin.disableTable(tableName);
123
124
125 admin.snapshot(snapshotName1, tableName);
126
127
128 admin.enableTable(tableName);
129 }
130
131 protected int getNumReplicas() {
132 return 1;
133 }
134
135 @After
136 public void tearDown() throws Exception {
137 if (admin.tableExists(tableName)) {
138 TEST_UTIL.deleteTable(tableName);
139 }
140 SnapshotTestingUtils.deleteAllSnapshots(admin);
141 SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
142 }
143
144 @Test(expected=SnapshotDoesNotExistException.class)
145 public void testCloneNonExistentSnapshot() throws IOException, InterruptedException {
146 String snapshotName = "random-snapshot-" + System.currentTimeMillis();
147 TableName tableName = TableName.valueOf("random-table-" + System.currentTimeMillis());
148 admin.cloneSnapshot(snapshotName, tableName);
149 }
150
151 @Test(expected = NamespaceNotFoundException.class)
152 public void testCloneOnMissingNamespace() throws IOException, InterruptedException {
153 TableName clonedTableName = TableName.valueOf("unknownNS:clonetb");
154 admin.cloneSnapshot(snapshotName1, clonedTableName);
155 }
156
157 @Test
158 public void testCloneSnapshot() throws IOException, InterruptedException {
159 TableName clonedTableName = TableName.valueOf("clonedtb-" + System.currentTimeMillis());
160 testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
161 testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
162 testCloneSnapshot(clonedTableName, emptySnapshot, 0);
163 }
164
165 private void testCloneSnapshot(final TableName tableName, final byte[] snapshotName,
166 int snapshotRows) throws IOException, InterruptedException {
167
168 admin.cloneSnapshot(snapshotName, tableName);
169 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, tableName, snapshotRows);
170
171 verifyReplicasCameOnline(tableName);
172 TEST_UTIL.deleteTable(tableName);
173 }
174
175 protected void verifyReplicasCameOnline(TableName tableName) throws IOException {
176 SnapshotTestingUtils.verifyReplicasCameOnline(tableName, admin, getNumReplicas());
177 }
178
179 @Test
180 public void testCloneSnapshotCrossNamespace() throws IOException, InterruptedException {
181 String nsName = "testCloneSnapshotCrossNamespace";
182 admin.createNamespace(NamespaceDescriptor.create(nsName).build());
183 TableName clonedTableName =
184 TableName.valueOf(nsName, "clonedtb-" + System.currentTimeMillis());
185 testCloneSnapshot(clonedTableName, snapshotName0, snapshot0Rows);
186 testCloneSnapshot(clonedTableName, snapshotName1, snapshot1Rows);
187 testCloneSnapshot(clonedTableName, emptySnapshot, 0);
188 }
189
190
191
192
193 @Test
194 public void testCloneLinksAfterDelete() throws IOException, InterruptedException {
195
196 TableName clonedTableName = TableName.valueOf("clonedtb1-" + System.currentTimeMillis());
197 admin.cloneSnapshot(snapshotName0, clonedTableName);
198 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
199
200
201 admin.disableTable(clonedTableName);
202 admin.snapshot(snapshotName2, clonedTableName);
203
204
205 TableName clonedTableName2 = TableName.valueOf("clonedtb2-" + System.currentTimeMillis());
206 admin.cloneSnapshot(snapshotName2, clonedTableName2);
207 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
208 admin.disableTable(clonedTableName2);
209
210
211 TEST_UTIL.deleteTable(tableName);
212 waitCleanerRun();
213
214
215 admin.enableTable(clonedTableName);
216 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName, snapshot0Rows);
217
218
219 admin.enableTable(clonedTableName2);
220 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
221 admin.disableTable(clonedTableName2);
222
223
224 TEST_UTIL.deleteTable(clonedTableName);
225 waitCleanerRun();
226
227
228 admin.enableTable(clonedTableName2);
229 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName2, snapshot0Rows);
230
231
232 TableName clonedTableName3 = TableName.valueOf("clonedtb3-" + System.currentTimeMillis());
233 admin.cloneSnapshot(snapshotName2, clonedTableName3);
234 SnapshotTestingUtils.verifyRowCount(TEST_UTIL, clonedTableName3, snapshot0Rows);
235
236
237 TEST_UTIL.deleteTable(clonedTableName2);
238 TEST_UTIL.deleteTable(clonedTableName3);
239 admin.deleteSnapshot(snapshotName2);
240 }
241
242
243
244
245
246 private void waitCleanerRun() throws InterruptedException {
247 TEST_UTIL.getMiniHBaseCluster().getMaster().getHFileCleaner().choreForTesting();
248 }
249 }