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.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.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.RestoreSnapshotException;
47 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
48 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
49
50 import com.google.common.base.Preconditions;
51 import org.apache.hadoop.hbase.util.FSUtils;
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 MetricsMaster metricsMaster;
69 private final MonitoredTask status;
70
71 private volatile boolean stopped = false;
72
73 public CloneSnapshotHandler(final MasterServices masterServices,
74 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor,
75 final MetricsMaster metricsMaster) {
76 super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
77 masterServices.getConfiguration(), null, masterServices);
78 this.metricsMaster = metricsMaster;
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 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
111 snapshot, snapshotDir, hTableDescriptor, tableRootDir, monitor, status);
112 RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
113
114
115 Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
116 "A clone should not have regions to restore");
117 Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
118 "A clone should not have regions to remove");
119
120
121 String msg = "Clone snapshot="+ snapshot.getName() +" on table=" + tableName + " completed!";
122 LOG.info(msg);
123 status.setStatus(msg + " Waiting for table to be enabled...");
124
125
126 return metaChanges.getRegionsToAdd();
127 } catch (Exception e) {
128 String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
129 " failed because " + e.getMessage();
130 LOG.error(msg, e);
131 IOException rse = new RestoreSnapshotException(msg, e, snapshot);
132
133
134 this.monitor.receive(new ForeignException(NAME, rse));
135 throw rse;
136 }
137 }
138
139 @Override
140 protected void completed(final Throwable exception) {
141 this.stopped = true;
142 if (exception != null) {
143 status.abort("Snapshot '" + snapshot.getName() + "' clone failed because " +
144 exception.getMessage());
145 } else {
146 status.markComplete("Snapshot '"+ snapshot.getName() +"' clone completed and table enabled!");
147 }
148 metricsMaster.addSnapshotClone(status.getCompletionTimestamp() - status.getStartTime());
149 super.completed(exception);
150 }
151
152 @Override
153 public boolean isFinished() {
154 return this.stopped;
155 }
156
157 @Override
158 public long getCompletionTimestamp() {
159 return this.status.getCompletionTimestamp();
160 }
161
162 @Override
163 public SnapshotDescription getSnapshot() {
164 return snapshot;
165 }
166
167 @Override
168 public void cancel(String why) {
169 if (this.stopped) return;
170 this.stopped = true;
171 String msg = "Stopping clone snapshot=" + snapshot + " because: " + why;
172 LOG.info(msg);
173 status.abort(msg);
174 this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
175 }
176
177 @Override
178 public ForeignException getExceptionIfFailed() {
179 return this.monitor.getException();
180 }
181
182 @Override
183 public void rethrowExceptionIfFailed() throws ForeignException {
184 monitor.rethrowException();
185 }
186 }