1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master.snapshot;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.concurrent.CancellationException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.classification.InterfaceAudience;
29 import org.apache.hadoop.fs.FileSystem;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.hbase.HRegionInfo;
32 import org.apache.hadoop.hbase.HTableDescriptor;
33 import org.apache.hadoop.hbase.catalog.CatalogTracker;
34 import org.apache.hadoop.hbase.catalog.MetaEditor;
35 import org.apache.hadoop.hbase.errorhandling.ForeignException;
36 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
37 import org.apache.hadoop.hbase.master.MasterFileSystem;
38 import org.apache.hadoop.hbase.master.MasterServices;
39 import org.apache.hadoop.hbase.master.SnapshotSentinel;
40 import org.apache.hadoop.hbase.master.handler.TableEventHandler;
41 import org.apache.hadoop.hbase.master.metrics.MasterMetrics;
42 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
43 import org.apache.hadoop.hbase.monitoring.TaskMonitor;
44 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
45 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
46 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
47 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
48 import org.apache.hadoop.hbase.util.Bytes;
49
50
51
52
53
54
55
56 @InterfaceAudience.Private
57 public class RestoreSnapshotHandler extends TableEventHandler implements SnapshotSentinel {
58 private static final Log LOG = LogFactory.getLog(RestoreSnapshotHandler.class);
59
60 private final HTableDescriptor hTableDescriptor;
61 private final SnapshotDescription snapshot;
62
63 private final ForeignExceptionDispatcher monitor;
64 private final MasterMetrics metricsMaster;
65 private final MonitoredTask status;
66
67 private volatile boolean stopped = false;
68
69 public RestoreSnapshotHandler(final MasterServices masterServices,
70 final SnapshotDescription snapshot, final HTableDescriptor htd,
71 final MasterMetrics metricsMaster) throws IOException {
72 super(EventType.C_M_RESTORE_SNAPSHOT, htd.getName(), masterServices, masterServices);
73 this.metricsMaster = metricsMaster;
74
75
76 this.snapshot = snapshot;
77
78
79 this.monitor = new ForeignExceptionDispatcher();
80
81
82 getTableDescriptor();
83
84
85 this.hTableDescriptor = htd;
86
87 this.status = TaskMonitor.get().createStatus(
88 "Restoring snapshot '" + snapshot.getName() + "' to table "
89 + hTableDescriptor.getNameAsString());
90 }
91
92
93
94
95
96
97
98
99
100 @Override
101 protected void handleTableOperation(List<HRegionInfo> hris) throws IOException {
102 MasterFileSystem fileSystemManager = masterServices.getMasterFileSystem();
103 CatalogTracker catalogTracker = masterServices.getCatalogTracker();
104 FileSystem fs = fileSystemManager.getFileSystem();
105 Path rootDir = fileSystemManager.getRootDir();
106 byte[] tableName = hTableDescriptor.getName();
107 Path tableDir = HTableDescriptor.getTableDir(rootDir, tableName);
108
109 try {
110
111 this.masterServices.getTableDescriptors().add(hTableDescriptor);
112
113
114 LOG.debug("Starting restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot));
115 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
116 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(
117 masterServices.getConfiguration(), fs,
118 snapshot, snapshotDir, hTableDescriptor, tableDir, monitor, status);
119 RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
120
121
122 hris.clear();
123 status.setStatus("Preparing to restore each region");
124 if (metaChanges.hasRegionsToAdd()) hris.addAll(metaChanges.getRegionsToAdd());
125 if (metaChanges.hasRegionsToRestore()) hris.addAll(metaChanges.getRegionsToRestore());
126 List<HRegionInfo> hrisToRemove = metaChanges.getRegionsToRemove();
127 MetaEditor.mutateRegions(catalogTracker, hrisToRemove, hris);
128
129
130 LOG.info("Restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot) + " on table=" +
131 Bytes.toString(tableName) + " completed!");
132 } catch (IOException e) {
133 String msg = "restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
134 + " failed. Try re-running the restore command.";
135 LOG.error(msg, e);
136 monitor.receive(new ForeignException(masterServices.getServerName().toString(), e));
137 throw new RestoreSnapshotException(msg, e);
138 } finally {
139 this.stopped = true;
140 }
141 }
142
143 @Override
144 protected void completed(final Throwable exception) {
145 this.stopped = true;
146 if (exception != null) {
147 status.abort("Restore snapshot '" + snapshot.getName() + "' failed because " +
148 exception.getMessage());
149 } else {
150 status.markComplete("Restore snapshot '"+ snapshot.getName() +"' completed!");
151 }
152 metricsMaster.addSnapshotRestore(status.getCompletionTimestamp() - status.getStartTime());
153 super.completed(exception);
154 }
155
156 @Override
157 public boolean isFinished() {
158 return this.stopped;
159 }
160
161 @Override
162 public long getCompletionTimestamp() {
163 return this.status.getCompletionTimestamp();
164 }
165
166 @Override
167 public SnapshotDescription getSnapshot() {
168 return snapshot;
169 }
170
171 @Override
172 public void cancel(String why) {
173 if (this.stopped) return;
174 this.stopped = true;
175 String msg = "Stopping restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
176 + " because: " + why;
177 LOG.info(msg);
178 CancellationException ce = new CancellationException(why);
179 this.monitor.receive(new ForeignException(masterServices.getServerName().toString(), ce));
180 }
181
182 @Override
183 public ForeignException getExceptionIfFailed() {
184 return this.monitor.getException();
185 }
186
187 @Override
188 public void rethrowExceptionIfFailed() throws ForeignException {
189 monitor.rethrowException();
190 }
191 }