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 java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.net.URI;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Random;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.classification.InterfaceAudience;
34 import org.apache.hadoop.classification.InterfaceStability;
35 import org.apache.hadoop.conf.Configuration;
36 import org.apache.hadoop.conf.Configured;
37 import org.apache.hadoop.fs.FSDataInputStream;
38 import org.apache.hadoop.fs.FSDataOutputStream;
39 import org.apache.hadoop.fs.FileChecksum;
40 import org.apache.hadoop.fs.FileStatus;
41 import org.apache.hadoop.fs.FileSystem;
42 import org.apache.hadoop.fs.FileUtil;
43 import org.apache.hadoop.fs.Path;
44 import org.apache.hadoop.fs.permission.FsPermission;
45 import org.apache.hadoop.hbase.TableName;
46 import org.apache.hadoop.hbase.HBaseConfiguration;
47 import org.apache.hadoop.hbase.HConstants;
48 import org.apache.hadoop.hbase.io.HFileLink;
49 import org.apache.hadoop.hbase.io.HLogLink;
50 import org.apache.hadoop.hbase.mapreduce.JobUtil;
51 import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
52 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
53 import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
54 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
55 import org.apache.hadoop.hbase.util.FSUtils;
56 import org.apache.hadoop.hbase.util.Pair;
57 import org.apache.hadoop.io.NullWritable;
58 import org.apache.hadoop.io.SequenceFile;
59 import org.apache.hadoop.io.Text;
60 import org.apache.hadoop.mapreduce.Job;
61 import org.apache.hadoop.mapreduce.Mapper;
62 import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
63 import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
64 import org.apache.hadoop.mapreduce.security.TokenCache;
65 import org.apache.hadoop.util.StringUtils;
66 import org.apache.hadoop.util.Tool;
67 import org.apache.hadoop.util.ToolRunner;
68
69
70
71
72
73
74
75
76 @InterfaceAudience.Public
77 @InterfaceStability.Evolving
78 public final class ExportSnapshot extends Configured implements Tool {
79 private static final Log LOG = LogFactory.getLog(ExportSnapshot.class);
80
81 private static final String CONF_FILES_USER = "snapshot.export.files.attributes.user";
82 private static final String CONF_FILES_GROUP = "snapshot.export.files.attributes.group";
83 private static final String CONF_FILES_MODE = "snapshot.export.files.attributes.mode";
84 private static final String CONF_CHECKSUM_VERIFY = "snapshot.export.checksum.verify";
85 private static final String CONF_OUTPUT_ROOT = "snapshot.export.output.root";
86 private static final String CONF_INPUT_ROOT = "snapshot.export.input.root";
87 private static final String CONF_BUFFER_SIZE = "snapshot.export.buffer.size";
88 private static final String CONF_MAP_GROUP = "snapshot.export.default.map.group";
89
90 static final String CONF_TEST_FAILURE = "test.snapshot.export.failure";
91 static final String CONF_TEST_RETRY = "test.snapshot.export.failure.retry";
92
93 private static final String INPUT_FOLDER_PREFIX = "export-files.";
94
95
96 public enum Counter { MISSING_FILES, COPY_FAILED, BYTES_EXPECTED, BYTES_COPIED, FILES_COPIED };
97
98 private static class ExportMapper extends Mapper<Text, NullWritable, NullWritable, NullWritable> {
99 final static int REPORT_SIZE = 1 * 1024 * 1024;
100 final static int BUFFER_SIZE = 64 * 1024;
101
102 private boolean testFailures;
103 private Random random;
104
105 private boolean verifyChecksum;
106 private String filesGroup;
107 private String filesUser;
108 private short filesMode;
109 private int bufferSize;
110
111 private FileSystem outputFs;
112 private Path outputArchive;
113 private Path outputRoot;
114
115 private FileSystem inputFs;
116 private Path inputArchive;
117 private Path inputRoot;
118
119 @Override
120 public void setup(Context context) throws IOException {
121 Configuration conf = context.getConfiguration();
122 verifyChecksum = conf.getBoolean(CONF_CHECKSUM_VERIFY, true);
123
124 filesGroup = conf.get(CONF_FILES_GROUP);
125 filesUser = conf.get(CONF_FILES_USER);
126 filesMode = (short)conf.getInt(CONF_FILES_MODE, 0);
127 outputRoot = new Path(conf.get(CONF_OUTPUT_ROOT));
128 inputRoot = new Path(conf.get(CONF_INPUT_ROOT));
129
130 inputArchive = new Path(inputRoot, HConstants.HFILE_ARCHIVE_DIRECTORY);
131 outputArchive = new Path(outputRoot, HConstants.HFILE_ARCHIVE_DIRECTORY);
132
133 testFailures = conf.getBoolean(CONF_TEST_FAILURE, false);
134
135 try {
136 inputFs = FileSystem.get(inputRoot.toUri(), conf);
137 } catch (IOException e) {
138 throw new IOException("Could not get the input FileSystem with root=" + inputRoot, e);
139 }
140
141 try {
142 outputFs = FileSystem.get(outputRoot.toUri(), conf);
143 } catch (IOException e) {
144 throw new IOException("Could not get the output FileSystem with root="+ outputRoot, e);
145 }
146
147
148 int defaultBlockSize = Math.max((int) outputFs.getDefaultBlockSize(), BUFFER_SIZE);
149 bufferSize = conf.getInt(CONF_BUFFER_SIZE, defaultBlockSize);
150 LOG.info("Using bufferSize=" + StringUtils.humanReadableInt(bufferSize));
151 }
152
153 @Override
154 public void map(Text key, NullWritable value, Context context)
155 throws InterruptedException, IOException {
156 Path inputPath = new Path(key.toString());
157 Path outputPath = getOutputPath(inputPath);
158
159 LOG.info("copy file input=" + inputPath + " output=" + outputPath);
160 copyFile(context, inputPath, outputPath);
161 }
162
163
164
165
166
167
168 private Path getOutputPath(final Path inputPath) throws IOException {
169 Path path;
170 if (HFileLink.isHFileLink(inputPath) || StoreFileInfo.isReference(inputPath)) {
171 String family = inputPath.getParent().getName();
172 TableName table =
173 HFileLink.getReferencedTableName(inputPath.getName());
174 String region = HFileLink.getReferencedRegionName(inputPath.getName());
175 String hfile = HFileLink.getReferencedHFileName(inputPath.getName());
176 path = new Path(FSUtils.getTableDir(new Path("./"), table),
177 new Path(region, new Path(family, hfile)));
178 } else if (isHLogLinkPath(inputPath)) {
179 String logName = inputPath.getName();
180 path = new Path(new Path(outputRoot, HConstants.HREGION_OLDLOGDIR_NAME), logName);
181 } else {
182 path = inputPath;
183 }
184 return new Path(outputArchive, path);
185 }
186
187
188
189
190 private void injectTestFailure(final Context context, final Path inputPath)
191 throws IOException {
192 if (testFailures) {
193 if (context.getConfiguration().getBoolean(CONF_TEST_RETRY, false)) {
194 if (random == null) {
195 random = new Random();
196 }
197
198
199
200
201 if (random.nextFloat() < 0.03) {
202 throw new IOException("TEST RETRY FAILURE: Unable to copy input=" + inputPath
203 + " time=" + System.currentTimeMillis());
204 }
205 } else {
206 context.getCounter(Counter.COPY_FAILED).increment(1);
207 throw new IOException("TEST FAILURE: Unable to copy input=" + inputPath);
208 }
209 }
210 }
211
212 private void copyFile(final Context context, final Path inputPath, final Path outputPath)
213 throws IOException {
214 injectTestFailure(context, inputPath);
215
216
217 FileStatus inputStat = getSourceFileStatus(context, inputPath);
218
219
220 if (outputFs.exists(outputPath)) {
221 FileStatus outputStat = outputFs.getFileStatus(outputPath);
222 if (outputStat != null && sameFile(inputStat, outputStat)) {
223 LOG.info("Skip copy " + inputPath + " to " + outputPath + ", same file.");
224 return;
225 }
226 }
227
228 FSDataInputStream in = openSourceFile(context, inputPath);
229 try {
230 context.getCounter(Counter.BYTES_EXPECTED).increment(inputStat.getLen());
231
232
233 outputFs.mkdirs(outputPath.getParent());
234 FSDataOutputStream out = outputFs.create(outputPath, true);
235 try {
236 copyData(context, inputPath, in, outputPath, out, inputStat.getLen());
237 } finally {
238 out.close();
239 }
240
241
242 if (!preserveAttributes(outputPath, inputStat)) {
243 LOG.warn("You may have to run manually chown on: " + outputPath);
244 }
245 } finally {
246 in.close();
247 }
248 }
249
250
251
252
253
254
255
256
257
258 private boolean preserveAttributes(final Path path, final FileStatus refStat) {
259 FileStatus stat;
260 try {
261 stat = outputFs.getFileStatus(path);
262 } catch (IOException e) {
263 LOG.warn("Unable to get the status for file=" + path);
264 return false;
265 }
266
267 try {
268 if (filesMode > 0 && stat.getPermission().toShort() != filesMode) {
269 outputFs.setPermission(path, new FsPermission(filesMode));
270 } else if (!stat.getPermission().equals(refStat.getPermission())) {
271 outputFs.setPermission(path, refStat.getPermission());
272 }
273 } catch (IOException e) {
274 LOG.warn("Unable to set the permission for file="+ stat.getPath() +": "+ e.getMessage());
275 return false;
276 }
277
278 String user = stringIsNotEmpty(filesUser) ? filesUser : refStat.getOwner();
279 String group = stringIsNotEmpty(filesGroup) ? filesGroup : refStat.getGroup();
280 if (stringIsNotEmpty(user) || stringIsNotEmpty(group)) {
281 try {
282 if (!(user.equals(stat.getOwner()) && group.equals(stat.getGroup()))) {
283 outputFs.setOwner(path, user, group);
284 }
285 } catch (IOException e) {
286 LOG.warn("Unable to set the owner/group for file="+ stat.getPath() +": "+ e.getMessage());
287 LOG.warn("The user/group may not exist on the destination cluster: user=" +
288 user + " group=" + group);
289 return false;
290 }
291 }
292
293 return true;
294 }
295
296 private boolean stringIsNotEmpty(final String str) {
297 return str != null && str.length() > 0;
298 }
299
300 private void copyData(final Context context,
301 final Path inputPath, final FSDataInputStream in,
302 final Path outputPath, final FSDataOutputStream out,
303 final long inputFileSize)
304 throws IOException {
305 final String statusMessage = "copied %s/" + StringUtils.humanReadableInt(inputFileSize) +
306 " (%.1f%%)";
307
308 try {
309 byte[] buffer = new byte[bufferSize];
310 long totalBytesWritten = 0;
311 int reportBytes = 0;
312 int bytesRead;
313
314 long stime = System.currentTimeMillis();
315 while ((bytesRead = in.read(buffer)) > 0) {
316 out.write(buffer, 0, bytesRead);
317 totalBytesWritten += bytesRead;
318 reportBytes += bytesRead;
319
320 if (reportBytes >= REPORT_SIZE) {
321 context.getCounter(Counter.BYTES_COPIED).increment(reportBytes);
322 context.setStatus(String.format(statusMessage,
323 StringUtils.humanReadableInt(totalBytesWritten),
324 (totalBytesWritten/(float)inputFileSize) * 100.0f) +
325 " from " + inputPath + " to " + outputPath);
326 reportBytes = 0;
327 }
328 }
329 long etime = System.currentTimeMillis();
330
331 context.getCounter(Counter.BYTES_COPIED).increment(reportBytes);
332 context.setStatus(String.format(statusMessage,
333 StringUtils.humanReadableInt(totalBytesWritten),
334 (totalBytesWritten/(float)inputFileSize) * 100.0f) +
335 " from " + inputPath + " to " + outputPath);
336
337
338 if (totalBytesWritten != inputFileSize) {
339 String msg = "number of bytes copied not matching copied=" + totalBytesWritten +
340 " expected=" + inputFileSize + " for file=" + inputPath;
341 throw new IOException(msg);
342 }
343
344 LOG.info("copy completed for input=" + inputPath + " output=" + outputPath);
345 LOG.info("size=" + totalBytesWritten +
346 " (" + StringUtils.humanReadableInt(totalBytesWritten) + ")" +
347 " time=" + StringUtils.formatTimeDiff(etime, stime) +
348 String.format(" %.3fM/sec", (totalBytesWritten / ((etime - stime)/1000.0))/1048576.0));
349 context.getCounter(Counter.FILES_COPIED).increment(1);
350 } catch (IOException e) {
351 LOG.error("Error copying " + inputPath + " to " + outputPath, e);
352 context.getCounter(Counter.COPY_FAILED).increment(1);
353 throw e;
354 }
355 }
356
357
358
359
360
361
362 private FSDataInputStream openSourceFile(Context context, final Path path) throws IOException {
363 try {
364 if (HFileLink.isHFileLink(path) || StoreFileInfo.isReference(path)) {
365 return new HFileLink(inputRoot, inputArchive, path).open(inputFs);
366 } else if (isHLogLinkPath(path)) {
367 String serverName = path.getParent().getName();
368 String logName = path.getName();
369 return new HLogLink(inputRoot, serverName, logName).open(inputFs);
370 }
371 return inputFs.open(path);
372 } catch (IOException e) {
373 context.getCounter(Counter.MISSING_FILES).increment(1);
374 LOG.error("Unable to open source file=" + path, e);
375 throw e;
376 }
377 }
378
379 private FileStatus getSourceFileStatus(Context context, final Path path) throws IOException {
380 try {
381 if (HFileLink.isHFileLink(path) || StoreFileInfo.isReference(path)) {
382 HFileLink link = new HFileLink(inputRoot, inputArchive, path);
383 return link.getFileStatus(inputFs);
384 } else if (isHLogLinkPath(path)) {
385 String serverName = path.getParent().getName();
386 String logName = path.getName();
387 return new HLogLink(inputRoot, serverName, logName).getFileStatus(inputFs);
388 }
389 return inputFs.getFileStatus(path);
390 } catch (FileNotFoundException e) {
391 context.getCounter(Counter.MISSING_FILES).increment(1);
392 LOG.error("Unable to get the status for source file=" + path, e);
393 throw e;
394 } catch (IOException e) {
395 LOG.error("Unable to get the status for source file=" + path, e);
396 throw e;
397 }
398 }
399
400 private FileChecksum getFileChecksum(final FileSystem fs, final Path path) {
401 try {
402 return fs.getFileChecksum(path);
403 } catch (IOException e) {
404 LOG.warn("Unable to get checksum for file=" + path, e);
405 return null;
406 }
407 }
408
409
410
411
412
413 private boolean sameFile(final FileStatus inputStat, final FileStatus outputStat) {
414
415 if (inputStat.getLen() != outputStat.getLen()) return false;
416
417
418 if (!verifyChecksum) return true;
419
420
421 FileChecksum inChecksum = getFileChecksum(inputFs, inputStat.getPath());
422 if (inChecksum == null) return false;
423
424 FileChecksum outChecksum = getFileChecksum(outputFs, outputStat.getPath());
425 if (outChecksum == null) return false;
426
427 return inChecksum.equals(outChecksum);
428 }
429
430
431
432
433
434
435 private static boolean isHLogLinkPath(final Path path) {
436 return path.depth() == 2;
437 }
438 }
439
440
441
442
443
444 private List<Pair<Path, Long>> getSnapshotFiles(final FileSystem fs, final Path snapshotDir)
445 throws IOException {
446 SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
447
448 final List<Pair<Path, Long>> files = new ArrayList<Pair<Path, Long>>();
449 final TableName table =
450 TableName.valueOf(snapshotDesc.getTable());
451 final Configuration conf = getConf();
452
453
454 SnapshotReferenceUtil.visitReferencedFiles(fs, snapshotDir,
455 new SnapshotReferenceUtil.FileVisitor() {
456 public void storeFile (final String region, final String family, final String hfile)
457 throws IOException {
458 Path path = HFileLink.createPath(table, region, family, hfile);
459 long size = new HFileLink(conf, path).getFileStatus(fs).getLen();
460 files.add(new Pair<Path, Long>(path, size));
461 }
462
463 public void recoveredEdits (final String region, final String logfile)
464 throws IOException {
465
466 }
467
468 public void logFile (final String server, final String logfile)
469 throws IOException {
470 long size = new HLogLink(conf, server, logfile).getFileStatus(fs).getLen();
471 files.add(new Pair<Path, Long>(new Path(server, logfile), size));
472 }
473 });
474
475 return files;
476 }
477
478
479
480
481
482
483
484
485
486 static List<List<Path>> getBalancedSplits(final List<Pair<Path, Long>> files, int ngroups) {
487
488 Collections.sort(files, new Comparator<Pair<Path, Long>>() {
489 public int compare(Pair<Path, Long> a, Pair<Path, Long> b) {
490 long r = a.getSecond() - b.getSecond();
491 return (r < 0) ? -1 : ((r > 0) ? 1 : 0);
492 }
493 });
494
495
496 List<List<Path>> fileGroups = new LinkedList<List<Path>>();
497 long[] sizeGroups = new long[ngroups];
498 int hi = files.size() - 1;
499 int lo = 0;
500
501 List<Path> group;
502 int dir = 1;
503 int g = 0;
504
505 while (hi >= lo) {
506 if (g == fileGroups.size()) {
507 group = new LinkedList<Path>();
508 fileGroups.add(group);
509 } else {
510 group = fileGroups.get(g);
511 }
512
513 Pair<Path, Long> fileInfo = files.get(hi--);
514
515
516 sizeGroups[g] += fileInfo.getSecond();
517 group.add(fileInfo.getFirst());
518
519
520 g += dir;
521 if (g == ngroups) {
522 dir = -1;
523 g = ngroups - 1;
524 } else if (g < 0) {
525 dir = 1;
526 g = 0;
527 }
528 }
529
530 if (LOG.isDebugEnabled()) {
531 for (int i = 0; i < sizeGroups.length; ++i) {
532 LOG.debug("export split=" + i + " size=" + StringUtils.humanReadableInt(sizeGroups[i]));
533 }
534 }
535
536 return fileGroups;
537 }
538
539 private static Path getInputFolderPath(Configuration conf)
540 throws IOException, InterruptedException {
541 Path stagingDir = JobUtil.getStagingDir(conf);
542 return new Path(stagingDir, INPUT_FOLDER_PREFIX +
543 String.valueOf(EnvironmentEdgeManager.currentTimeMillis()));
544 }
545
546
547
548
549
550
551
552 private static Path[] createInputFiles(final Configuration conf, final Path inputFolderPath,
553 final List<Pair<Path, Long>> snapshotFiles, int mappers)
554 throws IOException, InterruptedException {
555 FileSystem fs = inputFolderPath.getFileSystem(conf);
556 LOG.debug("Input folder location: " + inputFolderPath);
557
558 List<List<Path>> splits = getBalancedSplits(snapshotFiles, mappers);
559 Path[] inputFiles = new Path[splits.size()];
560
561 Text key = new Text();
562 for (int i = 0; i < inputFiles.length; i++) {
563 List<Path> files = splits.get(i);
564 inputFiles[i] = new Path(inputFolderPath, String.format("export-%d.seq", i));
565 SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, inputFiles[i],
566 Text.class, NullWritable.class);
567 LOG.debug("Input split: " + i);
568 try {
569 for (Path file: files) {
570 LOG.debug(file.toString());
571 key.set(file.toString());
572 writer.append(key, NullWritable.get());
573 }
574 } finally {
575 writer.close();
576 }
577 }
578
579 return inputFiles;
580 }
581
582
583
584
585 private void runCopyJob(final Path inputRoot, final Path outputRoot,
586 final List<Pair<Path, Long>> snapshotFiles, final boolean verifyChecksum,
587 final String filesUser, final String filesGroup, final int filesMode,
588 final int mappers) throws IOException, InterruptedException, ClassNotFoundException {
589 Configuration conf = getConf();
590 if (filesGroup != null) conf.set(CONF_FILES_GROUP, filesGroup);
591 if (filesUser != null) conf.set(CONF_FILES_USER, filesUser);
592 conf.setInt(CONF_FILES_MODE, filesMode);
593 conf.setBoolean(CONF_CHECKSUM_VERIFY, verifyChecksum);
594 conf.set(CONF_OUTPUT_ROOT, outputRoot.toString());
595 conf.set(CONF_INPUT_ROOT, inputRoot.toString());
596 conf.setInt("mapreduce.job.maps", mappers);
597
598 Job job = new Job(conf);
599 job.setJobName("ExportSnapshot");
600 job.setJarByClass(ExportSnapshot.class);
601 TableMapReduceUtil.addDependencyJars(job);
602 job.setMapperClass(ExportMapper.class);
603 job.setInputFormatClass(SequenceFileInputFormat.class);
604 job.setOutputFormatClass(NullOutputFormat.class);
605 job.setMapSpeculativeExecution(false);
606 job.setNumReduceTasks(0);
607
608
609 Path inputFolderPath = getInputFolderPath(conf);
610 for (Path path: createInputFiles(conf, inputFolderPath, snapshotFiles, mappers)) {
611 LOG.debug("Add Input Path=" + path);
612 SequenceFileInputFormat.addInputPath(job, path);
613 }
614
615 try {
616
617 TokenCache.obtainTokensForNamenodes(job.getCredentials(),
618 new Path[] { inputRoot, outputRoot }, conf);
619
620
621 if (!job.waitForCompletion(true)) {
622
623
624 throw new ExportSnapshotException("Copy Files Map-Reduce Job failed");
625 }
626 } finally {
627
628 try {
629 inputFolderPath.getFileSystem(conf).delete(inputFolderPath, true);
630 } catch (IOException e) {
631 LOG.warn("Unable to remove MR input folder: " + inputFolderPath, e);
632 }
633 }
634 }
635
636 private void verifySnapshot(final Configuration baseConf,
637 final FileSystem fs, final Path rootDir, final Path snapshotDir) throws IOException {
638
639 Configuration conf = new Configuration(baseConf);
640 FSUtils.setRootDir(conf, rootDir);
641 FSUtils.setFsDefault(conf, snapshotDir);
642 SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir);
643 SnapshotReferenceUtil.verifySnapshot(conf, fs, snapshotDir, snapshotDesc);
644 }
645
646
647
648
649
650 @Override
651 public int run(String[] args) throws IOException {
652 boolean verifyChecksum = true;
653 String snapshotName = null;
654 boolean overwrite = false;
655 String filesGroup = null;
656 String filesUser = null;
657 Path outputRoot = null;
658 int filesMode = 0;
659 int mappers = 0;
660
661 Configuration conf = getConf();
662
663
664 for (int i = 0; i < args.length; i++) {
665 String cmd = args[i];
666 try {
667 if (cmd.equals("-snapshot")) {
668 snapshotName = args[++i];
669 } else if (cmd.equals("-copy-to")) {
670 outputRoot = new Path(args[++i]);
671 } else if (cmd.equals("-copy-from")) {
672 Path sourceDir = new Path(args[++i]);
673 URI defaultFs = sourceDir.getFileSystem(conf).getUri();
674 FSUtils.setFsDefault(conf, new Path(defaultFs));
675 FSUtils.setRootDir(conf, sourceDir);
676 } else if (cmd.equals("-no-checksum-verify")) {
677 verifyChecksum = false;
678 } else if (cmd.equals("-mappers")) {
679 mappers = Integer.parseInt(args[++i]);
680 } else if (cmd.equals("-chuser")) {
681 filesUser = args[++i];
682 } else if (cmd.equals("-chgroup")) {
683 filesGroup = args[++i];
684 } else if (cmd.equals("-chmod")) {
685 filesMode = Integer.parseInt(args[++i], 8);
686 } else if (cmd.equals("-overwrite")) {
687 overwrite = true;
688 } else if (cmd.equals("-h") || cmd.equals("--help")) {
689 printUsageAndExit();
690 } else {
691 System.err.println("UNEXPECTED: " + cmd);
692 printUsageAndExit();
693 }
694 } catch (Exception e) {
695 printUsageAndExit();
696 }
697 }
698
699
700 if (snapshotName == null) {
701 System.err.println("Snapshot name not provided.");
702 printUsageAndExit();
703 }
704
705 if (outputRoot == null) {
706 System.err.println("Destination file-system not provided.");
707 printUsageAndExit();
708 }
709
710 Path inputRoot = FSUtils.getRootDir(conf);
711 FileSystem inputFs = FileSystem.get(inputRoot.toUri(), conf);
712 LOG.debug("inputFs=" + inputFs.getUri().toString() + " inputRoot=" + inputRoot);
713 FileSystem outputFs = FileSystem.get(outputRoot.toUri(), conf);
714 LOG.debug("outputFs=" + outputFs.getUri().toString() + " outputRoot=" + outputRoot.toString());
715
716 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, inputRoot);
717 Path snapshotTmpDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshotName, outputRoot);
718 Path outputSnapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, outputRoot);
719
720
721 if (outputFs.exists(outputSnapshotDir)) {
722 if (overwrite) {
723 if (!outputFs.delete(outputSnapshotDir, true)) {
724 System.err.println("Unable to remove existing snapshot directory: " + outputSnapshotDir);
725 return 1;
726 }
727 } else {
728 System.err.println("The snapshot '" + snapshotName +
729 "' already exists in the destination: " + outputSnapshotDir);
730 return 1;
731 }
732 }
733
734
735 if (outputFs.exists(snapshotTmpDir)) {
736 if (overwrite) {
737 if (!outputFs.delete(snapshotTmpDir, true)) {
738 System.err.println("Unable to remove existing snapshot tmp directory: " + snapshotTmpDir);
739 return 1;
740 }
741 } else {
742 System.err.println("A snapshot with the same name '"+ snapshotName +"' may be in-progress");
743 System.err.println("Please check " + snapshotTmpDir + ". If the snapshot has completed, ");
744 System.err.println("consider removing "+ snapshotTmpDir +" by using the -overwrite option");
745 return 1;
746 }
747 }
748
749
750 LOG.info("Loading Snapshot hfile list");
751 final List<Pair<Path, Long>> files = getSnapshotFiles(inputFs, snapshotDir);
752 if (mappers == 0 && files.size() > 0) {
753 mappers = 1 + (files.size() / conf.getInt(CONF_MAP_GROUP, 10));
754 mappers = Math.min(mappers, files.size());
755 }
756
757
758
759
760 try {
761 LOG.info("Copy Snapshot Manifest");
762 FileUtil.copy(inputFs, snapshotDir, outputFs, snapshotTmpDir, false, false, conf);
763 } catch (IOException e) {
764 throw new ExportSnapshotException("Failed to copy the snapshot directory: from=" +
765 snapshotDir + " to=" + snapshotTmpDir, e);
766 }
767
768
769
770
771 try {
772 if (files.size() == 0) {
773 LOG.warn("There are 0 store file to be copied. There may be no data in the table.");
774 } else {
775 runCopyJob(inputRoot, outputRoot, files, verifyChecksum,
776 filesUser, filesGroup, filesMode, mappers);
777 }
778
779
780 LOG.info("Finalize the Snapshot Export");
781 if (!outputFs.rename(snapshotTmpDir, outputSnapshotDir)) {
782 throw new ExportSnapshotException("Unable to rename snapshot directory from=" +
783 snapshotTmpDir + " to=" + outputSnapshotDir);
784 }
785
786
787 LOG.info("Verify snapshot validity");
788 verifySnapshot(conf, outputFs, outputRoot, outputSnapshotDir);
789
790 LOG.info("Export Completed: " + snapshotName);
791 return 0;
792 } catch (Exception e) {
793 LOG.error("Snapshot export failed", e);
794 outputFs.delete(snapshotTmpDir, true);
795 outputFs.delete(outputSnapshotDir, true);
796 return 1;
797 }
798 }
799
800
801 private void printUsageAndExit() {
802 System.err.printf("Usage: bin/hbase %s [options]%n", getClass().getName());
803 System.err.println(" where [options] are:");
804 System.err.println(" -h|-help Show this help and exit.");
805 System.err.println(" -snapshot NAME Snapshot to restore.");
806 System.err.println(" -copy-to NAME Remote destination hdfs://");
807 System.err.println(" -copy-from NAME Input folder hdfs:// (default hbase.rootdir)");
808 System.err.println(" -no-checksum-verify Do not verify checksum.");
809 System.err.println(" -overwrite Rewrite the snapshot manifest if already exists");
810 System.err.println(" -chuser USERNAME Change the owner of the files to the specified one.");
811 System.err.println(" -chgroup GROUP Change the group of the files to the specified one.");
812 System.err.println(" -chmod MODE Change the permission of the files to the specified one.");
813 System.err.println(" -mappers Number of mappers to use during the copy (mapreduce.job.maps).");
814 System.err.println();
815 System.err.println("Examples:");
816 System.err.println(" hbase " + getClass().getName() + " \\");
817 System.err.println(" -snapshot MySnapshot -copy-to hdfs://srv2:8082/hbase \\");
818 System.err.println(" -chuser MyUser -chgroup MyGroup -chmod 700 -mappers 16");
819 System.err.println();
820 System.err.println(" hbase " + getClass().getName() + " \\");
821 System.err.println(" -snapshot MySnapshot -copy-from hdfs://srv2:8082/hbase \\");
822 System.err.println(" -copy-to hdfs://srv1:50070/hbase \\");
823 System.exit(1);
824 }
825
826
827
828
829
830
831
832
833 static int innerMain(final Configuration conf, final String [] args) throws Exception {
834 return ToolRunner.run(conf, new ExportSnapshot(), args);
835 }
836
837 public static void main(String[] args) throws Exception {
838 System.exit(innerMain(HBaseConfiguration.create(), args));
839 }
840 }