Skip to content

Commit

Permalink
MDEV-427/MDEV-5713 Add systemd script with notify functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
grooverdan authored and Sergey Vojtovich committed Oct 12, 2015
1 parent 92271c7 commit 20c2ae3
Show file tree
Hide file tree
Showing 23 changed files with 551 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ support-files/config.huge.ini
support-files/config.medium.ini
support-files/config.small.ini
support-files/mariadb.pc
support-files/mariadb@.service
support-files/my-huge.cnf
support-files/my-innodb-heavy-4G.cnf
support-files/my-large.cnf
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ INCLUDE(pcre)
INCLUDE(ctest)
INCLUDE(plugin)
INCLUDE(install_macros)
INCLUDE(systemd)
INCLUDE(mysql_add_executable)

# Handle options
Expand Down Expand Up @@ -328,6 +329,8 @@ CHECK_JEMALLOC()

CHECK_PCRE()

CHECK_SYSTEMD()

IF(CMAKE_CROSSCOMPILING)
SET(IMPORT_EXECUTABLES "IMPORTFILE-NOTFOUND" CACHE FILEPATH "Path to import_executables.cmake from a native build")
INCLUDE(${IMPORT_EXECUTABLES})
Expand Down
76 changes: 76 additions & 0 deletions cmake/systemd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright (c) 2015, Daniel Black. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

INCLUDE(FindPkgConfig)
# http://www.cmake.org/cmake/help/v3.0/module/FindPkgConfig.html

MACRO(CHECK_SYSTEMD)
IF(UNIX)
SET(WITH_SYSTEMD "auto" CACHE STRING "Compile with systemd socket activation and notification")
IF(WITH_SYSTEMD STREQUAL "yes" OR WITH_SYSTEMD STREQUAL "auto")
IF(PKG_CONFIG_FOUND)
IF(WITH_SYSTEMD STREQUAL "yes")
pkg_check_modules(LIBSYSTEMD REQUIRED libsystemd)
ELSE()
pkg_check_modules(LIBSYSTEMD libsystemd)
ENDIF()
IF(HAVE_DLOPEN)
SET(LIBSYSTEMD ${LIBSYSTEMD_LIBRARIES})
#SET(CMAKE_REQUIRED_FLAGS ${LIBSYSTEMD_CFLAGS})
SET(MYSQLD_LINK_FLAGS "${MYSQLD_LINK_FLAGS} ${LIBSYSTEMD_LDFLAGS}")
ELSE()
SET(LIBSYSTEMD ${LIBSYSTEMD_STATIC_LIBRARIES})
#SET(CMAKE_REQUIRED_FLAGS ${LIBSYSTEMD_STATIC_CFLAGS})
SET(MYSQLD_LINK_FLAGS "${MYSQLD_LINK_FLAGS} ${LIBSYSTEMD_STATIC_LDFLAGS}")
ENDIF()
ELSE()
SET(LIBSYSTEMD systemd)
ENDIF()
SET(CMAKE_REQUIRED_LIBRARIES ${LIBSYSTEMD})
CHECK_C_SOURCE_COMPILES(
"
#include <systemd/sd-daemon.h>
int main()
{
sd_listen_fds(0);
}"
HAVE_SYSTEMD)
CHECK_INCLUDE_FILES(systemd/sd-daemon.h HAVE_SYSTEMD_SD_DAEMON_H)
CHECK_FUNCTION_EXISTS(sd_listen_fds HAVE_SYSTEMD_SD_LISTEN_FDS)
CHECK_FUNCTION_EXISTS(sd_notify HAVE_SYSTEMD_SD_NOTIFY)
CHECK_FUNCTION_EXISTS(sd_notifyf HAVE_SYSTEMD_SD_NOTIFYF)
IF(HAVE_SYSTEMD AND HAVE_SYSTEMD_SD_DAEMON_H AND HAVE_SYSTEMD_SD_LISTEN_FDS
AND HAVE_SYSTEMD_SD_NOTIFY AND HAVE_SYSTEMD_SD_NOTIFYF)
ADD_DEFINITIONS(-DHAVE_SYSTEMD)
# should be from pkg-config --variable=systemdsystemconfdir systemd
# Missing CMake macro: http://public.kitware.com/Bug/view.php?id=15634
SET(SYSTEMD_SYSTEM_CONFDIR /etc/systemd/system)
# should be from pkg-config --variable=systemdsystemunitdir systemd
SET(SYSTEMD_SYSTEM_UNITDIR /usr/lib/systemd/system/)
MESSAGE(STATUS "Systemd features enabled")
ELSE()
UNSET(LIBSYSTEMD)
UNSET(HAVE_SYSTEMD_SD_DAEMON_H)
UNSET(HAVE_SYSTEMD_SD_LISTEN_FDS)
UNSET(HAVE_SYSTEMD_SD_NOTIFY)
UNSET(HAVE_SYSTEMD_SD_NOTIFYF)
MESSAGE(STATUS "Systemd features not enabled")
IF(WITH_SYSTEMD STREQUAL "yes")
MESSAGE(FATAL_ERROR "Requested WITH_SYSTEMD=YES however no dependencies installed/found")
ENDIF()
ENDIF()
ENDIF()
ENDIF()
ENDMACRO()
1 change: 1 addition & 0 deletions config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
#cmakedefine HAVE_LIBCRYPT 1
#cmakedefine HAVE_LIBMTMALLOC 1
#cmakedefine HAVE_LIBWRAP 1
#cmakedefine HAVE_SYSTEMD 1
/* Does "struct timespec" have a "sec" and "nsec" field? */
#cmakedefine HAVE_TIMESPEC_TS_SEC 1

