threadpool_common.cc 6.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (C) 2012 Monty Program 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; 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16 17 18 19 20 21 22 23 24
#include <my_global.h>
#include <violite.h>
#include <sql_priv.h>
#include <sql_class.h>
#include <my_pthread.h>
#include <scheduler.h>
#include <sql_connect.h>
#include <sql_audit.h>
#include <debug_sync.h>
25
#include <threadpool.h>
26 27 28


/* Threadpool parameters */
29

30 31 32 33 34
uint threadpool_min_threads;
uint threadpool_idle_timeout;
uint threadpool_size;
uint threadpool_stall_limit;
uint threadpool_max_threads;
35
uint threadpool_oversubscribe;
36

Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
37 38 39
/* Stats */
TP_STATISTICS tp_stats;

40

41
extern "C" pthread_key(struct st_my_thread_var*, THR_KEY_mysys);
42
extern bool do_command(THD*);
43

44
/*
45
  Worker threads contexts, and THD contexts.
46
  =========================================
47 48
  
  Both worker threads and connections have their sets of thread local variables 
49 50
  At the moment it is mysys_var (this has specific data for dbug, my_error and 
  similar goodies), and PSI per-client structure.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

  Whenever query is executed following needs to be done:

  1. Save worker thread context.
  2. Change TLS variables to connection specific ones using thread_attach(THD*).
     This function does some additional work , e.g setting up 
     thread_stack/thread_ends_here pointers.
  3. Process query
  4. Restore worker thread context.

  Connection login and termination follows similar schema w.r.t saving and 
  restoring contexts. 

  For both worker thread, and for the connection, mysys variables are created 
  using my_thread_init() and freed with my_thread_end().

67
*/
68
struct Worker_thread_context
69
{
70 71
  PSI_thread *psi_thread;
  st_my_thread_var* mysys_var;
72

73
  void save()
74
  {
75 76
    psi_thread=  PSI_server?PSI_server->get_thread():0;
    mysys_var= (st_my_thread_var *)pthread_getspecific(THR_KEY_mysys);
77 78
  }

79
  void restore()
80
  {
81 82 83 84 85
    if (PSI_server)
      PSI_server->set_thread(psi_thread);
    pthread_setspecific(THR_KEY_mysys,mysys_var);
    pthread_setspecific(THR_THD, 0);
    pthread_setspecific(THR_MALLOC, 0);
86
  }
87
};
88 89 90


/*
91
  Attach/associate the connection with the OS thread,
92
*/
93
static bool thread_attach(THD* thd)
94
{
95 96 97
  pthread_setspecific(THR_KEY_mysys,thd->mysys_var);
  thd->thread_stack=(char*)&thd;
  thd->store_globals();
98
  if (PSI_server)
99 100
    PSI_server->set_thread(thd->event_scheduler.m_psi);
  return 0;
101 102 103 104 105 106
}


int threadpool_add_connection(THD *thd)
{
  int retval=1;
107 108 109 110
  Worker_thread_context worker_context;
  worker_context.save();

  /*
111 112
    Create a new connection context: mysys_thread_var and PSI thread
    Store them in THD.
113 114 115 116 117 118 119 120 121 122 123
  */

  pthread_setspecific(THR_KEY_mysys, 0);
  my_thread_init();
  thd->mysys_var= (st_my_thread_var *)pthread_getspecific(THR_KEY_mysys);
  if (!thd->mysys_var)
  {
    /* Out of memory? */
    worker_context.restore();
    return 1;
  }
124

125
  /* Create new PSI thread for use with the THD. */
126 127 128 129 130
  if (PSI_server)
  {
    thd->event_scheduler.m_psi = 
      PSI_server->new_thread(key_thread_one_connection, thd, thd->thread_id);
  }
131 132 133 134 135 136 137 138 139


  /* Login. */
  thread_attach(thd);
  ulonglong now= microsecond_interval_timer();
  thd->prior_thr_create_utime= now;
  thd->start_utime= now;
  thd->thr_create_utime= now;

140
  if (!setup_connection_thread_globals(thd))
141
  {
142
    if (!login_connection(thd))
143
    {
144 145 146 147 148 149 150 151 152 153
      prepare_new_connection_state(thd);
      
      /* 
        Check if THD is ok, as prepare_new_connection_state()
        can fail, for example if init command failed.
      */
      if (thd_is_connection_alive(thd))
      {
        retval= 0;
        thd->net.reading_or_writing= 1;
154
        thd->skip_wait_timeout= true;
155
      }
156 157
    }
  }
158
  worker_context.restore();
159 160 161
  return retval;
}

