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