2017-01-31 19:53:05 +00:00
|
|
|
#!/bin/sh
|
2008-12-08 06:42:56 +00:00
|
|
|
### BEGIN INIT INFO
|
2017-01-31 19:53:05 +00:00
|
|
|
# Provides: clickhouse-server
|
2016-05-31 02:40:03 +00:00
|
|
|
# Default-Start: 2 3 4 5
|
|
|
|
# Default-Stop: 0 1 6
|
2016-05-27 20:29:45 +00:00
|
|
|
# Required-Start:
|
|
|
|
# Required-Stop:
|
2017-01-31 19:53:05 +00:00
|
|
|
# Short-Description: Yandex clickhouse-server daemon
|
2008-12-08 06:42:56 +00:00
|
|
|
### END INIT INFO
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
|
|
|
|
CLICKHOUSE_USER=clickhouse
|
|
|
|
CLICKHOUSE_GROUP=${CLICKHOUSE_USER}
|
2014-12-11 16:55:25 +00:00
|
|
|
SHELL=/bin/bash
|
2017-01-31 19:53:05 +00:00
|
|
|
PROGRAM=clickhouse-server
|
2008-12-08 06:42:56 +00:00
|
|
|
SYSCONFDIR=/etc/$PROGRAM
|
2017-02-03 13:52:59 +00:00
|
|
|
CLICKHOUSE_LOGDIR=/var/log/clickhouse-server
|
2017-01-31 19:53:05 +00:00
|
|
|
CLICKHOUSE_LOGDIR_USER=root
|
|
|
|
CLICKHOUSE_DATADIR=/var/lib/$CLICKHOUSE_USER
|
|
|
|
CLICKHOUSE_DATADIR_OLD=/opt/clickhouse
|
2008-12-08 06:42:56 +00:00
|
|
|
LOCALSTATEDIR=/var/lock
|
|
|
|
BINDIR=/usr/bin
|
2017-01-31 19:53:05 +00:00
|
|
|
CLICKHOUSE_CRONFILE=/etc/cron.d/clickhouse-server
|
|
|
|
CLICKHOUSE_CONFIG=$SYSCONFDIR/config.xml
|
2008-12-08 06:42:56 +00:00
|
|
|
LOCKFILE=$LOCALSTATEDIR/$PROGRAM
|
|
|
|
RETVAL=0
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
CLICKHOUSE_PIDDIR=/var/run/$PROGRAM
|
|
|
|
CLICKHOUSE_PIDFILE="$CLICKHOUSE_PIDDIR/$PROGRAM.pid"
|
2014-12-09 12:44:50 +00:00
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
# Override defaults from optional config file
|
|
|
|
test -f /etc/default/clickhouse && . /etc/default/clickhouse
|
2014-12-09 12:44:50 +00:00
|
|
|
|
2016-06-10 13:07:37 +00:00
|
|
|
# On x86_64, check for required instruction set.
|
2016-06-10 19:36:35 +00:00
|
|
|
if uname -mpi | grep -q 'x86_64'; then
|
|
|
|
if ! grep -q 'sse4_2' /proc/cpuinfo; then
|
|
|
|
# On KVM, cpuinfo could falsely not report SSE 4.2 support, so skip the check.
|
2016-06-10 20:31:24 +00:00
|
|
|
if ! grep -q 'Common KVM processor' /proc/cpuinfo; then
|
2016-06-26 05:17:26 +00:00
|
|
|
|
|
|
|
# Some other VMs also report wrong flags in cpuinfo.
|
|
|
|
# Tricky way to test for instruction set:
|
|
|
|
# create temporary binary and run it;
|
|
|
|
# if it get caught illegal instruction signal,
|
|
|
|
# then required instruction set is not supported really.
|
|
|
|
#
|
|
|
|
# Generated this way:
|
|
|
|
# gcc -xc -Os -static -nostdlib - <<< 'void _start() { __asm__("pcmpgtq %%xmm0, %%xmm1; mov $0x3c, %%rax; xor %%rdi, %%rdi; syscall":::"memory"); }' && strip -R .note.gnu.build-id -R .comment -R .eh_frame -s ./a.out && gzip -c -9 ./a.out | base64 -w0; echo
|
|
|
|
|
|
|
|
if ! (echo -n 'H4sICAwAW1cCA2Eub3V0AKt39XFjYmRkgAEmBjsGEI+H0QHMd4CKGyCUAMUsGJiBJDNQNUiYlQEZOKDQclB9cnD9CmCSBYqJBRxQOvBpSQobGfqIAWn8FuYnPI4fsAGyPQz/87MeZtArziguKSpJTGLQK0mtKGGgGHADMSgoYH6AhTMPNHyE0NQzYuEzYzEXFr6CBPQDANAsXKTwAQAA' | base64 -d | gzip -d > /tmp/clickhouse_test_sse42 && chmod a+x /tmp/clickhouse_test_sse42 && /tmp/clickhouse_test_sse42); then
|
2017-01-31 19:53:05 +00:00
|
|
|
echo 'Warning! SSE 4.2 instruction set is not supported'
|
|
|
|
#exit 3
|
2016-06-26 05:17:26 +00:00
|
|
|
fi
|
2016-06-10 19:36:35 +00:00
|
|
|
fi
|
2016-06-10 13:07:37 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
SUPPORTED_COMMANDS="{start|stop|status|restart|forcestop|forcerestart|reload|condstart|condstop|condrestart|condreload|initdb}"
|
2015-03-25 11:36:46 +00:00
|
|
|
is_supported_command()
|
|
|
|
{
|
|
|
|
echo $SUPPORTED_COMMANDS | grep -E "(\{|\|)$1(\||})" &> /dev/null
|
|
|
|
}
|
|
|
|
|
2014-12-03 12:09:00 +00:00
|
|
|
generate_program_name()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
echo $PROGRAM
|
2014-12-03 12:09:00 +00:00
|
|
|
}
|
2014-11-28 16:47:02 +00:00
|
|
|
|
|
|
|
generate_pid_name()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
echo $CLICKHOUSE_PIDFILE
|
2014-11-28 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 12:44:50 +00:00
|
|
|
specific_log_file_for_each_process()
|
2014-11-28 16:47:02 +00:00
|
|
|
{
|
2014-12-09 12:44:50 +00:00
|
|
|
echo ""
|
2014-11-28 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is_running()
|
|
|
|
{
|
|
|
|
pidfile=$1
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
[ -r $pidfile ] && pgrep -s $(cat "$pidfile") 1> /dev/null 2> /dev/null
|
2014-11-28 16:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
running_processes()
|
|
|
|
{
|
|
|
|
running=0
|
2017-01-31 19:53:05 +00:00
|
|
|
if is_running $CLICKHOUSE_PIDFILE; then
|
|
|
|
running=$(($running + 1))
|
|
|
|
fi
|
2014-11-28 16:47:02 +00:00
|
|
|
echo $running
|
|
|
|
}
|
|
|
|
|
|
|
|
any_runs()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
if [ $(running_processes) -gt 0 ]; then return 0; else return 1; fi
|
2008-12-08 06:42:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 11:35:14 +00:00
|
|
|
all_runs()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
[ $(running_processes) -eq 1 ]
|
2015-02-10 11:35:14 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 06:42:56 +00:00
|
|
|
wait4done()
|
|
|
|
{
|
2014-11-28 16:47:02 +00:00
|
|
|
while any_runs; do
|
2008-12-08 06:42:56 +00:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
|
|
|
|
initdb()
|
|
|
|
{
|
|
|
|
if ! getent group ${CLICKHOUSE_USER} >/dev/null; then
|
|
|
|
echo "Can't chown to non-existing user ${CLICKHOUSE_USER}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if ! getent passwd ${CLICKHOUSE_GROUP} >/dev/null; then
|
|
|
|
echo "Can't chown to non-existing group ${CLICKHOUSE_GROUP}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! `su -s $SHELL ${CLICKHOUSE_USER} -c "test -r ${CLICKHOUSE_CONFIG}"`; then
|
|
|
|
echo "Warning! clickhouse config [${CLICKHOUSE_CONFIG}] not readable by user [${CLICKHOUSE_USER}]"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! `su -s $SHELL ${CLICKHOUSE_USER} -c "test -O ${CLICKHOUSE_DATADIR} && test -G ${CLICKHOUSE_DATADIR}"`; then
|
|
|
|
echo "Changing owner of [${CLICKHOUSE_DATADIR}] to [${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP}]"
|
|
|
|
chown -R ${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP} ${CLICKHOUSE_DATADIR}
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! `su -s $SHELL ${CLICKHOUSE_USER} -c "test -w ${CLICKHOUSE_LOGDIR}"`; then
|
2017-02-15 16:11:16 +00:00
|
|
|
echo "Changing owner of [${CLICKHOUSE_LOGDIR}/*] to [${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP}]"
|
|
|
|
chown -R ${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP} ${CLICKHOUSE_LOGDIR}/*
|
2017-01-31 19:53:05 +00:00
|
|
|
echo "Changing owner of [${CLICKHOUSE_LOGDIR}] to [${CLICKHOUSE_LOGDIR_USER}:${CLICKHOUSE_GROUP}]"
|
2017-02-15 16:11:16 +00:00
|
|
|
chown ${CLICKHOUSE_LOGDIR_USER}:${CLICKHOUSE_GROUP} ${CLICKHOUSE_LOGDIR}
|
2017-01-31 19:53:05 +00:00
|
|
|
fi
|
|
|
|
|
2017-02-14 12:11:46 +00:00
|
|
|
# Temporary fix for old metrika user, remove after 2017-06-01
|
2017-02-13 19:46:59 +00:00
|
|
|
if [ ! -z ${CLICKHOUSE_DATADIR_OLD} ] && [ -d ${CLICKHOUSE_DATADIR_OLD} ] && ! `su -s $SHELL ${CLICKHOUSE_USER} -c "test -w ${CLICKHOUSE_DATADIR_OLD}"` ; then
|
2017-01-31 19:53:05 +00:00
|
|
|
echo "Changing owner of old [${CLICKHOUSE_DATADIR_OLD}] to [${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP}]"
|
|
|
|
chown -R ${CLICKHOUSE_USER}:${CLICKHOUSE_GROUP} ${CLICKHOUSE_DATADIR_OLD}
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2008-12-08 06:42:56 +00:00
|
|
|
start()
|
|
|
|
{
|
2011-03-21 14:16:55 +00:00
|
|
|
[ -x $BINDIR/$PROGRAM ] || exit 0
|
2008-12-08 06:42:56 +00:00
|
|
|
local EXIT_STATUS
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
|
|
|
echo -n "Start $PROGRAM service: "
|
2016-05-11 20:36:11 +00:00
|
|
|
ulimit -n 262144
|
2008-12-08 06:42:56 +00:00
|
|
|
|
2015-02-10 11:35:14 +00:00
|
|
|
if all_runs; then
|
2008-12-08 06:42:56 +00:00
|
|
|
echo -n "already running "
|
|
|
|
EXIT_STATUS=1
|
|
|
|
else
|
2017-01-31 19:53:05 +00:00
|
|
|
mkdir -p $CLICKHOUSE_PIDDIR
|
|
|
|
chown -R $CLICKHOUSE_USER:$CLICKHOUSE_GROUP $CLICKHOUSE_PIDDIR
|
|
|
|
initdb
|
|
|
|
if ! is_running $CLICKHOUSE_PIDFILE; then
|
|
|
|
rm -f $CLICKHOUSE_PIDFILE
|
2014-12-29 18:52:50 +00:00
|
|
|
# чтобы лок не удерживался в течении времени жизни дочернего процесса, освободим лок
|
2017-01-31 19:53:05 +00:00
|
|
|
su -l $CLICKHOUSE_USER -s $SHELL -c "flock -u 9; exec -a $(generate_program_name $i) \"$BINDIR/$PROGRAM\" --daemon --pid-file=\"$CLICKHOUSE_PIDFILE\" --config-file=\"$CLICKHOUSE_CONFIG\" $(specific_log_file_for_each_process $i)"
|
2014-11-28 16:47:02 +00:00
|
|
|
EXIT_STATUS=$?
|
2017-01-31 19:53:05 +00:00
|
|
|
if [ $EXIT_STATUS -ne 0 ]; then
|
2014-11-28 16:47:02 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
2008-12-08 06:42:56 +00:00
|
|
|
fi
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
if [ $EXIT_STATUS -eq 0 ]; then
|
2008-12-08 06:42:56 +00:00
|
|
|
echo "DONE"
|
|
|
|
else
|
|
|
|
echo "FAILED"
|
|
|
|
fi
|
|
|
|
|
|
|
|
return $EXIT_STATUS
|
|
|
|
}
|
|
|
|
|
|
|
|
stop()
|
|
|
|
{
|
|
|
|
local EXIT_STATUS
|
2008-12-18 11:07:42 +00:00
|
|
|
EXIT_STATUS=0
|
2008-12-08 06:42:56 +00:00
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
if [ -f $CLICKHOUSE_PIDFILE ]; then
|
2008-12-08 06:42:56 +00:00
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
echo -n "Stop $PROGRAM service: "
|
2014-12-29 17:31:30 +00:00
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
kill -TERM `cat "$CLICKHOUSE_PIDFILE"`
|
2008-12-08 06:42:56 +00:00
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
wait4done
|
|
|
|
|
|
|
|
echo "DONE"
|
|
|
|
fi
|
2008-12-08 06:42:56 +00:00
|
|
|
return $EXIT_STATUS
|
|
|
|
}
|
|
|
|
|
|
|
|
restart()
|
|
|
|
{
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2010-03-09 19:01:07 +00:00
|
|
|
forcestop()
|
|
|
|
{
|
|
|
|
local EXIT_STATUS
|
|
|
|
EXIT_STATUS=0
|
|
|
|
|
|
|
|
echo -n "Stop $PROGRAM service: "
|
|
|
|
|
2017-01-31 19:53:05 +00:00
|
|
|
kill -9 `cat "$CLICKHOUSE_PIDFILE"`
|
2014-12-29 17:31:30 +00:00
|
|
|
|
|
|
|
wait4done
|
2010-03-09 19:01:07 +00:00
|
|
|
|
|
|
|
echo "DONE"
|
|
|
|
return $EXIT_STATUS
|
|
|
|
}
|
|
|
|
|
|
|
|
forcerestart()
|
|
|
|
{
|
|
|
|
forcestop
|
|
|
|
start
|
|
|
|
}
|
|
|
|
|
2009-04-03 14:48:11 +00:00
|
|
|
enable_cron()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
[ ! -z $CLICKHOUSE_CRONFILE ] && sed -i 's/^#*//' "$CLICKHOUSE_CRONFILE"
|
2009-04-03 14:48:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
disable_cron()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
[ ! -z $CLICKHOUSE_CRONFILE ] && sed -i 's/^#*/#/' "$CLICKHOUSE_CRONFILE"
|
2009-04-03 14:48:11 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 17:31:30 +00:00
|
|
|
is_cron_disabled()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
[ -z $CLICKHOUSE_CRONFILE ] && return 0
|
|
|
|
[[ `grep -E "^#.*" $CLICKHOUSE_CRONFILE` == `cat $CLICKHOUSE_CRONFILE` ]];
|
2014-12-29 17:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
# See how we were called.
|
|
|
|
EXIT_STATUS=0
|
|
|
|
case "$1" in
|
2008-12-08 06:42:56 +00:00
|
|
|
start)
|
2009-04-06 08:32:43 +00:00
|
|
|
start && enable_cron
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
|
|
|
stop)
|
2009-04-03 14:48:11 +00:00
|
|
|
disable_cron && stop
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
|
|
|
restart)
|
2009-04-10 13:21:55 +00:00
|
|
|
restart && enable_cron
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
2010-03-09 19:01:07 +00:00
|
|
|
forcestop)
|
|
|
|
disable_cron && forcestop
|
|
|
|
;;
|
|
|
|
forcerestart)
|
|
|
|
forcerestart && enable_cron
|
|
|
|
;;
|
2008-12-08 06:42:56 +00:00
|
|
|
reload)
|
|
|
|
restart
|
|
|
|
;;
|
|
|
|
condstart)
|
2015-02-10 11:35:14 +00:00
|
|
|
all_runs || start
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
|
|
|
condstop)
|
2014-11-28 16:47:02 +00:00
|
|
|
any_runs && stop
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
|
|
|
condrestart)
|
2014-11-28 16:47:02 +00:00
|
|
|
any_runs && restart
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
|
|
|
condreload)
|
2014-11-28 16:47:02 +00:00
|
|
|
any_runs && restart
|
2008-12-08 06:42:56 +00:00
|
|
|
;;
|
2017-01-31 19:53:05 +00:00
|
|
|
initdb)
|
|
|
|
initdb
|
|
|
|
;;
|
2014-12-29 17:31:30 +00:00
|
|
|
esac
|
|
|
|
|
|
|
|
exit $EXIT_STATUS
|
|
|
|
}
|
2008-12-08 06:42:56 +00:00
|
|
|
|
2015-03-24 15:28:02 +00:00
|
|
|
status()
|
|
|
|
{
|
2017-01-31 19:53:05 +00:00
|
|
|
if [ $(running_processes) -eq 1 ]; then
|
2015-03-24 15:28:02 +00:00
|
|
|
echo "$PROGRAM service is running"
|
|
|
|
else
|
|
|
|
if is_cron_disabled; then
|
|
|
|
echo "$PROGRAM service is stopped";
|
|
|
|
else
|
2017-02-03 13:52:59 +00:00
|
|
|
echo "$PROGRAM: process unexpectedly terminated"
|
2015-03-24 15:28:02 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
# Running commands whith not need blocking
|
2015-03-25 11:36:46 +00:00
|
|
|
if ! is_supported_command "$1"; then
|
|
|
|
echo "Usage: ${0##*/} $SUPPORTED_COMMANDS"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2017-02-03 13:52:59 +00:00
|
|
|
if [ "$1" == "status" ]; then
|
2015-03-24 15:28:02 +00:00
|
|
|
status
|
|
|
|
exit 0
|
|
|
|
fi
|
2015-03-25 11:36:46 +00:00
|
|
|
|
2014-12-29 17:31:30 +00:00
|
|
|
(
|
2014-12-29 18:52:50 +00:00
|
|
|
if flock -n 9; then
|
|
|
|
main "$@"
|
|
|
|
else
|
|
|
|
echo "Init script is already running" && exit 1
|
|
|
|
fi
|
2014-12-29 17:31:30 +00:00
|
|
|
) 9> $LOCKFILE
|