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 java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.master.AssignmentManager;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.zookeeper.KeeperException;
33
34
35
36
37
38
39
40
41
42
43
44
45 public class ZKTable {
46
47
48
49
50
51 private static final Log LOG = LogFactory.getLog(ZKTable.class);
52 private final ZooKeeperWatcher watcher;
53
54
55
56
57
58
59 private final Map<String, TableState> cache =
60 new HashMap<String, TableState>();
61
62
63
64
65
66
67
68
69 public static enum TableState {
70 ENABLED,
71 DISABLED,
72 DISABLING,
73 ENABLING
74 };
75
76 public ZKTable(final ZooKeeperWatcher zkw) throws KeeperException {
77 super();
78 this.watcher = zkw;
79 populateTableStates();
80 }
81
82
83
84
85
86
87
88 private void populateTableStates()
89 throws KeeperException {
90 synchronized (this.cache) {
91 List<String> children =
92 ZKUtil.listChildrenNoWatch(this.watcher, this.watcher.tableZNode);
93 for (String child: children) {
94 TableState state = getTableState(this.watcher, child);
95 if (state != null) this.cache.put(child, state);
96 }
97 }
98 }
99
100
101
102
103
104
105
106 private static TableState getTableState(final ZooKeeperWatcher zkw,
107 final String child)
108 throws KeeperException {
109 String znode = ZKUtil.joinZNode(zkw.tableZNode, child);
110 byte [] data = ZKUtil.getData(zkw, znode);
111 if (data == null || data.length <= 0) {
112
113 return null;
114 }
115 String str = Bytes.toString(data);
116 try {
117 return TableState.valueOf(str);
118 } catch (IllegalArgumentException e) {
119 throw new IllegalArgumentException(str);
120 }
121 }
122
123
124
125
126
127
128
129 public void setDisabledTable(String tableName)
130 throws KeeperException {
131 synchronized (this.cache) {
132 if (!isDisablingOrDisabledTable(tableName)) {
133 LOG.warn("Moving table " + tableName + " state to disabled but was " +
134 "not first in disabling state: " + this.cache.get(tableName));
135 }
136 setTableState(tableName, TableState.DISABLED);
137 }
138 }
139
140
141
142
143
144
145
146 public void setDisablingTable(final String tableName)
147 throws KeeperException {
148 synchronized (this.cache) {
149 if (!isEnabledOrDisablingTable(tableName)) {
150 LOG.warn("Moving table " + tableName + " state to disabling but was " +
151 "not first in enabled state: " + this.cache.get(tableName));
152 }
153 setTableState(tableName, TableState.DISABLING);
154 }
155 }
156
157
158
159
160
161
162
163 public void setEnablingTable(final String tableName)
164 throws KeeperException {
165 synchronized (this.cache) {
166 if (!isDisabledOrEnablingTable(tableName)) {
167 LOG.warn("Moving table " + tableName + " state to disabling but was " +
168 "not first in enabled state: " + this.cache.get(tableName));
169 }
170 setTableState(tableName, TableState.ENABLING);
171 }
172 }
173
174 private void setTableState(final String tableName, final TableState state)
175 throws KeeperException {
176 String znode = ZKUtil.joinZNode(this.watcher.tableZNode, tableName);
177 if (ZKUtil.checkExists(this.watcher, znode) == -1) {
178 ZKUtil.createAndFailSilent(this.watcher, znode);
179 }
180 synchronized (this.cache) {
181 ZKUtil.setData(this.watcher, znode, Bytes.toBytes(state.toString()));
182 this.cache.put(tableName, state);
183 }
184 }
185
186 public boolean isDisabledTable(final String tableName) {
187 return isTableState(tableName, TableState.DISABLED);
188 }
189
190
191
192
193
194
195
196
197
198
199 public static boolean isDisabledTable(final ZooKeeperWatcher zkw,
200 final String tableName)
201 throws KeeperException {
202 TableState state = getTableState(zkw, tableName);
203 return isTableState(TableState.DISABLED, state);
204 }
205
206 public boolean isDisablingTable(final String tableName) {
207 return isTableState(tableName, TableState.DISABLING);
208 }
209
210 public boolean isEnablingTable(final String tableName) {
211 return isTableState(tableName, TableState.ENABLING);
212 }
213
214 public boolean isEnabledTable(String tableName) {
215 synchronized (this.cache) {
216
217 return !this.cache.containsKey(tableName);
218 }
219 }
220
221
222
223
224
225
226
227
228
229
230 public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
231 final String tableName)
232 throws KeeperException {
233 return getTableState(zkw, tableName) == null;
234 }
235
236 public boolean isDisablingOrDisabledTable(final String tableName) {
237 synchronized (this.cache) {
238 return isDisablingTable(tableName) || isDisabledTable(tableName);
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252 public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
253 final String tableName)
254 throws KeeperException {
255 TableState state = getTableState(zkw, tableName);
256 return isTableState(TableState.DISABLING, state) ||
257 isTableState(TableState.DISABLED, state);
258 }
259
260 public boolean isEnabledOrDisablingTable(final String tableName) {
261 synchronized (this.cache) {
262 return isEnabledTable(tableName) || isDisablingTable(tableName);
263 }
264 }
265
266 public boolean isDisabledOrEnablingTable(final String tableName) {
267 synchronized (this.cache) {
268 return isDisabledTable(tableName) || isEnablingTable(tableName);
269 }
270 }
271
272 private boolean isTableState(final String tableName, final TableState state) {
273 synchronized (this.cache) {
274 TableState currentState = this.cache.get(tableName);
275 return isTableState(currentState, state);
276 }
277 }
278
279 private static boolean isTableState(final TableState expectedState,
280 final TableState currentState) {
281 return currentState != null && currentState.equals(expectedState);
282 }
283
284
285
286
287
288
289
290 public void setEnabledTable(final String tableName)
291 throws KeeperException {
292 synchronized (this.cache) {
293 if (this.cache.remove(tableName) == null) {
294 LOG.warn("Moving table " + tableName + " state to enabled but was " +
295 "already enabled");
296 }
297 ZKUtil.deleteNodeFailSilent(this.watcher,
298 ZKUtil.joinZNode(this.watcher.tableZNode, tableName));
299 }
300 }
301
302
303
304
305
306 public Set<String> getDisabledTables() {
307 Set<String> disabledTables = new HashSet<String>();
308 synchronized (this.cache) {
309 Set<String> tables = this.cache.keySet();
310 for (String table: tables) {
311 if (isDisabledTable(table)) disabledTables.add(table);
312 }
313 }
314 return disabledTables;
315 }
316
317
318
319
320
321
322 public static Set<String> getDisabledTables(ZooKeeperWatcher zkw)
323 throws KeeperException {
324 Set<String> disabledTables = new HashSet<String>();
325 List<String> children =
326 ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
327 for (String child: children) {
328 TableState state = getTableState(zkw, child);
329 if (state == TableState.DISABLED) disabledTables.add(child);
330 }
331 return disabledTables;
332 }
333
334
335
336
337
338
339 public static Set<String> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
340 throws KeeperException {
341 Set<String> disabledTables = new HashSet<String>();
342 List<String> children =
343 ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
344 for (String child: children) {
345 TableState state = getTableState(zkw, child);
346 if (state == TableState.DISABLED || state == TableState.DISABLING)
347 disabledTables.add(child);
348 }
349 return disabledTables;
350 }
351 }