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  
19  package org.apache.hadoop.hbase.chaos.actions;
20  
21  import java.util.List;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.ServerName;
25  import org.apache.hadoop.hbase.util.FSUtils;
26  import org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper;
27  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
28  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
29  import org.apache.hadoop.hdfs.DFSUtil;
30  import org.apache.hadoop.hdfs.HAUtil;
31  import org.apache.hadoop.hdfs.server.namenode.ha.proto.HAZKInfoProtos.ActiveNodeInfo;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Action that tries to restart the active namenode.
37   */
38  public class RestartActiveNameNodeAction extends RestartActionBaseAction {
39    private static final Logger LOG =
40        LoggerFactory.getLogger(RestartActiveNameNodeAction.class);
41  
42    // Value taken from org.apache.hadoop.ha.ActiveStandbyElector.java, variable :- LOCK_FILENAME
43    private static final String ACTIVE_NN_LOCK_NAME = "ActiveStandbyElectorLock";
44  
45    // Value taken from org.apache.hadoop.ha.ZKFailoverController.java
46    // variable :- ZK_PARENT_ZNODE_DEFAULT and ZK_PARENT_ZNODE_KEY
47    private static final String ZK_PARENT_ZNODE_DEFAULT = "/hadoop-ha";
48    private static final String ZK_PARENT_ZNODE_KEY = "ha.zookeeper.parent-znode";
49  
50    public RestartActiveNameNodeAction(long sleepTime) {
51      super(sleepTime);
52    }
53  
54    @Override protected Logger getLogger() {
55      return LOG;
56    }
57  
58    @Override
59    public void perform() throws Exception {
60      getLogger().info("Performing action: Restart active namenode");
61      Configuration conf = FSUtils.getRootDir(getConf()).getFileSystem(getConf()).getConf();
62      String nameServiceID = DFSUtil.getNamenodeNameServiceId(conf);
63      if (!HAUtil.isHAEnabled(conf, nameServiceID)) {
64        throw new Exception("HA for namenode is not enabled");
65      }
66      ZooKeeperWatcher zkw = null;
67      RecoverableZooKeeper rzk = null;
68      String activeNamenode = null;
69      String hadoopHAZkNode = conf.get(ZK_PARENT_ZNODE_KEY, ZK_PARENT_ZNODE_DEFAULT);
70      try {
71        zkw = new ZooKeeperWatcher(conf, "get-active-namenode", null);
72        rzk = zkw.getRecoverableZooKeeper();
73        String hadoopHAZkNodePath = ZKUtil.joinZNode(hadoopHAZkNode, nameServiceID);
74        List<String> subChildern = ZKUtil.listChildrenNoWatch(zkw, hadoopHAZkNodePath);
75        for (String eachEntry : subChildern) {
76          if (eachEntry.contains(ACTIVE_NN_LOCK_NAME)) {
77            byte[] data =
78                rzk.getData(ZKUtil.joinZNode(hadoopHAZkNodePath, ACTIVE_NN_LOCK_NAME), false,
79                  null);
80            ActiveNodeInfo proto = ActiveNodeInfo.parseFrom(data);
81            activeNamenode = proto.getHostname();
82          }
83        }
84      } finally {
85        if (zkw != null) {
86          zkw.close();
87        }
88      }
89      if (activeNamenode == null) {
90        throw new Exception("No active Name node found in zookeeper under " + hadoopHAZkNode);
91      }
92      getLogger().info("Found active namenode host:" + activeNamenode);
93      ServerName activeNNHost = ServerName.valueOf(activeNamenode, -1, -1);
94      getLogger().info("Restarting Active NameNode :" + activeNamenode);
95      restartNameNode(activeNNHost, sleepTime);
96    }
97  }