1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util.hbck;
20
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Map.Entry;
28 import java.util.Set;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.Abortable;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.client.HConnection;
36 import org.apache.hadoop.hbase.replication.ReplicationException;
37 import org.apache.hadoop.hbase.replication.ReplicationFactory;
38 import org.apache.hadoop.hbase.replication.ReplicationPeers;
39 import org.apache.hadoop.hbase.replication.ReplicationQueueInfo;
40 import org.apache.hadoop.hbase.replication.ReplicationQueuesClient;
41 import org.apache.hadoop.hbase.replication.ReplicationStateZKBase;
42 import org.apache.hadoop.hbase.util.HBaseFsck;
43 import org.apache.hadoop.hbase.util.HBaseFsck.ErrorReporter;
44 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
45 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
46 import org.apache.zookeeper.KeeperException;
47
48
49
50
51 @InterfaceAudience.Private
52 public class ReplicationChecker {
53 private static final Log LOG = LogFactory.getLog(ReplicationChecker.class);
54 private final ZooKeeperWatcher zkw;
55 private ErrorReporter errorReporter;
56 private ReplicationQueuesClient queuesClient;
57 private ReplicationPeers replicationPeers;
58 private ReplicationQueueDeletor queueDeletor;
59
60 private Map<String, List<String>> undeletedQueueIds = new HashMap<String, List<String>>();
61
62 private Set<String> undeletedHFileRefsQueueIds = new HashSet<>();
63 private final String hfileRefsZNode;
64
65 public ReplicationChecker(Configuration conf, ZooKeeperWatcher zkw, HConnection connection,
66 ErrorReporter errorReporter) throws IOException {
67 try {
68 this.zkw = zkw;
69 this.errorReporter = errorReporter;
70 this.queuesClient = ReplicationFactory.getReplicationQueuesClient(zkw, conf, connection);
71 this.queuesClient.init();
72 this.replicationPeers = ReplicationFactory.getReplicationPeers(zkw, conf, this.queuesClient,
73 connection);
74 this.replicationPeers.init();
75 this.queueDeletor = new ReplicationQueueDeletor(zkw, conf, connection);
76 } catch (ReplicationException e) {
77 throw new IOException("failed to construct ReplicationChecker", e);
78 }
79
80 String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
81 String replicationZNode = ZKUtil.joinZNode(this.zkw.baseZNode, replicationZNodeName);
82 String hfileRefsZNodeName =
83 conf.get(ReplicationStateZKBase.ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_KEY,
84 ReplicationStateZKBase.ZOOKEEPER_ZNODE_REPLICATION_HFILE_REFS_DEFAULT);
85 hfileRefsZNode = ZKUtil.joinZNode(replicationZNode, hfileRefsZNodeName);
86 }
87
88 public boolean hasUnDeletedQueues() {
89 return errorReporter.getErrorList()
90 .contains(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE);
91 }
92
93 public void checkUnDeletedQueues() throws IOException {
94 Set<String> peerIds = new HashSet<String>(this.replicationPeers.getAllPeerIds());
95 try {
96 List<String> replicators = this.queuesClient.getListOfReplicators();
97 if (replicators == null || replicators.isEmpty()) {
98 return;
99 }
100 for (String replicator : replicators) {
101 List<String> queueIds = this.queuesClient.getAllQueues(replicator);
102 for (String queueId : queueIds) {
103 ReplicationQueueInfo queueInfo = new ReplicationQueueInfo(queueId);
104 if (!peerIds.contains(queueInfo.getPeerId())) {
105 if (!undeletedQueueIds.containsKey(replicator)) {
106 undeletedQueueIds.put(replicator, new ArrayList<String>());
107 }
108 undeletedQueueIds.get(replicator).add(queueId);
109
110 String msg = "Undeleted replication queue for removed peer found: "
111 + String.format("[removedPeerId=%s, replicator=%s, queueId=%s]",
112 queueInfo.getPeerId(), replicator, queueId);
113 errorReporter.reportError(
114 HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE, msg);
115 }
116 }
117 }
118 } catch (KeeperException ke) {
119 throw new IOException(ke);
120 }
121
122 checkUnDeletedHFileRefsQueues(peerIds);
123 }
124
125 private void checkUnDeletedHFileRefsQueues(Set<String> peerIds) throws IOException {
126 try {
127 if (-1 == ZKUtil.checkExists(zkw, hfileRefsZNode)) {
128 return;
129 }
130 List<String> listOfPeers = this.queuesClient.getAllPeersFromHFileRefsQueue();
131 Set<String> peers = new HashSet<>(listOfPeers);
132 peers.removeAll(peerIds);
133 if (!peers.isEmpty()) {
134 undeletedHFileRefsQueueIds.addAll(peers);
135 String msg =
136 "Undeleted replication hfile-refs queue for removed peer found: "
137 + undeletedHFileRefsQueueIds + " under hfile-refs node " + hfileRefsZNode;
138 errorReporter.reportError(HBaseFsck.ErrorReporter.ERROR_CODE.UNDELETED_REPLICATION_QUEUE,
139 msg);
140 }
141 } catch (KeeperException e) {
142 throw new IOException("Failed to get list of all peers from hfile-refs znode "
143 + hfileRefsZNode, e);
144 }
145 }
146
147 private static class ReplicationQueueDeletor extends ReplicationStateZKBase {
148 public ReplicationQueueDeletor(ZooKeeperWatcher zk, Configuration conf, Abortable abortable) {
149 super(zk, conf, abortable);
150 }
151
152 public void removeQueue(String replicator, String queueId) throws IOException {
153 String queueZnodePath = ZKUtil.joinZNode(ZKUtil.joinZNode(this.queuesZNode, replicator),
154 queueId);
155 try {
156 ZKUtil.deleteNodeRecursively(this.zookeeper, queueZnodePath);
157 LOG.info("remove replication queue, replicator: " + replicator + ", queueId: " + queueId);
158 } catch (KeeperException e) {
159 throw new IOException("failed to delete queue, replicator: " + replicator + ", queueId: "
160 + queueId);
161 }
162 }
163 }
164
165 public void fixUnDeletedQueues() throws IOException {
166 for (Entry<String, List<String>> replicatorAndQueueIds : undeletedQueueIds.entrySet()) {
167 String replicator = replicatorAndQueueIds.getKey();
168 for (String queueId : replicatorAndQueueIds.getValue()) {
169 queueDeletor.removeQueue(replicator, queueId);
170 }
171 }
172 fixUnDeletedHFileRefsQueue();
173 }
174
175 private void fixUnDeletedHFileRefsQueue() throws IOException {
176 for (String hfileRefsQueueId : undeletedHFileRefsQueueIds) {
177 String node = ZKUtil.joinZNode(hfileRefsZNode, hfileRefsQueueId);
178 try {
179 ZKUtil.deleteNodeRecursively(this.zkw, node);
180 LOG.info("Successfully deleted hfile-refs queue " + hfileRefsQueueId + " from path "
181 + hfileRefsZNode);
182 } catch (KeeperException e) {
183 throw new IOException("Failed to delete hfile-refs queue " + hfileRefsQueueId
184 + " from path " + hfileRefsZNode);
185 }
186 }
187 }
188 }