1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.snapshot;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.net.URI;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.HashSet;
31 import java.util.Set;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.FileSystem;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileUtil;
40 import org.apache.hadoop.fs.FSDataOutputStream;
41 import org.apache.hadoop.fs.Path;
42 import org.apache.hadoop.hbase.HBaseTestingUtility;
43 import org.apache.hadoop.hbase.HColumnDescriptor;
44 import org.apache.hadoop.hbase.HConstants;
45 import org.apache.hadoop.hbase.HRegionInfo;
46 import org.apache.hadoop.hbase.HTableDescriptor;
47 import org.apache.hadoop.hbase.KeyValue;
48 import org.apache.hadoop.hbase.MediumTests;
49 import org.apache.hadoop.hbase.MiniHBaseCluster;
50 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
51 import org.apache.hadoop.hbase.client.HBaseAdmin;
52 import org.apache.hadoop.hbase.client.HTable;
53 import org.apache.hadoop.hbase.util.Bytes;
54 import org.apache.hadoop.hbase.util.FSUtils;
55 import org.apache.hadoop.hbase.util.Pair;
56 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
57 import org.apache.hadoop.hbase.regionserver.HRegion;
58 import org.apache.hadoop.hbase.snapshot.ExportSnapshot;
59 import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
60 import org.apache.hadoop.mapreduce.Job;
61 import org.junit.After;
62 import org.junit.AfterClass;
63 import org.junit.Before;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 import org.junit.experimental.categories.Category;
67
68
69
70
71 @Category(MediumTests.class)
72 public class TestExportSnapshot {
73 private final Log LOG = LogFactory.getLog(getClass());
74
75 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
76
77 private final static byte[] FAMILY = Bytes.toBytes("cf");
78
79 private byte[] emptySnapshotName;
80 private byte[] snapshotName;
81 private byte[] tableName;
82 private HBaseAdmin admin;
83
84 @BeforeClass
85 public static void setUpBeforeClass() throws Exception {
86 TEST_UTIL.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
87 TEST_UTIL.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
88 TEST_UTIL.getConfiguration().setInt("hbase.client.pause", 250);
89 TEST_UTIL.getConfiguration().setInt("hbase.client.retries.number", 6);
90 TEST_UTIL.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
91 TEST_UTIL.getConfiguration().setInt("mapreduce.map.max.attempts", 10);
92 TEST_UTIL.getConfiguration().setInt("mapred.map.max.attempts", 10);
93 TEST_UTIL.startMiniCluster(3);
94 }
95
96 @AfterClass
97 public static void tearDownAfterClass() throws Exception {
98 TEST_UTIL.shutdownMiniCluster();
99 }
100
101
102
103
104 @Before
105 public void setUp() throws Exception {
106 this.admin = TEST_UTIL.getHBaseAdmin();
107
108 long tid = System.currentTimeMillis();
109 tableName = Bytes.toBytes("testtb-" + tid);
110 snapshotName = Bytes.toBytes("snaptb0-" + tid);
111 emptySnapshotName = Bytes.toBytes("emptySnaptb0-" + tid);
112
113
114 SnapshotTestingUtils.createTable(TEST_UTIL, tableName, FAMILY);
115
116
117 admin.snapshot(emptySnapshotName, tableName);
118
119
120 HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName);
121 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 1000, FAMILY);
122
123
124 admin.snapshot(snapshotName, tableName);
125 }
126
127 @After
128 public void tearDown() throws Exception {
129 TEST_UTIL.deleteTable(tableName);
130 SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getHBaseAdmin());
131 SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
132 admin.close();
133 }
134
135
136
137
138
139
140
141
142
143
144
145 @Test
146 public void testBalanceSplit() throws Exception {
147
148 List<Pair<Path, Long>> files = new ArrayList<Pair<Path, Long>>();
149 for (long i = 0; i <= 20; i++) {
150 files.add(new Pair<Path, Long>(new Path("file-" + i), i));
151 }
152
153
154
155
156
157
158
159 List<List<Path>> splits = ExportSnapshot.getBalancedSplits(files, 5);
160 assertEquals(5, splits.size());
161 assertEquals(Arrays.asList(new Path("file-20"), new Path("file-11"),
162 new Path("file-10"), new Path("file-1"), new Path("file-0")), splits.get(0));
163 assertEquals(Arrays.asList(new Path("file-19"), new Path("file-12"),
164 new Path("file-9"), new Path("file-2")), splits.get(1));
165 assertEquals(Arrays.asList(new Path("file-18"), new Path("file-13"),
166 new Path("file-8"), new Path("file-3")), splits.get(2));
167 assertEquals(Arrays.asList(new Path("file-17"), new Path("file-14"),
168 new Path("file-7"), new Path("file-4")), splits.get(3));
169 assertEquals(Arrays.asList(new Path("file-16"), new Path("file-15"),
170 new Path("file-6"), new Path("file-5")), splits.get(4));
171 }
172
173
174
175
176 @Test
177 public void testExportFileSystemState() throws Exception {
178 testExportFileSystemState(tableName, snapshotName, 2);
179 }
180
181 @Test
182 public void testEmptyExportFileSystemState() throws Exception {
183 testExportFileSystemState(tableName, emptySnapshotName, 1);
184 }
185
186 @Test
187 public void testConsecutiveExports() throws Exception {
188 Path copyDir = TEST_UTIL.getDataTestDir("export-" + System.currentTimeMillis());
189 testExportFileSystemState(tableName, snapshotName, 2, copyDir, false);
190 testExportFileSystemState(tableName, snapshotName, 2, copyDir, true);
191 removeExportDir(copyDir);
192 }
193
194
195
196
197
198 @Test
199 public void testSnapshotWithRefsExportFileSystemState() throws Exception {
200 Configuration conf = TEST_UTIL.getConfiguration();
201
202 final byte[] tableWithRefsName = Bytes.toBytes("tableWithRefs");
203 final String snapshotName = "tableWithRefs";
204 final String TEST_FAMILY = Bytes.toString(FAMILY);
205 final String TEST_HFILE = "abc";
206
207 final SnapshotDescription sd = SnapshotDescription.newBuilder()
208 .setName(snapshotName).setTable(Bytes.toString(tableWithRefsName)).build();
209
210 FileSystem fs = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getFileSystem();
211 Path rootDir = TEST_UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
212 Path archiveDir = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
213
214 HTableDescriptor htd = new HTableDescriptor(tableWithRefsName);
215 htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
216
217
218 HRegion r0 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
219 conf, htd, null, true, true);
220 Path storeFile = new Path(new Path(r0.getRegionDir(), TEST_FAMILY), TEST_HFILE);
221 FSDataOutputStream out = fs.create(storeFile);
222 out.write(Bytes.toBytes("Test Data"));
223 out.close();
224 r0.close();
225
226
227
228 HRegion r1 = HRegion.createHRegion(new HRegionInfo(htd.getName()), archiveDir,
229 conf, htd, null, true, true);
230 out = fs.create(new Path(new Path(r1.getRegionDir(), TEST_FAMILY),
231 storeFile.getName() + '.' + r0.getRegionInfo().getEncodedName()));
232 out.write(Bytes.toBytes("Test Data"));
233 out.close();
234 r1.close();
235
236 Path tableDir = HTableDescriptor.getTableDir(archiveDir, tableWithRefsName);
237 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
238 FileUtil.copy(fs, tableDir, fs, snapshotDir, false, conf);
239 SnapshotDescriptionUtils.writeSnapshotInfo(sd, snapshotDir, fs);
240
241 testExportFileSystemState(tableWithRefsName, Bytes.toBytes(snapshotName), 2);
242 }
243
244 private void testExportFileSystemState(final byte[] tableName, final byte[] snapshotName,
245 int filesExpected) throws Exception {
246 Path copyDir = TEST_UTIL.getDataTestDir("export-" + System.currentTimeMillis());
247 testExportFileSystemState(tableName, snapshotName, filesExpected, copyDir, false);
248 removeExportDir(copyDir);
249 }
250
251
252
253
254 private void testExportFileSystemState(final byte[] tableName, final byte[] snapshotName,
255 int filesExpected, Path copyDir, boolean overwrite) throws Exception {
256 URI hdfsUri = FileSystem.get(TEST_UTIL.getConfiguration()).getUri();
257 FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
258 copyDir = copyDir.makeQualified(fs);
259
260 List<String> opts = new ArrayList<String>();
261 opts.add("-snapshot");
262 opts.add(Bytes.toString(snapshotName));
263 opts.add("-copy-to");
264 opts.add(copyDir.toString());
265 if (overwrite) opts.add("-overwrite");
266
267
268 int res = ExportSnapshot.innerMain(TEST_UTIL.getConfiguration(),
269 opts.toArray(new String[opts.size()]));
270 assertEquals(0, res);
271
272
273 FileStatus[] rootFiles = fs.listStatus(copyDir);
274 assertEquals(filesExpected, rootFiles.length);
275 for (FileStatus fileStatus: rootFiles) {
276 String name = fileStatus.getPath().getName();
277 assertTrue(fileStatus.isDir());
278 assertTrue(name.equals(HConstants.SNAPSHOT_DIR_NAME) || name.equals(".archive"));
279 }
280
281
282 final FileSystem hdfs = FileSystem.get(hdfsUri, TEST_UTIL.getConfiguration());
283 final Path snapshotDir = new Path(HConstants.SNAPSHOT_DIR_NAME, Bytes.toString(snapshotName));
284 verifySnapshot(hdfs, new Path(TEST_UTIL.getDefaultRootDirPath(), snapshotDir),
285 fs, new Path(copyDir, snapshotDir));
286 verifyArchive(fs, copyDir, tableName, Bytes.toString(snapshotName));
287 FSUtils.logFileSystemState(hdfs, snapshotDir, LOG);
288 }
289
290
291
292
293 @Test
294 public void testExportFailure() throws Exception {
295 assertEquals(1, runExportAndInjectFailures(snapshotName, false));
296 }
297
298
299
300
301 private int runExportAndInjectFailures(final byte[] snapshotName, boolean retry)
302 throws Exception {
303 Path copyDir = TEST_UTIL.getDataTestDir("export-" + System.currentTimeMillis());
304 URI hdfsUri = FileSystem.get(TEST_UTIL.getConfiguration()).getUri();
305 FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
306 copyDir = copyDir.makeQualified(fs);
307
308 Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
309 conf.setBoolean(ExportSnapshot.CONF_TEST_FAILURE, true);
310 conf.setBoolean(ExportSnapshot.CONF_TEST_RETRY, retry);
311
312
313 int res = ExportSnapshot.innerMain(conf, new String[] {
314 "-snapshot", Bytes.toString(snapshotName),
315 "-copy-to", copyDir.toString()
316 });
317 return res;
318 }
319
320
321
322
323 private void verifySnapshot(final FileSystem fs1, final Path root1,
324 final FileSystem fs2, final Path root2) throws IOException {
325 Set<String> s = new HashSet<String>();
326 assertEquals(listFiles(fs1, root1, root1), listFiles(fs2, root2, root2));
327 }
328
329
330
331
332 private void verifyArchive(final FileSystem fs, final Path rootDir,
333 final byte[] tableName, final String snapshotName) throws IOException {
334 final Path exportedSnapshot = new Path(rootDir,
335 new Path(HConstants.SNAPSHOT_DIR_NAME, snapshotName));
336 final Path exportedArchive = new Path(rootDir, HConstants.HFILE_ARCHIVE_DIRECTORY);
337 LOG.debug(listFiles(fs, exportedArchive, exportedArchive));
338 SnapshotReferenceUtil.visitReferencedFiles(fs, exportedSnapshot,
339 new SnapshotReferenceUtil.FileVisitor() {
340 public void storeFile (final String region, final String family, final String hfile)
341 throws IOException {
342 verifyNonEmptyFile(new Path(exportedArchive,
343 new Path(Bytes.toString(tableName), new Path(region, new Path(family, hfile)))));
344 }
345
346 public void recoveredEdits (final String region, final String logfile)
347 throws IOException {
348 verifyNonEmptyFile(new Path(exportedSnapshot,
349 new Path(Bytes.toString(tableName), new Path(region, logfile))));
350 }
351
352 public void logFile (final String server, final String logfile)
353 throws IOException {
354 verifyNonEmptyFile(new Path(exportedSnapshot, new Path(server, logfile)));
355 }
356
357 private void verifyNonEmptyFile(final Path path) throws IOException {
358 assertTrue(path + " should exist", fs.exists(path));
359 assertTrue(path + " should not be empty", fs.getFileStatus(path).getLen() > 0);
360 }
361 });
362 }
363
364 private Set<String> listFiles(final FileSystem fs, final Path root, final Path dir)
365 throws IOException {
366 Set<String> files = new HashSet<String>();
367 int rootPrefix = root.toString().length();
368 FileStatus[] list = FSUtils.listStatus(fs, dir);
369 if (list != null) {
370 for (FileStatus fstat: list) {
371 LOG.debug(fstat.getPath());
372 if (fstat.isDir()) {
373 files.addAll(listFiles(fs, root, fstat.getPath()));
374 } else {
375 files.add(fstat.getPath().toString().substring(rootPrefix));
376 }
377 }
378 }
379 return files;
380 }
381
382 private void removeExportDir(final Path path) throws IOException {
383 FileSystem fs = FileSystem.get(path.toUri(), new Configuration());
384 FSUtils.logFileSystemState(fs, path, LOG);
385 fs.delete(path, true);
386 }
387 }