1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.master.snapshot;
19
20 import java.io.IOException;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.HRegionInfo;
29 import org.apache.hadoop.hbase.ServerName;
30 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
31 import org.apache.hadoop.hbase.errorhandling.ForeignException;
32 import org.apache.hadoop.hbase.master.MasterServices;
33 import org.apache.hadoop.hbase.procedure.Procedure;
34 import org.apache.hadoop.hbase.procedure.ProcedureCoordinator;
35 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
36 import org.apache.hadoop.hbase.snapshot.HBaseSnapshotException;
37 import org.apache.hadoop.hbase.util.Pair;
38
39 import com.google.common.collect.Lists;
40
41
42
43
44
45
46 @InterfaceAudience.Private
47 public class EnabledTableSnapshotHandler extends TakeSnapshotHandler {
48
49 private static final Log LOG = LogFactory.getLog(EnabledTableSnapshotHandler.class);
50 private final ProcedureCoordinator coordinator;
51
52 public EnabledTableSnapshotHandler(SnapshotDescription snapshot, MasterServices master,
53 final SnapshotManager manager) {
54 super(snapshot, master, manager);
55 this.coordinator = manager.getCoordinator();
56 }
57
58 @Override
59 public EnabledTableSnapshotHandler prepare() throws Exception {
60 return (EnabledTableSnapshotHandler) super.prepare();
61 }
62
63
64
65
66
67
68
69
70
71 @Override
72 protected void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regions)
73 throws HBaseSnapshotException, IOException {
74 Set<String> regionServers = new HashSet<String>(regions.size());
75 for (Pair<HRegionInfo, ServerName> region : regions) {
76 if (region != null && region.getFirst() != null && region.getSecond() != null) {
77 HRegionInfo hri = region.getFirst();
78 if (hri.isOffline() && (hri.isSplit() || hri.isSplitParent())) continue;
79 regionServers.add(region.getSecond().toString());
80 }
81 }
82
83
84 Procedure proc = coordinator.startProcedure(this.monitor, this.snapshot.getName(),
85 this.snapshot.toByteArray(), Lists.newArrayList(regionServers));
86 if (proc == null) {
87 String msg = "Failed to submit distributed procedure for snapshot '"
88 + snapshot.getName() + "'";
89 LOG.error(msg);
90 throw new HBaseSnapshotException(msg);
91 }
92
93 try {
94
95
96 proc.waitForCompleted();
97 LOG.info("Done waiting - online snapshot for " + this.snapshot.getName());
98
99
100 for (Pair<HRegionInfo, ServerName> region : regions) {
101 HRegionInfo regionInfo = region.getFirst();
102 if (regionInfo.isOffline() && (regionInfo.isSplit() || regionInfo.isSplitParent()) &&
103 RegionReplicaUtil.isDefaultReplica(regionInfo)) {
104 LOG.info("Take disabled snapshot of offline region=" + regionInfo);
105 snapshotDisabledRegion(regionInfo);
106 }
107 }
108 } catch (InterruptedException e) {
109 ForeignException ee =
110 new ForeignException("Interrupted while waiting for snapshot to finish", e);
111 monitor.receive(ee);
112 Thread.currentThread().interrupt();
113 } catch (ForeignException e) {
114 monitor.receive(e);
115 }
116 }
117 }