1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver.snapshot;
19
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorCompletionService;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.LinkedBlockingQueue;
29 import java.util.concurrent.ThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.classification.InterfaceAudience;
35 import org.apache.hadoop.classification.InterfaceStability;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.hbase.DaemonThreadFactory;
38 import org.apache.hadoop.hbase.errorhandling.ForeignException;
39 import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
40 import org.apache.hadoop.hbase.master.snapshot.MasterSnapshotVerifier;
41 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
42 import org.apache.hadoop.hbase.procedure.ProcedureMember;
43 import org.apache.hadoop.hbase.procedure.ProcedureMemberRpcs;
44 import org.apache.hadoop.hbase.procedure.Subprocedure;
45 import org.apache.hadoop.hbase.procedure.SubprocedureFactory;
46 import org.apache.hadoop.hbase.procedure.ZKProcedureMemberRpcs;
47 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
48 import org.apache.hadoop.hbase.regionserver.HRegion;
49 import org.apache.hadoop.hbase.regionserver.HRegionServer;
50 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
51 import org.apache.hadoop.hbase.snapshot.SnapshotCreationException;
52 import org.apache.hadoop.hbase.util.Bytes;
53 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
54 import org.apache.zookeeper.KeeperException;
55
56 import com.google.protobuf.InvalidProtocolBufferException;
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @InterfaceAudience.Private
72 @InterfaceStability.Unstable
73 public class RegionServerSnapshotManager {
74 private static final Log LOG = LogFactory.getLog(RegionServerSnapshotManager.class);
75
76
77 private static final String CONCURENT_SNAPSHOT_TASKS_KEY = "hbase.snapshot.region.concurrentTasks";
78 private static final int DEFAULT_CONCURRENT_SNAPSHOT_TASKS = 3;
79
80
81 public static final String SNAPSHOT_REQUEST_THREADS_KEY = "hbase.snapshot.region.pool.threads";
82
83 public static final int SNAPSHOT_REQUEST_THREADS_DEFAULT = 10;
84
85
86 public static final String SNAPSHOT_TIMEOUT_MILLIS_KEY = "hbase.snapshot.region.timeout";
87
88 public static final long SNAPSHOT_TIMEOUT_MILLIS_DEFAULT = 60000;
89
90
91 public static final String SNAPSHOT_REQUEST_WAKE_MILLIS_KEY = "hbase.snapshot.region.wakefrequency";
92
93 private static final long SNAPSHOT_REQUEST_WAKE_MILLIS_DEFAULT = 500;
94
95 private final RegionServerServices rss;
96 private final ProcedureMemberRpcs memberRpcs;
97 private final ProcedureMember member;
98
99
100
101
102
103
104
105
106 RegionServerSnapshotManager(Configuration conf, HRegionServer parent,
107 ProcedureMemberRpcs memberRpc, ProcedureMember procMember) {
108 this.rss = parent;
109 this.memberRpcs = memberRpc;
110 this.member = procMember;
111 }
112
113
114
115
116
117
118 public RegionServerSnapshotManager(RegionServerServices rss)
119 throws KeeperException {
120 this.rss = rss;
121 ZooKeeperWatcher zkw = rss.getZooKeeper();
122 this.memberRpcs = new ZKProcedureMemberRpcs(zkw,
123 SnapshotManager.ONLINE_SNAPSHOT_CONTROLLER_DESCRIPTION);
124
125
126 Configuration conf = rss.getConfiguration();
127 long wakeMillis = conf.getLong(SNAPSHOT_REQUEST_WAKE_MILLIS_KEY, SNAPSHOT_REQUEST_WAKE_MILLIS_DEFAULT);
128 long keepAlive = conf.getLong(SNAPSHOT_TIMEOUT_MILLIS_KEY, SNAPSHOT_TIMEOUT_MILLIS_DEFAULT);
129 int opThreads = conf.getInt(SNAPSHOT_REQUEST_THREADS_KEY, SNAPSHOT_REQUEST_THREADS_DEFAULT);
130
131
132 ThreadPoolExecutor pool = ProcedureMember.defaultPool(wakeMillis, keepAlive, opThreads,
133 rss.getServerName().toString());
134 this.member = new ProcedureMember(memberRpcs, pool, new SnapshotSubprocedureBuilder());
135 }
136
137
138
139
140 public void start() {
141 LOG.debug("Start Snapshot Manager " + rss.getServerName().toString());
142 this.memberRpcs.start(rss.getServerName().toString(), member);
143 }
144
145
146
147
148
149
150 public void stop(boolean force) throws IOException {
151 String mode = force ? "abruptly" : "gracefully";
152 LOG.info("Stopping RegionServerSnapshotManager " + mode + ".");
153
154 try {
155 this.member.close();
156 } finally {
157 this.memberRpcs.close();
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public Subprocedure buildSubprocedure(SnapshotDescription snapshot) {
172
173
174 if (rss.isStopping() || rss.isStopped()) {
175 throw new IllegalStateException("Can't start snapshot on RS: " + rss.getServerName()
176 + ", because stopping/stopped!");
177 }
178
179
180
181 List<HRegion> involvedRegions;
182 try {
183 involvedRegions = getRegionsToSnapshot(snapshot);
184 } catch (IOException e1) {
185 throw new IllegalStateException("Failed to figure out if we should handle a snapshot - "
186 + "something has gone awry with the online regions.", e1);
187 }
188
189
190
191
192
193 LOG.debug("Launching subprocedure for snapshot " + snapshot.getName() + " from table "
194 + snapshot.getTable());
195 ForeignExceptionDispatcher exnDispatcher = new ForeignExceptionDispatcher();
196 Configuration conf = rss.getConfiguration();
197 long timeoutMillis = conf.getLong(SNAPSHOT_TIMEOUT_MILLIS_KEY,
198 SNAPSHOT_TIMEOUT_MILLIS_DEFAULT);
199 long wakeMillis = conf.getLong(SNAPSHOT_REQUEST_WAKE_MILLIS_KEY,
200 SNAPSHOT_REQUEST_WAKE_MILLIS_DEFAULT);
201
202 switch (snapshot.getType()) {
203 case FLUSH:
204 SnapshotSubprocedurePool taskManager =
205 new SnapshotSubprocedurePool(rss.getServerName().toString(), conf);
206 return new FlushSnapshotSubprocedure(member, exnDispatcher, wakeMillis,
207 timeoutMillis, involvedRegions, snapshot, taskManager);
208 default:
209 throw new UnsupportedOperationException("Unrecognized snapshot type:" + snapshot.getType());
210 }
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 private List<HRegion> getRegionsToSnapshot(SnapshotDescription snapshot) throws IOException {
229 byte[] table = Bytes.toBytes(snapshot.getTable());
230 return rss.getOnlineRegions(table);
231 }
232
233
234
235
236 public class SnapshotSubprocedureBuilder implements SubprocedureFactory {
237
238 @Override
239 public Subprocedure buildSubprocedure(String name, byte[] data) {
240 try {
241
242 SnapshotDescription snapshot = SnapshotDescription.parseFrom(data);
243 return RegionServerSnapshotManager.this.buildSubprocedure(snapshot);
244 } catch (InvalidProtocolBufferException e) {
245 throw new IllegalArgumentException("Could not read snapshot information from request.");
246 }
247 }
248
249 }
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264 static class SnapshotSubprocedurePool {
265 private final ExecutorCompletionService<Void> taskPool;
266 private final ThreadPoolExecutor executor;
267 private volatile boolean stopped;
268 private final List<Future<Void>> futures = new ArrayList<Future<Void>>();
269 private final String name;
270
271 SnapshotSubprocedurePool(String name, Configuration conf) {
272
273 long keepAlive = conf.getLong(
274 RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_KEY,
275 RegionServerSnapshotManager.SNAPSHOT_TIMEOUT_MILLIS_DEFAULT);
276 int threads = conf.getInt(CONCURENT_SNAPSHOT_TASKS_KEY, DEFAULT_CONCURRENT_SNAPSHOT_TASKS);
277 this.name = name;
278 executor = new ThreadPoolExecutor(1, threads, keepAlive, TimeUnit.MILLISECONDS,
279 new LinkedBlockingQueue<Runnable>(), new DaemonThreadFactory("rs("
280 + name + ")-snapshot-pool"));
281 taskPool = new ExecutorCompletionService<Void>(executor);
282 }
283
284 boolean hasTasks() {
285 return futures.size() != 0;
286 }
287
288
289
290
291
292
293
294 void submitTask(final Callable<Void> task) {
295 Future<Void> f = this.taskPool.submit(task);
296 futures.add(f);
297 }
298
299
300
301
302
303
304
305
306
307 boolean waitForOutstandingTasks() throws ForeignException, InterruptedException {
308 LOG.debug("Waiting for local region snapshots to finish.");
309
310 int sz = futures.size();
311 try {
312
313 for (int i = 0; i < sz; i++) {
314 Future<Void> f = taskPool.take();
315 f.get();
316 if (!futures.remove(f)) {
317 LOG.warn("unexpected future" + f);
318 }
319 LOG.debug("Completed " + (i+1) + "/" + sz + " local region snapshots.");
320 }
321 LOG.debug("Completed " + sz + " local region snapshots.");
322 return true;
323 } catch (InterruptedException e) {
324 LOG.warn("Got InterruptedException in SnapshotSubprocedurePool", e);
325 if (!stopped) {
326 Thread.currentThread().interrupt();
327 throw new ForeignException("SnapshotSubprocedurePool", e);
328 }
329
330 } catch (ExecutionException e) {
331 if (e.getCause() instanceof ForeignException) {
332 LOG.warn("Rethrowing ForeignException from SnapshotSubprocedurePool", e);
333 throw (ForeignException)e.getCause();
334 }
335 LOG.warn("Got Exception in SnapshotSubprocedurePool", e);
336 throw new ForeignException(name, e.getCause());
337 } finally {
338 cancelTasks();
339 }
340 return false;
341 }
342
343
344
345
346
347 void cancelTasks() throws InterruptedException {
348 Collection<Future<Void>> tasks = futures;
349 LOG.debug("cancelling " + tasks.size() + " tasks for snapshot " + name);
350 for (Future<Void> f: tasks) {
351
352
353
354
355 f.cancel(false);
356 }
357
358
359 LOG.debug(taskPool);
360 while (!futures.isEmpty()) {
361
362 LOG.warn("Removing cancelled elements from taskPool");
363 futures.remove(taskPool.take());
364 }
365 stop();
366 }
367
368
369
370
371 void stop() {
372 if (this.stopped) return;
373
374 this.stopped = true;
375 this.executor.shutdownNow();
376 }
377 }
378 }