1
0
mirror of https://github.com/MariaDB/server.git synced 2025-07-30 16:24:05 +03:00

Patch for IM in scope of working on BUG#24415: Instance manager test

im_daemon_life_cycle fails randomly.

1. Move IM-angel functionality into a separate file, create Angel class.
2. Be more verbose;
3. Fix typo in FLUSH INSTANCES implementation;
4. Polishing.


mysql-test/r/im_options.result:
  Updated result file.
mysql-test/t/im_cmd_line.imtest:
  Updated test.
server-tools/instance-manager/IMService.cpp:
  Move HandleServiceOptions() into IMService::main().
server-tools/instance-manager/IMService.h:
  Move HandleServiceOptions() into IMService::main().
server-tools/instance-manager/Makefile.am:
  Added angel.cc and angel.h.
server-tools/instance-manager/WindowsService.cpp:
  Initialize class-members in constructor.
server-tools/instance-manager/WindowsService.h:
  Initialize class-members in constructor.
server-tools/instance-manager/commands.cc:
  Return actual error code (ER_OUT_OF_RESOURCES or ER_THERE_IS_ACTIVE_INSTANCE)
  from FLUSH INSTANCES.
server-tools/instance-manager/manager.cc:
  1. Return actual error code from Manager::flush_instances().
  2. Be more verbose.
server-tools/instance-manager/manager.h:
  Return actual error code from Manager::flush_instances().
server-tools/instance-manager/mysqlmanager.cc:
  Move IM-angel functionality into separate file (angel.cc).
server-tools/instance-manager/priv.cc:
  Use return bool datatype instead int{ 0, 1 }.
server-tools/instance-manager/priv.h:
  Use return bool datatype instead int{ 0, 1 }.
server-tools/instance-manager/angel.cc:
  IM-angel functionality.
server-tools/instance-manager/angel.h:
  IM-angel functionality.
This commit is contained in:
unknown
2007-02-18 15:45:28 +03:00
parent 64a6018594
commit f2f1e4d852
15 changed files with 655 additions and 353 deletions

View File

