1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.zookeeper;
20
21 import java.io.InterruptedIOException;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.CoordinatedStateException;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.TableStateManager;
34 import org.apache.hadoop.hbase.classification.InterfaceAudience;
35 import org.apache.hadoop.hbase.exceptions.DeserializationException;
36 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
37 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
38 import org.apache.zookeeper.KeeperException;
39
40 import com.google.protobuf.InvalidProtocolBufferException;
41
42
43
44
45
46
47
48
49
50
51 @InterfaceAudience.Private
52 public class ZKTableStateManager implements TableStateManager {
53
54
55
56
57
58 private static final Log LOG = LogFactory.getLog(ZKTableStateManager.class);
59 private final ZooKeeperWatcher watcher;
60
61
62
63
64
65
66 private final Map<TableName, ZooKeeperProtos.Table.State> cache =
67 new HashMap<TableName, ZooKeeperProtos.Table.State>();
68
69 public ZKTableStateManager(final ZooKeeperWatcher zkw) throws KeeperException,
70 InterruptedException {
71 super();
72 this.watcher = zkw;
73 populateTableStates();
74 }
75
76
77
78
79
80 private void populateTableStates() throws KeeperException, InterruptedException {
81 synchronized (this.cache) {
82 List<String> children = ZKUtil.listChildrenNoWatch(this.watcher, this.watcher.tableZNode);
83 if (children == null) return;
84 for (String child: children) {
85 TableName tableName = TableName.valueOf(child);
86 ZooKeeperProtos.Table.State state = getTableState(this.watcher, tableName);
87 if (state != null) this.cache.put(tableName, state);
88 }
89 }
90 }
91
92
93
94
95
96
97 @Override
98 public void setTableState(TableName tableName, ZooKeeperProtos.Table.State state)
99 throws CoordinatedStateException {
100 synchronized (this.cache) {
101 LOG.warn("Moving table " + tableName + " state from " + this.cache.get(tableName)
102 + " to " + state);
103 try {
104 setTableStateInZK(tableName, state);
105 } catch (KeeperException e) {
106 throw new CoordinatedStateException(e);
107 }
108 }
109 }
110
111
112
113
114
115 @Override
116 public boolean setTableStateIfInStates(TableName tableName,
117 ZooKeeperProtos.Table.State newState,
118 ZooKeeperProtos.Table.State... states)
119 throws CoordinatedStateException {
120 synchronized (this.cache) {
121
122
123 if (
124 (newState == ZooKeeperProtos.Table.State.DISABLING) &&
125 this.cache.get(tableName) != null && !isTableState(tableName, states) ||
126 (newState != ZooKeeperProtos.Table.State.DISABLING &&
127 !isTableState(tableName, states) )) {
128 return false;
129 }
130 try {
131 setTableStateInZK(tableName, newState);
132 } catch (KeeperException e) {
133 throw new CoordinatedStateException(e);
134 }
135 return true;
136 }
137 }
138
139
140
141
142
143 @Override
144 public boolean setTableStateIfNotInStates(TableName tableName,
145 ZooKeeperProtos.Table.State newState,
146 ZooKeeperProtos.Table.State... states)
147 throws CoordinatedStateException {
148 synchronized (this.cache) {
149 if (isTableState(tableName, states)) {
150
151
152
153
154
155
156 try {
157 ZooKeeperProtos.Table.State curstate = getTableState(watcher, tableName);
158
159 if (isTableInState(Arrays.asList(states), curstate)) {
160 return false;
161 }
162 } catch (KeeperException e) {
163 throw new CoordinatedStateException(e);
164 } catch (InterruptedException e) {
165 throw new CoordinatedStateException(e);
166 }
167 }
168 try {
169 setTableStateInZK(tableName, newState);
170 } catch (KeeperException e) {
171 throw new CoordinatedStateException(e);
172 }
173 return true;
174 }
175 }
176
177 private void setTableStateInZK(final TableName tableName,
178 final ZooKeeperProtos.Table.State state)
179 throws KeeperException {
180 String znode = ZKUtil.joinZNode(this.watcher.tableZNode, tableName.getNameAsString());
181 if (ZKUtil.checkExists(this.watcher, znode) == -1) {
182 ZKUtil.createAndFailSilent(this.watcher, znode);
183 }
184 synchronized (this.cache) {
185 ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
186 builder.setState(state);
187 byte [] data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
188 ZKUtil.setData(this.watcher, znode, data);
189 this.cache.put(tableName, state);
190 }
191 }
192
193
194
195
196 @Override
197 public boolean isTableState(final TableName tableName,
198 final ZooKeeperProtos.Table.State... states) {
199 return isTableState(tableName, false, states);
200 }
201
202
203
204
205 @Override
206 public boolean isTableState(final TableName tableName, final boolean checkSource,
207 final ZooKeeperProtos.Table.State... states) {
208 boolean isTableInSpecifiedState;
209 synchronized (this.cache) {
210 ZooKeeperProtos.Table.State currentState = this.cache.get(tableName);
211 if (checkSource) {
212
213
214 try {
215 ZooKeeperProtos.Table.State stateInZK = getTableState(watcher, tableName);
216
217 if (currentState != stateInZK) {
218 if (stateInZK != null) {
219 this.cache.put(tableName, stateInZK);
220 } else {
221 this.cache.remove(tableName);
222 }
223 currentState = stateInZK;
224 }
225 } catch (KeeperException | InterruptedException e) {
226
227 }
228 }
229 return isTableInState(Arrays.asList(states), currentState);
230 }
231 }
232
233
234
235
236
237 @Override
238 public void setDeletedTable(final TableName tableName)
239 throws CoordinatedStateException {
240 synchronized (this.cache) {
241 if (this.cache.remove(tableName) == null) {
242 LOG.warn("Moving table " + tableName + " state to deleted but was " +
243 "already deleted");
244 }
245 try {
246 ZKUtil.deleteNodeFailSilent(this.watcher,
247 ZKUtil.joinZNode(this.watcher.tableZNode, tableName.getNameAsString()));
248 } catch (KeeperException e) {
249 throw new CoordinatedStateException(e);
250 }
251 }
252 }
253
254
255
256
257
258
259
260 @Override
261 public boolean isTablePresent(final TableName tableName) {
262 synchronized (this.cache) {
263 ZooKeeperProtos.Table.State state = this.cache.get(tableName);
264 return !(state == null);
265 }
266 }
267
268
269
270
271
272
273 @Override
274 public Set<TableName> getTablesInStates(ZooKeeperProtos.Table.State... states)
275 throws InterruptedIOException, CoordinatedStateException {
276 try {
277 return getAllTables(states);
278 } catch (KeeperException e) {
279 throw new CoordinatedStateException(e);
280 }
281 }
282
283
284
285
286 @Override
287 public void checkAndRemoveTableState(TableName tableName, ZooKeeperProtos.Table.State states,
288 boolean deletePermanentState)
289 throws CoordinatedStateException {
290 synchronized (this.cache) {
291 if (isTableState(tableName, states)) {
292 this.cache.remove(tableName);
293 if (deletePermanentState) {
294 try {
295 ZKUtil.deleteNodeFailSilent(this.watcher,
296 ZKUtil.joinZNode(this.watcher.tableZNode, tableName.getNameAsString()));
297 } catch (KeeperException e) {
298 throw new CoordinatedStateException(e);
299 }
300 }
301 }
302 }
303 }
304
305
306
307
308
309
310 Set<TableName> getAllTables(final ZooKeeperProtos.Table.State... states)
311 throws KeeperException, InterruptedIOException {
312
313 Set<TableName> allTables = new HashSet<TableName>();
314 List<String> children =
315 ZKUtil.listChildrenNoWatch(watcher, watcher.tableZNode);
316 if(children == null) return allTables;
317 for (String child: children) {
318 TableName tableName = TableName.valueOf(child);
319 ZooKeeperProtos.Table.State state;
320 try {
321 state = getTableState(watcher, tableName);
322 } catch (InterruptedException e) {
323 throw new InterruptedIOException();
324 }
325 for (ZooKeeperProtos.Table.State expectedState: states) {
326 if (state == expectedState) {
327 allTables.add(tableName);
328 break;
329 }
330 }
331 }
332 return allTables;
333 }
334
335
336
337
338
339
340
341
342 private ZooKeeperProtos.Table.State getTableState(final ZooKeeperWatcher zkw,
343 final TableName tableName)
344 throws KeeperException, InterruptedException {
345 String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
346 byte [] data = ZKUtil.getData(zkw, znode);
347 if (data == null || data.length <= 0) return null;
348 try {
349 ProtobufUtil.expectPBMagicPrefix(data);
350 ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
351 int magicLen = ProtobufUtil.lengthOfPBMagic();
352 ZooKeeperProtos.Table t = builder.mergeFrom(data, magicLen, data.length - magicLen).build();
353 return t.getState();
354 } catch (InvalidProtocolBufferException e) {
355 KeeperException ke = new KeeperException.DataInconsistencyException();
356 ke.initCause(e);
357 throw ke;
358 } catch (DeserializationException e) {
359 throw ZKUtil.convert(e);
360 }
361 }
362
363
364
365
366
367 private boolean isTableInState(final List<ZooKeeperProtos.Table.State> expectedStates,
368 final ZooKeeperProtos.Table.State currentState) {
369 return currentState != null && expectedStates.contains(currentState);
370 }
371 }