#!/bin/sh
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon for mysql

### BEGIN INIT INFO
# Provides: mysql-proxy 
# Short-Description: start and stop mysql proxy
# Description: mysql-proxy is the user (i.e. mysql-client) frontend for \
#              Qserv czar service. \
#              It receive SQL queries, process it using lua plugin, \
#              and send it to Qserv czar. \
#              Once Qserv czar have returned the results, mysql-proxy \
#              sends it to mysql-client. \
### END INIT INFO

DIR=$(cd "$(dirname "$0")"; pwd -P)

# Source functions library.
. ${DIR}/qserv-functions

NAME="mysql-proxy"
DAEMON=${NAME}

source_sysconfig ${DIR}

# Check variables which are not controlled by application
check_writable ${NAME} "QSERV_RUN_DIR"
check_writable ${NAME} "QSERV_PID_DIR"
check_writable ${NAME} "QSERV_LOG_DIR"
check_readable ${NAME} "LUA_DIR"
check_readable ${NAME} "QSERV_DIR"

prime_qserv() {
    # This query is expected to fail but it causes the proxy to load the qserv czar and start it.
    # This is important as qserv-czar cannot register itself until this happens.
    # It also reduces the amount of time needed to answer the first query to a czar significantly.
    # TODO: See if changing the proxy to load qserv when it starts instead of waiting for a query
    #       is reasonable and appropriate.
    ${MYSQL_DIR}/bin/mysql -uqsmaster -P4040 -h127.0.0.1 -e "SELECT dummyCol FROM dummyDb.dummyTb LIMIT 1"
}

# These parameters should be in mysql-proxy configuration file
err_msg="Failure while starting ${NAME}:"
if [ -z "${QSERV_UNIX_USER}" ]; then
    log_failure_msg "${local_err_msg} \$QSERV_UNIX_USER is an empty \
string"
    exit 1
fi

# Set default mysql-proxy configuration.
LUA_SHARE_DIR="${LUA_DIR}/share/lua/5.1"
LUA_LIB_DIR="${LUA_DIR}/lib/lua/5.1"
PROXY_OPTIONS="--daemon \
        --proxy-lua-script=${QSERV_DIR}/share/lua/qserv/mysqlProxy.lua \
        --lua-path=\"${LUA_SHARE_DIR}/?.lua;${LUA_SHARE_DIR}/?/init.lua;${LUA_SHARE_DIR}/?/?.lua\" \
        --lua-cpath=\"${QSERV_DIR}/lib/lua/qserv/czarProxy.so;${LUA_LIB_DIR}/?.so;${LUA_LIB_DIR}/?/?.so\""
pidfile=${QSERV_PID_DIR}/mysql-proxy.pid
PROXY_CONFIG=${QSERV_RUN_DIR}/etc/my-proxy.cnf
lockfile=${QSERV_RUN_DIR}/var/lock/subsys/$NAME

VALG=""
#VALG="valgrind --leak-check=full"
#VALG="valgrind --leak-check=no"
#VALG="valgrind --tool=callgrind"

LUA_LOG_FILE=${QSERV_LOG_DIR}/mysql-proxy-lua.log
logfile=${QSERV_LOG_DIR}/mysql-proxy.log
QSERV_CONFIG=${QSERV_RUN_DIR}/etc/qserv-czar.cnf

# WARNING : can only use the --user switch if running as root
proxy_user_option=""
if [ "$USER" = "root" ]; then
    proxy_user=${QSERV_UNIX_USER}
    proxy_user_option="--user=$proxy_user"
fi

# jemalloc profilling
#JEMU='LD_PRELOAD=${JEMALLOC_DIR}/lib/libjemalloc.so'
#JEMD='MALLOC_CONF=MALLOC_CONF=prof:true,lg_prof_interval:30,lg_prof_sample:17,prof_prefix:/qserv/run/var/log/jeprof'
JEMU=
JEMD=

cmd="QSERV_CONFIG=${QSERV_CONFIG} ${JEMU} ${JEMD} ${VALG} ${DAEMON} \
    $PROXY_OPTIONS --pid-file=$pidfile $proxy_user_option \
    --defaults-file=${PROXY_CONFIG} >> ${LUA_LOG_FILE} 2>&1 &"

start_cmd() {
    start -p $pidfile -l $lockfile $NAME "${cmd}"
    ret=$?
    [ $ret -eq 0 ] || echo "See startup logfile : ${logfile}"
    prime_qserv
    return $ret
}

stop_cmd() {
    stop -p $pidfile -l $lockfile $NAME
}

# See how we were called.
case "$1" in
    start)
        start_cmd
        ;;
    stop)
        stop_cmd
        ;;
    restart)
        stop_cmd
        start_cmd
        ;;
    condrestart|try-restart)
        if status_of_proc -p $pidfile "$DAEMON" "$NAME" >&/dev/null; then
            stop_cmd
            start_cmd
        fi
        ;;
    status)
        status_of_proc -p $pidfile "$DAEMON" "$NAME" && exit 0 || exit $?
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}"
        exit 2
        ;;
esac

exit $?