@ -17,140 +17,138 @@
#include <my_sys.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef __WIN__
#include <pwd.h>
#include <grp.h>
#include <sys/wait.h>
#endif
#include "angel.h"
#include "log.h"
#include "manager.h"
#include "options.h"
#include "priv.h"
#include "user_management_commands.h"
#ifdef __WIN__
#include "IMService.h"
#include "WindowsService.h"
#endif
/*
Few notes about Instance Manager architecture:
Instance Manager consisits of two processes: the angel process, and the
instance manager process. Responsibilities of the angel process is to
monitor the instance manager process, and restart it in case of
failure/shutdown. The angel process is started only if startup option
'--run-as-service' is provided.
The Instance Manager process consists of several
subsystems (thread sets):
- the signal handling thread: it's responsibilities are to handle
user signals and propogate them to the other threads. All other threads
are accounted in the signal handler thread Thread Registry.
- the listener: listens all sockets. There is a listening
socket for each (mysql, http, snmp, rendezvous (?)) subsystem.
- mysql subsystem: Instance Manager acts like an ordinary MySQL Server,
but with very restricted command set. Each MySQL client connection is
handled in a separate thread. All MySQL client connections threads
constitute mysql subsystem.
- http subsystem: it is also possible to talk with Instance Manager via
http. One thread per http connection is used. Threads are pooled.
- 'snmp' connections (FIXME: I know nothing about it yet)
- rendezvous threads
Instance Manager consists of two processes: the angel process (IM-angel),
and the manager process (IM-main). Responsibilities of IM-angel is to
monitor IM-main, and restart it in case of failure/shutdown. IM-angel is
started only if startup option '--run-as-service' is provided.
IM-main consists of several subsystems (thread sets):
- the signal handling thread
The signal thread handles user signals and propagates them to the
other threads. All other threads are accounted in the signal handler
thread Thread Registry.
- the listener
The listener listens to all sockets. There is a listening socket for
each subsystem (TCP/IP, UNIX socket).
- mysql subsystem
Instance Manager acts like an ordinary MySQL Server, but with very
restricted command set. Each MySQL client connection is handled in a
separate thread. All MySQL client connections threads constitute
mysql subsystem.
*/
static void init_environment(char *progname);
static int im_main(int argc, char *argv[]);
#ifndef __WIN__
static void daemonize(const char *log_file_name);
static void angel();
static struct passwd *check_user(const char *user);
static int set_user(const char *user, struct passwd *user_info);
static struct passwd *check_user();
static bool switch_user();
#endif
/*
main, entry point
- init environment
- handle options
- daemonize and run angel process (if necessary)
- run manager process
*/
/************************************************************************/
/**
The entry point.
*************************************************************************/
int main(int argc, char *argv[])
{
int return_value= 1;
init_environment(argv[0]);
int return_value;
if ((return_value= Options::load(argc, argv)))
goto main_end;
/* Initialize. */
if (Options::User_management::cmd)
{
return_value= Options::User_management::cmd->execute();
MY_INIT(argv[0]);
log_init();
umask(0117);
srand((unsigned int) time(0));
goto main_end;
}
/* Main function. */
#ifndef __WIN__
log_info("IM: started.");
struct passwd *user_info;
return_value= im_main(argc, argv);
if ((user_info= check_user(Options::Daemon::user)))
{
if (set_user(Options::Daemon::user, user_info))
{
return_value= 1;
goto main_end;
}
}
log_info("IM: finished.");
if (Options::Daemon::run_as_service)
{
/* forks, and returns only in child */
daemonize(Options::Daemon::log_file_name);
/* forks again, and returns only in child: parent becomes angel */
angel();
}
/* Cleanup. */
(void) Manager::main(); /* ignore the return value for now */
#else
if (!Options::Service::stand_alone)
{
if (HandleServiceOptions())
{
return_value= 1;
goto main_end;
}
}
else
{
(void) Manager::main(); /* ignore the return value for now */
}
#endif
return_value= 0;
main_end:
Options::cleanup();
my_end(0);
return return_value;
}
/******************* Auxilary functions implementation **********************/
/************************************************************************/
/**
Instance Manager main functionality.
*************************************************************************/
int im_main(int argc, char *argv[])
{
int rc;
if ((rc= Options::load(argc, argv)))
return rc;
if (Options::User_management::cmd)
return Options::User_management::cmd->execute();
#ifndef __WIN__
if (switch_user())
return 1;
return Options::Daemon::run_as_service ?
Angel::main() :
Manager::main();
#else
return Options::Service::stand_alone ?
Manager::main() :
IMService::main();
#endif
}
/**************************************************************************
OS-specific functions implementation.
**************************************************************************/
#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
/* Change to run as another user if started with --user */
static struct passwd *check_user(const char *user)
/************************************************************************/
/**
Change to run as another user if started with --user.
*************************************************************************/
static struct passwd *check_user()
{
const char *user= Options::Daemon::user;
struct passwd *user_info;
uid_t user_id= geteuid();
@ -195,200 +193,36 @@ err:
return NULL;
}
static int set_user(const char *user, struct passwd *user_info)
/************************************************************************/
/**
Switch user.
*************************************************************************/
static bool switch_user()
{
DBUG_ASSERT(user_info);
struct passwd *user_info= check_user();
if (!user_info)
return FALSE;
#ifdef HAVE_INITGROUPS
initgroups((char*) user,user_info->pw_gid);
initgroups(Options::Daemon::user, user_info->pw_gid);
#endif
if (setgid(user_info->pw_gid) == -1)
{
log_error("setgid() failed");
return 1;
return TRUE;
}
if (setuid(user_info->pw_uid) == -1)
{
log_error("setuid() failed");
return 1;
}
return 0;
}
#endif
/*
Init environment, common for daemon and non-daemon
*/
static void init_environment(char *progname)
{
MY_INIT(progname);
log_init();
umask(0117);
srand((unsigned int) time(0));
}
#ifndef __WIN__
/*
Become a UNIX service
SYNOPSIS
daemonize()
*/
static void daemonize(const char *log_file_name)
{
pid_t pid= fork();
switch (pid) {
case -1: // parent, fork error
die("daemonize(): fork failed, %s", strerror(errno));
case 0: // child, fork ok
int fd;
/*
Become a session leader: setsid must succeed because child is
guaranteed not to be a process group leader (it belongs to the
process group of the parent.)
The goal is not to have a controlling terminal.
*/
setsid();
/*
As we now don't have a controlling terminal we will not receive
tty-related signals - no need to ignore them.
*/
close(STDIN_FILENO);
fd= open(log_file_name, O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0)
die("daemonize(): failed to open log file %s, %s", log_file_name,
strerror(errno));
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd != STDOUT_FILENO && fd != STDERR_FILENO)
close(fd);
/* TODO: chroot() and/or chdir() here */
break;
default:
/* successfully exit from parent */
exit(0);
}
}
enum { CHILD_OK= 0, CHILD_NEED_RESPAWN, CHILD_EXIT_ANGEL };
static volatile sig_atomic_t child_status= CHILD_OK;
/*
Signal handler for SIGCHLD: reap child, analyze child exit status, and set
child_status appropriately.
*/
void reap_child(int __attribute__((unused)) signo)
{
int child_exit_status;
/* As we have only one child, no need to cycle waitpid */
if (waitpid(0, &child_exit_status, WNOHANG) > 0)
{
if (WIFSIGNALED(child_exit_status))
child_status= CHILD_NEED_RESPAWN;
else
/*
As reap_child is not called for SIGSTOP, we should be here only
if the child exited normally.
*/
child_status= CHILD_EXIT_ANGEL;
}
}
static volatile sig_atomic_t is_terminated= 0;
/*
Signal handler for terminate signals - SIGTERM, SIGHUP, SIGINT.
Set termination status and return.
(q) do we need to handle SIGQUIT?
*/
void terminate(int signo)
{
is_terminated= signo;
}
/*
Fork a child and monitor it.
User can explicitly kill the angel process with SIGTERM/SIGHUP/SIGINT.
Angel process will exit silently if mysqlmanager exits normally.
*/
static void angel()
{
/* install signal handlers */
sigset_t zeromask; // to sigsuspend in parent
struct sigaction sa_chld, sa_term;
struct sigaction sa_chld_out, sa_term_out, sa_int_out, sa_hup_out;
sigemptyset(&zeromask);
sigemptyset(&sa_chld.sa_mask);
sigemptyset(&sa_term.sa_mask);
sa_chld.sa_handler= reap_child;
sa_chld.sa_flags= SA_NOCLDSTOP;
sa_term.sa_handler= terminate;
sa_term.sa_flags= 0;
/* sigaction can fail only on wrong arguments */
sigaction(SIGCHLD, &sa_chld, &sa_chld_out);
sigaction(SIGTERM, &sa_term, &sa_term_out);
sigaction(SIGINT, &sa_term, &sa_int_out);
sigaction(SIGHUP, &sa_term, &sa_hup_out);
/* spawn a child */
spawn:
pid_t pid= fork();
switch (pid) {
case -1:
die("angel(): fork failed, %s", strerror(errno));
case 0: // child, success
/*
restore default actions for signals to let the manager work with
signals as he wishes
*/
sigaction(SIGCHLD, &sa_chld_out, 0);
sigaction(SIGTERM, &sa_term_out, 0);
sigaction(SIGINT, &sa_int_out, 0);
sigaction(SIGHUP, &sa_hup_out, 0);
/* Here we return to main, and fall into manager */
break;
default: // parent, success
pid= getpid(); /* Get our pid. */
log_info("Angel pid file: '%s'; PID: %d.",
(const char *) Options::Daemon::angel_pid_file_name,
(int) pid);
create_pid_file(Options::Daemon::angel_pid_file_name, pid);
while (child_status == CHILD_OK && is_terminated == 0)
sigsuspend(&zeromask);
if (is_terminated)
log_info("angel got signal %d, exiting", is_terminated);
else if (child_status == CHILD_NEED_RESPAWN)
{
child_status= CHILD_OK;
log_error("angel(): mysqlmanager exited abnormally: respawning...");
sleep(1); /* don't respawn too fast */
goto spawn;
}
/*
mysqlmanager successfully exited, let's silently evaporate
If we return to main we will fall into the manager functionality,
so let's simply exit().
*/
exit(0);
return TRUE;
}
return FALSE;
}
#endif