View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper;
21  
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.hadoop.hbase.master.AssignmentManager;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.apache.hadoop.hbase.zookeeper.ZKTable;
29  import org.apache.hadoop.hbase.zookeeper.ZKTable.TableState;
30  import org.apache.zookeeper.KeeperException;
31  
32  /**
33   * Non-instantiable class that provides helper functions for
34   * clients other than {@link AssignmentManager} for reading the
35   * state of a table in ZK.
36   *
37   * <p>Does not cache state like {@link ZKTable}, actually reads from ZK each call.
38   */
39  public class ZKTableReadOnly {
40  
41    private ZKTableReadOnly() {}
42  
43    /**
44     * Go to zookeeper and see if state of table is {@link TableState#DISABLED}.
45     * This method does not use cache as {@link #isDisabledTable(String)} does.
46     * This method is for clients other than {@link AssignmentManager}
47     * @param zkw
48     * @param tableName
49     * @return True if table is enabled.
50     * @throws KeeperException
51     */
52    public static boolean isDisabledTable(final ZooKeeperWatcher zkw,
53        final String tableName)
54    throws KeeperException {
55      TableState state = getTableState(zkw, tableName);
56      return isTableState(TableState.DISABLED, state);
57    }
58  
59    /**
60     * Go to zookeeper and see if state of table is {@link TableState#ENABLED}.
61     * @param zkw
62     * @param tableName
63     * @return True if table is enabled.
64     * @throws KeeperException
65     */
66    public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
67        final String tableName) throws KeeperException {
68      TableState state = getTableState(zkw, tableName);
69      // If a table is ENABLED then we are removing table state znode in 0.92
70      // but in 0.94 we keep it in ENABLED state.
71      return state == null || state == TableState.ENABLED;
72    }
73  
74    /**
75     * Go to zookeeper and see if state of table is {@link TableState#DISABLING}
76     * of {@link TableState#DISABLED}.
77     * @param zkw
78     * @param tableName
79     * @return True if table is enabled.
80     * @throws KeeperException
81     */
82    public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
83        final String tableName) throws KeeperException {
84      TableState state = getTableState(zkw, tableName);
85      return isTableState(TableState.DISABLING, state) ||
86        isTableState(TableState.DISABLED, state);
87    }
88  
89    /**
90     * Gets a list of all the tables set as disabled in zookeeper.
91     * @return Set of disabled tables, empty Set if none
92     * @throws KeeperException
93     */
94    public static Set<String> getDisabledTables(ZooKeeperWatcher zkw)
95    throws KeeperException {
96      Set<String> disabledTables = new HashSet<String>();
97      List<String> children =
98        ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
99      for (String child: children) {
100       TableState state = getTableState(zkw, child);
101       if (state == TableState.DISABLED) disabledTables.add(child);
102     }
103     return disabledTables;
104   }
105 
106   /**
107    * Gets a list of all the tables set as disabled in zookeeper.
108    * @return Set of disabled tables, empty Set if none
109    * @throws KeeperException
110    */
111   public static Set<String> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
112   throws KeeperException {
113     Set<String> disabledTables = new HashSet<String>();
114     List<String> children =
115       ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
116     for (String child: children) {
117       TableState state = getTableState(zkw, child);
118       if (state == TableState.DISABLED || state == TableState.DISABLING)
119         disabledTables.add(child);
120     }
121     return disabledTables;
122   }
123 
124   static boolean isTableState(final TableState expectedState,
125     final TableState currentState) {
126     return currentState != null && currentState.equals(expectedState);
127   }
128 
129   /**
130    * Read the TableState from ZooKeeper
131    * @throws KeeperException
132    */
133   static TableState getTableState(final ZooKeeperWatcher zkw,
134     final String child) throws KeeperException {
135     return getTableState(zkw, zkw.clientTableZNode, child);
136   }
137 
138   /**
139    * @deprecated Only for 0.92/0.94 compatibility.  Use getTableState(zkw, child) instead.
140    */
141   static TableState getTableState(final ZooKeeperWatcher zkw,
142     final String parent, final String child) throws KeeperException {
143     String znode = ZKUtil.joinZNode(parent, child);
144     byte [] data = ZKUtil.getData(zkw, znode);
145     if (data == null || data.length <= 0) {
146       return null;
147     }
148     String str = Bytes.toString(data);
149     try {
150       return TableState.valueOf(str);
151     } catch (IllegalArgumentException e) {
152       throw new IllegalArgumentException(str);
153     }
154   }
155 }