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.List;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.conf.Configured;
27 import org.apache.hadoop.hbase.Abortable;
28 import org.apache.hadoop.hbase.HBaseConfiguration;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
31 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
32 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
33 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos.ReplicationPeer;
34 import org.apache.hadoop.hbase.replication.ReplicationStateZKBase;
35 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
36 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
37 import org.apache.hadoop.util.Tool;
38 import org.apache.hadoop.util.ToolRunner;
39 import org.apache.zookeeper.KeeperException;
40 import org.apache.zookeeper.KeeperException.NoNodeException;
41
42
43
44
45 public class ZKDataMigrator extends Configured implements Tool {
46
47 private static final Log LOG = LogFactory.getLog(ZKDataMigrator.class);
48
49 @Override
50 public int run(String[] as) throws Exception {
51 Configuration conf = getConf();
52 ZooKeeperWatcher zkw = null;
53 try {
54 zkw = new ZooKeeperWatcher(getConf(), "Migrate ZK data to PB.",
55 new ZKDataMigratorAbortable());
56 if (ZKUtil.checkExists(zkw, zkw.baseZNode) == -1) {
57 LOG.info("No hbase related data available in zookeeper. returning..");
58 return 0;
59 }
60 List<String> children = ZKUtil.listChildrenNoWatch(zkw, zkw.baseZNode);
61 if (children == null) {
62 LOG.info("No child nodes to mirgrate. returning..");
63 return 0;
64 }
65 String childPath = null;
66 for (String child : children) {
67 childPath = ZKUtil.joinZNode(zkw.baseZNode, child);
68 if (child.equals(conf.get("zookeeper.znode.rootserver", "root-region-server"))) {
69
70
71 ZKUtil.deleteNodeRecursively(zkw, childPath);
72
73 } else if (child.equals(conf.get("zookeeper.znode.rs", "rs"))) {
74
75
76 ZKUtil.deleteNodeRecursively(zkw, childPath);
77 } else if (child.equals(conf.get("zookeeper.znode.draining.rs", "draining"))) {
78
79
80
81
82 ZKUtil.deleteNodeRecursively(zkw, childPath);
83 } else if (child.equals(conf.get("zookeeper.znode.master", "master"))) {
84
85
86 ZKUtil.deleteNodeRecursively(zkw, childPath);
87 } else if (child.equals(conf.get("zookeeper.znode.backup.masters", "backup-masters"))) {
88
89
90 ZKUtil.deleteNodeRecursively(zkw, childPath);
91 } else if (child.equals(conf.get("zookeeper.znode.state", "shutdown"))) {
92
93
94 ZKUtil.deleteNodeRecursively(zkw, childPath);
95 } else if (child.equals(conf.get("zookeeper.znode.unassigned", "unassigned"))) {
96
97
98
99
100 ZKUtil.deleteNodeRecursively(zkw, childPath);
101 } else if (child.equals(conf.get("zookeeper.znode.tableEnableDisable", "table"))
102 || child.equals(conf.get("zookeeper.znode.masterTableEnableDisable", "table"))) {
103 checkAndMigrateTableStatesToPB(zkw);
104 } else if (child.equals(conf.get("zookeeper.znode.masterTableEnableDisable92",
105 "table92"))) {
106
107
108 ZKUtil.deleteNodeRecursively(zkw, childPath);
109 } else if (child.equals(conf.get("zookeeper.znode.splitlog", "splitlog"))) {
110
111
112 ZKUtil.deleteNodeRecursively(zkw, childPath);
113 } else if (child.equals(conf.get("zookeeper.znode.replication", "replication"))) {
114 checkAndMigrateReplicationNodesToPB(zkw);
115 } else if (child.equals(conf.get("zookeeper.znode.clusterId", "hbaseid"))) {
116
117 ZKUtil.deleteNodeRecursively(zkw, childPath);
118 } else if (child.equals(SnapshotManager.ONLINE_SNAPSHOT_CONTROLLER_DESCRIPTION)) {
119
120 ZKUtil.deleteNodeRecursively(zkw, childPath);
121 } else if (child.equals(conf.get("zookeeper.znode.acl.parent", "acl"))) {
122
123 ZKUtil.deleteNodeRecursively(zkw, childPath);
124 }
125 }
126 } catch (Exception e) {
127 LOG.error("Got exception while updating znodes ", e);
128 throw new IOException(e);
129 } finally {
130 if (zkw != null) {
131 zkw.close();
132 }
133 }
134 return 0;
135 }
136
137 private void checkAndMigrateTableStatesToPB(ZooKeeperWatcher zkw) throws KeeperException,
138 InterruptedException {
139 List<String> tables = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
140 if (tables == null) {
141 LOG.info("No table present to migrate table state to PB. returning..");
142 return;
143 }
144 for (String table : tables) {
145 String znode = ZKUtil.joinZNode(zkw.tableZNode, table);
146
147
148 if (table.equals("-ROOT-") || table.equals(".META.")) {
149 ZKUtil.deleteNode(zkw, znode);
150 continue;
151 }
152 byte[] data = ZKUtil.getData(zkw, znode);
153 if (ProtobufUtil.isPBMagicPrefix(data)) continue;
154 ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
155 builder.setState(ZooKeeperProtos.Table.State.valueOf(Bytes.toString(data)));
156 data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
157 ZKUtil.setData(zkw, znode, data);
158 }
159 }
160
161 private void checkAndMigrateReplicationNodesToPB(ZooKeeperWatcher zkw) throws KeeperException,
162 InterruptedException {
163 String replicationZnodeName = getConf().get("zookeeper.znode.replication", "replication");
164 String replicationPath = ZKUtil.joinZNode(zkw.baseZNode, replicationZnodeName);
165 List<String> replicationZnodes = ZKUtil.listChildrenNoWatch(zkw, replicationPath);
166 if (replicationZnodes == null) {
167 LOG.info("No replication related znodes present to migrate. returning..");
168 return;
169 }
170 for (String child : replicationZnodes) {
171 String znode = ZKUtil.joinZNode(replicationPath, child);
172 if (child.equals(getConf().get("zookeeper.znode.replication.peers", "peers"))) {
173 List<String> peers = ZKUtil.listChildrenNoWatch(zkw, znode);
174 if (peers == null || peers.isEmpty()) {
175 LOG.info("No peers present to migrate. returning..");
176 continue;
177 }
178 checkAndMigratePeerZnodesToPB(zkw, znode, peers);
179 } else if (child.equals(getConf().get("zookeeper.znode.replication.state", "state"))) {
180
181 ZKUtil.deleteNodeRecursively(zkw, znode);
182 } else if (child.equals(getConf().get("zookeeper.znode.replication.rs", "rs"))) {
183 List<String> rsList = ZKUtil.listChildrenNoWatch(zkw, znode);
184 if (rsList == null || rsList.isEmpty()) continue;
185 for (String rs : rsList) {
186 checkAndMigrateQueuesToPB(zkw, znode, rs);
187 }
188 }
189 }
190 }
191
192 private void checkAndMigrateQueuesToPB(ZooKeeperWatcher zkw, String znode, String rs)
193 throws KeeperException, NoNodeException, InterruptedException {
194 String rsPath = ZKUtil.joinZNode(znode, rs);
195 List<String> peers = ZKUtil.listChildrenNoWatch(zkw, rsPath);
196 if (peers == null || peers.isEmpty()) return;
197 String peerPath = null;
198 for (String peer : peers) {
199 peerPath = ZKUtil.joinZNode(rsPath, peer);
200 List<String> files = ZKUtil.listChildrenNoWatch(zkw, peerPath);
201 if (files == null || files.isEmpty()) continue;
202 String filePath = null;
203 for (String file : files) {
204 filePath = ZKUtil.joinZNode(peerPath, file);
205 byte[] data = ZKUtil.getData(zkw, filePath);
206 if (data == null || Bytes.equals(data, HConstants.EMPTY_BYTE_ARRAY)) continue;
207 if (ProtobufUtil.isPBMagicPrefix(data)) continue;
208 ZKUtil.setData(zkw, filePath,
209 ZKUtil.positionToByteArray(Long.parseLong(Bytes.toString(data))));
210 }
211 }
212 }
213
214 private void checkAndMigratePeerZnodesToPB(ZooKeeperWatcher zkw, String znode,
215 List<String> peers) throws KeeperException, NoNodeException, InterruptedException {
216 for (String peer : peers) {
217 String peerZnode = ZKUtil.joinZNode(znode, peer);
218 byte[] data = ZKUtil.getData(zkw, peerZnode);
219 if (!ProtobufUtil.isPBMagicPrefix(data)) {
220 migrateClusterKeyToPB(zkw, peerZnode, data);
221 }
222 String peerStatePath = ZKUtil.joinZNode(peerZnode,
223 getConf().get("zookeeper.znode.replication.peers.state", "peer-state"));
224 if (ZKUtil.checkExists(zkw, peerStatePath) != -1) {
225 data = ZKUtil.getData(zkw, peerStatePath);
226 if (ProtobufUtil.isPBMagicPrefix(data)) continue;
227 migratePeerStateToPB(zkw, data, peerStatePath);
228 }
229 }
230 }
231
232 private void migrateClusterKeyToPB(ZooKeeperWatcher zkw, String peerZnode, byte[] data)
233 throws KeeperException, NoNodeException {
234 ReplicationPeer peer = ZooKeeperProtos.ReplicationPeer.newBuilder()
235 .setClusterkey(Bytes.toString(data)).build();
236 ZKUtil.setData(zkw, peerZnode, ProtobufUtil.prependPBMagic(peer.toByteArray()));
237 }
238
239 private void migratePeerStateToPB(ZooKeeperWatcher zkw, byte[] data,
240 String peerStatePath)
241 throws KeeperException, NoNodeException {
242 String state = Bytes.toString(data);
243 if (ZooKeeperProtos.ReplicationState.State.ENABLED.name().equals(state)) {
244 ZKUtil.setData(zkw, peerStatePath, ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
245 } else if (ZooKeeperProtos.ReplicationState.State.DISABLED.name().equals(state)) {
246 ZKUtil.setData(zkw, peerStatePath, ReplicationStateZKBase.DISABLED_ZNODE_BYTES);
247 }
248 }
249
250 public static void main(String args[]) throws Exception {
251 System.exit(ToolRunner.run(HBaseConfiguration.create(), new ZKDataMigrator(), args));
252 }
253
254 static class ZKDataMigratorAbortable implements Abortable {
255 private boolean aborted = false;
256
257 @Override
258 public void abort(String why, Throwable e) {
259 LOG.error("Got aborted with reason: " + why + ", and error: " + e);
260 this.aborted = true;
261 }
262
263 @Override
264 public boolean isAborted() {
265 return this.aborted;
266 }
267 }
268 }