1 /**
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package org.apache.hadoop.hbase.catalog;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.ServerName;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
27 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
28 import org.apache.zookeeper.KeeperException;
29
30 /**
31 * Makes changes to the location of <code>-ROOT-</code> in ZooKeeper.
32 */
33 public class RootLocationEditor {
34 private static final Log LOG = LogFactory.getLog(RootLocationEditor.class);
35
36 /**
37 * Deletes the location of <code>-ROOT-</code> in ZooKeeper.
38 * @param zookeeper zookeeper reference
39 * @throws KeeperException unexpected zookeeper exception
40 */
41 public static void deleteRootLocation(ZooKeeperWatcher zookeeper)
42 throws KeeperException {
43 LOG.info("Unsetting ROOT region location in ZooKeeper");
44 try {
45 // Just delete the node. Don't need any watches, only we will create it.
46 ZKUtil.deleteNode(zookeeper, zookeeper.rootServerZNode);
47 } catch(KeeperException.NoNodeException nne) {
48 // Has already been deleted
49 }
50 }
51
52 /**
53 * Sets the location of <code>-ROOT-</code> in ZooKeeper to the
54 * specified server address.
55 * @param zookeeper zookeeper reference
56 * @param location The server hosting <code>-ROOT-</code>
57 * @throws KeeperException unexpected zookeeper exception
58 */
59 public static void setRootLocation(ZooKeeperWatcher zookeeper,
60 final ServerName location)
61 throws KeeperException {
62 LOG.info("Setting ROOT region location in ZooKeeper as " + location);
63 try {
64 ZKUtil.createAndWatch(zookeeper, zookeeper.rootServerZNode,
65 Bytes.toBytes(location.toString()));
66 } catch(KeeperException.NodeExistsException nee) {
67 LOG.debug("ROOT region location already existed, updated location");
68 ZKUtil.setData(zookeeper, zookeeper.rootServerZNode,
69 Bytes.toBytes(location.toString()));
70 }
71 }
72 }