mariadb/server-tools/instance-manager/guardian.h
unknown a163ae30f2 Replace the approach using Foo_thread_args + Foo_thread and manually
spawned threads with a reusable class Thread.

This is the second idea implemented in the Alik's patch for
BUG#22306: STOP INSTANCE can not be applied for instances in Crashed,
Failed and Abandoned.
Commiting separately to ease review process. 


server-tools/instance-manager/commands.cc:
  Remove an unused header.
server-tools/instance-manager/guardian.cc:
  Use Thread framework instead of manually spawning the Guardian thread.
  Tidy up.
server-tools/instance-manager/guardian.h:
  Use Thread framework instead of manually spawning the Guardian thread.
server-tools/instance-manager/instance.cc:
  Use Thread framework instead of manually spawning the instance
  monitoring thread.
server-tools/instance-manager/listener.cc:
  Use Thread framework instead of manually spawning the 
  mysql connection thread.
server-tools/instance-manager/listener.h:
  Use Thread framework instead of manually spawning the 
  mysql connection thread.
  Rename Listener_thread to Listener for brevity.
server-tools/instance-manager/manager.cc:
  Change references to pointers, as per the coding style.
  Use Thread framework instead of manually spawning threads.
server-tools/instance-manager/mysql_connection.cc:
  Get rid of Mysql_connection_thread_args. Use class Thread framework
  instead. Rename Mysql_connection_thread to Mysql_connection for brevity.
server-tools/instance-manager/mysql_connection.h:
  Get rid of Mysql_connection_thread_args. Use class Thread framework
  instead. Rename Mysql_connection_thread to Mysql_connection for brevity.
server-tools/instance-manager/priv.cc:
  Move set_stacksize_and_create_thread to thread_registry.cc and make it
  static: it is not used anywhere else now.
server-tools/instance-manager/priv.h:
  No public set_stacksize_n_create_thread
server-tools/instance-manager/thread_registry.cc:
  Implement a base Thread class to be used for all Instance Manager
  threads.
server-tools/instance-manager/thread_registry.h:
  Implement a base Thread class to be used for all Instance Manager
  threads.
2006-11-17 16:11:04 +03:00

134 lines
4 KiB
C++

#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 */
#include "thread_registry.h"
#include <my_sys.h>
#include <my_list.h>
#if defined(__GNUC__) && defined(USE_PRAGMA_INTERFACE)
#pragma interface
#endif
class Instance;
class Instance_map;
class Thread_registry;
struct GUARD_NODE;
/**
The guardian thread is responsible for monitoring and restarting of guarded
instances.
*/
class Guardian: public Thread
{
public:
/* states of an instance */
enum enum_instance_state { NOT_STARTED= 1, STARTING, STARTED, JUST_CRASHED,
CRASHED, CRASHED_AND_ABANDONED, STOPPING };
/*
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.) */
enum_instance_state state;
/* 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;
};
/* Return client state name. */
static const char *get_instance_state_name(enum_instance_state state);
Guardian(Thread_registry *thread_registry_arg,
Instance_map *instance_map_arg,
uint monitoring_interval_arg);
virtual ~Guardian();
/* Initialize or refresh the list of guarded instances */
int init();
/* Request guardian shutdown. Stop instances if needed */
void request_shutdown();
/* Start instance protection */
int guard(Instance *instance, bool nolock= FALSE);
/* Stop instance protection */
int stop_guard(Instance *instance);
/* Returns TRUE if guardian thread is stopped */
int is_stopped();
void lock();
void unlock();
/*
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);
protected:
/* Main funtion of the thread */
virtual void run();
public:
pthread_cond_t COND_guardian;
private:
/* Prepares Guardian shutdown. Stops instances is needed */
int stop_instances();
/* check instance state and act accordingly */
void process_instance(Instance *instance, GUARD_NODE *current_node,
LIST **guarded_instances, LIST *elem);
int stopped;
private:
pthread_mutex_t LOCK_guardian;
Thread_info thread_info;
int monitoring_interval;
Thread_registry *thread_registry;
Instance_map *instance_map;
LIST *guarded_instances;
MEM_ROOT alloc;
/* this variable is set to TRUE when we want to stop Guardian thread */
bool shutdown_requested;
};
inline Guardian::enum_instance_state
Guardian::get_instance_state(LIST *instance_node)
{
return ((GUARD_NODE *) instance_node->data)->state;
}
#endif /* INCLUDES_MYSQL_INSTANCE_MANAGER_GUARDIAN_H */