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  import org.apache.hadoop.hbase.TableName;
24  import org.apache.hadoop.hbase.exceptions.DeserializationException;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
27  import org.apache.zookeeper.KeeperException;
28  
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Set;
32  
33  /**
34   * Non-instantiable class that provides helper functions for
35   * clients other than AssignmentManager for reading the
36   * state of a table in ZK.
37   *
38   * <p>Does not cache state like {@link ZKTable}, actually reads from ZK each call.
39   */
40  public class ZKTableReadOnly {
41  
42    private ZKTableReadOnly() {}
43    
44    /**
45     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#DISABLED}.
46     * This method does not use cache.
47     * This method is for clients other than AssignmentManager
48     * @param zkw
49     * @param tableName
50     * @return True if table is enabled.
51     * @throws KeeperException
52     */
53    public static boolean isDisabledTable(final ZooKeeperWatcher zkw,
54        final TableName tableName)
55    throws KeeperException {
56      ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
57      return isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
58    }
59  
60    /**
61     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#ENABLED}.
62     * This method does not use cache.
63     * This method is for clients other than AssignmentManager
64     * @param zkw
65     * @param tableName
66     * @return True if table is enabled.
67     * @throws KeeperException
68     */
69    public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
70        final TableName tableName)
71    throws KeeperException {
72      return getTableState(zkw, tableName) == ZooKeeperProtos.Table.State.ENABLED;
73    }
74  
75    /**
76     * Go to zookeeper and see if state of table is {@code ZooKeeperProtos.Table.State#DISABLING}
77     * of {@code ZooKeeperProtos.Table.State#DISABLED}.
78     * This method does not use cache.
79     * This method is for clients other than AssignmentManager.
80     * @param zkw
81     * @param tableName
82     * @return True if table is enabled.
83     * @throws KeeperException
84     */
85    public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
86        final TableName tableName)
87    throws KeeperException {
88      ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
89      return isTableState(ZooKeeperProtos.Table.State.DISABLING, state) ||
90        isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
91    }
92  
93    /**
94     * Gets a list of all the tables set as disabled in zookeeper.
95     * @return Set of disabled tables, empty Set if none
96     * @throws KeeperException
97     */
98    public static Set<TableName> getDisabledTables(ZooKeeperWatcher zkw)
99    throws KeeperException {
100     Set<TableName> disabledTables = new HashSet<TableName>();
101     List<String> children =
102       ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
103     for (String child: children) {
104       TableName tableName =
105           TableName.valueOf(child);
106       ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
107       if (state == ZooKeeperProtos.Table.State.DISABLED) disabledTables.add(tableName);
108     }
109     return disabledTables;
110   }
111 
112   /**
113    * Gets a list of all the tables set as disabled in zookeeper.
114    * @return Set of disabled tables, empty Set if none
115    * @throws KeeperException
116    */
117   public static Set<TableName> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
118   throws KeeperException {
119     Set<TableName> disabledTables = new HashSet<TableName>();
120     List<String> children =
121       ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
122     for (String child: children) {
123       TableName tableName =
124           TableName.valueOf(child);
125       ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
126       if (state == ZooKeeperProtos.Table.State.DISABLED ||
127           state == ZooKeeperProtos.Table.State.DISABLING)
128         disabledTables.add(tableName);
129     }
130     return disabledTables;
131   }
132 
133   static boolean isTableState(final ZooKeeperProtos.Table.State expectedState,
134       final ZooKeeperProtos.Table.State currentState) {
135     return currentState != null && currentState.equals(expectedState);
136   }
137 
138   /**
139    * @param zkw
140    * @param tableName
141    * @return Null or {@link ZooKeeperProtos.Table.State} found in znode.
142    * @throws KeeperException
143    */
144   static ZooKeeperProtos.Table.State getTableState(final ZooKeeperWatcher zkw,
145       final TableName tableName)
146   throws KeeperException {
147     String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
148     byte [] data = ZKUtil.getData(zkw, znode);
149     if (data == null || data.length <= 0) return ZooKeeperProtos.Table.State.ENABLED;
150     try {
151       ProtobufUtil.expectPBMagicPrefix(data);
152       ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
153       int magicLen = ProtobufUtil.lengthOfPBMagic();
154       ZooKeeperProtos.Table t = builder.mergeFrom(data, magicLen, data.length - magicLen).build();
155       return t.getState();
156     } catch (InvalidProtocolBufferException e) {
157       KeeperException ke = new KeeperException.DataInconsistencyException();
158       ke.initCause(e);
159       throw ke;
160     } catch (DeserializationException e) {
161       throw ZKUtil.convert(e);
162     }
163   }
164 }