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 private final boolean restoreAcl;
67
68 private final ForeignExceptionDispatcher monitor;
69 private final MetricsSnapshot metricsSnapshot = new MetricsSnapshot();
70 private final MonitoredTask status;
71
72 private RestoreSnapshotHelper.RestoreMetaChanges metaChanges;
73
74 private volatile boolean stopped = false;
75
76 public CloneSnapshotHandler(final MasterServices masterServices,
77 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor,
78 final boolean restoreAcl) {
79 super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
80 masterServices.getConfiguration(), null, masterServices);
81
82
83 this.snapshot = snapshot;
84 this.restoreAcl = restoreAcl;
85
86
87 this.monitor = new ForeignExceptionDispatcher();
88 this.status = TaskMonitor.get().createStatus("Cloning snapshot '" + snapshot.getName() +
89 "' to table " + hTableDescriptor.getTableName());
90 }
91
92 @Override
93 public CloneSnapshotHandler prepare() throws NotAllMetaRegionsOnlineException,
94 TableExistsException, IOException {
95 return (CloneSnapshotHandler) super.prepare();
96 }
97
98
99
100
101
102
103 @Override
104 protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
105 final TableName tableName) throws IOException {
106 status.setStatus("Creating regions for table: " + tableName);
107 FileSystem fs = fileSystemManager.getFileSystem();
108 Path rootDir = fileSystemManager.getRootDir();
109
110 try {
111
112 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
113 SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshot);
114 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
115 manifest, hTableDescriptor, tableRootDir, monitor, status);
116 metaChanges = restoreHelper.restoreHdfsRegions();
117
118
119 Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
120 "A clone should not have regions to restore");
121 Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
122 "A clone should not have regions to remove");
123
124
125 if (restoreAcl && snapshot.hasUsersAndPermissions()
126 && SnapshotDescriptionUtils.isSecurityAvailable(conf)) {
127 RestoreSnapshotHelper.restoreSnapshotACL(snapshot, tableName, conf);
128 }
129
130
131 String msg = "Clone snapshot="+ snapshot.getName() +" on table=" + tableName + " completed!";
132 LOG.info(msg);
133 status.setStatus(msg + " Waiting for table to be enabled...");
134
135
136 return metaChanges.getRegionsToAdd();
137 } catch (Exception e) {
138 String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
139 " failed because " + e.getMessage();
140 LOG.error(msg, e);
141 IOException rse = new RestoreSnapshotException(msg, e, snapshot);
142
143
144 this.monitor.receive(new ForeignException(NAME, rse));
145 throw rse;
146 }
147 }
148
149 @Override
150 protected void addRegionsToMeta(final List<HRegionInfo> regionInfos,
151 int regionReplication)
152 throws IOException {
153 super.addRegionsToMeta(regionInfos, regionReplication);
154 metaChanges.updateMetaParentRegions(this.server.getConnection(), regionInfos);
155 }
156
157 @Override
158 protected void completed(final Throwable exception) {
159 this.stopped = true;
160 if (exception != null) {
161 status.abort("Snapshot '" + snapshot.getName() + "' clone failed because " +
162 exception.getMessage());
163 } else {
164 status.markComplete("Snapshot '"+ snapshot.getName() +"' clone completed and table enabled!");
165 }
166 metricsSnapshot.addSnapshotClone(status.getCompletionTimestamp() - status.getStartTime());
167 super.completed(exception);
168 }
169
170 @Override
171 public boolean isFinished() {
172 return this.stopped;
173 }
174
175 @Override
176 public long getCompletionTimestamp() {
177 return this.status.getCompletionTimestamp();
178 }
179
180 @Override
181 public SnapshotDescription getSnapshot() {
182 return snapshot;
183 }
184
185 @Override
186 public void cancel(String why) {
187 if (this.stopped) return;
188 this.stopped = true;
189 String msg = "Stopping clone snapshot=" + snapshot + " because: " + why;
190 LOG.info(msg);
191 status.abort(msg);
192 this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
193 }
194
195 @Override
196 public ForeignException getExceptionIfFailed() {
197 return this.monitor.getException();
198 }
199
200 @Override
201 public void rethrowExceptionIfFailed() throws ForeignException {
202 monitor.rethrowException();
203 }
204 }