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