1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.snapshot;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.CountDownLatch;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.fs.FileSystem;
34 import org.apache.hadoop.fs.Path;
35 import org.apache.hadoop.hbase.HBaseTestingUtility;
36 import org.apache.hadoop.hbase.HConstants;
37 import org.apache.hadoop.hbase.HRegionInfo;
38 import org.apache.hadoop.hbase.HTableDescriptor;
39 import org.apache.hadoop.hbase.LargeTests;
40 import org.apache.hadoop.hbase.TableNotFoundException;
41 import org.apache.hadoop.hbase.client.HBaseAdmin;
42 import org.apache.hadoop.hbase.client.HTable;
43 import org.apache.hadoop.hbase.master.HMaster;
44 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
45 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
46 import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
47 import org.apache.hadoop.hbase.regionserver.HRegion;
48 import org.apache.hadoop.hbase.regionserver.HRegionServer;
49 import org.apache.hadoop.hbase.util.Bytes;
50 import org.apache.hadoop.hbase.util.FSTableDescriptors;
51 import org.apache.hadoop.hbase.util.FSUtils;
52 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
53 import org.junit.After;
54 import org.junit.AfterClass;
55 import org.junit.Before;
56 import org.junit.BeforeClass;
57 import org.junit.Test;
58 import org.junit.experimental.categories.Category;
59
60
61
62
63
64
65
66
67
68 @Category(LargeTests.class)
69 public class TestFlushSnapshotFromClient {
70 private static final Log LOG = LogFactory.getLog(TestFlushSnapshotFromClient.class);
71 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
72 private static final int NUM_RS = 2;
73 private static final String STRING_TABLE_NAME = "test";
74 private static final byte[] TEST_FAM = Bytes.toBytes("fam");
75 private static final byte[] TABLE_NAME = Bytes.toBytes(STRING_TABLE_NAME);
76
77
78
79
80
81 @BeforeClass
82 public static void setupCluster() throws Exception {
83 setupConf(UTIL.getConfiguration());
84 UTIL.startMiniCluster(NUM_RS);
85 }
86
87 private static void setupConf(Configuration conf) {
88
89 conf.setInt("hbase.regionsever.info.port", -1);
90
91 conf.setInt("hbase.hregion.memstore.flush.size", 25000);
92
93
94 conf.setInt("hbase.hstore.compaction.min", 10);
95 conf.setInt("hbase.hstore.compactionThreshold", 10);
96
97 conf.setInt("hbase.hstore.blockingStoreFiles", 12);
98
99 conf.setInt("hbase.client.retries.number", 1);
100
101 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
102
103 conf.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY,
104 ConstantSizeRegionSplitPolicy.class.getName());
105 }
106
107 @Before
108 public void setup() throws Exception {
109 UTIL.createTable(TABLE_NAME, TEST_FAM);
110 }
111
112 @After
113 public void tearDown() throws Exception {
114 UTIL.deleteTable(TABLE_NAME);
115
116 try {
117 UTIL.getTestFileSystem().delete(new Path(UTIL.getDefaultRootDirPath(), ".archive"), true);
118 } catch (IOException e) {
119 LOG.warn("Failure to delete archive directory", e);
120 }
121 }
122
123 @AfterClass
124 public static void cleanupTest() throws Exception {
125 try {
126 UTIL.shutdownMiniCluster();
127 } catch (Exception e) {
128 LOG.warn("failure shutting down cluster", e);
129 }
130 }
131
132
133
134
135
136 @Test
137 public void testFlushTableSnapshot() throws Exception {
138 HBaseAdmin admin = UTIL.getHBaseAdmin();
139
140 SnapshotTestingUtils.assertNoSnapshots(admin);
141
142
143 HTable table = new HTable(UTIL.getConfiguration(), TABLE_NAME);
144 UTIL.loadTable(table, TEST_FAM);
145
146
147 Set<String> snapshotServers = new HashSet<String>();
148 List<RegionServerThread> servers = UTIL.getMiniHBaseCluster().getLiveRegionServerThreads();
149 for (RegionServerThread server : servers) {
150 if (server.getRegionServer().getOnlineRegions(TABLE_NAME).size() > 0) {
151 snapshotServers.add(server.getRegionServer().getServerName().toString());
152 }
153 }
154
155 LOG.debug("FS state before snapshot:");
156 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
157 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
158
159
160 String snapshotString = "offlineTableSnapshot";
161 byte[] snapshot = Bytes.toBytes(snapshotString);
162 admin.snapshot(snapshotString, STRING_TABLE_NAME, SnapshotDescription.Type.FLUSH);
163 LOG.debug("Snapshot completed.");
164
165
166 List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertOneSnapshotThatMatches(admin,
167 snapshot, TABLE_NAME);
168
169
170 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
171 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
172 LOG.debug("FS state after snapshot:");
173 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
174 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
175
176 SnapshotTestingUtils.confirmSnapshotValid(snapshots.get(0), TABLE_NAME, TEST_FAM, rootDir,
177 admin, fs, false, new Path(rootDir, HConstants.HREGION_LOGDIR_NAME), snapshotServers);
178
179 admin.deleteSnapshot(snapshot);
180 snapshots = admin.listSnapshots();
181 SnapshotTestingUtils.assertNoSnapshots(admin);
182 }
183
184 @Test
185 public void testSnapshotFailsOnNonExistantTable() throws Exception {
186 HBaseAdmin admin = UTIL.getHBaseAdmin();
187
188 SnapshotTestingUtils.assertNoSnapshots(admin);
189 String tableName = "_not_a_table";
190
191
192 boolean fail = false;
193 do {
194 try {
195 admin.getTableDescriptor(Bytes.toBytes(tableName));
196 fail = true;
197 LOG.error("Table:" + tableName + " already exists, checking a new name");
198 tableName = tableName+"!";
199 } catch (TableNotFoundException e) {
200 fail = false;
201 }
202 } while (fail);
203
204
205 try {
206 admin.snapshot("fail", tableName, SnapshotDescription.Type.FLUSH);
207 fail("Snapshot succeeded even though there is not table.");
208 } catch (SnapshotCreationException e) {
209 LOG.info("Correctly failed to snapshot a non-existant table:" + e.getMessage());
210 }
211 }
212
213 @Test(timeout = 60000)
214 public void testAsyncFlushSnapshot() throws Exception {
215 HBaseAdmin admin = UTIL.getHBaseAdmin();
216 SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("asyncSnapshot")
217 .setTable(STRING_TABLE_NAME).setType(SnapshotDescription.Type.FLUSH).build();
218
219
220 admin.takeSnapshotAsync(snapshot);
221
222
223 HMaster master = UTIL.getMiniHBaseCluster().getMaster();
224 SnapshotTestingUtils.waitForSnapshotToComplete(master, new HSnapshotDescription(snapshot), 200);
225 LOG.info(" === Async Snapshot Completed ===");
226 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
227 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
228
229 SnapshotTestingUtils.assertOneSnapshotThatMatches(admin, snapshot);
230
231
232 admin.deleteSnapshot(snapshot.getName());
233 LOG.info(" === Async Snapshot Deleted ===");
234 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
235 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
236
237 SnapshotTestingUtils.assertNoSnapshots(admin);
238 LOG.info(" === Async Snapshot Test Completed ===");
239
240 }
241
242
243
244
245 @Test
246 public void testFlushCreateListDestroy() throws Exception {
247 LOG.debug("------- Starting Snapshot test -------------");
248 HBaseAdmin admin = UTIL.getHBaseAdmin();
249
250 SnapshotTestingUtils.assertNoSnapshots(admin);
251
252 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
253
254 waitForTableToBeOnline(TABLE_NAME);
255
256 String snapshotName = "flushSnapshotCreateListDestroy";
257
258 admin.snapshot(snapshotName, STRING_TABLE_NAME, SnapshotDescription.Type.FLUSH);
259 logFSTree(new Path(UTIL.getConfiguration().get(HConstants.HBASE_DIR)));
260
261
262 List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertOneSnapshotThatMatches(admin,
263 snapshotName, STRING_TABLE_NAME);
264
265
266 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
267 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
268 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshots.get(0), rootDir);
269 assertTrue(fs.exists(snapshotDir));
270 FSUtils.logFileSystemState(UTIL.getTestFileSystem(), snapshotDir, LOG);
271 Path snapshotinfo = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
272 assertTrue(fs.exists(snapshotinfo));
273
274
275 HTableDescriptor desc = FSTableDescriptors.getTableDescriptor(fs, rootDir, TABLE_NAME);
276 HTableDescriptor snapshotDesc = FSTableDescriptors.getTableDescriptor(fs,
277 SnapshotDescriptionUtils.getSnapshotsDir(rootDir), Bytes.toBytes(snapshotName));
278 assertEquals(desc, snapshotDesc);
279
280
281 List<HRegionInfo> regions = admin.getTableRegions(TABLE_NAME);
282 for (HRegionInfo info : regions) {
283 String regionName = info.getEncodedName();
284 Path regionDir = new Path(snapshotDir, regionName);
285 HRegionInfo snapshotRegionInfo = HRegion.loadDotRegionInfoFileContent(fs, regionDir);
286 assertEquals(info, snapshotRegionInfo);
287
288 Path familyDir = new Path(regionDir, Bytes.toString(TEST_FAM));
289 assertTrue(fs.exists(familyDir));
290
291 assertTrue(fs.listStatus(familyDir).length > 0);
292 }
293
294
295 admin.deleteSnapshot(snapshotName);
296 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
297 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
298
299
300 SnapshotTestingUtils.assertNoSnapshots(admin);
301 LOG.debug("------- Flush-Snapshot Create List Destroy-------------");
302 }
303
304
305
306
307
308
309 @Test(timeout=60000)
310 public void testConcurrentSnapshottingAttempts() throws IOException, InterruptedException {
311 final String STRING_TABLE2_NAME = STRING_TABLE_NAME + "2";
312 final byte[] TABLE2_NAME = Bytes.toBytes(STRING_TABLE2_NAME);
313
314 int ssNum = 20;
315 HBaseAdmin admin = UTIL.getHBaseAdmin();
316
317 SnapshotTestingUtils.assertNoSnapshots(admin);
318
319 UTIL.createTable(TABLE2_NAME, TEST_FAM);
320
321 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
322 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE2_NAME), TEST_FAM);
323
324 waitForTableToBeOnline(TABLE_NAME);
325 waitForTableToBeOnline(TABLE2_NAME);
326
327 final CountDownLatch toBeSubmitted = new CountDownLatch(ssNum);
328
329 class SSRunnable implements Runnable {
330 SnapshotDescription ss;
331 SSRunnable(SnapshotDescription ss) {
332 this.ss = ss;
333 }
334
335 @Override
336 public void run() {
337 try {
338 HBaseAdmin admin = UTIL.getHBaseAdmin();
339 LOG.info("Submitting snapshot request: " + SnapshotDescriptionUtils.toString(ss));
340 admin.takeSnapshotAsync(ss);
341 } catch (Exception e) {
342 LOG.info("Exception during snapshot request: " + SnapshotDescriptionUtils.toString(ss)
343 + ". This is ok, we expect some", e);
344 }
345 LOG.info("Submitted snapshot request: " + SnapshotDescriptionUtils.toString(ss));
346 toBeSubmitted.countDown();
347 }
348 };
349
350
351 SnapshotDescription[] descs = new SnapshotDescription[ssNum];
352 for (int i = 0; i < ssNum; i++) {
353 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
354 builder.setTable((i % 2) == 0 ? STRING_TABLE_NAME : STRING_TABLE2_NAME);
355 builder.setName("ss"+i);
356 builder.setType(SnapshotDescription.Type.FLUSH);
357 descs[i] = builder.build();
358 }
359
360
361 for (int i=0 ; i < ssNum; i++) {
362 new Thread(new SSRunnable(descs[i])).start();
363 }
364
365
366 toBeSubmitted.await();
367
368
369 while (true) {
370 int doneCount = 0;
371 for (SnapshotDescription ss : descs) {
372 try {
373 if (admin.isSnapshotFinished(ss)) {
374 doneCount++;
375 }
376 } catch (Exception e) {
377 LOG.warn("Got an exception when checking for snapshot " + ss.getName(), e);
378 doneCount++;
379 }
380 }
381 if (doneCount == descs.length) {
382 break;
383 }
384 Thread.sleep(100);
385 }
386
387
388 logFSTree(new Path(UTIL.getConfiguration().get(HConstants.HBASE_DIR)));
389
390 List<SnapshotDescription> taken = admin.listSnapshots();
391 int takenSize = taken.size();
392 LOG.info("Taken " + takenSize + " snapshots: " + taken);
393 assertTrue("We expect at least 1 request to be rejected because of we concurrently" +
394 " issued many requests", takenSize < ssNum && takenSize > 0);
395
396
397 int t1SnapshotsCount = 0;
398 int t2SnapshotsCount = 0;
399 for (SnapshotDescription ss : taken) {
400 if (ss.getTable().equals(STRING_TABLE_NAME)) {
401 t1SnapshotsCount++;
402 } else if (ss.getTable().equals(STRING_TABLE2_NAME)) {
403 t2SnapshotsCount++;
404 }
405 }
406 assertTrue("We expect at least 1 snapshot of table1 ", t1SnapshotsCount > 0);
407 assertTrue("We expect at least 1 snapshot of table2 ", t2SnapshotsCount > 0);
408
409
410 for (SnapshotDescription ss : taken) {
411 admin.deleteSnapshot(ss.getName());
412 }
413 UTIL.deleteTable(TABLE2_NAME);
414 }
415
416 private void logFSTree(Path root) throws IOException {
417 FSUtils.logFileSystemState(UTIL.getDFSCluster().getFileSystem(), root, LOG);
418 }
419
420 private void waitForTableToBeOnline(final byte[] tableName) throws IOException {
421 HRegionServer rs = UTIL.getRSForFirstRegionInTable(tableName);
422 List<HRegion> onlineRegions = rs.getOnlineRegions(tableName);
423 for (HRegion region : onlineRegions) {
424 region.waitForFlushesAndCompactions();
425 }
426 }
427 }