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.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.hadoop.hbase.master.AssignmentManager;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.apache.hadoop.hbase.zookeeper.ZKTable;
29 import org.apache.hadoop.hbase.zookeeper.ZKTable.TableState;
30 import org.apache.zookeeper.KeeperException;
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 TableState state = getTableState(zkw, tableName);
56 return isTableState(TableState.DISABLED, state);
57 }
58
59
60
61
62
63
64
65
66 public static boolean isEnabledTable(final ZooKeeperWatcher zkw,
67 final String tableName) throws KeeperException {
68 TableState state = getTableState(zkw, tableName);
69 return state == null || state == TableState.ENABLED;
70 }
71
72
73
74
75
76
77
78
79
80 public static boolean isDisablingOrDisabledTable(final ZooKeeperWatcher zkw,
81 final String tableName) throws KeeperException {
82 TableState state = getTableState(zkw, tableName);
83 return isTableState(TableState.DISABLING, state) ||
84 isTableState(TableState.DISABLED, state);
85 }
86
87
88
89
90
91
92 public static Set<String> getDisabledTables(ZooKeeperWatcher zkw)
93 throws KeeperException {
94 Set<String> disabledTables = new HashSet<String>();
95 List<String> children =
96 ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
97 for (String child: children) {
98 TableState state = getTableState(zkw, child);
99 if (state == TableState.DISABLED) disabledTables.add(child);
100 }
101 return disabledTables;
102 }
103
104
105
106
107
108
109 public static Set<String> getDisabledOrDisablingTables(ZooKeeperWatcher zkw)
110 throws KeeperException {
111 Set<String> disabledTables = new HashSet<String>();
112 List<String> children =
113 ZKUtil.listChildrenNoWatch(zkw, zkw.clientTableZNode);
114 for (String child: children) {
115 TableState state = getTableState(zkw, child);
116 if (state == TableState.DISABLED || state == TableState.DISABLING)
117 disabledTables.add(child);
118 }
119 return disabledTables;
120 }
121
122 static boolean isTableState(final TableState expectedState,
123 final TableState currentState) {
124 return currentState != null && currentState.equals(expectedState);
125 }
126
127
128
129
130
131 static TableState getTableState(final ZooKeeperWatcher zkw,
132 final String child) throws KeeperException {
133 return getTableState(zkw, zkw.clientTableZNode, child);
134 }
135
136
137
138
139 static TableState getTableState(final ZooKeeperWatcher zkw,
140 final String parent, final String child) throws KeeperException {
141 String znode = ZKUtil.joinZNode(parent, child);
142 byte [] data = ZKUtil.getData(zkw, znode);
143 if (data == null || data.length <= 0) {
144 return null;
145 }
146 String str = Bytes.toString(data);
147 try {
148 return TableState.valueOf(str);
149 } catch (IllegalArgumentException e) {
150 throw new IllegalArgumentException(str);
151 }
152 }
153 }