1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.client;
20
21 import java.util.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.TableName;
29 import org.apache.hadoop.hbase.HBaseTestingUtility;
30 import org.apache.hadoop.hbase.HColumnDescriptor;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.testclassification.LargeTests;
35 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
36 import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
37 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Assert;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Ignore;
45 import org.junit.Test;
46 import org.junit.experimental.categories.Category;
47
48
49
50
51 @Category(LargeTests.class)
52 public class TestSnapshotCloneIndependence {
53 private static final Log LOG = LogFactory.getLog(TestSnapshotCloneIndependence.class);
54
55 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
56
57 private static final int NUM_RS = 2;
58 private static final String STRING_TABLE_NAME = "test";
59 private static final String TEST_FAM_STR = "fam";
60 private static final byte[] TEST_FAM = Bytes.toBytes(TEST_FAM_STR);
61 private static final TableName TABLE_NAME = TableName.valueOf(STRING_TABLE_NAME);
62 private static final int CLEANER_INTERVAL = 10;
63
64
65
66
67
68 @BeforeClass
69 public static void setupCluster() throws Exception {
70 setupConf(UTIL.getConfiguration());
71 UTIL.startMiniCluster(NUM_RS);
72 }
73
74 private static void setupConf(Configuration conf) {
75
76 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
77
78 conf.setInt("hbase.regionsever.info.port", -1);
79
80 conf.setInt("hbase.hregion.memstore.flush.size", 25000);
81
82
83 conf.setInt("hbase.hstore.compaction.min", 10);
84 conf.setInt("hbase.hstore.compactionThreshold", 10);
85
86 conf.setInt("hbase.hstore.blockingStoreFiles", 12);
87 conf.setInt("hbase.regionserver.msginterval", 100);
88 conf.setBoolean("hbase.master.enabletable.roundrobin", true);
89
90 conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
91 ConstantSizeRegionSplitPolicy.class.getName());
92
93 conf.setInt("hbase.master.cleaner.interval", CLEANER_INTERVAL);
94 conf.setInt("hbase.master.hfilecleaner.plugins.snapshot.period", CLEANER_INTERVAL);
95
96
97
98 conf.setInt("hbase.master.hfilecleaner.ttl", CLEANER_INTERVAL);
99 }
100
101 @Before
102 public void setup() throws Exception {
103 UTIL.createTable(TABLE_NAME, TEST_FAM);
104 }
105
106 @After
107 public void tearDown() throws Exception {
108 UTIL.deleteTable(TABLE_NAME);
109 SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
110 SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
111 }
112
113 @AfterClass
114 public static void cleanupTest() throws Exception {
115 try {
116 UTIL.shutdownMiniCluster();
117 } catch (Exception e) {
118 LOG.warn("failure shutting down cluster", e);
119 }
120 }
121
122
123
124
125
126 @Test (timeout=300000)
127 public void testOnlineSnapshotAppendIndependent() throws Exception {
128 runTestSnapshotAppendIndependent(true);
129 }
130
131
132
133
134
135 @Test (timeout=300000)
136 public void testOfflineSnapshotAppendIndependent() throws Exception {
137 runTestSnapshotAppendIndependent(false);
138 }
139
140
141
142
143
144 @Test (timeout=300000)
145 public void testOnlineSnapshotMetadataChangesIndependent() throws Exception {
146 runTestSnapshotMetadataChangesIndependent(true);
147 }
148
149
150
151
152
153 @Test (timeout=300000)
154 public void testOfflineSnapshotMetadataChangesIndependent() throws Exception {
155 runTestSnapshotMetadataChangesIndependent(false);
156 }
157
158
159
160
161
162 @Test (timeout=300000)
163 public void testOfflineSnapshotRegionOperationsIndependent() throws Exception {
164 runTestRegionOperationsIndependent(false);
165 }
166
167
168
169
170
171 @Test (timeout=300000)
172 public void testOnlineSnapshotRegionOperationsIndependent() throws Exception {
173 runTestRegionOperationsIndependent(true);
174 }
175
176 @Test (timeout=300000)
177 public void testOfflineSnapshotDeleteIndependent() throws Exception {
178 runTestSnapshotDeleteIndependent(false);
179 }
180
181 @Ignore
182 @Test (timeout=300000)
183 public void testOnlineSnapshotDeleteIndependent() throws Exception {
184 runTestSnapshotDeleteIndependent(true);
185 }
186
187 private static void waitOnSplit(final HTable t, int originalCount) throws Exception {
188 for (int i = 0; i < 200; i++) {
189 try {
190 Thread.sleep(50);
191 } catch (InterruptedException e) {
192
193 Thread.currentThread().interrupt();
194 }
195 if (t.getAllRegionLocations().size() > originalCount) {
196 return;
197 }
198 }
199 throw new Exception("Split did not increase the number of regions");
200 }
201
202
203
204
205
206
207 private void runTestSnapshotAppendIndependent(boolean online) throws Exception {
208 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
209 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
210
211 Admin admin = UTIL.getHBaseAdmin();
212 final long startTime = System.currentTimeMillis();
213 final TableName localTableName =
214 TableName.valueOf(STRING_TABLE_NAME + startTime);
215
216 try (Table original = UTIL.createTable(localTableName, TEST_FAM)) {
217 UTIL.loadTable(original, TEST_FAM);
218 final int origTableRowCount = UTIL.countRows(original);
219
220
221 final String snapshotNameAsString = "snapshot_" + localTableName;
222 byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
223
224 SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
225 snapshotNameAsString, rootDir, fs, online);
226
227 if (!online) {
228 admin.enableTable(localTableName);
229 }
230 TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
231 admin.cloneSnapshot(snapshotName, cloneTableName);
232
233 try (Table clonedTable = new HTable(UTIL.getConfiguration(), cloneTableName)){
234 final int clonedTableRowCount = UTIL.countRows(clonedTable);
235
236 Assert.assertEquals(
237 "The line counts of original and cloned tables do not match after clone. ",
238 origTableRowCount, clonedTableRowCount);
239
240
241 final String rowKey = "new-row-" + System.currentTimeMillis();
242
243 Put p = new Put(Bytes.toBytes(rowKey));
244 p.add(TEST_FAM, Bytes.toBytes("someQualifier"), Bytes.toBytes("someString"));
245 original.put(p);
246
247
248 Assert.assertEquals("The row count of the original table was not modified by the put",
249 origTableRowCount + 1, UTIL.countRows(original));
250 Assert.assertEquals(
251 "The row count of the cloned table changed as a result of addition to the original",
252 clonedTableRowCount, UTIL.countRows(clonedTable));
253
254 p = new Put(Bytes.toBytes(rowKey));
255 p.add(TEST_FAM, Bytes.toBytes("someQualifier"), Bytes.toBytes("someString"));
256 clonedTable.put(p);
257
258
259 Assert.assertEquals(
260 "The row count of the original table was modified by the put to the clone",
261 origTableRowCount + 1, UTIL.countRows(original));
262 Assert.assertEquals("The row count of the cloned table was not modified by the put",
263 clonedTableRowCount + 1, UTIL.countRows(clonedTable));
264 }
265 }
266 }
267
268
269
270
271
272 private void runTestRegionOperationsIndependent(boolean online) throws Exception {
273 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
274 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
275
276
277 Admin admin = UTIL.getHBaseAdmin();
278 final long startTime = System.currentTimeMillis();
279 final TableName localTableName =
280 TableName.valueOf(STRING_TABLE_NAME + startTime);
281 HTable original = UTIL.createTable(localTableName, TEST_FAM);
282 UTIL.loadTable(original, TEST_FAM);
283 final int loadedTableCount = UTIL.countRows(original);
284 System.out.println("Original table has: " + loadedTableCount + " rows");
285
286 final String snapshotNameAsString = "snapshot_" + localTableName;
287
288
289 SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
290 snapshotNameAsString, rootDir, fs, online);
291
292 if (!online) {
293 admin.enableTable(localTableName);
294 }
295
296 TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
297
298
299 byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
300 admin.cloneSnapshot(snapshotName, cloneTableName);
301
302
303 original.clearRegionCache();
304 List<HRegionInfo> originalTableHRegions = admin.getTableRegions(localTableName);
305
306 final int originalRegionCount = originalTableHRegions.size();
307 final int cloneTableRegionCount = admin.getTableRegions(cloneTableName).size();
308 Assert.assertEquals(
309 "The number of regions in the cloned table is different than in the original table.",
310 originalRegionCount, cloneTableRegionCount);
311
312
313 admin.splitRegion(originalTableHRegions.get(0).getRegionName());
314 waitOnSplit(original, originalRegionCount);
315
316
317 final int cloneTableRegionCount2 = admin.getTableRegions(cloneTableName).size();
318 Assert.assertEquals(
319 "The number of regions in the cloned table changed though none of its regions were split.",
320 cloneTableRegionCount, cloneTableRegionCount2);
321 }
322
323
324
325
326
327
328 private void runTestSnapshotMetadataChangesIndependent(boolean online) throws Exception {
329 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
330 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
331
332
333 Admin admin = UTIL.getHBaseAdmin();
334 final long startTime = System.currentTimeMillis();
335 final TableName localTableName =
336 TableName.valueOf(STRING_TABLE_NAME + startTime);
337 HTable original = UTIL.createTable(localTableName, TEST_FAM);
338 UTIL.loadTable(original, TEST_FAM);
339
340 final String snapshotNameAsString = "snapshot_" + localTableName;
341
342
343 SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
344 snapshotNameAsString, rootDir, fs, online);
345
346 if (!online) {
347 admin.enableTable(localTableName);
348 }
349 TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
350
351
352 byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
353 admin.cloneSnapshot(snapshotName, cloneTableName);
354
355
356 byte[] TEST_FAM_2 = Bytes.toBytes("fam2");
357 HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAM_2);
358
359 admin.disableTable(localTableName);
360 admin.addColumn(localTableName, hcd);
361
362
363 admin.enableTable(localTableName);
364
365
366
367
368 HTableDescriptor originalTableDescriptor = original.getTableDescriptor();
369 HTableDescriptor clonedTableDescriptor = admin.getTableDescriptor(cloneTableName);
370
371 Assert.assertTrue("The original family was not found. There is something wrong. ",
372 originalTableDescriptor.hasFamily(TEST_FAM));
373 Assert.assertTrue("The original family was not found in the clone. There is something wrong. ",
374 clonedTableDescriptor.hasFamily(TEST_FAM));
375
376 Assert.assertTrue("The new family was not found. ",
377 originalTableDescriptor.hasFamily(TEST_FAM_2));
378 Assert.assertTrue("The new family was not found. ",
379 !clonedTableDescriptor.hasFamily(TEST_FAM_2));
380 }
381
382
383
384
385
386
387 private void runTestSnapshotDeleteIndependent(boolean online) throws Exception {
388 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
389 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
390
391 final Admin admin = UTIL.getHBaseAdmin();
392 final long startTime = System.currentTimeMillis();
393 final TableName localTableName =
394 TableName.valueOf(STRING_TABLE_NAME + startTime);
395
396 try (Table original = UTIL.createTable(localTableName, TEST_FAM)) {
397 UTIL.loadTable(original, TEST_FAM);
398 }
399
400
401 final String snapshotNameAsString = "snapshot_" + localTableName;
402 byte[] snapshotName = Bytes.toBytes(snapshotNameAsString);
403
404 SnapshotTestingUtils.createSnapshotAndValidate(admin, localTableName, TEST_FAM_STR,
405 snapshotNameAsString, rootDir, fs, online);
406
407 if (!online) {
408 admin.enableTable(localTableName);
409 }
410 TableName cloneTableName = TableName.valueOf("test-clone-" + localTableName);
411 admin.cloneSnapshot(snapshotName, cloneTableName);
412
413
414 admin.majorCompact(localTableName);
415
416
417 admin.deleteSnapshot(snapshotName);
418
419
420 Thread.sleep(10000);
421
422 try (Table original = UTIL.getConnection().getTable(localTableName)) {
423 try (Table clonedTable = UTIL.getConnection().getTable(cloneTableName)) {
424
425 final int origTableRowCount = UTIL.countRows(original);
426 final int clonedTableRowCount = UTIL.countRows(clonedTable);
427 Assert.assertEquals(origTableRowCount, clonedTableRowCount);
428 }
429 }
430 }
431 }