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 import java.util.concurrent.ThreadPoolExecutor;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.hbase.HRegionInfo;
31 import org.apache.hadoop.hbase.ServerName;
32 import org.apache.hadoop.hbase.client.RegionReplicaUtil;
33 import org.apache.hadoop.hbase.errorhandling.ForeignException;
34 import org.apache.hadoop.hbase.master.MasterServices;
35 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
36 import org.apache.hadoop.hbase.snapshot.ClientSnapshotDescriptionUtils;
37 import org.apache.hadoop.hbase.snapshot.SnapshotManifest;
38 import org.apache.hadoop.hbase.util.FSUtils;
39 import org.apache.hadoop.hbase.util.ModifyRegionUtils;
40 import org.apache.hadoop.hbase.util.Pair;
41 import org.apache.zookeeper.KeeperException;
42
43
44
45
46
47
48 @InterfaceAudience.Private
49 @InterfaceStability.Evolving
50 public class DisabledTableSnapshotHandler extends TakeSnapshotHandler {
51 private static final Log LOG = LogFactory.getLog(DisabledTableSnapshotHandler.class);
52
53
54
55
56
57
58
59 public DisabledTableSnapshotHandler(SnapshotDescription snapshot,
60 final MasterServices masterServices, final SnapshotManager snapshotManager)
61 throws IOException {
62 super(snapshot, masterServices, snapshotManager);
63 }
64
65 @Override
66 public DisabledTableSnapshotHandler prepare() throws Exception {
67 return (DisabledTableSnapshotHandler) super.prepare();
68 }
69
70
71
72 @Override
73 public void snapshotRegions(List<Pair<HRegionInfo, ServerName>> regionsAndLocations)
74 throws IOException, KeeperException {
75 try {
76
77
78
79 Set<HRegionInfo> regions = new HashSet<HRegionInfo>();
80 for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
81
82 HRegionInfo hri = p.getFirst();
83 if (RegionReplicaUtil.isDefaultReplica(hri)) {
84 regions.add(hri);
85 }
86 }
87
88
89 String msg = "Starting to write region info and WALs for regions for offline snapshot:"
90 + ClientSnapshotDescriptionUtils.toString(snapshot);
91 LOG.info(msg);
92 status.setStatus(msg);
93
94 ThreadPoolExecutor exec = SnapshotManifest.createExecutor(conf, "DisabledTableSnapshot");
95 try {
96 ModifyRegionUtils.editRegions(exec, regions, new ModifyRegionUtils.RegionEditTask() {
97 @Override
98 public void editRegion(final HRegionInfo regionInfo) throws IOException {
99 snapshotManifest.addRegion(FSUtils.getTableDir(rootDir, snapshotTable), regionInfo);
100 }
101 });
102 } finally {
103 exec.shutdown();
104 }
105 } catch (Exception e) {
106
107 String reason = "Failed snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot)
108 + " due to exception:" + e.getMessage();
109 ForeignException ee = new ForeignException(reason, e);
110 monitor.receive(ee);
111 status.abort("Snapshot of table: "+ snapshotTable + " failed because " + e.getMessage());
112 } finally {
113 LOG.debug("Marking snapshot" + ClientSnapshotDescriptionUtils.toString(snapshot)
114 + " as finished.");
115 }
116 }
117 }