1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.util;
19
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.client.TableState;
30 import org.apache.hadoop.hbase.exceptions.DeserializationException;
31 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
33 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
35 import org.apache.zookeeper.KeeperException;
36
37
38
39
40 @InterfaceAudience.Private
41 public class ZKDataMigrator {
42
43 private static final Log LOG = LogFactory.getLog(ZKDataMigrator.class);
44
45
46
47
48
49
50
51
52
53 @Deprecated
54 public static Map<TableName, TableState.State> queryForTableStates(ZooKeeperWatcher zkw)
55 throws KeeperException, InterruptedException {
56 Map<TableName, TableState.State> rv = new HashMap<>();
57 List<String> children = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
58 if (children == null)
59 return rv;
60 for (String child: children) {
61 TableName tableName = TableName.valueOf(child);
62 ZooKeeperProtos.DeprecatedTableState.State state = getTableState(zkw, tableName);
63 TableState.State newState = TableState.State.ENABLED;
64 if (state != null) {
65 switch (state) {
66 case ENABLED:
67 newState = TableState.State.ENABLED;
68 break;
69 case DISABLED:
70 newState = TableState.State.DISABLED;
71 break;
72 case DISABLING:
73 newState = TableState.State.DISABLING;
74 break;
75 case ENABLING:
76 newState = TableState.State.ENABLING;
77 break;
78 default:
79 }
80 }
81 rv.put(tableName, newState);
82 }
83 return rv;
84 }
85
86
87
88
89
90
91
92
93 @Deprecated
94 private static ZooKeeperProtos.DeprecatedTableState.State getTableState(
95 final ZooKeeperWatcher zkw, final TableName tableName)
96 throws KeeperException, InterruptedException {
97 String znode = ZKUtil.joinZNode(zkw.tableZNode, tableName.getNameAsString());
98 byte [] data = ZKUtil.getData(zkw, znode);
99 if (data == null || data.length <= 0) return null;
100 try {
101 ProtobufUtil.expectPBMagicPrefix(data);
102 ZooKeeperProtos.DeprecatedTableState.Builder builder =
103 ZooKeeperProtos.DeprecatedTableState.newBuilder();
104 int magicLen = ProtobufUtil.lengthOfPBMagic();
105 ProtobufUtil.mergeFrom(builder, data, magicLen, data.length - magicLen);
106 return builder.getState();
107 } catch (IOException e) {
108 KeeperException ke = new KeeperException.DataInconsistencyException();
109 ke.initCause(e);
110 throw ke;
111 } catch (DeserializationException e) {
112 throw ZKUtil.convert(e);
113 }
114 }
115
116 }