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.exceptions.DeserializationException;
24 import org.apache.hadoop.hbase.ServerName;
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
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class MetaRegionTracker extends ZooKeeperNodeTracker {
37
38
39
40
41
42
43
44
45 public MetaRegionTracker(ZooKeeperWatcher watcher, Abortable abortable) {
46 super(watcher, watcher.metaServerZNode, abortable);
47 }
48
49
50
51
52
53 public boolean isLocationAvailable() {
54 return super.getData(true) != null;
55 }
56
57
58
59
60
61
62 public ServerName getMetaRegionLocation() throws InterruptedException {
63 try {
64 return ServerName.parseFrom(super.getData(true));
65 } catch (DeserializationException e) {
66 LOG.warn("Failed parse", e);
67 return null;
68 }
69 }
70
71
72
73
74
75
76
77
78 public static ServerName getMetaRegionLocation(final ZooKeeperWatcher zkw)
79 throws KeeperException {
80 try {
81 return ServerName.parseFrom(ZKUtil.getData(zkw, zkw.metaServerZNode));
82 } catch (DeserializationException e) {
83 throw ZKUtil.convert(e);
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public ServerName waitMetaRegionLocation(long timeout)
98 throws InterruptedException {
99 if (false == checkIfBaseNodeAvailable()) {
100 String errorMsg = "Check the value configured in 'zookeeper.znode.parent'. "
101 + "There could be a mismatch with the one configured in the master.";
102 LOG.error(errorMsg);
103 throw new IllegalArgumentException(errorMsg);
104 }
105 try {
106 return ServerName.parseFrom(super.blockUntilAvailable(timeout, true));
107 } catch (DeserializationException e) {
108 LOG.warn("Failed parse", e);
109 return null;
110 }
111 }
112
113
114
115
116
117
118
119
120 public static void setMetaLocation(ZooKeeperWatcher zookeeper,
121 final ServerName location)
122 throws KeeperException {
123 LOG.info("Setting hbase:meta region location in ZooKeeper as " + location);
124
125
126 byte [] data = toByteArray(location);
127 try {
128 ZKUtil.createAndWatch(zookeeper, zookeeper.metaServerZNode, data);
129 } catch(KeeperException.NodeExistsException nee) {
130 LOG.debug("META region location already existed, updated location");
131 ZKUtil.setData(zookeeper, zookeeper.metaServerZNode, data);
132 }
133 }
134
135
136
137
138
139
140 static byte [] toByteArray(final ServerName sn) {
141
142 HBaseProtos.ServerName pbsn =
143 HBaseProtos.ServerName.newBuilder()
144 .setHostName(sn.getHostname())
145 .setPort(sn.getPort())
146 .setStartCode(sn.getStartcode())
147 .build();
148
149 ZooKeeperProtos.MetaRegionServer pbrsr =
150 ZooKeeperProtos.MetaRegionServer.newBuilder()
151 .setServer(pbsn)
152 .setRpcVersion(HConstants.RPC_CURRENT_VERSION)
153 .build();
154 return ProtobufUtil.prependPBMagic(pbrsr.toByteArray());
155 }
156
157
158
159
160
161
162 public static void deleteMetaLocation(ZooKeeperWatcher zookeeper)
163 throws KeeperException {
164 LOG.info("Unsetting hbase:meta region location in ZooKeeper");
165 try {
166
167 ZKUtil.deleteNode(zookeeper, zookeeper.metaServerZNode);
168 } catch(KeeperException.NoNodeException nne) {
169
170 }
171 }
172
173
174
175
176
177
178
179
180 public static ServerName blockUntilAvailable(final ZooKeeperWatcher zkw,
181 final long timeout)
182 throws InterruptedException {
183 byte [] data = ZKUtil.blockUntilAvailable(zkw, zkw.metaServerZNode, timeout);
184 if (data == null) return null;
185 try {
186 return ServerName.parseFrom(data);
187 } catch (DeserializationException e) {
188 LOG.warn("Failed parse", e);
189 return null;
190 }
191 }
192 }