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