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.hbase.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.NotAllMetaRegionsOnlineException;
35 import org.apache.hadoop.hbase.TableExistsException;
36 import org.apache.hadoop.hbase.errorhandling.ForeignException;
37 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
38 import org.apache.hadoop.hbase.master.MasterServices;
39 import org.apache.hadoop.hbase.master.MetricsSnapshot;
40 import org.apache.hadoop.hbase.master.SnapshotSentinel;
41 import org.apache.hadoop.hbase.master.handler.CreateTableHandler;
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.ClientSnapshotDescriptionUtils;
46 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
47 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
48 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
49 import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
50
51 import com.google.common.base.Preconditions;
52
53
54
55
56
57
58
59 @InterfaceAudience.Private
60 public class CloneSnapshotHandler extends CreateTableHandler implements SnapshotSentinel {
61 private static final Log LOG = LogFactory.getLog(CloneSnapshotHandler.class);
62
63 private final static String NAME = "Master CloneSnapshotHandler";
64
65 private final SnapshotDescription snapshot;
66
67 private final ForeignExceptionDispatcher monitor;
68 private final MetricsSnapshot metricsSnapshot = new MetricsSnapshot();
69 private final MonitoredTask status;
70
71 private RestoreSnapshotHelper.RestoreMetaChanges metaChanges;
72
73 private volatile boolean stopped = false;
74
75 public CloneSnapshotHandler(final MasterServices masterServices,
76 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor) {
77 super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
78 masterServices.getConfiguration(), null, masterServices);
79
80
81 this.snapshot = snapshot;
82
83
84 this.monitor = new ForeignExceptionDispatcher();
85 this.status = TaskMonitor.get().createStatus("Cloning snapshot '" + snapshot.getName() +
86 "' to table " + hTableDescriptor.getTableName());
87 }
88
89 @Override
90 public CloneSnapshotHandler prepare() throws NotAllMetaRegionsOnlineException,
91 TableExistsException, IOException {
92 return (CloneSnapshotHandler) super.prepare();
93 }
94
95
96
97
98
99
100 @Override
101 protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
102 final TableName tableName) throws IOException {
103 status.setStatus("Creating regions for table: " + tableName);
104 FileSystem fs = fileSystemManager.getFileSystem();
105 Path rootDir = fileSystemManager.getRootDir();
106
107 try {
108
109 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
110 SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshot);
111 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
112 manifest, hTableDescriptor, tableRootDir, monitor, status);
113 metaChanges = restoreHelper.restoreHdfsRegions();
114
115
116 Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
117 "A clone should not have regions to restore");
118 Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
119 "A clone should not have regions to remove");
120
121
122 String msg = "Clone snapshot="+ snapshot.getName() +" on table=" + tableName + " completed!";
123 LOG.info(msg);
124 status.setStatus(msg + " Waiting for table to be enabled...");
125
126
127 return metaChanges.getRegionsToAdd();
128 } catch (Exception e) {
129 String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
130 " failed because " + e.getMessage();
131 LOG.error(msg, e);
132 IOException rse = new RestoreSnapshotException(msg, e, snapshot);
133
134
135 this.monitor.receive(new ForeignException(NAME, rse));
136 throw rse;
137 }
138 }
139
140 @Override
141 protected void addRegionsToMeta(final List<HRegionInfo> regionInfos,
142 int regionReplication)
143 throws IOException {
144 super.addRegionsToMeta(regionInfos, regionReplication);
145 metaChanges.updateMetaParentRegions(this.server.getConnection(), regionInfos);
146 }
147
148 @Override
149 protected void completed(final Throwable exception) {
150 this.stopped = true;
151 if (exception != null) {
152 status.abort("Snapshot '" + snapshot.getName() + "' clone failed because " +
153 exception.getMessage());
154 } else {
155 status.markComplete("Snapshot '"+ snapshot.getName() +"' clone completed and table enabled!");
156 }
157 metricsSnapshot.addSnapshotClone(status.getCompletionTimestamp() - status.getStartTime());
158 super.completed(exception);
159 }
160
161 @Override
162 public boolean isFinished() {
163 return this.stopped;
164 }
165
166 @Override
167 public long getCompletionTimestamp() {
168 return this.status.getCompletionTimestamp();
169 }
170
171 @Override
172 public SnapshotDescription getSnapshot() {
173 return snapshot;
174 }
175
176 @Override
177 public void cancel(String why) {
178 if (this.stopped) return;
179 this.stopped = true;
180 String msg = "Stopping clone snapshot=" + snapshot + " because: " + why;
181 LOG.info(msg);
182 status.abort(msg);
183 this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
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 }