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