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.LinkedList;
24 import java.util.List;
25 import java.util.concurrent.CancellationException;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.classification.InterfaceAudience;
30 import org.apache.hadoop.fs.FileSystem;
31 import org.apache.hadoop.fs.Path;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.catalog.CatalogTracker;
35 import org.apache.hadoop.hbase.catalog.MetaEditor;
36 import org.apache.hadoop.hbase.errorhandling.ForeignException;
37 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
38 import org.apache.hadoop.hbase.master.AssignmentManager;
39 import org.apache.hadoop.hbase.master.MasterFileSystem;
40 import org.apache.hadoop.hbase.master.MasterServices;
41 import org.apache.hadoop.hbase.master.SnapshotSentinel;
42 import org.apache.hadoop.hbase.master.handler.TableEventHandler;
43 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
44 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotException;
45 import org.apache.hadoop.hbase.snapshot.RestoreSnapshotHelper;
46 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
47 import org.apache.hadoop.hbase.util.Bytes;
48
49
50
51
52
53
54
55 @InterfaceAudience.Private
56 public class RestoreSnapshotHandler extends TableEventHandler implements SnapshotSentinel {
57 private static final Log LOG = LogFactory.getLog(RestoreSnapshotHandler.class);
58
59 private final HTableDescriptor hTableDescriptor;
60 private final SnapshotDescription snapshot;
61
62 private final ForeignExceptionDispatcher monitor;
63 private volatile boolean stopped = false;
64
65 public RestoreSnapshotHandler(final MasterServices masterServices,
66 final SnapshotDescription snapshot, final HTableDescriptor htd)
67 throws IOException {
68 super(EventType.C_M_RESTORE_SNAPSHOT, htd.getName(), masterServices, masterServices);
69
70
71 this.snapshot = snapshot;
72
73
74 this.monitor = new ForeignExceptionDispatcher();
75
76
77 getTableDescriptor();
78
79
80 this.hTableDescriptor = htd;
81 }
82
83
84
85
86
87
88
89
90
91 @Override
92 protected void handleTableOperation(List<HRegionInfo> hris) throws IOException {
93 MasterFileSystem fileSystemManager = masterServices.getMasterFileSystem();
94 CatalogTracker catalogTracker = masterServices.getCatalogTracker();
95 FileSystem fs = fileSystemManager.getFileSystem();
96 Path rootDir = fileSystemManager.getRootDir();
97 byte[] tableName = hTableDescriptor.getName();
98 Path tableDir = HTableDescriptor.getTableDir(rootDir, tableName);
99
100 try {
101
102 this.masterServices.getTableDescriptors().add(hTableDescriptor);
103
104
105 LOG.debug("Starting restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot));
106 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshot, rootDir);
107 RestoreSnapshotHelper restoreHelper = new RestoreSnapshotHelper(
108 masterServices.getConfiguration(), fs,
109 snapshot, snapshotDir, hTableDescriptor, tableDir, monitor);
110 RestoreSnapshotHelper.RestoreMetaChanges metaChanges = restoreHelper.restoreHdfsRegions();
111
112
113
114
115
116
117
118
119 forceRegionsOffline(metaChanges);
120 forceRegionsOffline(metaChanges);
121
122
123
124
125
126
127
128
129
130
131 List<HRegionInfo> hrisToRemove = new LinkedList<HRegionInfo>();
132 if (metaChanges.hasRegionsToRemove()) hrisToRemove.addAll(metaChanges.getRegionsToRemove());
133 MetaEditor.deleteRegions(catalogTracker, hrisToRemove);
134
135
136
137
138
139
140
141 hris.clear();
142 if (metaChanges.hasRegionsToAdd()) hris.addAll(metaChanges.getRegionsToAdd());
143 MetaEditor.addRegionsToMeta(catalogTracker, hris);
144 if (metaChanges.hasRegionsToRestore()) {
145 MetaEditor.overwriteRegions(catalogTracker, metaChanges.getRegionsToRestore());
146 }
147 metaChanges.updateMetaParentRegions(catalogTracker, hris);
148
149
150 LOG.info("Restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot) + " on table=" +
151 Bytes.toString(tableName) + " completed!");
152 } catch (IOException e) {
153 String msg = "restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
154 + " failed. Try re-running the restore command.";
155 LOG.error(msg, e);
156 monitor.receive(new ForeignException(masterServices.getServerName().toString(), e));
157 throw new RestoreSnapshotException(msg, e);
158 } finally {
159 this.stopped = true;
160 }
161 }
162
163 private void forceRegionsOffline(final RestoreSnapshotHelper.RestoreMetaChanges metaChanges) {
164 forceRegionsOffline(metaChanges.getRegionsToAdd());
165 forceRegionsOffline(metaChanges.getRegionsToRestore());
166 forceRegionsOffline(metaChanges.getRegionsToRemove());
167 }
168
169 private void forceRegionsOffline(final List<HRegionInfo> hris) {
170 AssignmentManager am = this.masterServices.getAssignmentManager();
171 if (hris != null) {
172 for (HRegionInfo hri: hris) {
173 am.regionOffline(hri);
174 }
175 }
176 }
177
178 @Override
179 public boolean isFinished() {
180 return this.stopped;
181 }
182
183 @Override
184 public SnapshotDescription getSnapshot() {
185 return snapshot;
186 }
187
188 @Override
189 public void cancel(String why) {
190 if (this.stopped) return;
191 this.stopped = true;
192 String msg = "Stopping restore snapshot=" + SnapshotDescriptionUtils.toString(snapshot)
193 + " because: " + why;
194 LOG.info(msg);
195 CancellationException ce = new CancellationException(why);
196 this.monitor.receive(new ForeignException(masterServices.getServerName().toString(), ce));
197 }
198
199 public ForeignException getExceptionIfFailed() {
200 return this.monitor.getException();
201 }
202 }