1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.zookeeper;
19
20 import org.apache.hadoop.hbase.classification.InterfaceAudience;
21 import org.apache.hadoop.hbase.Abortable;
22 import org.apache.hadoop.hbase.HConstants;
23 import org.apache.hadoop.hbase.ServerName;
24 import org.apache.hadoop.hbase.exceptions.DeserializationException;
25 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos;
27 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
28 import org.apache.zookeeper.KeeperException;
29 import org.apache.zookeeper.data.Stat;
30
31 import java.io.IOException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @InterfaceAudience.Private
52 public class MasterAddressTracker extends ZooKeeperNodeTracker {
53
54
55
56
57
58
59
60
61
62
63
64 public MasterAddressTracker(ZooKeeperWatcher watcher, Abortable abortable) {
65 super(watcher, watcher.getMasterAddressZNode(), abortable);
66 }
67
68
69
70
71
72
73 public ServerName getMasterAddress() {
74 return getMasterAddress(false);
75 }
76
77
78
79
80
81
82
83
84
85 public ServerName getMasterAddress(final boolean refresh) {
86 try {
87 return ServerName.parseFrom(super.getData(refresh));
88 } catch (DeserializationException e) {
89 LOG.warn("Failed parse", e);
90 return null;
91 }
92 }
93
94
95
96
97
98
99
100
101
102
103
104 public static ServerName getMasterAddress(final ZooKeeperWatcher zkw)
105 throws KeeperException, IOException {
106 byte [] data = ZKUtil.getData(zkw, zkw.getMasterAddressZNode());
107
108 if (data == null){
109 throw new IOException("Can't get master address from ZooKeeper; znode data == null");
110 }
111 try {
112 return ServerName.parseFrom(data);
113 } catch (DeserializationException e) {
114 KeeperException ke = new KeeperException.DataInconsistencyException();
115 ke.initCause(e);
116 throw ke;
117 }
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 public static boolean setMasterAddress(final ZooKeeperWatcher zkw,
132 final String znode, final ServerName master)
133 throws KeeperException {
134 return ZKUtil.createEphemeralNodeAndWatch(zkw, znode, toByteArray(master));
135 }
136
137
138
139
140
141 public boolean hasMaster() {
142 return super.getData(false) != null;
143 }
144
145
146
147
148
149
150 static byte [] toByteArray(final ServerName sn) {
151 ZooKeeperProtos.Master.Builder mbuilder = ZooKeeperProtos.Master.newBuilder();
152 HBaseProtos.ServerName.Builder snbuilder = HBaseProtos.ServerName.newBuilder();
153 snbuilder.setHostName(sn.getHostname());
154 snbuilder.setPort(sn.getPort());
155 snbuilder.setStartCode(sn.getStartcode());
156 mbuilder.setMaster(snbuilder.build());
157 mbuilder.setRpcVersion(HConstants.RPC_CURRENT_VERSION);
158 return ProtobufUtil.prependPBMagic(mbuilder.build().toByteArray());
159 }
160
161
162
163
164
165
166 public static boolean deleteIfEquals(ZooKeeperWatcher zkw, final String content) {
167 if (content == null){
168 throw new IllegalArgumentException("Content must not be null");
169 }
170
171 try {
172 Stat stat = new Stat();
173 byte[] data = ZKUtil.getDataNoWatch(zkw, zkw.getMasterAddressZNode(), stat);
174 ServerName sn = ServerName.parseFrom(data);
175 if (sn != null && content.equals(sn.toString())) {
176 return (ZKUtil.deleteNode(zkw, zkw.getMasterAddressZNode(), stat.getVersion()));
177 }
178 } catch (KeeperException e) {
179 LOG.warn("Can't get or delete the master znode", e);
180 } catch (DeserializationException e) {
181 LOG.warn("Can't get or delete the master znode", e);
182 }
183
184 return false;
185 }
186 }