1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import java.io.IOException;
21 import java.io.InterruptedIOException;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.HBaseIOException;
27 import org.apache.hadoop.hbase.HRegionInfo;
28 import org.apache.hadoop.hbase.HRegionLocation;
29 import org.apache.hadoop.hbase.RegionLocations;
30 import org.apache.hadoop.hbase.ServerName;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
33 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
34 import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
35 import org.apache.hadoop.hbase.zookeeper.ZKTableStateClientSideReader;
36 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
37 import org.apache.zookeeper.KeeperException;
38
39
40
41
42 class ZKConnectionRegistry implements ConnectionRegistry {
43 private static final Log LOG = LogFactory.getLog(ZKConnectionRegistry.class);
44
45 ConnectionManager.HConnectionImplementation hci;
46
47 @Override
48 public void init(Connection connection) {
49 if (!(connection instanceof ConnectionManager.HConnectionImplementation)) {
50 throw new RuntimeException("This registry depends on HConnectionImplementation");
51 }
52 this.hci = (ConnectionManager.HConnectionImplementation)connection;
53 }
54
55 @Override
56 public ServerName getActiveMaster() throws IOException {
57 ServerName sn;
58 try (ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher()) {
59 sn = MasterAddressTracker.getMasterAddress(zkw);
60 } catch (KeeperException e) {
61 throw new HBaseIOException(e);
62 }
63 return sn;
64 }
65
66 @Override
67 public RegionLocations getMetaRegionLocations() throws IOException {
68 try (ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher();) {
69 if (LOG.isTraceEnabled()) {
70 LOG.trace("Looking up meta region location in ZK," + " connection=" + this);
71 }
72 List<ServerName> servers = new MetaTableLocator().blockUntilAvailable(zkw, hci.rpcTimeout,
73 hci.getConfiguration());
74 if (LOG.isTraceEnabled()) {
75 if (servers == null) {
76 LOG.trace("Looked up meta region location, connection=" + this +
77 "; servers = null");
78 } else {
79 StringBuilder str = new StringBuilder();
80 for (ServerName s : servers) {
81 str.append(s.toString());
82 str.append(" ");
83 }
84 LOG.trace("Looked up meta region location, connection=" + this +
85 "; servers = " + str.toString());
86 }
87 }
88 if (servers == null) return null;
89 HRegionLocation[] locs = new HRegionLocation[servers.size()];
90 int i = 0;
91 for (ServerName server : servers) {
92 HRegionInfo h = RegionReplicaUtil.getRegionInfoForReplica(
93 HRegionInfo.FIRST_META_REGIONINFO, i);
94 if (server == null) locs[i++] = null;
95 else locs[i++] = new HRegionLocation(h, server, 0);
96 }
97 return new RegionLocations(locs);
98 } catch (InterruptedException e) {
99 Thread.currentThread().interrupt();
100 return null;
101 }
102 }
103
104 private String clusterId = null;
105
106 @Override
107 public String getClusterId() {
108 if (this.clusterId != null) return this.clusterId;
109
110
111 try (ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher()) {
112 this.clusterId = ZKClusterId.readClusterIdZNode(zkw);
113 if (this.clusterId == null) {
114 LOG.info("ClusterId read in ZooKeeper is null");
115 }
116 } catch (KeeperException | IOException e) {
117 LOG.warn("Can't retrieve clusterId from Zookeeper", e);
118 }
119 return this.clusterId;
120 }
121
122 @Override
123 public boolean isTableOnlineState(TableName tableName, boolean enabled)
124 throws IOException {
125 ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher();
126 try {
127 if (enabled) {
128 return ZKTableStateClientSideReader.isEnabledTable(zkw, tableName);
129 }
130 return ZKTableStateClientSideReader.isDisabledTable(zkw, tableName);
131 } catch (KeeperException e) {
132 throw new IOException("Enable/Disable failed", e);
133 } catch (InterruptedException e) {
134 throw new InterruptedIOException();
135 } finally {
136 zkw.close();
137 }
138 }
139
140 @Override
141 public int getCurrentNrHRS() throws IOException {
142 try (ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher()) {
143
144
145 return ZKUtil.getNumberOfChildren(zkw, zkw.rsZNode);
146 } catch (KeeperException ke) {
147 throw new IOException("Unexpected ZooKeeper exception", ke);
148 }
149 }
150
151 @Override
152 public void close() {
153 }
154 }