1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.snapshot;
19
20 import java.io.IOException;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.classification.InterfaceAudience;
28 import org.apache.hadoop.classification.InterfaceStability;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.errorhandling.ForeignException;
33 import org.apache.hadoop.hbase.errorhandling.TimeoutExceptionInjector;
34 import org.apache.hadoop.hbase.master.MasterServices;
35 import org.apache.hadoop.hbase.master.MetricsMaster;
36 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
37 import org.apache.hadoop.hbase.monitoring.TaskMonitor;
38 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
39 import org.apache.hadoop.hbase.regionserver.HRegion;
40 import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
41 import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
42 import org.apache.hadoop.hbase.snapshot.CopyRecoveredEditsTask;
43 import org.apache.hadoop.hbase.snapshot.ReferenceRegionHFilesTask;
44 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
45 import org.apache.hadoop.hbase.snapshot.TableInfoCopyTask;
46 import org.apache.hadoop.hbase.snapshot.TakeSnapshotUtils;
47 import org.apache.hadoop.hbase.util.FSUtils;
48 import org.apache.hadoop.hbase.util.Pair;
49 import org.apache.zookeeper.KeeperException;
50
51
52
53
54
55
56 @InterfaceAudience.Private
57 @InterfaceStability.Evolving
58 public class DisabledTableSnapshotHandler extends TakeSnapshotHandler {
59 private static final Log LOG = LogFactory.getLog(DisabledTableSnapshotHandler.class);
60 private final TimeoutExceptionInjector timeoutInjector;
61
62
63
64
65
66 public DisabledTableSnapshotHandler(SnapshotDescription snapshot,
67 final MasterServices masterServices, final MetricsMaster metricsMaster) {
68 super(snapshot, masterServices, metricsMaster);
69
70
71 timeoutInjector = TakeSnapshotUtils.getMasterTimerAndBindToMonitor(snapshot, conf, monitor);
72 }
73
74 @Override
75 public DisabledTableSnapshotHandler prepare() throws Exception {
76 return (DisabledTableSnapshotHandler) super.prepare();
77 }
78
79
80
81 @Override
82 public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations)
83 throws IOException, KeeperException {
84 try {
85 timeoutInjector.start();
86
87 Path snapshotDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
88
89
90
91
92 Set<String> serverNames = new HashSet<String>();
93 Set<HRegionInfo> regions = new HashSet<HRegionInfo>();
94 for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
95 regions.add(p.getFirst());
96 serverNames.add(p.getSecond().toString());
97 }
98
99
100 String msg = "Starting to write region info and WALs for regions for offline snapshot:"
101 + ClientSnapshotDescriptionUtils.toString(snapshot);
102 LOG.info(msg);
103 status.setStatus(msg);
104 for (HRegionInfo regionInfo : regions) {
105
106 HRegionFileSystem regionFs = HRegionFileSystem.createRegionOnFileSystem(conf, fs,
107 snapshotDir, regionInfo);
108
109
110 monitor.rethrowException();
111
112
113 Path regionDir = HRegion.getRegionDir(rootDir, regionInfo);
114 Path snapshotRegionDir = regionFs.getRegionDir();
115 new CopyRecoveredEditsTask(snapshot, monitor, fs, regionDir, snapshotRegionDir).call();
116 monitor.rethrowException();
117 status.setStatus("Completed copying recovered edits for offline snapshot of table: "
118 + snapshotTable);
119
120
121 new ReferenceRegionHFilesTask(snapshot, monitor, regionDir, fs, snapshotRegionDir).call();
122 monitor.rethrowException();
123 status.setStatus("Completed referencing HFiles for offline snapshot of table: " +
124 snapshotTable);
125 }
126
127
128 LOG.info("Starting to copy tableinfo for offline snapshot: " +
129 ClientSnapshotDescriptionUtils.toString(snapshot));
130 TableInfoCopyTask tableInfoCopyTask = new TableInfoCopyTask(this.monitor, snapshot, fs,
131 FSUtils.getRootDir(conf));
132 tableInfoCopyTask.call();
133 monitor.rethrowException();
134 status.setStatus("Finished copying tableinfo for snapshot of table: " +
135 snapshotTable);
136 } catch (Exception e) {
137
138 String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot)
139 + " due to exception:" + e.getMessage();
140 ForeignException ee = new ForeignException(reason, e);
141 monitor.receive(ee);
142 status.abort("Snapshot of table: "+ snapshotTable +
143 " failed because " + e.getMessage());
144 } finally {
145 LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot)
146 + " as finished.");
147
148
149
150 timeoutInjector.complete();
151 }
152 }
153 }