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