HBASE-26811 Secondary replica may be disabled for read forever (#4182)
[hbase.git] / bin / hbase-cleanup.sh
blob92b40cca6ae078a994f0e727857a1f0a7c0091c5
1 #!/usr/bin/env bash
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 # */
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)"
36 bin=`dirname "$0"`
37 bin=`cd "$bin">/dev/null; pwd`
39 # This will set HBASE_HOME, etc.
40 . "$bin"/hbase-config.sh
42 case $1 in
43 --cleanZk|--cleanHdfs|--cleanAll|--cleanAcls)
44 matches="yes" ;;
45 *) ;;
46 esac
47 if [ $# -ne 1 -o "$matches" = "" ]; then
48 echo $usage
49 exit 1;
52 format_option=$1;
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`
62 check_for_znodes() {
63 command=$1;
64 case $command in
65 regionservers)
66 zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs`
67 if [ "$zchild" == "null" ]; then zchild="rs"; fi
69 backupmasters)
70 zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.backup.masters`
71 if [ "$zchild" == "null" ]; then zchild="backup-masters"; fi
73 esac
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."
77 echo #force a newline
78 exit 1;
79 else
80 echo -n "All ZNode(s) of $command are expired."
82 echo #force a newline
85 execute_zk_command() {
86 command=$1;
87 "$bin"/hbase zkcli $command 2>&1
90 execute_hdfs_command() {
91 command=$1;
92 "$bin"/hbase org.apache.hadoop.fs.FsShell $command 2>&1
95 execute_clean_acls() {
96 command=$1;
97 "$bin"/hbase org.apache.hadoop.hbase.zookeeper.ZkAclReset $command 2>&1
100 clean_up() {
101 case $1 in
102 --cleanZk)
103 execute_zk_command "deleteall ${zparent}";
105 --cleanHdfs)
106 execute_hdfs_command "-rm -R ${hrootdir}"
107 if [ "${hbasewaldir}" != "null" ]; then
108 execute_hdfs_command "-rm -R ${hbasewaldir}"
111 --cleanAll)
112 execute_zk_command "deleteall ${zparent}";
113 execute_hdfs_command "-rm -R ${hrootdir}"
114 if [ "${hbasewaldir}" != "null" ]; then
115 execute_hdfs_command "-rm -R ${hbasewaldir}"
118 --cleanAcls)
119 execute_clean_acls;
123 esac
126 check_znode_exists() {
127 command=$1
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
145 exit 1
146 else
147 echo "Active Master ZNode also expired."
149 echo #force a newline
150 else
151 echo "HBase parent znode ${zparent} does not exist."
154 # cleans zookeeper and/or hdfs data.
155 clean_up $format_option