Expand Down
1 change: 1 addition & 0 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Build-Depends: bison,
zlib1g-dev (>= 1:1.1.3-5~),
${MAYBE_LIBCRACK}
libjemalloc-dev (>= 3.0.0~) [linux-any]
libsystemd-daemon-dev | libsystemd-dev, dh-systemd
Standards-Version: 3.8.2
Homepage: http://mariadb.org/
Vcs-Git: https://github.com/MariaDB/server.git
Expand Down
1 change: 1 addition & 0 deletions debian/mariadb-server-10.1.files.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ usr/bin/wsrep_sst_mysqldump
usr/bin/wsrep_sst_rsync
usr/bin/wsrep_sst_xtrabackup
usr/bin/wsrep_sst_xtrabackup-v2
usr/bin/mariadb-system-convert
usr/share/doc/mariadb-server-10.1/mysqld.sym.gz
usr/share/doc/mariadb-server-10.1/INFO_SRC
usr/share/doc/mariadb-server-10.1/INFO_BIN
Expand Down
6 changes: 6 additions & 0 deletions debian/mariadb-server-10.1.postinst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ if [ "$1" = "configure" ]; then
db_go
fi

# copy out any mysqld_safe settings
systemd_conf=/etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf
if [ -x /usr/bin/mariadb-service-convert -a ! -f "${systemd_conf}" ]; then
mkdir -p /etc/systemd/system/mariadb.service.d
/usr/bin/mariadb-service-convert > "${systemd_conf}"
fi
fi

db_stop # in case invoke failes
Expand Down
9 changes: 9 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ install: build
install -m 0644 $(builddir)/Docs/INFO_SRC $(TMP)/usr/share/doc/mariadb-server-10.1/INFO_SRC
install -m 0644 $(builddir)/Docs/INFO_BIN $(TMP)/usr/share/doc/mariadb-server-10.1/INFO_BIN

# systemd helpers
install -m 0755 scripts/mariadb-service-convert $(TMP)/usr/bin/
install -d $(TMP)/etc/systemd/system/mariadb@bootstrap.service.d/
install -m 0644 $(BUILDDIR)/support-files/mariadb-bootstrap.conf \
$(TMP)/etc/systemd/system/mariadb@bootstrap.service.d/wsrep-new-cluster.conf

# mariadb-test
mv $(TMP)/usr/mysql-test $(TMP)/usr/share/mysql

