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 java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.TreeMap;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
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.CompoundConfiguration;
35 import org.apache.hadoop.hbase.classification.InterfaceAudience;
36 import org.apache.hadoop.hbase.exceptions.DeserializationException;
37 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
38 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.BytesBytesPair;
39 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
40 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
41 import org.apache.hadoop.hbase.replication.ReplicationPeer.PeerState;
42 import org.apache.hadoop.hbase.util.Bytes;
43 import org.apache.hadoop.hbase.util.Pair;
44 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
45 import org.apache.hadoop.hbase.zookeeper.ZKUtil.ZKUtilOp;
46 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
47 import org.apache.zookeeper.KeeperException;
48
49 import com.google.protobuf.ByteString;
50 import com.google.protobuf.InvalidProtocolBufferException;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 @InterfaceAudience.Private
79 public class ReplicationPeersZKImpl extends ReplicationStateZKBase implements ReplicationPeers {
80
81
82 private Map<String, ReplicationPeerZKImpl> peerClusters;
83 private final String tableCFsNodeName;
84
85 private static final Log LOG = LogFactory.getLog(ReplicationPeersZKImpl.class);
86
87 public ReplicationPeersZKImpl(final ZooKeeperWatcher zk, final Configuration conf,
88 Abortable abortable) {
89 super(zk, conf, abortable);
90 this.tableCFsNodeName = conf.get("zookeeper.znode.replication.peers.tableCFs", "tableCFs");
91 this.peerClusters = new ConcurrentHashMap<String, ReplicationPeerZKImpl>();
92 }
93
94 @Override
95 public void init() throws ReplicationException {
96 try {
97 if (ZKUtil.checkExists(this.zookeeper, this.peersZNode) < 0) {
98 ZKUtil.createWithParents(this.zookeeper, this.peersZNode);
99 }
100 } catch (KeeperException e) {
101 throw new ReplicationException("Could not initialize replication peers", e);
102 }
103 addExistingPeers();
104 }
105
106 @Override
107 public void addPeer(String id, ReplicationPeerConfig peerConfig, String tableCFs)
108 throws ReplicationException {
109 try {
110 if (peerExists(id)) {
111 throw new IllegalArgumentException("Cannot add a peer with id=" + id
112 + " because that id already exists.");
113 }
114
115 if(id.contains("-")){
116 throw new IllegalArgumentException("Found invalid peer name:" + id);
117 }
118
119 ZKUtil.createWithParents(this.zookeeper, this.peersZNode);
120 List<ZKUtilOp> listOfOps = new ArrayList<ZKUtil.ZKUtilOp>();
121 ZKUtilOp op1 =
122 ZKUtilOp.createAndFailSilent(ZKUtil.joinZNode(this.peersZNode, id),
123 toByteArray(peerConfig));
124
125
126
127
128 ZKUtilOp op2 = ZKUtilOp.createAndFailSilent(getPeerStateNode(id), ENABLED_ZNODE_BYTES);
129 String tableCFsStr = (tableCFs == null) ? "" : tableCFs;
130 ZKUtilOp op3 = ZKUtilOp.createAndFailSilent(getTableCFsNode(id), Bytes.toBytes(tableCFsStr));
131 listOfOps.add(op1);
132 listOfOps.add(op2);
133 listOfOps.add(op3);
134 ZKUtil.multiOrSequential(this.zookeeper, listOfOps, false);
135 } catch (KeeperException e) {
136 throw new ReplicationException("Could not add peer with id=" + id + ", peerConfif="
137 + peerConfig, e);
138 }
139 }
140
141 @Override
142 public void removePeer(String id) throws ReplicationException {
143 try {
144 if (!peerExists(id)) {
145 throw new IllegalArgumentException("Cannot remove peer with id=" + id
146 + " because that id does not exist.");
147 }
148 ZKUtil.deleteNodeRecursively(this.zookeeper, ZKUtil.joinZNode(this.peersZNode, id));
149 } catch (KeeperException e) {
150 throw new ReplicationException("Could not remove peer with id=" + id, e);
151 }
152 }
153
154 @Override
155 public void enablePeer(String id) throws ReplicationException {
156 changePeerState(id, ZooKeeperProtos.ReplicationState.State.ENABLED);
157 LOG.info("peer " + id + " is enabled");
158 }
159
160 @Override
161 public void disablePeer(String id) throws ReplicationException {
162 changePeerState(id, ZooKeeperProtos.ReplicationState.State.DISABLED);
163 LOG.info("peer " + id + " is disabled");
164 }
165
166 @Override
167 public String getPeerTableCFsConfig(String id) throws ReplicationException {
168 try {
169 if (!peerExists(id)) {
170 throw new IllegalArgumentException("peer " + id + " doesn't exist");
171 }
172 try {
173 return Bytes.toString(ZKUtil.getData(this.zookeeper, getTableCFsNode(id)));
174 } catch (Exception e) {
175 throw new ReplicationException(e);
176 }
177 } catch (KeeperException e) {
178 throw new ReplicationException("Unable to get tableCFs of the peer with id=" + id, e);
179 }
180 }
181
182 @Override
183 public void setPeerTableCFsConfig(String id, String tableCFsStr) throws ReplicationException {
184 try {
185 if (!peerExists(id)) {
186 throw new IllegalArgumentException("Cannot set peer tableCFs because id=" + id
187 + " does not exist.");
188 }
189 String tableCFsZKNode = getTableCFsNode(id);
190 byte[] tableCFs = Bytes.toBytes(tableCFsStr);
191 if (ZKUtil.checkExists(this.zookeeper, tableCFsZKNode) != -1) {
192 ZKUtil.setData(this.zookeeper, tableCFsZKNode, tableCFs);
193 } else {
194 ZKUtil.createAndWatch(this.zookeeper, tableCFsZKNode, tableCFs);
195 }
196 LOG.info("Peer tableCFs with id= " + id + " is now " + tableCFsStr);
197 } catch (KeeperException e) {
198 throw new ReplicationException("Unable to change tableCFs of the peer with id=" + id, e);
199 }
200 }
201
202 @Override
203 public Map<String, List<String>> getTableCFs(String id) throws IllegalArgumentException {
204 ReplicationPeer replicationPeer = this.peerClusters.get(id);
205 if (replicationPeer == null) {
206 throw new IllegalArgumentException("Peer with id= " + id + " is not connected");
207 }
208 return replicationPeer.getTableCFs();
209 }
210
211 @Override
212 public boolean getStatusOfPeer(String id) {
213 ReplicationPeer replicationPeer = this.peerClusters.get(id);
214 if (replicationPeer == null) {
215 throw new IllegalArgumentException("Peer with id= " + id + " is not connected");
216 }
217 return this.peerClusters.get(id).getPeerState() == PeerState.ENABLED;
218 }
219
220 @Override
221 public boolean getStatusOfPeerFromBackingStore(String id) throws ReplicationException {
222 try {
223 if (!peerExists(id)) {
224 throw new IllegalArgumentException("peer " + id + " doesn't exist");
225 }
226 String peerStateZNode = getPeerStateNode(id);
227 try {
228 return ReplicationPeerZKImpl.isStateEnabled(ZKUtil.getData(this.zookeeper, peerStateZNode));
229 } catch (KeeperException e) {
230 throw new ReplicationException(e);
231 } catch (DeserializationException e) {
232 throw new ReplicationException(e);
233 }
234 } catch (KeeperException e) {
235 throw new ReplicationException("Unable to get status of the peer with id=" + id +
236 " from backing store", e);
237 }
238 }
239
240 @Override
241 public Map<String, ReplicationPeerConfig> getAllPeerConfigs() {
242 Map<String, ReplicationPeerConfig> peers = new TreeMap<String, ReplicationPeerConfig>();
243 List<String> ids = null;
244 try {
245 ids = ZKUtil.listChildrenNoWatch(this.zookeeper, this.peersZNode);
246 for (String id : ids) {
247 ReplicationPeerConfig peerConfig = getReplicationPeerConfig(id);
248 if (peerConfig == null) {
249 LOG.warn("Failed to get replication peer configuration of clusterid=" + id
250 + " znode content, continuing.");
251 continue;
252 }
253 peers.put(id, peerConfig);
254 }
255 } catch (KeeperException e) {
256 this.abortable.abort("Cannot get the list of peers ", e);
257 } catch (ReplicationException e) {
258 this.abortable.abort("Cannot get the list of peers ", e);
259 }
260 return peers;
261 }
262
263 @Override
264 public ReplicationPeerConfig getReplicationPeerConfig(String peerId) throws ReplicationException {
265 String znode = ZKUtil.joinZNode(this.peersZNode, peerId);
266 byte[] data = null;
267 try {
268 data = ZKUtil.getData(this.zookeeper, znode);
269 } catch (KeeperException e) {
270 throw new ReplicationException("Error getting configuration for peer with id=" + peerId, e);
271 }
272 if (data == null) {
273 LOG.error("Could not get configuration for peer because it doesn't exist. peerId=" + peerId);
274 return null;
275 }
276
277 try {
278 return parsePeerFrom(data);
279 } catch (DeserializationException e) {
280 LOG.warn("Failed to parse cluster key from peerId=" + peerId
281 + ", specifically the content from the following znode: " + znode);
282 return null;
283 }
284 }
285
286 @Override
287 public Pair<ReplicationPeerConfig, Configuration> getPeerConf(String peerId)
288 throws ReplicationException {
289 ReplicationPeerConfig peerConfig = getReplicationPeerConfig(peerId);
290
291 if (peerConfig == null) {
292 return null;
293 }
294
295 Configuration otherConf = new Configuration(this.conf);
296 try {
297 if (peerConfig.getClusterKey() != null && !peerConfig.getClusterKey().isEmpty()) {
298 ZKUtil.applyClusterKeyToConf(otherConf, peerConfig.getClusterKey());
299 }
300 } catch (IOException e) {
301 LOG.error("Can't get peer configuration for peerId=" + peerId + " because:", e);
302 return null;
303 }
304
305 if (!peerConfig.getConfiguration().isEmpty()) {
306 CompoundConfiguration compound = new CompoundConfiguration();
307 compound.add(otherConf);
308 compound.addStringMap(peerConfig.getConfiguration());
309 return new Pair<ReplicationPeerConfig, Configuration>(peerConfig, compound);
310 }
311
312 return new Pair<ReplicationPeerConfig, Configuration>(peerConfig, otherConf);
313 }
314
315 @Override
316 public Set<String> getPeerIds() {
317 return peerClusters.keySet();
318 }
319
320 @Override
321 public ReplicationPeer getPeer(String peerId) {
322 return peerClusters.get(peerId);
323 }
324
325
326
327
328 @Override
329 public List<String> getAllPeerIds() {
330 List<String> ids = null;
331 try {
332 ids = ZKUtil.listChildrenAndWatchThem(this.zookeeper, this.peersZNode);
333 } catch (KeeperException e) {
334 this.abortable.abort("Cannot get the list of peers ", e);
335 }
336 return ids;
337 }
338
339
340
341
342
343 private void addExistingPeers() throws ReplicationException {
344 List<String> znodes = null;
345 try {
346 znodes = ZKUtil.listChildrenNoWatch(this.zookeeper, this.peersZNode);
347 } catch (KeeperException e) {
348 throw new ReplicationException("Error getting the list of peer clusters.", e);
349 }
350 if (znodes != null) {
351 for (String z : znodes) {
352 createAndAddPeer(z);
353 }
354 }
355 }
356
357 @Override
358 public boolean peerAdded(String peerId) throws ReplicationException {
359 return createAndAddPeer(peerId);
360 }
361
362 @Override
363 public void peerRemoved(String peerId) {
364 ReplicationPeer rp = this.peerClusters.get(peerId);
365 if (rp != null) {
366 this.peerClusters.remove(peerId);
367 }
368 }
369
370
371
372
373
374
375 public boolean createAndAddPeer(String peerId) throws ReplicationException {
376 if (peerClusters == null) {
377 return false;
378 }
379 if (this.peerClusters.containsKey(peerId)) {
380 return false;
381 }
382
383 ReplicationPeerZKImpl peer = null;
384 try {
385 peer = createPeer(peerId);
386 } catch (Exception e) {
387 throw new ReplicationException("Error adding peer with id=" + peerId, e);
388 }
389 if (peer == null) {
390 return false;
391 }
392 ReplicationPeerZKImpl previous = ((ConcurrentMap<String, ReplicationPeerZKImpl>) peerClusters)
393 .putIfAbsent(peerId, peer);
394 if (previous == null) {
395 LOG.info("Added new peer cluster=" + peer.getPeerConfig().getClusterKey());
396 } else {
397 LOG.info("Peer already present, " + previous.getPeerConfig().getClusterKey()
398 + ", new cluster=" + peer.getPeerConfig().getClusterKey());
399 }
400 return true;
401 }
402
403 private String getTableCFsNode(String id) {
404 return ZKUtil.joinZNode(this.peersZNode, ZKUtil.joinZNode(id, this.tableCFsNodeName));
405 }
406
407 private String getPeerStateNode(String id) {
408 return ZKUtil.joinZNode(this.peersZNode, ZKUtil.joinZNode(id, this.peerStateNodeName));
409 }
410
411
412
413
414
415
416 private void changePeerState(String id, ZooKeeperProtos.ReplicationState.State state)
417 throws ReplicationException {
418 try {
419 if (!peerExists(id)) {
420 throw new IllegalArgumentException("Cannot enable/disable peer because id=" + id
421 + " does not exist.");
422 }
423 String peerStateZNode = getPeerStateNode(id);
424 byte[] stateBytes =
425 (state == ZooKeeperProtos.ReplicationState.State.ENABLED) ? ENABLED_ZNODE_BYTES
426 : DISABLED_ZNODE_BYTES;
427 if (ZKUtil.checkExists(this.zookeeper, peerStateZNode) != -1) {
428 ZKUtil.setData(this.zookeeper, peerStateZNode, stateBytes);
429 } else {
430 ZKUtil.createAndWatch(this.zookeeper, peerStateZNode, stateBytes);
431 }
432 LOG.info("Peer with id= " + id + " is now " + state.name());
433 } catch (KeeperException e) {
434 throw new ReplicationException("Unable to change state of the peer with id=" + id, e);
435 }
436 }
437
438
439
440
441
442
443
444 private ReplicationPeerZKImpl createPeer(String peerId) throws ReplicationException {
445 Pair<ReplicationPeerConfig, Configuration> pair = getPeerConf(peerId);
446 if (pair == null) {
447 return null;
448 }
449 Configuration peerConf = pair.getSecond();
450
451 ReplicationPeerZKImpl peer = new ReplicationPeerZKImpl(peerConf, peerId, pair.getFirst());
452 try {
453 peer.startStateTracker(this.zookeeper, this.getPeerStateNode(peerId));
454 } catch (KeeperException e) {
455 throw new ReplicationException("Error starting the peer state tracker for peerId=" +
456 peerId, e);
457 }
458
459 try {
460 peer.startTableCFsTracker(this.zookeeper, this.getTableCFsNode(peerId));
461 } catch (KeeperException e) {
462 throw new ReplicationException("Error starting the peer tableCFs tracker for peerId=" +
463 peerId, e);
464 }
465
466 return peer;
467 }
468
469
470
471
472
473
474 private static ReplicationPeerConfig parsePeerFrom(final byte[] bytes)
475 throws DeserializationException {
476 if (ProtobufUtil.isPBMagicPrefix(bytes)) {
477 int pblen = ProtobufUtil.lengthOfPBMagic();
478 ZooKeeperProtos.ReplicationPeer.Builder builder =
479 ZooKeeperProtos.ReplicationPeer.newBuilder();
480 ZooKeeperProtos.ReplicationPeer peer;
481 try {
482 peer = builder.mergeFrom(bytes, pblen, bytes.length - pblen).build();
483 } catch (InvalidProtocolBufferException e) {
484 throw new DeserializationException(e);
485 }
486 return convert(peer);
487 } else {
488 if (bytes.length > 0) {
489 return new ReplicationPeerConfig().setClusterKey(Bytes.toString(bytes));
490 }
491 return new ReplicationPeerConfig().setClusterKey("");
492 }
493 }
494
495 private static ReplicationPeerConfig convert(ZooKeeperProtos.ReplicationPeer peer) {
496 ReplicationPeerConfig peerConfig = new ReplicationPeerConfig();
497 if (peer.hasClusterkey()) {
498 peerConfig.setClusterKey(peer.getClusterkey());
499 }
500 if (peer.hasReplicationEndpointImpl()) {
501 peerConfig.setReplicationEndpointImpl(peer.getReplicationEndpointImpl());
502 }
503
504 for (BytesBytesPair pair : peer.getDataList()) {
505 peerConfig.getPeerData().put(pair.getFirst().toByteArray(), pair.getSecond().toByteArray());
506 }
507
508 for (NameStringPair pair : peer.getConfigurationList()) {
509 peerConfig.getConfiguration().put(pair.getName(), pair.getValue());
510 }
511 return peerConfig;
512 }
513
514 private static ZooKeeperProtos.ReplicationPeer convert(ReplicationPeerConfig peerConfig) {
515 ZooKeeperProtos.ReplicationPeer.Builder builder = ZooKeeperProtos.ReplicationPeer.newBuilder();
516 if (peerConfig.getClusterKey() != null) {
517 builder.setClusterkey(peerConfig.getClusterKey());
518 }
519 if (peerConfig.getReplicationEndpointImpl() != null) {
520 builder.setReplicationEndpointImpl(peerConfig.getReplicationEndpointImpl());
521 }
522
523 for (Map.Entry<byte[], byte[]> entry : peerConfig.getPeerData().entrySet()) {
524 builder.addData(BytesBytesPair.newBuilder()
525 .setFirst(ByteString.copyFrom(entry.getKey()))
526 .setSecond(ByteString.copyFrom(entry.getValue()))
527 .build());
528 }
529
530 for (Map.Entry<String, String> entry : peerConfig.getConfiguration().entrySet()) {
531 builder.addConfiguration(NameStringPair.newBuilder()
532 .setName(entry.getKey())
533 .setValue(entry.getValue())
534 .build());
535 }
536
537 return builder.build();
538 }
539
540
541
542
543
544
545
546 private static byte[] toByteArray(final ReplicationPeerConfig peerConfig) {
547 byte[] bytes = convert(peerConfig).toByteArray();
548 return ProtobufUtil.prependPBMagic(bytes);
549 }
550
551
552 }