1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.zookeeper;
21
22 import org.apache.hadoop.hbase.classification.InterfaceAudience;
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.io.IOException;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34
35
36
37
38
39
40
41 @InterfaceAudience.Private
42 public class ZKTableReadOnly {
43
44 private ZKTableReadOnly() {}
45
46
47
48
49
50
51
52
53
54
55 public static boolean isDisabledTable(final ZooKeeperWatcher zkw,
56 final TableName tableName)
57 throws KeeperException {
58 ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
59 return isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
60 }
61
62
63
64
65
66
67
68
69
70
71 public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
72 final TableName tableName)
73 throws KeeperException {
74 return getTableState(zkw, tableName) == ZooKeeperProtos.Table.State.ENABLED;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
88 final TableName tableName)
89 throws KeeperException {
90 ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
91 return isTableState(ZooKeeperProtos.Table.State.DISABLING, state) ||
92 isTableState(ZooKeeperProtos.Table.State.DISABLED, state);
93 }
94
95
96
97
98
99
100 public static Set<TableName> getDisabledTables(ZooKeeperWatcher zkw)
101 throws KeeperException {
102 Set<TableName> disabledTables = new HashSet<TableName>();
103 List<String> children =
104 ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
105 for (String child: children) {
106 TableName tableName =
107 TableName.valueOf(child);
108 ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
109 if (state == ZooKeeperProtos.Table.State.DISABLED) disabledTables.add(tableName);
110 }
111 return disabledTables;
112 }
113
114
115
116
117
118
119 public static Set<TableName> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
120 throws KeeperException {
121 Set<TableName> disabledTables = new HashSet<TableName>();
122 List<String> children =
123 ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
124 for (String child: children) {
125 TableName tableName =
126 TableName.valueOf(child);
127 ZooKeeperProtos.Table.State state = getTableState(zkw, tableName);
128 if (state == ZooKeeperProtos.Table.State.DISABLED ||
129 state == ZooKeeperProtos.Table.State.DISABLING)
130 disabledTables.add(tableName);
131 }
132 return disabledTables;
133 }
134
135 static boolean isTableState(final ZooKeeperProtos.Table.State expectedState,
136 final ZooKeeperProtos.Table.State currentState) {
137 return currentState != null && currentState.equals(expectedState);
138 }
139
140
141
142
143
144
145
146 static ZooKeeperProtos.Table.State getTableState(final ZooKeeperWatcher zkw,
147 final TableName tableName)
148 throws KeeperException {
149 String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
150 byte [] data = ZKUtil.getData(zkw, znode);
151 if (data == null || data.length <= 0) return null;
152 try {
153 ProtobufUtil.expectPBMagicPrefix(data);
154 ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
155 int magicLen = ProtobufUtil.lengthOfPBMagic();
156 ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
157 return builder.getState();
158 } catch (IOException e) {
159 KeeperException ke = new KeeperException.DataInconsistencyException();
160 ke.initCause(e);
161 throw ke;
162 } catch (DeserializationException e) {
163 throw ZKUtil.convert(e);
164 }
165 }
166 }