Expand Down Expand Up @@ -215,7 +221,10 @@ binary-indep: build install
dh_installexamples -i
dh_installmenu -i
dh_installlogrotate -i
dh_systemd_enable -i support-files/mariadb.service
dh_systemd_enable --no-enable support-files/mariadb@.service
dh_installinit -i
dh_systemd_start -i --restart-after-upgrade mariadb.service
dh_installcron -i
dh_installman -i
dh_installinfo -i
Expand Down
17 changes: 17 additions & 0 deletions include/my_systemd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#ifndef MY_SYSTEMD_INCLUDED
#define MY_SYSTEMD_INCLUDED

#if defined(HAVE_SYSTEMD) && !defined(EMBEDDED_LIBRARY)
#include <systemd/sd-daemon.h>

#else



#define sd_notify(X, Y)
#define sd_notifyf(E, F, ...)

#endif

#endif /* MY_SYSTEMD_INCLUDED */
1 change: 1 addition & 0 deletions scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ ELSE()
mysqld_multi
mysqld_safe
${WSREP_BINARIES}
mariadb-service-convert
)
FOREACH(file ${BIN_SCRIPTS})
IF(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.sh)
Expand Down
86 changes: 86 additions & 0 deletions scripts/mariadb-service-convert
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
# Copyright (c) 2015, Daniel Black. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# PURPOSE:
#
# Used to generate a mariadb.service file based on the curent mysql/maridb settings
#
# This is to assist distro maintainers in migrating to systemd service definations from
# a user mysqld_safe settings in the my.cnf files.
#
# Redirect output to user directory like /etc/systemd/system/mariadb.service.d/migrated-from-my.cnf-settings.conf

tz_old=$TZ

get_params()
{
# does a return so needs to be wrapped in a function
# . /usr/bin/mysqld_safe --simulate
. scripts/mysqld_safe --simulate
}

get_params

echo "# converted using $0"
echo "#"
echo

echo '[Service]'

echo


if [[ ( "$user" != "root" && "$user" != "mysql" ) || "${SET_USER}" == 1 ]]; then
echo User=$user
fi


[ -n "${open_files}" ] && echo LimitNOFILE=$open_files
[ -n "${core_file_size}" ] && echo LimitCore=$core_file_size
[[ "${niceness}" -gt 0 ]] && echo Nice=$niceness
[ "${TZ}" != "${tz_old}" ] && echo Environment=\"TZ=${TZ}\"

if [ -n "$mysqld_ld_preload" ]; then
new_text="$mysqld_ld_preload"
[ -n "$LD_PRELOAD" ] && new_text="$new_text $LD_PRELOAD"
echo Environment=\"LD_PRELOAD=`shell_quote_string "$new_text"`\"
fi

if [ -n "$mysqld_ld_library_path" ]; then
new_text="$mysqld_ld_library_path"
[ -n "$LD_LIBRARY_PATH" ] && new_text="$new_text:$LD_LIBRARY_PATH"
echo Environment=\"LD_LIBRARY_PATH=`shell_quote_string "$new_text"`\"
fi

if [[ $want_syslog -eq 1 ]]; then
echo StandardError=syslog
echo SyslogFacility=daemon
echo SyslogLevel=error
echo SyslogLevelPrefix=${syslog_tag_mysqld}
fi

if [[ "${flush_caches}" -gt 0 ]]; then
echo ExecStartPre=sync
echo ExecStartPre=sysctl -q -w vm.drop_caches=3
fi

if [[ "${numa_interleave}" -gt 0 ]]; then
echo
echo ExecStart=numactl --interleave=all ${cmd} '${OPTIONS}'
echo
fi

[ -n "${CRASH_SCRIPT}" ] && echo FailureAction=${CRASH_SCRIPT}
11 changes: 9 additions & 2 deletions scripts/mysqld_safe.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mysqld_ld_library_path=
flush_caches=0
numa_interleave=0
wsrep_on=0
simulate=0

# Initial logging status: error log is not open, and not using syslog
logging=init
Expand Down Expand Up @@ -81,6 +82,7 @@ Usage: $0 [OPTIONS]
--malloc-lib=LIB Preload shared library LIB if available
--mysqld=FILE Use the specified file as mysqld
--mysqld-version=VERSION Use "mysqld-VERSION" as mysqld
--simulate Simulate the start to detect errors but don't start
--nice=NICE Set the scheduling priority of mysqld
--no-auto-restart Exit after starting mysqld
--nowatch Exit after starting mysqld
Expand Down Expand Up @@ -131,6 +133,7 @@ my_which ()
}

