2004-10-23 11:32:52 +04:00
|
|
|
#ifndef INCLUDES_MYSQL_INSTANCE_MANAGER_GUARDIAN_H
|
|
|
|
#define INCLUDES_MYSQL_INSTANCE_MANAGER_GUARDIAN_H
|
|
|
|
/* Copyright (C) 2004 MySQL AB
|
|
|
|
|
|
|
|
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; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
2006-05-18 18:57:50 +04:00
|
|
|
#include "thread_registry.h"
|
2006-11-17 16:11:04 +03:00
|
|
|
#include <my_sys.h>
|
|
|
|
#include <my_list.h>
|
2006-05-18 18:57:50 +04:00
|
|
|
|
2005-09-23 21:28:56 +03:00
|
|
|
#if defined(__GNUC__) && defined(USE_PRAGMA_INTERFACE)
|
2004-10-23 11:32:52 +04:00
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
more fixes for IM to substitude mysqld_safe in startup scripts
BitKeeper/deleted/.del-thread_repository.cc~bba09f64f8cb4037:
Delete: server-tools/instance-manager/thread_repository.cc
BitKeeper/deleted/.del-thread_repository.h~e6a3b9cab7a6612a:
Delete: server-tools/instance-manager/thread_repository.h
server-tools/instance-manager/commands.cc:
All instances are guarded by default now, so we need to perform check on whether the instance is nonguarded,
rather then guarded when adding it to the list of guarded instnces.
server-tools/instance-manager/guardian.cc:
Guardian rewritten to start instances by default, and shut them down, when exiting. Behaviour of the guardian
in case of the instance crash has changed. Now it tries to restart an instance constantly in the first 2
seconds after the crash was noticed, and then it tries restart an instance once in the MONITORING_INTERVAL.
If it failed to restart instance for "restart_retry" (compiled-in value) times, guardian stops trying to
restart it.
server-tools/instance-manager/guardian.h:
Several new functions and variables declared.
server-tools/instance-manager/instance.cc:
now start doesn't call stop(), but rather tries to remove the pidfile itself
server-tools/instance-manager/instance.h:
cleanup
server-tools/instance-manager/instance_map.cc:
no more "admin" options
server-tools/instance-manager/instance_map.h:
User and password purged from instance_map options, as IM shouldn't know them
server-tools/instance-manager/instance_options.cc:
new option added -- shutdown_delay, guarded option now called nonguaded and has the opposite meaning
server-tools/instance-manager/instance_options.h:
appropriate changes, reflecting options addition/removal
server-tools/instance-manager/manager.cc:
shutdown process is complicated a bit -- at first signal thread should stop guardian, and only then the IM
itself
server-tools/instance-manager/messages.cc:
update error message
server-tools/instance-manager/options.cc:
admin user/password purged from mysqlmanager options
2005-01-31 23:54:08 +03:00
|
|
|
class Instance;
|
2004-10-23 11:32:52 +04:00
|
|
|
class Instance_map;
|
2005-02-11 14:21:59 +03:00
|
|
|
class Thread_registry;
|
|
|
|
struct GUARD_NODE;
|
2004-10-23 11:32:52 +04:00
|
|
|
|
2006-11-17 16:11:04 +03:00
|
|
|
/**
|
2004-10-23 11:32:52 +04:00
|
|
|
The guardian thread is responsible for monitoring and restarting of guarded
|
|
|
|
instances.
|
|
|
|
*/
|
|
|
|
|
2006-11-17 16:11:04 +03:00
|
|
|
class Guardian: public Thread
|
2004-10-23 11:32:52 +04:00
|
|
|
{
|
|
|
|
public:
|
2005-02-27 18:41:34 +03:00
|
|
|
/* states of an instance */
|
2005-08-29 23:29:35 +04:00
|
|
|
enum enum_instance_state { NOT_STARTED= 1, STARTING, STARTED, JUST_CRASHED,
|
|
|
|
CRASHED, CRASHED_AND_ABANDONED, STOPPING };
|
2005-02-27 18:41:34 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
The Guardian list node structure. Guardian utilizes it to store
|
|
|
|
guarded instances plus some additional info.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct GUARD_NODE
|
|
|
|
{
|
|
|
|
Instance *instance;
|
|
|
|
/* state of an instance (i.e. STARTED, CRASHED, etc.) */
|
2005-08-29 23:29:35 +04:00
|
|
|
enum_instance_state state;
|
2005-02-27 18:41:34 +03:00
|
|
|
/* the amount of attemts to restart instance (cleaned up at success) */
|
|
|
|
int restart_counter;
|
|
|
|
/* triggered at a crash */
|
|
|
|
time_t crash_moment;
|
|
|
|
/* General time field. Used to provide timeouts (at shutdown and restart) */
|
|
|
|
time_t last_checked;
|
|
|
|
};
|
|
|
|
|
2006-05-18 18:57:50 +04:00
|
|
|
/* Return client state name. */
|
|
|
|
static const char *get_instance_state_name(enum_instance_state state);
|
2005-02-27 18:41:34 +03:00
|
|
|
|
2006-11-17 16:11:04 +03:00
|
|
|
Guardian(Thread_registry *thread_registry_arg,
|
|
|
|
Instance_map *instance_map_arg,
|
|
|
|
uint monitoring_interval_arg);
|
|
|
|
virtual ~Guardian();
|
2005-02-27 18:41:34 +03:00
|
|
|
/* Initialize or refresh the list of guarded instances */
|
2005-02-11 14:21:59 +03:00
|
|
|
int init();
|
|
|
|
/* Request guardian shutdown. Stop instances if needed */
|
2006-10-20 22:26:40 +04:00
|
|
|
void request_shutdown();
|
2005-02-11 14:21:59 +03:00
|
|
|
/* Start instance protection */
|
2005-02-27 18:41:34 +03:00
|
|
|
int guard(Instance *instance, bool nolock= FALSE);
|
2005-02-11 14:21:59 +03:00
|
|
|
/* Stop instance protection */
|
2004-10-26 23:22:12 +04:00
|
|
|
int stop_guard(Instance *instance);
|
2006-05-18 18:57:50 +04:00
|
|
|
/* Returns TRUE if guardian thread is stopped */
|
2005-02-11 14:21:59 +03:00
|
|
|
int is_stopped();
|
2005-08-05 14:35:30 +04:00
|
|
|
void lock();
|
|
|
|
void unlock();
|
2004-10-26 23:22:12 +04:00
|
|
|
|
2006-05-18 18:57:50 +04:00
|
|
|
/*
|
|
|
|
Return an internal list node for the given instance if the instance is
|
|
|
|
managed by Guardian. Otherwise, return NULL.
|
|
|
|
|
|
|
|
MT-NOTE: must be called under acquired lock.
|
|
|
|
*/
|
|
|
|
LIST *find_instance_node(Instance *instance);
|
|
|
|
|
|
|
|
/* The operation is used to check if the instance is active or not. */
|
|
|
|
bool is_active(Instance *instance);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Return state of the given instance list node. The pointer must specify
|
|
|
|
a valid list node.
|
|
|
|
*/
|
|
|
|
inline enum_instance_state get_instance_state(LIST *instance_node);
|
2006-11-17 16:11:04 +03:00
|
|
|
protected:
|
|
|
|
/* Main funtion of the thread */
|
|
|
|
virtual void run();
|
2006-05-18 18:57:50 +04:00
|
|
|
|
2005-01-25 13:54:56 +03:00
|
|
|
public:
|
|
|
|
pthread_cond_t COND_guardian;
|
|
|
|
|
2004-10-26 23:22:12 +04:00
|
|
|
private:
|
2005-02-11 14:21:59 +03:00
|
|
|
/* Prepares Guardian shutdown. Stops instances is needed */
|
2006-10-20 22:26:40 +04:00
|
|
|
int stop_instances();
|
2005-02-11 14:21:59 +03:00
|
|
|
/* check instance state and act accordingly */
|
|
|
|
void process_instance(Instance *instance, GUARD_NODE *current_node,
|
|
|
|
LIST **guarded_instances, LIST *elem);
|
2006-05-18 18:57:50 +04:00
|
|
|
|
2005-02-11 14:21:59 +03:00
|
|
|
int stopped;
|
2004-10-23 11:32:52 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
pthread_mutex_t LOCK_guardian;
|
|
|
|
Thread_info thread_info;
|
2006-11-17 16:11:04 +03:00
|
|
|
int monitoring_interval;
|
|
|
|
Thread_registry *thread_registry;
|
|
|
|
Instance_map *instance_map;
|
2004-10-23 11:32:52 +04:00
|
|
|
LIST *guarded_instances;
|
|
|
|
MEM_ROOT alloc;
|
more fixes for IM to substitude mysqld_safe in startup scripts
BitKeeper/deleted/.del-thread_repository.cc~bba09f64f8cb4037:
Delete: server-tools/instance-manager/thread_repository.cc
BitKeeper/deleted/.del-thread_repository.h~e6a3b9cab7a6612a:
Delete: server-tools/instance-manager/thread_repository.h
server-tools/instance-manager/commands.cc:
All instances are guarded by default now, so we need to perform check on whether the instance is nonguarded,
rather then guarded when adding it to the list of guarded instnces.
server-tools/instance-manager/guardian.cc:
Guardian rewritten to start instances by default, and shut them down, when exiting. Behaviour of the guardian
in case of the instance crash has changed. Now it tries to restart an instance constantly in the first 2
seconds after the crash was noticed, and then it tries restart an instance once in the MONITORING_INTERVAL.
If it failed to restart instance for "restart_retry" (compiled-in value) times, guardian stops trying to
restart it.
server-tools/instance-manager/guardian.h:
Several new functions and variables declared.
server-tools/instance-manager/instance.cc:
now start doesn't call stop(), but rather tries to remove the pidfile itself
server-tools/instance-manager/instance.h:
cleanup
server-tools/instance-manager/instance_map.cc:
no more "admin" options
server-tools/instance-manager/instance_map.h:
User and password purged from instance_map options, as IM shouldn't know them
server-tools/instance-manager/instance_options.cc:
new option added -- shutdown_delay, guarded option now called nonguaded and has the opposite meaning
server-tools/instance-manager/instance_options.h:
appropriate changes, reflecting options addition/removal
server-tools/instance-manager/manager.cc:
shutdown process is complicated a bit -- at first signal thread should stop guardian, and only then the IM
itself
server-tools/instance-manager/messages.cc:
update error message
server-tools/instance-manager/options.cc:
admin user/password purged from mysqlmanager options
2005-01-31 23:54:08 +03:00
|
|
|
/* this variable is set to TRUE when we want to stop Guardian thread */
|
2005-02-11 14:21:59 +03:00
|
|
|
bool shutdown_requested;
|
2004-10-23 11:32:52 +04:00
|
|
|
};
|
|
|
|
|
2006-05-18 18:57:50 +04:00
|
|
|
|
2006-11-16 23:36:20 +03:00
|
|
|
inline Guardian::enum_instance_state
|
|
|
|
Guardian::get_instance_state(LIST *instance_node)
|
2006-05-18 18:57:50 +04:00
|
|
|
{
|
|
|
|
return ((GUARD_NODE *) instance_node->data)->state;
|
|
|
|
}
|
|
|
|
|
2004-10-23 11:32:52 +04:00
|
|
|
#endif /* INCLUDES_MYSQL_INSTANCE_MANAGER_GUARDIAN_H */
|