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.catalog.CatalogTracker;
37 import org.apache.hadoop.hbase.errorhandling.ForeignException;
38 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
39 import org.apache.hadoop.hbase.master.MasterServices;
40 import org.apache.hadoop.hbase.master.MetricsSnapshot;
41 import org.apache.hadoop.hbase.master.SnapshotSentinel;
42 import org.apache.hadoop.hbase.master.handler.CreateTableHandler;
43 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
44 import org.apache.hadoop.hbase.monitoring.TaskMonitor;
45 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
46 import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
47 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
48 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
49 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
50 import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
51
52 import com.google.common.base.Preconditions;
53
54
55
56
57
58
59
60 @InterfaceAudience.Private
61 public class CloneSnapshotHandler extends CreateTableHandler implements SnapshotSentinel {
62 private static final Log LOG = LogFactory.getLog(CloneSnapshotHandler.class);
63
64 private final static String NAME = "Master CloneSnapshotHandler";
65
66 private final SnapshotDescription snapshot;
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 super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
79 masterServices.getConfiguration(), null, masterServices);
80
81
82 this.snapshot = snapshot;
83
84
85 this.monitor = new ForeignExceptionDispatcher();
86 this.status = TaskMonitor.get().createStatus("Cloning snapshot '" + snapshot.getName() +
87 "' to table " + hTableDescriptor.getTableName());
88 }
89
90 @Override
91 public CloneSnapshotHandler prepare() throws NotAllMetaRegionsOnlineException,
92 TableExistsException, IOException {
93 return (CloneSnapshotHandler) super.prepare();
94 }
95
96
97
98
99
100
101 @Override
102 protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir,
103 final TableName tableName) throws IOException {
104 status.setStatus("Creating regions for table: " + tableName);
105 FileSystem fs = fileSystemManager.getFileSystem();
106 Path rootDir = fileSystemManager.getRootDir();
107
108 try {
109
110 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
111 SnapshotManifest manifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshot);
112 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
113 manifest, hTableDescriptor, tableRootDir, monitor, status);
114 metaChanges = restoreHelper.restoreHdfsRegions();
115
116
117 Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
118 "A clone should not have regions to restore");
119 Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
120 "A clone should not have regions to remove");
121
122
123 String msg = "Clone snapshot="+ snapshot.getName() +" on table=" + tableName + " completed!";
124 LOG.info(msg);
125 status.setStatus(msg + " Waiting for table to be enabled...");
126
127
128 return metaChanges.getRegionsToAdd();
129 } catch (Exception e) {
130 String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) +
131 " failed because " + e.getMessage();
132 LOG.error(msg, e);
133 IOException rse = new RestoreSnapshotException(msg, e, snapshot);
134
135
136 this.monitor.receive(new ForeignException(NAME, rse));
137 throw rse;
138 }
139 }
140
141 @Override
142 protected void addRegionsToMeta(final CatalogTracker ct, final List<HRegionInfo> regionInfos)
143 throws IOException {
144 super.addRegionsToMeta(ct, regionInfos);
145 metaChanges.updateMetaParentRegions(ct, 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 }