log_generic () {
[ $simulate -eq 1 ] && return
priority="$1"
shift

Expand Down Expand Up @@ -318,6 +321,7 @@ parse_arguments() {
MYSQLD="mysqld"
fi
;;
--simulate) simulate=1 ;;
--nice=*) niceness="$val" ;;
--nowatch|--no[-_]watch|--no[-_]auto[-_]restart) nowatch=1 ;;
--open[-_]files[-_]limit=*) open_files="$val" ;;
Expand Down Expand Up @@ -862,7 +866,7 @@ fi
#
# If there exists an old pid file, check if the daemon is already running
# Note: The switches to 'ps' may depend on your operating system
if test -f "$pid_file"
if test -f "$pid_file" && [ $simulate -eq 0 ]
then
PID=`cat "$pid_file"`
if @CHECK_PID@
Expand Down Expand Up @@ -937,7 +941,9 @@ fi
# ulimit -n 256 > /dev/null 2>&1 # Fix for BSD and FreeBSD systems
#fi


cmd="`mysqld_ld_preload_text`$NOHUP_NICENESS"
[ $simulate -eq 0 ] && cmd=''

#
# Set mysqld's memory interleave policy.
Expand All @@ -957,7 +963,7 @@ then
fi

# Launch mysqld with numactl.
cmd="$cmd numactl --interleave=all"
[ $simulate -eq 0 ] && cmd="$cmd numactl --interleave=all"
elif test $numa_interleave -eq 1
then
log_error "--numa-interleave is not supported on this platform"
Expand All @@ -971,6 +977,7 @@ do
done
cmd="$cmd $args"
# Avoid 'nohup: ignoring input' warning
[ $simulate -eq 0 ] && cmd='true'
test -n "$NOHUP_NICENESS" && cmd="$cmd < /dev/null"

log_notice "Starting $MYSQLD daemon with databases from $DATADIR"
Expand Down
3 changes: 2 additions & 1 deletion sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ TARGET_LINK_LIBRARIES(sql ${MYSQLD_STATIC_PLUGIN_LIBS}
mysys mysys_ssl dbug strings vio pcre ${LIBJEMALLOC}
${LIBWRAP} ${LIBCRYPT} ${LIBDL} ${CMAKE_THREAD_LIBS_INIT}
${WSREP_LIB}
${SSL_LIBRARIES})
${SSL_LIBRARIES}
${LIBSYSTEMD})

IF(WIN32)
SET(MYSQLD_SOURCE main.cc nt_servc.cc nt_servc.h message.rc)
Expand Down
10 changes: 10 additions & 0 deletions sql/mysqld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
#include <poll.h>
#endif

#include <my_systemd.h>

#define mysqld_charset &my_charset_latin1

/* We have HAVE_valgrind below as this speeds up the shutdown of MySQL */
Expand Down Expand Up @@ -1891,6 +1893,8 @@ static void __cdecl kill_server(int sig_ptr)
else
sql_print_error(ER_DEFAULT(ER_GOT_SIGNAL),my_progname,sig); /* purecov: inspected */

sd_notify(0, "STOPPING=1");

#ifdef HAVE_SMEM
/*
Send event to smem_event_connect_request for aborting
Expand Down Expand Up @@ -6543,6 +6547,11 @@ void handle_connections_sockets()
socket_flags=fcntl(mysql_socket_getfd(unix_sock), F_GETFL, 0);
#endif

#ifdef HAVE_SYSTEMD
sd_notify(0, "READY=1\n"
"STATUS=Taking your SQL requests now...");
#endif

DBUG_PRINT("general",("Waiting for connections."));
MAYBE_BROKEN_SYSCALL;
while (!abort_loop)
Expand Down Expand Up @@ -6757,6 +6766,7 @@ void handle_connections_sockets()
create_new_thread(thd);
set_current_thd(0);
}
sd_notify(0, "STOPPING=1");
DBUG_VOID_RETURN;
}

Expand Down
Loading

0 comments on commit 20c2ae3

Please sign in to comment.