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       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       // Delete -ROOT- table state znode since its no longer present in 0.95.0
147       // onwards.
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         // This is no longer used in >=0.95.x
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 }