Commit 96b90824 authored by unknown's avatar unknown
Browse files

Merge bk-internal.mysql.com:/home/bk/mysql-5.1-runtime

into  bodhi.local:/opt/local/work/m51lamp


server-tools/instance-manager/commands.cc:
  Auto merged
server-tools/instance-manager/instance.cc:
  Auto merged
server-tools/instance-manager/mysql_connection.cc:
  Resolve a merge conflict manually./Mysq
parents 3bb26768 a163ae30
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@
#include "guardian.h"
#include "instance_map.h"
#include "log.h"
#include "manager.h"
#include "messages.h"
#include "mysqld_error.h"
#include "mysql_manager_error.h"
+27 −36
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
#endif

#include "guardian.h"

#include <string.h>
#include <sys/types.h>
#include <signal.h>
@@ -30,15 +29,6 @@
#include "log.h"
#include "mysql_manager_error.h"


pthread_handler_t guardian_thread_func(void *arg)
{
  Guardian *guardian= (Guardian *) arg;
  guardian->run();
  return 0;
}


const char *
Guardian::get_instance_state_name(enum_instance_state state)
{
@@ -68,18 +58,19 @@ Guardian::get_instance_state_name(enum_instance_state state)
  return NULL; /* just to ignore compiler warning. */
}

/* {{{ Constructor & destructor. */

Guardian::Guardian(Thread_registry &thread_registry_arg,
Guardian::Guardian(Thread_registry *thread_registry_arg,
                   Instance_map *instance_map_arg,
                                 uint monitoring_interval_arg) :
  Guardian_args(thread_registry_arg, instance_map_arg,
                       monitoring_interval_arg),
  thread_info(pthread_self(), TRUE), guarded_instances(0)
                   uint monitoring_interval_arg)
  :monitoring_interval(monitoring_interval_arg),
  shutdown_requested(FALSE),
  stopped(FALSE),
  thread_registry(thread_registry_arg),
  instance_map(instance_map_arg)
{
  pthread_mutex_init(&LOCK_guardian, 0);
  pthread_cond_init(&COND_guardian, 0);
  shutdown_requested= FALSE;
  stopped= FALSE;
  init_alloc_root(&alloc, MEM_ROOT_BLOCK_SIZE, 0);
}

@@ -94,6 +85,8 @@ Guardian::~Guardian()
  pthread_cond_destroy(&COND_guardian);
}

/* }}} */


