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