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.SnapshotSentinel;
40 import org.apache.hadoop.hbase.master.handler.CreateTableHandler;
41 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
42 import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
43 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
44 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
45
46 import com.google.common.base.Preconditions;
47
48
49
50
51
52
53
54 @InterfaceAudience.Private
55 public class CloneSnapshotHandler extends CreateTableHandler implements SnapshotSentinel {
56 private static final Log LOG = LogFactory.getLog(CloneSnapshotHandler.class);
57
58 private final static String NAME = "Master CloneSnapshotHandler";
59
60 private final SnapshotDescription snapshot;
61
62 private final ForeignExceptionDispatcher monitor;
63
64 private volatile boolean stopped = false;
65
66 public CloneSnapshotHandler(final MasterServices masterServices,
67 final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
68 throws NotAllMetaRegionsOnlineException, TableExistsException, IOException {
69 super(masterServices, masterServices.getMasterFileSystem(), hTableDescriptor,
70 masterServices.getConfiguration(), null, masterServices);
71
72
73 this.snapshot = snapshot;
74
75
76 this.monitor = new ForeignExceptionDispatcher();
77 }
78
79 @Override
80 public CloneSnapshotHandler prepare() throws NotAllMetaRegionsOnlineException,
81 TableExistsException, IOException {
82 return (CloneSnapshotHandler) super.prepare();
83 }
84
85
86
87
88
89
90 @Override
91 protected List<HRegionInfo> handleCreateHdfsRegions(final Path tableRootDir, final String tableName)
92 throws IOException {
93 FileSystem fs = fileSystemManager.getFileSystem();
94 Path rootDir = fileSystemManager.getRootDir();
95 Path tableDir = new Path(tableRootDir, tableName);
96
97 try {
98
99 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
100 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(conf, fs,
101 snapshot, snapshotDir, hTableDescriptor, tableDir, monitor);
102 RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
103
104
105 Preconditions.checkArgument(!metaChanges.hasRegionsToRestore(),
106 "A clone should not have regions to restore");
107 Preconditions.checkArgument(!metaChanges.hasRegionsToRemove(),
108 "A clone should not have regions to remove");
109
110
111 LOG.info("Clone snapshot=" + snapshot.getName() + " on table=" + tableName + " completed!");
112
113
114 return metaChanges.getRegionsToAdd();
115 } catch (Exception e) {
116 String msg = "clone snapshot=" + ClientSnapshotDescriptionUtils.toString(snapshot) + " failed";
117 LOG.error(msg, e);
118 IOException rse = new RestoreSnapshotException(msg, e, snapshot);
119
120
121 this.monitor.receive(new ForeignException(NAME, rse));
122 throw rse;
123 }
124 }
125
126 @Override
127 protected void completed(final Throwable exception) {
128 this.stopped = true;
129 super.completed(exception);
130 }
131
132 @Override
133 public boolean isFinished() {
134 return this.stopped;
135 }
136
137 @Override
138 public SnapshotDescription getSnapshot() {
139 return snapshot;
140 }
141
142 @Override
143 public void cancel(String why) {
144 if (this.stopped) return;
145 this.stopped = true;
146 LOG.info("Stopping clone snapshot=" + snapshot + " because: " + why);
147 this.monitor.receive(new ForeignException(NAME, new CancellationException(why)));
148 }
149
150 @Override
151 public ForeignException getExceptionIfFailed() {
152 return this.monitor.getException();
153 }
154 }