forked from arp7/HadoopTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdfs_balancer.sh
168 lines (150 loc) · 4.55 KB
/
hdfs_balancer.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/bash
# ==============================================================================
# HDFS Balancer Script
# Based on the INPUT value this script automatically pulls
# the top / under utilized hosts for HDFS balancing
# ==============================================================================
# Usage Function
usage ()
{
echo -e "Usage: $BASENAME {start|stop|status}"
}
# Global Declaration
GLOBAL ()
{
BASENAME=$(basename $0)
PID_FILE="/tmp/$BASENAME.pid"
PID=`cat $PID_FILE`
HDFS="/usr/bin/hdfs"
LOGDIR="/var/log/hdfs-balancer"
LOG="$LOGDIR/balancer.log"
}
# Start the Balancer service
start ()
{
USER_CK
GLOBAL
echo -n "Starting $0"
PIDFILE
echo $$ > ${PID_FILE}
# PICK_NODES
export INPUT=4 # Top X && Bottom X nodes = 2 x X Nodes would be balanced.
HDFS_KEYTAB
mkdir -p /var/log/hdfs-balancer
while :
do
GET_HOSTS
# SLOW_BALANCER_RUN
FAST_BALANCER_RUN
sleep 5
done
}
# Stop the Balancer service
stop ()
{
USER_CK
GLOBAL
printf "Stopping $0: \n"
PRE_CK
BALANCER=`ps -ef | grep $0 | grep -v grep | awk '{print $2}' | head -n1`
true > $PID_FILE
kill -9 $BALANCER
}
# Status on the Balancer Run
status ()
{
USER_CK
GLOBAL
if [[ $(ps -ef | grep $0 | egrep -v 'status|grep') > /dev/null ]]; then
echo -e "`ps -p $PID | tail -1 | awk '{print $NF}'` Running..."
egrep 'Executing|^Time' $LOG | tail -2
grep 'M' $LOG | tail -1 | grep -v ^Time
echo -e "\nFetching Current Utilization Status... Please Wait...\n"
CURRENT=`grep Executing $LOG | tail -1 | awk '{print $NF}' | perl -i -pe 's/\,/|/g'`
echo -e "`$HDFS dfsadmin -report | egrep '^DFS Remaining%|^Hostname' | sed 'N;s/\n/ /' | egrep "$CURRENT"`\n"
else
echo -e "`hostname`: Balancer Stopped"
fi
}
# Ensure the existence of PID file
PIDFILE ()
{
if [[ ! -e $PID_FILE ]]; then
touch $PID_FILE
fi
}
# Pick the Number of nodes to balance
PICK_NODES ()
{
printf "INFO: The below value would Twice the Number of Input Nodes\n"
printf "Enter the Number of Hosts to Balance and press [ENTER]: "
read INPUT
}
## Previlege User Check
USER_CK ()
{
if ! [[ $(whoami) =~ ^(hdfs)$ ]]; then
echo "Must be a hdfs user to run $0"
exit 1;
fi
}
## Pre-Check
PRE_CK ()
{
BPID=`ps -ef | grep balancer.Balancer | grep -v grep | awk '{print $2}' | tail -n1`
if [[ "" != "$BPID" ]]; then
echo -e "\n`date`: Found !!! Balancer Running on `hostname`. Terminating..."
kill -9 $BPID
else
echo -e "\n`hostname`: Balancer Not Running"
fi
}
# Generating the Keytabs
HDFS_KEYTAB ()
{
/usr/bin/kinit -kt /etc/security/keytabs/hdfs.headless.keytab $(/usr/bin/klist -kt /etc/security/keytabs/hdfs.headless.keytab | tail -1 | awk '{print $NF}')
}
# Picking the Top and Under utilized host and excludes the Dead node
GET_HOSTS ()
{
TOPX=`hdfs dfsadmin -report | egrep '^DFS Remaining%|^Hostname' | sed 'N;s/\n/ /' | sort -n -k 5,5 | grep -vwE "(0)" |head -n $INPUT | awk '{print $2}' | paste -s -d, -`
BOTTOMX=`hdfs dfsadmin -report | egrep '^DFS Remaining%|^Hostname' | sed 'N;s/\n/ /' | sort -n -k 5,5 | grep -vwE "(0)" |tail -n $INPUT | awk '{print $2}' | paste -s -d, -`
HOSTS="${TOPX},${BOTTOMX}"
}
# Run the Balancer
SLOW_BALANCER_RUN ()
{
PRE_CK
echo "INFO: Executing SLOW_BALANCER_RUN"
echo -e "`date`: Executing the balancer between $HOSTS" >> $LOG
HDFS_KEYTAB
$HDFS balancer -Ddfs.datanode.balance.bandwidthPerSec=104857600 -Ddfs.balancer.max-size-to-move=10737418240 -DFS.datanode.balance.max.concurrent.moves=10 -threshold 5 -include $HOSTS 1>>$LOG
}
### The Below Balancer runs in full throttle
FAST_BALANCER_RUN ()
{
PRE_CK
echo "INFO: Executing FAST_BALANCER_RUN"
echo -e "`date`: Executing the balancer against $HOSTS" >> $LOG
HDFS_KEYTAB
$HDFS balancer -Ddfs.datanode.balance.bandwidthPerSec=1048576000 -Ddfs.balancer.max-size-to-move=53687091200 -DFS.datanode.balance.max.concurrent.moves=10 -Ddfs.balancer.getBlocks.size=2147483648 -Ddfs.balancer.getBlocks.min-block-size=10485760 -threshold 4 -include $HOSTS 1>>$LOG
}
#----------------------------------------------------------------------
# Main Logic
#----------------------------------------------------------------------
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
usage
exit 4
;;
esac
exit 0