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 com.google.protobuf.InvalidProtocolBufferException;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
29  import org.apache.zookeeper.KeeperException;
30  
31  import java.util.HashSet;
32  import java.util.List;
33  import java.util.Set;
34  
35  /**
36   * Non-instantiable class that provides helper functions to learn
37   * about HBase table state for code running on client side (hence, not having
38   * access to consensus context).
39   *
40   * Doesn't cache any table state, just goes directly to ZooKeeper.
41   * TODO: decouple this class from ZooKeeper.
42   */
43  @InterfaceAudience.Private
44  public class ZKTableStateClientSideReader {
45  
46    private ZKTableStateClientSideReader() {}
47    
48    /**
49     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#DISABLED}.
50     * This method does not use cache.
51     * This method is for clients other than AssignmentManager
52     * @param zkw ZooKeeperWatcher instance to use
53     * @param tableName table we're checking
54     * @return True if table is enabled.
55     * @throws KeeperException
56     */
57    public static boolean isDisabledTable(final ZooKeeperWatcher zkw,
58        final TableName tableName)
59        throws KeeperException, InterruptedException {
60      ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
61      return isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
62    }
63  
64    /**
65     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#ENABLED}.
66     * This method does not use cache.
67     * This method is for clients other than AssignmentManager
68     * @param zkw ZooKeeperWatcher instance to use
69     * @param tableName table we're checking
70     * @return True if table is enabled.
71     * @throws KeeperException
72     */
73    public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
74        final TableName tableName)
75        throws KeeperException, InterruptedException {
76      return getTableState(zkw, tableName) == ZooKeeperProtos.Table.State.ENABLED;
77    }
78  
79    /**
80     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#DISABLING}
81     * of {@code ZooKeeperProtos.Table.State#DISABLED}.
82     * This method does not use cache.
83     * This method is for clients other than AssignmentManager.
84     * @param zkw ZooKeeperWatcher instance to use
85     * @param tableName table we're checking
86     * @return True if table is enabled.
87     * @throws KeeperException
88     */
89    public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
90        final TableName tableName)
91        throws KeeperException, InterruptedException {
92      ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
93      return isTableState(ZooKeeperProtos.Table.State.DISABLING, state) ||
94        isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
95    }
96  
97    /**
98     * Gets a list of all the tables set as disabled in zookeeper.
99     * @return Set of disabled tables, empty Set if none
100    * @throws KeeperException
101    */
102   public static Set<TableName> getDisabledTables(ZooKeeperWatcher zkw)
103       throws KeeperException, InterruptedException {
104     Set<TableName> disabledTables = new HashSet<TableName>();
105     List<String> children =
106       ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
107     for (String child: children) {
108       TableName tableName =
109           TableName.valueOf(child);
110       ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
111       if (state == ZooKeeperProtos.Table.State.DISABLED) disabledTables.add(tableName);
112     }
113     return disabledTables;
114   }
115 
116   /**
117    * Gets a list of all the tables set as disabled in zookeeper.
118    * @return Set of disabled tables, empty Set if none
119    * @throws KeeperException
120    */
121   public static Set<TableName> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
122       throws KeeperException, InterruptedException {
123     return
124         getTablesInStates(
125           zkw,
126           ZooKeeperProtos.Table.State.DISABLED,
127           ZooKeeperProtos.Table.State.DISABLING);
128   }
129 
130   /**
131    * Gets a list of all the tables set as enabling in zookeeper.
132    * @param zkw ZooKeeperWatcher instance to use
133    * @return Set of enabling tables, empty Set if none
134    * @throws KeeperException
135    * @throws InterruptedException
136    */
137   public static Set<TableName> getEnablingTables(ZooKeeperWatcher zkw)
138       throws KeeperException, InterruptedException {
139     return getTablesInStates(zkw, ZooKeeperProtos.Table.State.ENABLING);
140   }
141 
142   /**
143    * Gets a list of tables that are set as one of the passing in states in zookeeper.
144    * @param zkw ZooKeeperWatcher instance to use
145    * @param states the list of states that a table could be in
146    * @return Set of tables in one of the states, empty Set if none
147    * @throws KeeperException
148    * @throws InterruptedException
149    */
150   private static Set<TableName> getTablesInStates(
151     ZooKeeperWatcher zkw,
152     ZooKeeperProtos.Table.State... states)
153       throws KeeperException, InterruptedException {
154     Set<TableName> tableNameSet = new HashSet<TableName>();
155     List<String> children = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
156     TableName tableName;
157     ZooKeeperProtos.Table.State tableState;
158     for (String child: children) {
159       tableName = TableName.valueOf(child);
160       tableState = getTableState(zkw, tableName);
161       for (ZooKeeperProtos.Table.State state : states) {
162          if (tableState == state) {
163            tableNameSet.add(tableName);
164            break;
165          }
166       }
167     }
168     return tableNameSet;
169   }
170 
171   static boolean isTableState(final ZooKeeperProtos.Table.State expectedState,
172       final ZooKeeperProtos.Table.State currentState) {
173     return currentState != null && currentState.equals(expectedState);
174   }
175 
176   /**
177    * @param zkw ZooKeeperWatcher instance to use
178    * @param tableName table we're checking
179    * @return Null or {@link ZooKeeperProtos.Table.State} found in znode.
180    * @throws KeeperException
181    */
182   static ZooKeeperProtos.Table.State getTableState(final ZooKeeperWatcher zkw,
183       final TableName tableName)
184       throws KeeperException, InterruptedException {
185     String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
186     byte [] data = ZKUtil.getData(zkw, znode);
187     if (data == null || data.length <= 0) return null;
188     try {
189       ProtobufUtil.expectPBMagicPrefix(data);
190       ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
191       int magicLen = ProtobufUtil.lengthOfPBMagic();
192       ZooKeeperProtos.Table t = builder.mergeFrom(data, magicLen, data.length - magicLen).build();
193       return t.getState();
194     } catch (InvalidProtocolBufferException e) {
195       KeeperException ke = new KeeperException.DataInconsistencyException();
196       ke.initCause(e);
197       throw ke;
198     } catch (DeserializationException e) {
199       throw ZKUtil.convert(e);
200     }
201   }
202 }