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
12 # * http://www.apache.org/licenses/LICENSE-2.0
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.
21 # cleans hbase related data from zookeeper and hdfs if no hbase process is alive.
23 # Environment Variables
25 # HBASE_REGIONSERVERS File naming remote hosts.
26 # Default is ${HADOOP_CONF_DIR}/regionservers
27 # HADOOP_CONF_DIR Alternate conf dir. Default is ${HADOOP_HOME}/conf.
28 # HBASE_CONF_DIR Alternate hbase conf dir. Default is ${HBASE_HOME}/conf.
29 # HBASE_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
30 # HBASE_SLAVE_TIMEOUT Seconds to wait for timing out a remote command.
31 # HBASE_SSH_OPTS Options passed to ssh when running remote commands.
34 usage
="Usage: hbase-cleanup.sh (--cleanZk|--cleanHdfs|--cleanAll|--cleanAcls)"
37 bin
=`cd "$bin">/dev/null; pwd`
39 # This will set HBASE_HOME, etc.
40 .
"$bin"/hbase-config.sh
43 --cleanZk|
--cleanHdfs|
--cleanAll|
--cleanAcls)
47 if [ $# -ne 1 -o "$matches" = "" ]; then
54 zparent
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent`
55 if [ "$zparent" == "null" ]; then zparent
="/hbase"; fi
57 hrootdir
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool hbase.rootdir`
58 if [ "$hrootdir" == "null" ]; then hrootdir
="file:///tmp/hbase-${USER}/hbase"; fi
60 hbasewaldir
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool hbase.wal.dir`
66 zchild
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs`
67 if [ "$zchild" == "null" ]; then zchild
="rs"; fi
70 zchild
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.backup.masters`
71 if [ "$zchild" == "null" ]; then zchild
="backup-masters"; fi
74 znodes
=`"$bin"/hbase zkcli ls $zparent/$zchild 2>&1 | tail -1 | sed "s/\[//" | sed "s/\]//"`
75 if [ "$znodes" != "" ]; then
76 echo -n "ZNode(s) [${znodes}] of $command are not expired. Exiting without cleaning hbase data."
80 echo -n "All ZNode(s) of $command are expired."
85 execute_zk_command
() {
87 "$bin"/hbase zkcli
$command 2>&1
90 execute_hdfs_command
() {
92 "$bin"/hbase org.apache.hadoop.fs.FsShell
$command 2>&1
95 execute_clean_acls
() {
97 "$bin"/hbase org.apache.hadoop.hbase.zookeeper.ZkAclReset
$command 2>&1
103 execute_zk_command
"rmr ${zparent}";
106 execute_hdfs_command
"-rm -R ${hrootdir}"
107 if [ "${hbasewaldir}" != "null" ]; then
108 execute_hdfs_command
"-rm -R ${hbasewaldir}"
112 execute_zk_command
"rmr ${zparent}";
113 execute_hdfs_command
"-rm -R ${hrootdir}"
114 if [ "${hbasewaldir}" != "null" ]; then
115 execute_hdfs_command
"-rm -R ${hbasewaldir}"
126 check_znode_exists
() {
128 "$bin"/hbase zkcli stat
$command 2>&1 |
grep "Node does not exist\|Connection refused"
131 check_znode_exists
$zparent
132 if [ $?
-ne 0 ]; then
133 # make sure the online region server(s) znode(s) have been deleted before continuing
134 check_for_znodes regionservers
135 # make sure the backup master(s) znode(s) has been deleted before continuing
136 check_for_znodes backupmasters
137 # make sure the master znode has been deleted before continuing
138 zmaster
=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.master`
139 if [ "$zmaster" == "null" ]; then zmaster
="master"; fi
140 zmaster
=$zparent/$zmaster
141 check_znode_exists
$zmaster
142 if [ $?
-ne 0 ]; then
143 echo -n "Master ZNode is not expired. Exiting without cleaning hbase data."
144 echo #force a new line
147 echo "Active Master ZNode also expired."
149 echo #force a newline
151 echo "HBase parent znode ${zparent} does not exist."
154 # cleans zookeeper and/or hdfs data.
155 clean_up
$format_option