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.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
35
36
37
38
39
40 public class ZKTableReadOnly {
41
42 private ZKTableReadOnly() {}
43
44
45
46
47
48
49
50
51
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
62
63
64
65
66
67
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
77
78
79
80
81
82
83
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
95
96
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
114
115
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
140
141
142
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 }