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      return state == null || state == TableState.ENABLED;
70    }
71  
72    /**
73     * Go to zookeeper and see if state of table is {@link TableState#DISABLING}
74     * of {@link TableState#DISABLED}.
75     * @param zkw
76     * @param tableName
77     * @return True if table is enabled.
78     * @throws KeeperException
79     */
80    public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
81        final String tableName) throws KeeperException {
82      TableState state = getTableState(zkw, tableName);
83      return isTableState(TableState.DISABLING, state) ||
84        isTableState(TableState.DISABLED, state);
85    }
86  
87    /**
88     * Gets a list of all the tables set as disabled in zookeeper.
89     * @return Set of disabled tables, empty Set if none
90     * @throws KeeperException
91     */
92    public static Set<String> getDisabledTables(ZooKeeperWatcher zkw)
93    throws KeeperException {
94      Set<String> disabledTables = new HashSet<String>();
95      List<String> children =
96        ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
97      for (String child: children) {
98        TableState state = getTableState(zkw, child);
99        if (state == TableState.DISABLED) disabledTables.add(child);
100     }
101     return disabledTables;
102   }
103 
104   /**
105    * Gets a list of all the tables set as disabled in zookeeper.
106    * @return Set of disabled tables, empty Set if none
107    * @throws KeeperException
108    */
109   public static Set<String> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
110   throws KeeperException {
111     Set<String> disabledTables = new HashSet<String>();
112     List<String> children =
113       ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
114     for (String child: children) {
115       TableState state = getTableState(zkw, child);
116       if (state == TableState.DISABLED || state == TableState.DISABLING)
117         disabledTables.add(child);
118     }
119     return disabledTables;
120   }
121 
122   static boolean isTableState(final TableState expectedState,
123     final TableState currentState) {
124     return currentState != null && currentState.equals(expectedState);
125   }
126 
127   /**
128    * Read the TableState from ZooKeeper
129    * @throws KeeperException
130    */
131   static TableState getTableState(final ZooKeeperWatcher zkw,
132     final String child) throws KeeperException {
133     return getTableState(zkw, zkw.clientTableZNode, child);
134   }
135 
136   /**
137    * @deprecated Only for 0.92/0.94 compatibility.  Use getTableState(zkw, child) instead.
138    */
139   static TableState getTableState(final ZooKeeperWatcher zkw,
140     final String parent, final String child) throws KeeperException {
141     String znode = ZKUtil.joinZNode(parent, child);
142     byte [] data = ZKUtil.getData(zkw, znode);
143     if (data == null || data.length <= 0) {
144       return null;
145     }
146     String str = Bytes.toString(data);
147     try {
148       return TableState.valueOf(str);
149     } catch (IllegalArgumentException e) {
150       throw new IllegalArgumentException(str);
151     }
152   }
153 }