View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Tool to migrate zookeeper data of older hbase versions(<0.95.0) to PB.
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            // -ROOT- region no longer present from 0.95.0, so we can remove this
70            // znode
71            ZKUtil.deleteNodeRecursively(zkw, childPath);
72            // TODO delete root table path from file system.
73          } else if (child.equals(conf.get("zookeeper.znode.rs", "rs"))) {
74            // Since there is no live region server instance during migration, we
75            // can remove this znode as well.
76            ZKUtil.deleteNodeRecursively(zkw, childPath);
77          } else if (child.equals(conf.get("zookeeper.znode.draining.rs", "draining"))) {
78            // If we want to migrate to 0.95.0 from older versions we need to stop
79            // the existing cluster. So there wont be any draining servers so we
80            // can
81            // remove it.
82            ZKUtil.deleteNodeRecursively(zkw, childPath);
83          } else if (child.equals(conf.get("zookeeper.znode.master", "master"))) {
84            // Since there is no live master instance during migration, we can
85            // remove this znode as well.
86            ZKUtil.deleteNodeRecursively(zkw, childPath);
87          } else if (child.equals(conf.get("zookeeper.znode.backup.masters", "backup-masters"))) {
88            // Since there is no live backup master instances during migration, we
89            // can remove this znode as well.
90            ZKUtil.deleteNodeRecursively(zkw, childPath);
91          } else if (child.equals(conf.get("zookeeper.znode.state", "shutdown"))) {
92            // shutdown node is not present from 0.95.0 onwards. Its renamed to
93            // "running". We can delete it.
94            ZKUtil.deleteNodeRecursively(zkw, childPath);
95          } else if (child.equals(conf.get("zookeeper.znode.unassigned", "unassigned"))) {
96            // Any way during clean cluster startup we will remove all unassigned
97            // region nodes. we can delete all children nodes as well. This znode
98            // is
99            // renamed to "regions-in-transition" from 0.95.0 onwards.
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           // This is replica of table states from tableZnode so we can remove
107           // this.
108           ZKUtil.deleteNodeRecursively(zkw, childPath);
109         } else if (child.equals(conf.get("zookeeper.znode.splitlog", "splitlog"))) {
110           // This znode no longer available from 0.95.0 onwards, we can remove
111           // it.
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           // it will be re-created by master.
117           ZKUtil.deleteNodeRecursively(zkw, childPath);
118         } else if (child.equals(SnapshotManager.ONLINE_SNAPSHOT_CONTROLLER_DESCRIPTION)) {
119           // not needed as it is transient.
120           ZKUtil.deleteNodeRecursively(zkw, childPath);
121         } else if (child.equals(conf.get("zookeeper.znode.acl.parent", "acl"))) {
122           // it will be re-created when hbase:acl is re-opened
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     List<String> tables = ZKUtil.listChildrenNoWatch(zkw, zkw.tableZNode);
139     if (tables == null) {
140       LOG.info("No table present to migrate table state to PB. returning..");
141       return;
142     }
143     for (String table : tables) {
144       String znode = ZKUtil.joinZNode(zkw.tableZNode, table);
145       // Delete -ROOT- table state znode since its no longer present in 0.95.0
146       // onwards.
147       if (table.equals("-ROOT-") || table.equals(".META.")) {
148         ZKUtil.deleteNode(zkw, znode);
149         continue;
150       }
151       byte[] data = ZKUtil.getData(zkw, znode);
152       if (ProtobufUtil.isPBMagicPrefix(data)) continue;
153       ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table.newBuilder();
154       builder.setState(ZooKeeperProtos.Table.State.valueOf(Bytes.toString(data)));
155       data = ProtobufUtil.prependPBMagic(builder.build().toByteArray());
156       ZKUtil.setData(zkw, znode, data);
157     }
158   }
159 
160   private void checkAndMigrateReplicationNodesToPB(ZooKeeperWatcher zkw) throws KeeperException {
161     String replicationZnodeName = getConf().get("zookeeper.znode.replication", "replication");
162     String replicationPath = ZKUtil.joinZNode(zkw.baseZNode, replicationZnodeName);
163     List<String> replicationZnodes = ZKUtil.listChildrenNoWatch(zkw, replicationPath);
164     if (replicationZnodes == null) {
165       LOG.info("No replication related znodes present to migrate. returning..");
166       return;
167     }
168     for (String child : replicationZnodes) {
169       String znode = ZKUtil.joinZNode(replicationPath, child);
170       if (child.equals(getConf().get("zookeeper.znode.replication.peers", "peers"))) {
171         List<String> peers = ZKUtil.listChildrenNoWatch(zkw, znode);
172         if (peers == null || peers.isEmpty()) {
173           LOG.info("No peers present to migrate. returning..");
174           continue;
175         }
176         checkAndMigratePeerZnodesToPB(zkw, znode, peers);
177       } else if (child.equals(getConf().get("zookeeper.znode.replication.state", "state"))) {
178         // This is no longer used in >=0.95.x
179         ZKUtil.deleteNodeRecursively(zkw, znode);
180       } else if (child.equals(getConf().get("zookeeper.znode.replication.rs", "rs"))) {
181         List<String> rsList = ZKUtil.listChildrenNoWatch(zkw, znode);
182         if (rsList == null || rsList.isEmpty()) continue;
183         for (String rs : rsList) {
184           checkAndMigrateQueuesToPB(zkw, znode, rs);
185         }
186       }
187     }
188   }
189 
190   private void checkAndMigrateQueuesToPB(ZooKeeperWatcher zkw, String znode, String rs)
191       throws KeeperException, NoNodeException {
192     String rsPath = ZKUtil.joinZNode(znode, rs);
193     List<String> peers = ZKUtil.listChildrenNoWatch(zkw, rsPath);
194     if (peers == null || peers.isEmpty()) return;
195     String peerPath = null;
196     for (String peer : peers) {
197       peerPath = ZKUtil.joinZNode(rsPath, peer);
198       List<String> files = ZKUtil.listChildrenNoWatch(zkw, peerPath);
199       if (files == null || files.isEmpty()) continue;
200       String filePath = null;
201       for (String file : files) {
202         filePath = ZKUtil.joinZNode(peerPath, file);
203         byte[] data = ZKUtil.getData(zkw, filePath);
204         if (data == null || Bytes.equals(data, HConstants.EMPTY_BYTE_ARRAY)) continue;
205         if (ProtobufUtil.isPBMagicPrefix(data)) continue;
206         ZKUtil.setData(zkw, filePath,
207           ZKUtil.positionToByteArray(Long.parseLong(Bytes.toString(data))));
208       }
209     }
210   }
211 
212   private void checkAndMigratePeerZnodesToPB(ZooKeeperWatcher zkw, String znode,
213       List<String> peers) throws KeeperException, NoNodeException {
214     for (String peer : peers) {
215       String peerZnode = ZKUtil.joinZNode(znode, peer);
216       byte[] data = ZKUtil.getData(zkw, peerZnode);
217       if (!ProtobufUtil.isPBMagicPrefix(data)) {
218         migrateClusterKeyToPB(zkw, peerZnode, data);
219       }
220       String peerStatePath = ZKUtil.joinZNode(peerZnode,
221         getConf().get("zookeeper.znode.replication.peers.state", "peer-state"));
222       if (ZKUtil.checkExists(zkw, peerStatePath) != -1) {
223         data = ZKUtil.getData(zkw, peerStatePath);
224         if (ProtobufUtil.isPBMagicPrefix(data)) continue;
225         migratePeerStateToPB(zkw, data, peerStatePath);
226       }
227     }
228   }
229 
230   private void migrateClusterKeyToPB(ZooKeeperWatcher zkw, String peerZnode, byte[] data)
231       throws KeeperException, NoNodeException {
232     ReplicationPeer peer = ZooKeeperProtos.ReplicationPeer.newBuilder()
233         .setClusterkey(Bytes.toString(data)).build();
234     ZKUtil.setData(zkw, peerZnode, ProtobufUtil.prependPBMagic(peer.toByteArray()));
235   }
236 
237   private void migratePeerStateToPB(ZooKeeperWatcher zkw, byte[] data,
238  String peerStatePath)
239       throws KeeperException, NoNodeException {
240     String state = Bytes.toString(data);
241     if (ZooKeeperProtos.ReplicationState.State.ENABLED.name().equals(state)) {
242       ZKUtil.setData(zkw, peerStatePath, ReplicationStateZKBase.ENABLED_ZNODE_BYTES);
243     } else if (ZooKeeperProtos.ReplicationState.State.DISABLED.name().equals(state)) {
244       ZKUtil.setData(zkw, peerStatePath, ReplicationStateZKBase.DISABLED_ZNODE_BYTES);
245     }
246   }
247 
248   public static void main(String args[]) throws Exception {
249     System.exit(ToolRunner.run(HBaseConfiguration.create(), new ZKDataMigrator(), args));
250   }
251 
252   static class ZKDataMigratorAbortable implements Abortable {
253     private boolean aborted = false;
254 
255     @Override
256     public void abort(String why, Throwable e) {
257       LOG.error("Got aborted with reason: " + why + ", and error: " + e);
258       this.aborted = true;
259     }
260 
261     @Override
262     public boolean isAborted() {
263       return this.aborted;
264     }
265   }
266 }