View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * A cluster registry that stores to zookeeper.
36   */
37  class ZooKeeperRegistry implements Registry {
38    static final Log LOG = LogFactory.getLog(ZooKeeperRegistry.class);
39    // Needs an instance of hci to function.  Set after construct this instance.
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      // No synchronized here, worse case we will retrieve it twice, that's
79      //  not an issue.
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       // We go to zk rather than to master to get count of regions to avoid
118       // HTable having a Master dependency.  See HBase-2828
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 }