0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 usage="Usage: spark-daemon.sh [--config <conf-dir>] (start|stop|submit|status) <spark-command> <spark-instance-number> <args...>"
0034
0035
0036 if [ $
0037 echo $usage
0038 exit 1
0039 fi
0040
0041 if [ -z "${SPARK_HOME}" ]; then
0042 export SPARK_HOME="$(cd "`dirname "$0"`"/..; pwd)"
0043 fi
0044
0045 . "${SPARK_HOME}/sbin/spark-config.sh"
0046
0047
0048
0049
0050
0051
0052 if [ "$1" == "--config" ]
0053 then
0054 shift
0055 conf_dir="$1"
0056 if [ ! -d "$conf_dir" ]
0057 then
0058 echo "ERROR : $conf_dir is not a directory"
0059 echo $usage
0060 exit 1
0061 else
0062 export SPARK_CONF_DIR="$conf_dir"
0063 fi
0064 shift
0065 fi
0066
0067 option=$1
0068 shift
0069 command=$1
0070 shift
0071 instance=$1
0072 shift
0073
0074 spark_rotate_log ()
0075 {
0076 log=$1;
0077 num=5;
0078 if [ -n "$2" ]; then
0079 num=$2
0080 fi
0081 if [ -f "$log" ]; then
0082 while [ $num -gt 1 ]; do
0083 prev=`expr $num - 1`
0084 [ -f "$log.$prev" ] && mv "$log.$prev" "$log.$num"
0085 num=$prev
0086 done
0087 mv "$log" "$log.$num";
0088 fi
0089 }
0090
0091 . "${SPARK_HOME}/bin/load-spark-env.sh"
0092
0093 if [ "$SPARK_IDENT_STRING" = "" ]; then
0094 export SPARK_IDENT_STRING="$USER"
0095 fi
0096
0097
0098 export SPARK_PRINT_LAUNCH_COMMAND="1"
0099
0100
0101 if [ "$SPARK_LOG_DIR" = "" ]; then
0102 export SPARK_LOG_DIR="${SPARK_HOME}/logs"
0103 fi
0104 mkdir -p "$SPARK_LOG_DIR"
0105 touch "$SPARK_LOG_DIR"/.spark_test > /dev/null 2>&1
0106 TEST_LOG_DIR=$?
0107 if [ "${TEST_LOG_DIR}" = "0" ]; then
0108 rm -f "$SPARK_LOG_DIR"/.spark_test
0109 else
0110 chown "$SPARK_IDENT_STRING" "$SPARK_LOG_DIR"
0111 fi
0112
0113 if [ "$SPARK_PID_DIR" = "" ]; then
0114 SPARK_PID_DIR=/tmp
0115 fi
0116
0117
0118 log="$SPARK_LOG_DIR/spark-$SPARK_IDENT_STRING-$command-$instance-$HOSTNAME.out"
0119 pid="$SPARK_PID_DIR/spark-$SPARK_IDENT_STRING-$command-$instance.pid"
0120
0121
0122 if [ "$SPARK_NICENESS" = "" ]; then
0123 export SPARK_NICENESS=0
0124 fi
0125
0126 execute_command() {
0127 if [ -z ${SPARK_NO_DAEMONIZE+set} ]; then
0128 nohup -- "$@" >> $log 2>&1 < /dev/null &
0129 newpid="$!"
0130
0131 echo "$newpid" > "$pid"
0132
0133
0134 for i in {1..10}
0135 do
0136 if [[ $(ps -p "$newpid" -o comm=) =~ "java" ]]; then
0137 break
0138 fi
0139 sleep 0.5
0140 done
0141
0142 sleep 2
0143
0144 if [[ ! $(ps -p "$newpid" -o comm=) =~ "java" ]]; then
0145 echo "failed to launch: $@"
0146 tail -10 "$log" | sed 's/^/ /'
0147 echo "full log in $log"
0148 fi
0149 else
0150 "$@"
0151 fi
0152 }
0153
0154 run_command() {
0155 mode="$1"
0156 shift
0157
0158 mkdir -p "$SPARK_PID_DIR"
0159
0160 if [ -f "$pid" ]; then
0161 TARGET_ID="$(cat "$pid")"
0162 if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
0163 echo "$command running as process $TARGET_ID. Stop it first."
0164 exit 1
0165 fi
0166 fi
0167
0168 if [ "$SPARK_MASTER" != "" ]; then
0169 echo rsync from "$SPARK_MASTER"
0170 rsync -a -e ssh --delete --exclude=.svn --exclude='logs/*' --exclude='contrib/hod/logs/*' "$SPARK_MASTER/" "${SPARK_HOME}"
0171 fi
0172
0173 spark_rotate_log "$log"
0174 echo "starting $command, logging to $log"
0175
0176 case "$mode" in
0177 (class)
0178 execute_command nice -n "$SPARK_NICENESS" "${SPARK_HOME}"/bin/spark-class "$command" "$@"
0179 ;;
0180
0181 (submit)
0182 execute_command nice -n "$SPARK_NICENESS" bash "${SPARK_HOME}"/bin/spark-submit --class "$command" "$@"
0183 ;;
0184
0185 (*)
0186 echo "unknown mode: $mode"
0187 exit 1
0188 ;;
0189 esac
0190
0191 }
0192
0193 case $option in
0194
0195 (submit)
0196 run_command submit "$@"
0197 ;;
0198
0199 (start)
0200 run_command class "$@"
0201 ;;
0202
0203 (stop)
0204
0205 if [ -f $pid ]; then
0206 TARGET_ID="$(cat "$pid")"
0207 if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
0208 echo "stopping $command"
0209 kill "$TARGET_ID" && rm -f "$pid"
0210 else
0211 echo "no $command to stop"
0212 fi
0213 else
0214 echo "no $command to stop"
0215 fi
0216 ;;
0217
0218 (status)
0219
0220 if [ -f $pid ]; then
0221 TARGET_ID="$(cat "$pid")"
0222 if [[ $(ps -p "$TARGET_ID" -o comm=) =~ "java" ]]; then
0223 echo $command is running.
0224 exit 0
0225 else
0226 echo $pid file is present but $command not running
0227 exit 1
0228 fi
0229 else
0230 echo $command not running.
0231 exit 2
0232 fi
0233 ;;
0234
0235 (*)
0236 echo $usage
0237 exit 1
0238 ;;
0239
0240 esac
0241
0242