162

163 164 165
void threadpool_remove_connection(THD *thd)
{

166 167 168 169
  Worker_thread_context worker_context;
  worker_context.save();

  thread_attach(thd);
170 171 172 173 174 175 176
  thd->net.reading_or_writing= 0;

  end_connection(thd);
  close_connection(thd, 0);

  unlink_thd(thd);
  mysql_cond_broadcast(&COND_thread_count);
177

178 179 180 181
  /*
    Free resources associated with this connection: 
    mysys thread_var and PSI thread.
  */
182 183 184
  my_thread_end();

  worker_context.restore();
185 186
}

187 188 189
/**
 Process a single client request or a single batch.
*/
190 191 192
int threadpool_process_request(THD *thd)
{
  int retval= 0;
193 194 195 196
  Worker_thread_context  worker_context;
  worker_context.save();

  thread_attach(thd);
197

Vladislav Vaintroub's avatar
Vladislav Vaintroub committed
198
  if (thd->killed >= KILL_CONNECTION)
199 200
  {
    /* 
201 202
      killed flag was set by timeout handler 
      or KILL command. Return error.
203
    */
204 205
    retval= 1;
    goto end;
206 207
  }

208

209 210 211 212 213 214 215 216 217
  /*
    In the loop below, the flow is essentially the copy of thead-per-connections
    logic, see do_handle_one_connection() in sql_connect.c

    The goal is to execute a single query, thus the loop is normally executed 
    only once. However for SSL connections, it can be executed multiple times 
    (SSL can preread and cache incoming data, and vio->has_data() checks if it 
    was the case).
  */
218 219 220 221 222 223 224
  for(;;)
  {
    Vio *vio;
    thd->net.reading_or_writing= 0;
    mysql_audit_release(thd);

    if ((retval= do_command(thd)) != 0)
225
      goto end;
226 227 228 229

    if (!thd_is_connection_alive(thd))
    {
      retval= 1;
230
      goto end;
231 232 233 234 235
    }

    vio= thd->net.vio;
    if (!vio->has_data(vio))
    { 
236
      /* More info on this debug sync is in sql_parse.cc*/
237
      DEBUG_SYNC(thd, "before_do_command_net_read");
238
      thd->net.reading_or_writing= 1;
239
      goto end;
240
    }
241
  }
242

243
end:
244
  worker_context.restore();
245 246 247 248 249 250 251 252 253 254 255 256 257 258
  return retval;
}


static scheduler_functions tp_scheduler_functions=
{
  0,                                  // max_threads
  NULL,
  NULL,
  tp_init,                            // init
  NULL,                               // init_new_connection_thread
  tp_add_connection,                  // add_connection
  tp_wait_begin,                      // thd_wait_begin
  tp_wait_end,                        // thd_wait_end
259
  post_kill_notification,             // post_kill_notification
260 261 262 263 264 265 266 267 268
  NULL,                               // end_thread
  tp_end                              // end
};

void pool_of_threads_scheduler(struct scheduler_functions *func,
    ulong *arg_max_connections,
    uint *arg_connection_count)
{
  *func = tp_scheduler_functions;
269
  func->max_threads= threadpool_max_threads;
270 271 272 273
  func->max_connections= arg_max_connections;
  func->connection_count= arg_connection_count;
  scheduler_init();
}