void Guardian::request_shutdown()
{
@@ -117,7 +110,7 @@ void Guardian::process_instance(Instance *instance,

  if (current_node->state == STOPPING)
  {
    /* this brach is executed during shutdown */
    /* this branch is executed during shutdown */
    if (instance->options.shutdown_delay)
    {
      /*
@@ -235,7 +228,7 @@ void Guardian::process_instance(Instance *instance,
/*
  Run guardian thread

  SYNOPSYS
  SYNOPSIS
    run()

  DESCRIPTION
@@ -252,9 +245,8 @@ void Guardian::run()

  log_info("Guardian: started.");

  thread_registry.register_thread(&thread_info);
  thread_registry->register_thread(&thread_info);

  my_thread_init();
  pthread_mutex_lock(&LOCK_guardian);

  /* loop, until all instances were shut down at the end */
@@ -275,7 +267,7 @@ void Guardian::run()

    /* check the loop predicate before sleeping */
    if (!(shutdown_requested && (!(guarded_instances))))
      thread_registry.cond_timedwait(&thread_info, &COND_guardian,
      thread_registry->cond_timedwait(&thread_info, &COND_guardian,
                                      &LOCK_guardian, &timeout);
  }

@@ -284,9 +276,8 @@ void Guardian::run()
  stopped= TRUE;
  pthread_mutex_unlock(&LOCK_guardian);
  /* now, when the Guardian is stopped we can stop the IM */
  thread_registry.unregister_thread(&thread_info);
  thread_registry.request_shutdown();
  my_thread_end();
  thread_registry->unregister_thread(&thread_info);
  thread_registry->request_shutdown();

  log_info("Guardian: finished.");
}
@@ -306,7 +297,7 @@ int Guardian::is_stopped()
  Initialize the list of guarded instances: loop through the Instance_map and
  add all of the instances, which don't have 'nonguarded' option specified.

  SYNOPSYS
  SYNOPSIS
    Guardian::init()

  NOTE: The operation should be invoked with the following locks acquired:
@@ -315,7 +306,7 @@ int Guardian::is_stopped()

  RETURN
    0 - ok
    1 - error occured
    1 - error occurred
*/

int Guardian::init()
@@ -344,7 +335,7 @@ int Guardian::init()
/*
  Add instance to the Guardian list

  SYNOPSYS
  SYNOPSIS
    guard()
    instance           the instance to be guarded
    nolock             whether we prefer do not lock Guardian here,
@@ -357,7 +348,7 @@ int Guardian::init()

  RETURN
    0 - ok
    1 - error occured
    1 - error occurred
*/

int Guardian::guard(Instance *instance, bool nolock)
@@ -418,7 +409,7 @@ int Guardian::stop_guard(Instance *instance)
  An internal method which is called at shutdown to unregister instances and
  attempt to stop them if requested.

  SYNOPSYS
  SYNOPSIS
    stop_instances()

  DESCRIPTION
@@ -431,7 +422,7 @@ int Guardian::stop_guard(Instance *instance)

  RETURN
    0 - ok
    1 - error occured
    1 - error occurred
*/

int Guardian::stop_instances()
+14 −29
Original line number Diff line number Diff line
@@ -16,11 +16,10 @@
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <my_global.h>
#include <my_sys.h>
#include <my_list.h>

#include "thread_registry.h"
#include <my_sys.h>
#include <my_list.h>

#if defined(__GNUC__) && defined(USE_PRAGMA_INTERFACE)
#pragma interface
@@ -31,30 +30,12 @@ class Instance_map;
class Thread_registry;
struct GUARD_NODE;

pthread_handler_t guardian_thread_func(void *arg);

struct Guardian_args
{
  Thread_registry &thread_registry;
  Instance_map *instance_map;
  int monitoring_interval;

  Guardian_args(Thread_registry &thread_registry_arg,
                       Instance_map *instance_map_arg,
                       uint monitoring_interval_arg) :
    thread_registry(thread_registry_arg),
    instance_map(instance_map_arg),
    monitoring_interval(monitoring_interval_arg)
  {}
};


/*
/**
  The guardian thread is responsible for monitoring and restarting of guarded
  instances.
*/

class Guardian: public Guardian_args
class Guardian: public Thread
{
public:
  /* states of an instance */
@@ -82,12 +63,10 @@ class Guardian: public Guardian_args
  /* Return client state name. */
  static const char *get_instance_state_name(enum_instance_state state);

  Guardian(Thread_registry &thread_registry_arg,
  Guardian(Thread_registry *thread_registry_arg,
           Instance_map *instance_map_arg,
           uint monitoring_interval_arg);
  ~Guardian();
  /* Main funtion of the thread */
  void run();
  virtual ~Guardian();
  /* Initialize or refresh the list of guarded instances */
  int init();
  /* Request guardian shutdown. Stop instances if needed */
@@ -117,6 +96,9 @@ class Guardian: public Guardian_args
    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;
@@ -133,6 +115,9 @@ class Guardian: public Guardian_args
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 */
+31 −29
Original line number Diff line number Diff line
@@ -44,9 +44,6 @@ static const char * const INSTANCE_NAME_PREFIX= Instance::DFLT_INSTANCE_NAME.str
static const int INSTANCE_NAME_PREFIX_LEN= Instance::DFLT_INSTANCE_NAME.length;


static void start_and_monitor_instance(Instance_options *old_instance_options,
                                       Instance_map *instance_map,
                                       Thread_registry *thread_registry);

#ifndef __WIN__
typedef pid_t My_process_info;
@@ -61,13 +58,24 @@ typedef PROCESS_INFORMATION My_process_info;
  to do it in a portable way.
*/

pthread_handler_t proxy(void *arg)
class Instance_monitor: public Thread
{
public:
  Instance_monitor(Instance *instance_arg) :instance(instance_arg) {}
protected:
  virtual void run();
  void start_and_monitor_instance(Instance_options *old_instance_options,
                                  Instance_map *instance_map,
                                  Thread_registry *thread_registry);
private:
  Instance *instance;
};

void Instance_monitor::run()
{
  Instance *instance= (Instance *) arg;
  start_and_monitor_instance(&instance->options,
                             instance->get_map(),
  start_and_monitor_instance(&instance->options, instance->get_map(),
                             &instance->thread_registry);
  return 0;
  delete this;
}

/*
@@ -242,14 +250,16 @@ static int start_process(Instance_options *instance_options,
    Function returns no value
*/

static void start_and_monitor_instance(Instance_options *old_instance_options,
void
Instance_monitor::
start_and_monitor_instance(Instance_options *old_instance_options,
                           Instance_map *instance_map,
                           Thread_registry *thread_registry)
{
  Instance_name instance_name(&old_instance_options->instance_name);
  Instance *current_instance;
  My_process_info process_info;
  Thread_info thread_info(pthread_self(), FALSE);
  Thread_info thread_info;

  log_info("Monitoring thread (instance: '%s'): started.",
           (const char *) instance_name.get_c_str());
@@ -258,12 +268,10 @@ static void start_and_monitor_instance(Instance_options *old_instance_options,
  {
    /*
      Register thread in Thread_registry to wait for it to stop on shutdown
      only if instance is nuarded. If instance is guarded, the thread will not
      only if instance is guarded. If instance is guarded, the thread will not
      finish, because nonguarded instances are not stopped on shutdown.
    */

    thread_registry->register_thread(&thread_info);
    my_thread_init();
    thread_registry->register_thread(&thread_info, FALSE);
  }

  /*
@@ -302,10 +310,7 @@ static void start_and_monitor_instance(Instance_options *old_instance_options,
  instance_map->unlock();

  if (!old_instance_options->nonguarded)
  {
    thread_registry->unregister_thread(&thread_info);
    my_thread_end();
  }

  log_info("Monitoring thread (instance: '%s'): finished.",
           (const char *) instance_name.get_c_str());
@@ -369,22 +374,19 @@ int Instance::start()

  if (configured && !is_running())
  {
    Instance_monitor *instance_monitor;
    remove_pid();

    pthread_t proxy_thd_id;
    pthread_attr_t proxy_thd_attr;
    int rc;
    instance_monitor= new Instance_monitor(this);

    pthread_attr_init(&proxy_thd_attr);
    pthread_attr_setdetachstate(&proxy_thd_attr, PTHREAD_CREATE_DETACHED);
    rc= pthread_create(&proxy_thd_id, &proxy_thd_attr, proxy,
                       this);
    pthread_attr_destroy(&proxy_thd_attr);
    if (rc)
    if (instance_monitor == NULL || instance_monitor->start_detached())
    {
      log_error("Instance::start(): pthread_create(proxy) failed");
      delete instance_monitor;
      log_error("Instance::start(): failed to create the monitoring thread"
                " to start an instance");
      return ER_CANNOT_START_INSTANCE;
    }
    /* The monitoring thread will delete itself when it's finished. */

    return 0;
  }
+32 −96
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@
#include <sys/un.h>
#endif

#include "instance_map.h"
#include "log.h"
#include "mysql_connection.h"
#include "options.h"
@@ -59,47 +58,18 @@ static void set_no_inherit(int socket)
}


/*
  Listener_thread - incapsulates listening functionality
*/

class Listener_thread: public Listener_thread_args
{
public:
  Listener_thread(const Listener_thread_args &args);
  ~Listener_thread();
  void run();
private:
  static const int LISTEN_BACK_LOG_SIZE= 5;     /* standard backlog size */
  ulong total_connection_count;
  Thread_info thread_info;

  int     sockets[2];
  int     num_sockets;
  fd_set  read_fds;
private:
  void handle_new_mysql_connection(Vio *vio);
  int   create_tcp_socket();
  int   create_unix_socket(struct sockaddr_un &unix_socket_address);
};


Listener_thread::Listener_thread(const Listener_thread_args &args) :
  Listener_thread_args(args.thread_registry, args.user_map, args.instance_map)
  ,total_connection_count(0)
  ,thread_info(pthread_self(), TRUE)
  ,num_sockets(0)
{
}


Listener_thread::~Listener_thread()
Listener::Listener(Thread_registry *thread_registry_arg,
                   User_map *user_map_arg)
  :thread_registry(thread_registry_arg),
  user_map(user_map_arg),
  total_connection_count(0),
  num_sockets(0)
{
}


/*
  Listener_thread::run() - listen all supported sockets and spawn a thread
  Listener::run() - listen all supported sockets and spawn a thread
  to handle incoming connection.
  Using 'die' in case of syscall failure is OK now - we don't hold any
  resources and 'die' kills the signal thread automatically. To be rewritten
@@ -108,11 +78,11 @@ Listener_thread::~Listener_thread()
  architecture.
*/

void Listener_thread::run()
void Listener::run()
{
  int i, n= 0;

  log_info("Listener_thread: started.");
  log_info("Listener: started.");

#ifndef __WIN__
  /* we use this var to check whether we are running on LinuxThreads */
@@ -125,9 +95,7 @@ void Listener_thread::run()
  linuxthreads= (thread_pid != manager_pid);
#endif

  thread_registry.register_thread(&thread_info);

  my_thread_init();
  thread_registry->register_thread(&thread_info);

  FD_ZERO(&read_fds);

@@ -146,7 +114,7 @@ void Listener_thread::run()
  n++;

  timeval tv;
  while (!thread_registry.is_shutdown())
  while (!thread_registry->is_shutdown())
  {
    fd_set read_fds_arg= read_fds;
    /*
@@ -166,7 +134,7 @@ void Listener_thread::run()
    if (rc == 0 || rc == -1)
    {
      if (rc == -1 && errno != EINTR)
        log_error("Listener_thread: select() failed, %s",
        log_error("Listener: select() failed, %s",
                  strerror(errno));
      continue;
    }
@@ -200,7 +168,7 @@ void Listener_thread::run()

  /* III. Release all resources and exit */

  log_info("Listener_thread: shutdown requested, exiting...");
  log_info("Listener: shutdown requested, exiting...");

  for (i= 0; i < num_sockets; i++)
    close(sockets[i]);
@@ -209,10 +177,9 @@ void Listener_thread::run()
  unlink(unix_socket_address.sun_path);
#endif

  thread_registry.unregister_thread(&thread_info);
  my_thread_end();
  thread_registry->unregister_thread(&thread_info);

  log_info("Listener_thread: finished.");
  log_info("Listener: finished.");
  return;

err:
@@ -220,13 +187,12 @@ void Listener_thread::run()
  for (i= 0; i < num_sockets; i++)
    close(sockets[i]);

  thread_registry.unregister_thread(&thread_info);
  thread_registry.request_shutdown();
  my_thread_end();
  thread_registry->unregister_thread(&thread_info);
  thread_registry->request_shutdown();
  return;
}

int Listener_thread::create_tcp_socket()
int Listener::create_tcp_socket()
{
  /* value to be set by setsockopt */
  int arg= 1;
@@ -265,7 +231,7 @@ int Listener_thread::create_tcp_socket()
  if (bind(ip_socket, (struct sockaddr *) &ip_socket_address,
           sizeof(ip_socket_address)))
  {
    log_error("Listener_thread: bind(ip socket) failed, '%s'",
    log_error("Listener: bind(ip socket) failed, '%s'",
              strerror(errno));
    close(ip_socket);
    return -1;
@@ -273,7 +239,7 @@ int Listener_thread::create_tcp_socket()

  if (listen(ip_socket, LISTEN_BACK_LOG_SIZE))
  {
    log_error("Listener_thread: listen(ip socket) failed, %s",
    log_error("Listener: listen(ip socket) failed, %s",
              strerror(errno));
    close(ip_socket);
    return -1;
@@ -292,7 +258,7 @@ int Listener_thread::create_tcp_socket()
}

#ifndef __WIN__
int Listener_thread::
int Listener::
create_unix_socket(struct sockaddr_un &unix_socket_address)
{
  int unix_socket= socket(AF_UNIX, SOCK_STREAM, 0);
@@ -318,7 +284,7 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)
  if (bind(unix_socket, (struct sockaddr *) &unix_socket_address,
           sizeof(unix_socket_address)))
  {
    log_error("Listener_thread: bind(unix socket) failed, "
    log_error("Listener: bind(unix socket) failed, "
              "socket file name is '%s', error '%s'",
              unix_socket_address.sun_path, strerror(errno));
    close(unix_socket);
@@ -329,7 +295,7 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)

  if (listen(unix_socket, LISTEN_BACK_LOG_SIZE))
  {
    log_error("Listener_thread: listen(unix socket) failed, %s",
    log_error("Listener: listen(unix socket) failed, %s",
              strerror(errno));
    close(unix_socket);
    return -1;
@@ -357,46 +323,16 @@ create_unix_socket(struct sockaddr_un &unix_socket_address)
    handle_new_mysql_connection()
*/

void Listener_thread::handle_new_mysql_connection(Vio *vio)
void Listener::handle_new_mysql_connection(Vio *vio)
{
  if (Mysql_connection_thread_args *mysql_thread_args=
      new Mysql_connection_thread_args(vio, thread_registry, user_map,
                                       ++total_connection_count,
                                       instance_map)
      )
  {
    /*
      Initialize thread attributes to create detached thread; it seems
      easier to do it ad-hoc than have a global variable for attributes.
    */
    pthread_t mysql_thd_id;
    pthread_attr_t mysql_thd_attr;
    pthread_attr_init(&mysql_thd_attr);
    pthread_attr_setdetachstate(&mysql_thd_attr, PTHREAD_CREATE_DETACHED);
    if (set_stacksize_n_create_thread(&mysql_thd_id, &mysql_thd_attr,
                                      mysql_connection, mysql_thread_args))
  Mysql_connection *mysql_connection=
    new Mysql_connection(thread_registry, user_map,
                         vio, ++total_connection_count);
  if (mysql_connection == NULL || mysql_connection->start_detached())
  {
      delete mysql_thread_args;
    log_error("handle_one_mysql_connection() failed");
    delete mysql_connection;
    vio_delete(vio);
      log_error("handle_one_mysql_connection():"
                "set_stacksize_n_create_thread(mysql) failed");
  }
    pthread_attr_destroy(&mysql_thd_attr);
  /* The connection will delete itself when the thread is finished */
}
  else
    vio_delete(vio);
}


pthread_handler_t listener(void *arg)
{
  Listener_thread_args *args= (Listener_thread_args *) arg;
  Listener_thread listener(*args);
  listener.run();
  /*
    args is a stack variable because listener thread lives as long as the
    manager process itself
  */
  return 0;
}
Loading