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
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.hbase.HRegionInfo;
26 import org.apache.hadoop.hbase.HRegionLocation;
27 import org.apache.hadoop.hbase.RegionLocations;
28 import org.apache.hadoop.hbase.ServerName;
29 import org.apache.hadoop.hbase.TableName;
30 import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
31 import org.apache.hadoop.hbase.zookeeper.ZKClusterId;
32 import org.apache.hadoop.hbase.zookeeper.ZKTableStateClientSideReader;
33 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34 import org.apache.zookeeper.KeeperException;
35
36
37
38
39 class ZooKeeperRegistry implements Registry {
40 static final Log LOG = LogFactory.getLog(ZooKeeperRegistry.class);
41
42 ConnectionManager.HConnectionImplementation hci;
43
44 @Override
45 public void init(Connection connection) {
46 if (!(connection instanceof ConnectionManager.HConnectionImplementation)) {
47 throw new RuntimeException("This registry depends on HConnectionImplementation");
48 }
49 this.hci = (ConnectionManager.HConnectionImplementation)connection;
50 }
51
52 @Override
53 public RegionLocations getMetaRegionLocation() throws IOException {
54 ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher();
55
56 try {
57 if (LOG.isTraceEnabled()) {
58 LOG.trace("Looking up meta region location in ZK," + " connection=" + this);
59 }
60 ServerName servername = new MetaTableLocator().blockUntilAvailable(zkw, hci.rpcTimeout);
61 if (LOG.isTraceEnabled()) {
62 LOG.trace("Looked up meta region location, connection=" + this +
63 "; serverName=" + ((servername == null) ? "null" : servername));
64 }
65 if (servername == null) return null;
66 HRegionLocation loc = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, servername, 0);
67 return new RegionLocations(new HRegionLocation[] {loc});
68 } catch (InterruptedException e) {
69 Thread.currentThread().interrupt();
70 return null;
71 } finally {
72 zkw.close();
73 }
74 }
75
76 private String clusterId = null;
77
78 @Override
79 public String getClusterId() {
80 if (this.clusterId != null) return this.clusterId;
81
82
83 ZooKeeperKeepAliveConnection zkw = null;
84 try {
85 zkw = hci.getKeepAliveZooKeeperWatcher();
86 this.clusterId = ZKClusterId.readClusterIdZNode(zkw);
87 if (this.clusterId == null) {
88 LOG.info("ClusterId read in ZooKeeper is null");
89 }
90 } catch (KeeperException e) {
91 LOG.warn("Can't retrieve clusterId from Zookeeper", e);
92 } catch (IOException e) {
93 LOG.warn("Can't retrieve clusterId from Zookeeper", e);
94 } finally {
95 if (zkw != null) zkw.close();
96 }
97 return this.clusterId;
98 }
99
100 @Override
101 public boolean isTableOnlineState(TableName tableName, boolean enabled)
102 throws IOException {
103 ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher();
104 try {
105 if (enabled) {
106 return ZKTableStateClientSideReader.isEnabledTable(zkw, tableName);
107 }
108 return ZKTableStateClientSideReader.isDisabledTable(zkw, tableName);
109 } catch (KeeperException e) {
110 throw new IOException("Enable/Disable failed", e);
111 } catch (InterruptedException e) {
112 throw new InterruptedIOException();
113 } finally {
114 zkw.close();
115 }
116 }
117
118 @Override
119 public int getCurrentNrHRS() throws IOException {
120 ZooKeeperKeepAliveConnection zkw = hci.getKeepAliveZooKeeperWatcher();
121 try {
122
123
124 return ZKUtil.getNumberOfChildren(zkw, zkw.rsZNode);
125 } catch (KeeperException ke) {
126 throw new IOException("Unexpected ZooKeeper exception", ke);
127 } finally {
128 zkw.close();
129 }
130 }
131 }