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