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.util.List;
22
23 import org.apache.hadoop.hbase.classification.InterfaceAudience;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.hbase.Abortable;
26 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
27 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
28 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
29 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
30 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31 import org.apache.zookeeper.KeeperException;
32
33
34
35
36
37 @InterfaceAudience.Private
38 public abstract class ReplicationStateZKBase {
39
40
41
42
43
44 protected final String peerStateNodeName;
45
46 protected final String replicationZNode;
47
48 protected final String peersZNode;
49
50 protected final String queuesZNode;
51
52 protected final String ourClusterKey;
53 protected final ZooKeeperWatcher zookeeper;
54 protected final Configuration conf;
55 protected final Abortable abortable;
56
57
58 public static final byte[] ENABLED_ZNODE_BYTES =
59 toByteArray(ZooKeeperProtos.ReplicationState.State.ENABLED);
60 public static final byte[] DISABLED_ZNODE_BYTES =
61 toByteArray(ZooKeeperProtos.ReplicationState.State.DISABLED);
62
63 public ReplicationStateZKBase(ZooKeeperWatcher zookeeper, Configuration conf,
64 Abortable abortable) {
65 this.zookeeper = zookeeper;
66 this.conf = conf;
67 this.abortable = abortable;
68
69 String replicationZNodeName = conf.get("zookeeper.znode.replication", "replication");
70 String peersZNodeName = conf.get("zookeeper.znode.replication.peers", "peers");
71 String queuesZNodeName = conf.get("zookeeper.znode.replication.rs", "rs");
72 this.peerStateNodeName = conf.get("zookeeper.znode.replication.peers.state", "peer-state");
73 this.ourClusterKey = ZKConfig.getZooKeeperClusterKey(this.conf);
74 this.replicationZNode = ZKUtil.joinZNode(this.zookeeper.baseZNode, replicationZNodeName);
75 this.peersZNode = ZKUtil.joinZNode(replicationZNode, peersZNodeName);
76 this.queuesZNode = ZKUtil.joinZNode(replicationZNode, queuesZNodeName);
77 }
78
79
80
81
82
83
84 protected List<String> getListOfReplicatorsZK() throws KeeperException {
85 List<String> result = null;
86 try {
87 result = ZKUtil.listChildrenNoWatch(this.zookeeper, this.queuesZNode);
88 } catch (KeeperException e) {
89 this.abortable.abort("Failed to get list of replicators", e);
90 throw e;
91 }
92 return result;
93 }
94
95
96
97
98
99
100
101 protected static byte[] toByteArray(final ZooKeeperProtos.ReplicationState.State state) {
102 byte[] bytes =
103 ZooKeeperProtos.ReplicationState.newBuilder().setState(state).build().toByteArray();
104 return ProtobufUtil.prependPBMagic(bytes);
105 }
106
107 protected boolean peerExists(String id) throws KeeperException {
108 return ZKUtil.checkExists(this.zookeeper, ZKUtil.joinZNode(this.peersZNode, id)) >= 0;
109 }
110
111
112
113
114
115
116 protected boolean isPeerPath(String path) {
117 return path.split("/").length == peersZNode.split("/").length + 1;
118 }
119 }