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