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  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   * A cluster registry that stores to zookeeper.
38   */
39  class ZooKeeperRegistry implements Registry {
40    static final Log LOG = LogFactory.getLog(ZooKeeperRegistry.class);
41    // Needs an instance of hci to function.  Set after construct this instance.
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      // No synchronized here, worse case we will retrieve it twice, that's
82      //  not an issue.
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       // We go to zk rather than to master to get count of regions to avoid
123       // HTable having a Master dependency.  See HBase-2828
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 }