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 SnapshotTestingUtils.deleteAllSnapshots(UTIL.getHBaseAdmin());
116 SnapshotTestingUtils.deleteArchiveDirectory(UTIL);
117 }
118
119 @AfterClass
120 public static void cleanupTest() throws Exception {
121 try {
122 UTIL.shutdownMiniCluster();
123 } catch (Exception e) {
124 LOG.warn("failure shutting down cluster", e);
125 }
126 }
127
128
129
130
131
132 @Test
133 public void testFlushTableSnapshot() throws Exception {
134 HBaseAdmin admin = UTIL.getHBaseAdmin();
135
136 SnapshotTestingUtils.assertNoSnapshots(admin);
137
138
139 HTable table = new HTable(UTIL.getConfiguration(), TABLE_NAME);
140 UTIL.loadTable(table, TEST_FAM);
141
142
143 Set<String> snapshotServers = new HashSet<String>();
144 List<RegionServerThread> servers = UTIL.getMiniHBaseCluster().getLiveRegionServerThreads();
145 for (RegionServerThread server : servers) {
146 if (server.getRegionServer().getOnlineRegions(TABLE_NAME).size() > 0) {
147 snapshotServers.add(server.getRegionServer().getServerName().toString());
148 }
149 }
150
151 LOG.debug("FS state before snapshot:");
152 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
153 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
154
155
156 String snapshotString = "offlineTableSnapshot";
157 byte[] snapshot = Bytes.toBytes(snapshotString);
158 admin.snapshot(snapshotString, STRING_TABLE_NAME, SnapshotDescription.Type.FLUSH);
159 LOG.debug("Snapshot completed.");
160
161
162 List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertOneSnapshotThatMatches(admin,
163 snapshot, TABLE_NAME);
164
165
166 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
167 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
168 LOG.debug("FS state after snapshot:");
169 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
170 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
171
172 SnapshotTestingUtils.confirmSnapshotValid(snapshots.get(0), TABLE_NAME, TEST_FAM, rootDir,
173 admin, fs, false, new Path(rootDir, HConstants.HREGION_LOGDIR_NAME), snapshotServers);
174
175 admin.deleteSnapshot(snapshot);
176 snapshots = admin.listSnapshots();
177 SnapshotTestingUtils.assertNoSnapshots(admin);
178 }
179
180 @Test
181 public void testSnapshotFailsOnNonExistantTable() throws Exception {
182 HBaseAdmin admin = UTIL.getHBaseAdmin();
183
184 SnapshotTestingUtils.assertNoSnapshots(admin);
185 String tableName = "_not_a_table";
186
187
188 boolean fail = false;
189 do {
190 try {
191 admin.getTableDescriptor(Bytes.toBytes(tableName));
192 fail = true;
193 LOG.error("Table:" + tableName + " already exists, checking a new name");
194 tableName = tableName+"!";
195 } catch (TableNotFoundException e) {
196 fail = false;
197 }
198 } while (fail);
199
200
201 try {
202 admin.snapshot("fail", tableName, SnapshotDescription.Type.FLUSH);
203 fail("Snapshot succeeded even though there is not table.");
204 } catch (SnapshotCreationException e) {
205 LOG.info("Correctly failed to snapshot a non-existant table:" + e.getMessage());
206 }
207 }
208
209 @Test(timeout = 60000)
210 public void testAsyncFlushSnapshot() throws Exception {
211 HBaseAdmin admin = UTIL.getHBaseAdmin();
212 SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("asyncSnapshot")
213 .setTable(STRING_TABLE_NAME).setType(SnapshotDescription.Type.FLUSH).build();
214
215
216 admin.takeSnapshotAsync(snapshot);
217
218
219 HMaster master = UTIL.getMiniHBaseCluster().getMaster();
220 SnapshotTestingUtils.waitForSnapshotToComplete(master, new HSnapshotDescription(snapshot), 200);
221 LOG.info(" === Async Snapshot Completed ===");
222 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
223 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
224
225 SnapshotTestingUtils.assertOneSnapshotThatMatches(admin, snapshot);
226
227
228 admin.deleteSnapshot(snapshot.getName());
229 LOG.info(" === Async Snapshot Deleted ===");
230 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
231 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
232
233 SnapshotTestingUtils.assertNoSnapshots(admin);
234 LOG.info(" === Async Snapshot Test Completed ===");
235
236 }
237
238
239
240
241 @Test
242 public void testFlushCreateListDestroy() throws Exception {
243 LOG.debug("------- Starting Snapshot test -------------");
244 HBaseAdmin admin = UTIL.getHBaseAdmin();
245
246 SnapshotTestingUtils.assertNoSnapshots(admin);
247
248 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
249
250 waitForTableToBeOnline(TABLE_NAME);
251
252 String snapshotName = "flushSnapshotCreateListDestroy";
253
254 admin.snapshot(snapshotName, STRING_TABLE_NAME, SnapshotDescription.Type.FLUSH);
255 logFSTree(new Path(UTIL.getConfiguration().get(HConstants.HBASE_DIR)));
256
257
258 List<SnapshotDescription> snapshots = SnapshotTestingUtils.assertOneSnapshotThatMatches(admin,
259 snapshotName, STRING_TABLE_NAME);
260
261
262 FileSystem fs = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
263 Path rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
264 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshots.get(0), rootDir);
265 assertTrue(fs.exists(snapshotDir));
266 FSUtils.logFileSystemState(UTIL.getTestFileSystem(), snapshotDir, LOG);
267 Path snapshotinfo = new Path(snapshotDir, SnapshotDescriptionUtils.SNAPSHOTINFO_FILE);
268 assertTrue(fs.exists(snapshotinfo));
269
270
271 HTableDescriptor desc = FSTableDescriptors.getTableDescriptor(fs, rootDir, TABLE_NAME);
272 HTableDescriptor snapshotDesc = FSTableDescriptors.getTableDescriptor(fs,
273 SnapshotDescriptionUtils.getSnapshotsDir(rootDir), Bytes.toBytes(snapshotName));
274 assertEquals(desc, snapshotDesc);
275
276
277 List<HRegionInfo> regions = admin.getTableRegions(TABLE_NAME);
278 for (HRegionInfo info : regions) {
279 String regionName = info.getEncodedName();
280 Path regionDir = new Path(snapshotDir, regionName);
281 HRegionInfo snapshotRegionInfo = HRegion.loadDotRegionInfoFileContent(fs, regionDir);
282 assertEquals(info, snapshotRegionInfo);
283
284 Path familyDir = new Path(regionDir, Bytes.toString(TEST_FAM));
285 assertTrue(fs.exists(familyDir));
286
287 assertTrue(fs.listStatus(familyDir).length > 0);
288 }
289
290
291 admin.deleteSnapshot(snapshotName);
292 FSUtils.logFileSystemState(UTIL.getTestFileSystem(),
293 FSUtils.getRootDir(UTIL.getConfiguration()), LOG);
294
295
296 SnapshotTestingUtils.assertNoSnapshots(admin);
297 LOG.debug("------- Flush-Snapshot Create List Destroy-------------");
298 }
299
300
301
302
303
304
305 @Test(timeout=60000)
306 public void testConcurrentSnapshottingAttempts() throws IOException, InterruptedException {
307 final String STRING_TABLE2_NAME = STRING_TABLE_NAME + "2";
308 final byte[] TABLE2_NAME = Bytes.toBytes(STRING_TABLE2_NAME);
309
310 int ssNum = 20;
311 HBaseAdmin admin = UTIL.getHBaseAdmin();
312
313 SnapshotTestingUtils.assertNoSnapshots(admin);
314
315 UTIL.createTable(TABLE2_NAME, TEST_FAM);
316
317 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE_NAME), TEST_FAM);
318 UTIL.loadTable(new HTable(UTIL.getConfiguration(), TABLE2_NAME), TEST_FAM);
319
320 waitForTableToBeOnline(TABLE_NAME);
321 waitForTableToBeOnline(TABLE2_NAME);
322
323 final CountDownLatch toBeSubmitted = new CountDownLatch(ssNum);
324
325 class SSRunnable implements Runnable {
326 SnapshotDescription ss;
327 SSRunnable(SnapshotDescription ss) {
328 this.ss = ss;
329 }
330
331 @Override
332 public void run() {
333 try {
334 HBaseAdmin admin = UTIL.getHBaseAdmin();
335 LOG.info("Submitting snapshot request: " + SnapshotDescriptionUtils.toString(ss));
336 admin.takeSnapshotAsync(ss);
337 } catch (Exception e) {
338 LOG.info("Exception during snapshot request: " + SnapshotDescriptionUtils.toString(ss)
339 + ". This is ok, we expect some", e);
340 }
341 LOG.info("Submitted snapshot request: " + SnapshotDescriptionUtils.toString(ss));
342 toBeSubmitted.countDown();
343 }
344 };
345
346
347 SnapshotDescription[] descs = new SnapshotDescription[ssNum];
348 for (int i = 0; i < ssNum; i++) {
349 SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
350 builder.setTable((i % 2) == 0 ? STRING_TABLE_NAME : STRING_TABLE2_NAME);
351 builder.setName("ss"+i);
352 builder.setType(SnapshotDescription.Type.FLUSH);
353 descs[i] = builder.build();
354 }
355
356
357 for (int i=0 ; i < ssNum; i++) {
358 new Thread(new SSRunnable(descs[i])).start();
359 }
360
361
362 toBeSubmitted.await();
363
364
365 while (true) {
366 int doneCount = 0;
367 for (SnapshotDescription ss : descs) {
368 try {
369 if (admin.isSnapshotFinished(ss)) {
370 doneCount++;
371 }
372 } catch (Exception e) {
373 LOG.warn("Got an exception when checking for snapshot " + ss.getName(), e);
374 doneCount++;
375 }
376 }
377 if (doneCount == descs.length) {
378 break;
379 }
380 Thread.sleep(100);
381 }
382
383
384 logFSTree(new Path(UTIL.getConfiguration().get(HConstants.HBASE_DIR)));
385
386 List<SnapshotDescription> taken = admin.listSnapshots();
387 int takenSize = taken.size();
388 LOG.info("Taken " + takenSize + " snapshots: " + taken);
389 assertTrue("We expect at least 1 request to be rejected because of we concurrently" +
390 " issued many requests", takenSize < ssNum && takenSize > 0);
391
392
393 int t1SnapshotsCount = 0;
394 int t2SnapshotsCount = 0;
395 for (SnapshotDescription ss : taken) {
396 if (ss.getTable().equals(STRING_TABLE_NAME)) {
397 t1SnapshotsCount++;
398 } else if (ss.getTable().equals(STRING_TABLE2_NAME)) {
399 t2SnapshotsCount++;
400 }
401 }
402 assertTrue("We expect at least 1 snapshot of table1 ", t1SnapshotsCount > 0);
403 assertTrue("We expect at least 1 snapshot of table2 ", t2SnapshotsCount > 0);
404
405
406 for (SnapshotDescription ss : taken) {
407 admin.deleteSnapshot(ss.getName());
408 }
409 UTIL.deleteTable(TABLE2_NAME);
410 }
411
412 private void logFSTree(Path root) throws IOException {
413 FSUtils.logFileSystemState(UTIL.getDFSCluster().getFileSystem(), root, LOG);
414 }
415
416 private void waitForTableToBeOnline(final byte[] tableName) throws IOException {
417 HRegionServer rs = UTIL.getRSForFirstRegionInTable(tableName);
418 List<HRegion> onlineRegions = rs.getOnlineRegions(tableName);
419 for (HRegion region : onlineRegions) {
420 region.waitForFlushesAndCompactions();
421 }
422 }
423 }