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.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 if (data == null){
108 throw new IOException("Can't get master address from ZooKeeper; znode data == null");
109 }
110 try {
111 return ServerName.parseFrom(data);
112 } catch (DeserializationException e) {
113 KeeperException ke = new KeeperException.DataInconsistencyException();
114 ke.initCause(e);
115 throw ke;
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130 public static boolean setMasterAddress(final ZooKeeperWatcher zkw,
131 final String znode, final ServerName master)
132 throws KeeperException {
133 return ZKUtil.createEphemeralNodeAndWatch(zkw, znode, toByteArray(master));
134 }
135
136
137
138
139
140 public boolean hasMaster() {
141 return super.getData(false) != null;
142 }
143
144
145
146
147
148
149 static byte [] toByteArray(final ServerName sn) {
150 ZooKeeperProtos.Master.Builder mbuilder = ZooKeeperProtos.Master.newBuilder();
151 HBaseProtos.ServerName.Builder snbuilder = HBaseProtos.ServerName.newBuilder();
152 snbuilder.setHostName(sn.getHostname());
153 snbuilder.setPort(sn.getPort());
154 snbuilder.setStartCode(sn.getStartcode());
155 mbuilder.setMaster(snbuilder.build());
156 mbuilder.setRpcVersion(HConstants.RPC_CURRENT_VERSION);
157 return ProtobufUtil.prependPBMagic(mbuilder.build().toByteArray());
158 }
159
160
161
162
163 public static boolean deleteIfEquals(ZooKeeperWatcher zkw, final String content) {
164 if (content == null){
165 throw new IllegalArgumentException("Content must not be null");
166 }
167
168 try {
169 Stat stat = new Stat();
170 byte[] data = ZKUtil.getDataNoWatch(zkw, zkw.getMasterAddressZNode(), stat);
171 ServerName sn = ServerName.parseFrom(data);
172 if (sn != null && content.equals(sn.toString())) {
173 return (ZKUtil.deleteNode(zkw, zkw.getMasterAddressZNode(), stat.getVersion()));
174 }
175 } catch (KeeperException e) {
176 LOG.warn("Can't get or delete the master znode", e);
177 } catch (DeserializationException e) {
178 LOG.warn("Can't get or delete the master znode", e);
179 }
180
181 return false;
182 }
183 }