1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.replication;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.classification.InterfaceAudience;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.Abortable;
26 import org.apache.hadoop.hbase.ServerName;
27 import org.apache.hadoop.hbase.exceptions.DeserializationException;
28 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
29 import org.apache.hadoop.hbase.zookeeper.ZooKeeperNodeTracker;
30 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31 import org.apache.zookeeper.KeeperException;
32
33 import java.io.Closeable;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.atomic.AtomicBoolean;
38
39
40
41
42
43
44 @InterfaceAudience.Private
45 public class ReplicationPeer implements Abortable, Closeable {
46 private static final Log LOG = LogFactory.getLog(ReplicationPeer.class);
47
48 private final String clusterKey;
49 private final String id;
50 private List<ServerName> regionServers = new ArrayList<ServerName>(0);
51 private final AtomicBoolean peerEnabled = new AtomicBoolean();
52
53 private ZooKeeperWatcher zkw;
54 private final Configuration conf;
55
56 private PeerStateTracker peerStateTracker;
57
58
59
60
61
62
63
64
65 public ReplicationPeer(Configuration conf, String key,
66 String id) throws IOException {
67 this.conf = conf;
68 this.clusterKey = key;
69 this.id = id;
70 this.reloadZkWatcher();
71 }
72
73
74
75
76
77
78
79
80 public void startStateTracker(ZooKeeperWatcher zookeeper, String peerStateNode)
81 throws KeeperException {
82 ReplicationZookeeper.ensurePeerEnabled(zookeeper, peerStateNode);
83 this.peerStateTracker = new PeerStateTracker(peerStateNode, zookeeper, this);
84 this.peerStateTracker.start();
85 try {
86 this.readPeerStateZnode();
87 } catch (DeserializationException e) {
88 throw ZKUtil.convert(e);
89 }
90 }
91
92 private void readPeerStateZnode() throws DeserializationException {
93 this.peerEnabled.set(ReplicationZookeeper.isStateEnabled(this.peerStateTracker.getData(false)));
94 }
95
96
97
98
99
100
101 public String getClusterKey() {
102 return clusterKey;
103 }
104
105
106
107
108
109 public AtomicBoolean getPeerEnabled() {
110 return peerEnabled;
111 }
112
113
114
115
116
117
118 public List<ServerName> getRegionServers() {
119 return regionServers;
120 }
121
122
123
124
125
126 public void setRegionServers(List<ServerName> regionServers) {
127 this.regionServers = regionServers;
128 }
129
130
131
132
133
134 public ZooKeeperWatcher getZkw() {
135 return zkw;
136 }
137
138
139
140
141
142 public String getId() {
143 return id;
144 }
145
146
147
148
149
150 public Configuration getConfiguration() {
151 return conf;
152 }
153
154 @Override
155 public void abort(String why, Throwable e) {
156 LOG.fatal("The ReplicationPeer coresponding to peer " + clusterKey
157 + " was aborted for the following reason(s):" + why, e);
158 }
159
160
161
162
163
164 public void reloadZkWatcher() throws IOException {
165 if (zkw != null) zkw.close();
166 zkw = new ZooKeeperWatcher(conf,
167 "connection to cluster: " + id, this);
168 }
169
170 @Override
171 public boolean isAborted() {
172
173
174 return false;
175 }
176
177 @Override
178 public void close() throws IOException {
179 if (zkw != null){
180 zkw.close();
181 }
182 }
183
184
185
186
187 public class PeerStateTracker extends ZooKeeperNodeTracker {
188
189 public PeerStateTracker(String peerStateZNode, ZooKeeperWatcher watcher,
190 Abortable abortable) {
191 super(watcher, peerStateZNode, abortable);
192 }
193
194 @Override
195 public synchronized void nodeDataChanged(String path) {
196 if (path.equals(node)) {
197 super.nodeDataChanged(path);
198 try {
199 readPeerStateZnode();
200 } catch (DeserializationException e) {
201 LOG.warn("Failed deserializing the content of " + path, e);
202 }
203 }
204 }
205 }
206 }