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.classification.InterfaceAudience;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.HRegionInfo;
30 import org.apache.hadoop.hbase.ServerName;
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.regionserver.HRegion;
37 import org.apache.hadoop.hbase.snapshot.HBaseSnapshotException;
38 import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
39 import org.apache.hadoop.hbase.util.Pair;
40
41 import com.google.common.collect.Lists;
42
43
44
45
46
47
48 @InterfaceAudience.Private
49 public class EnabledTableSnapshotHandler extends TakeSnapshotHandler {
50
51 private static final Log LOG = LogFactory.getLog(EnabledTableSnapshotHandler.class);
52 private final ProcedureCoordinator coordinator;
53
54 public EnabledTableSnapshotHandler(SnapshotDescription snapshot, MasterServices master,
55 SnapshotManager manager) throws IOException {
56 super(snapshot, master);
57 this.coordinator = manager.getCoordinator();
58 }
59
60
61
62
63
64
65
66
67
68 @Override
69 protected void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regions)
70 throws HBaseSnapshotException, IOException {
71 Set<String> regionServers = new HashSet<String>(regions.size());
72 for (Pair<HRegionInfo, ServerName> region : regions) {
73 if (region != null && region.getFirst() != null && region.getSecond() != null) {
74 HRegionInfo hri = region.getFirst();
75 if (hri.isOffline() && (hri.isSplit() || hri.isSplitParent())) continue;
76 regionServers.add(region.getSecond().toString());
77 }
78 }
79
80
81 Procedure proc = coordinator.startProcedure(this.monitor, this.snapshot.getName(),
82 this.snapshot.toByteArray(), Lists.newArrayList(regionServers));
83 if (proc == null) {
84 String msg = "Failed to submit distributed procedure for snapshot '"
85 + snapshot.getName() + "'";
86 LOG.error(msg);
87 throw new HBaseSnapshotException(msg);
88 }
89
90 try {
91
92
93 proc.waitForCompleted();
94 LOG.info("Done waiting - online snapshot for " + this.snapshot.getName());
95
96
97 Path snapshotDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshot, rootDir);
98 for (Pair<HRegionInfo, ServerName> region : regions) {
99 HRegionInfo regionInfo = region.getFirst();
100 if (regionInfo.isOffline() && (regionInfo.isSplit() || regionInfo.isSplitParent())) {
101 if (!fs.exists(new Path(snapshotDir, regionInfo.getEncodedName()))) {
102 LOG.info("Take disabled snapshot of offline region=" + regionInfo);
103 snapshotDisabledRegion(regionInfo);
104 }
105 }
106 }
107 } catch (InterruptedException e) {
108 ForeignException ee =
109 new ForeignException("Interrupted while waiting for snapshot to finish", e);
110 monitor.receive(ee);
111 Thread.currentThread().interrupt();
112 } catch (ForeignException e) {
113 monitor.receive(e);
114 }
115 }
116 }