Commit f8c1059c authored by Sergei Golubchik's avatar Sergei Golubchik

move wt* maintainance from maria to the server

include/waiting_threads.h:
  C_MODE_START/END
mysys/waiting_threads.c:
  relax the checks - auto init thd in will_wait_for,
  allow to destroy uninited thd (=noop),
  allow a "release" an unexistent resource (=noop),
sql/sql_class.cc:
  move wt* maintainance from maria to the server.
  do THD::cleanup after ha_close_connection() and plugin_thdvar_cleanup().
storage/maria/unittest/trnman-t.c:
  update to new prototype
parent 5a4cdba5
......@@ -18,6 +18,9 @@
#include <my_global.h>
#include <my_sys.h>
C_MODE_START
#include <lf.h>
typedef struct st_wt_resource_id WT_RESOURCE_ID;
......@@ -155,4 +158,6 @@ void wt_thd_release(WT_THD *, WT_RESOURCE_ID *);
#define wt_thd_release_all(THD) wt_thd_release((THD), 0)
int wt_resource_id_memcmp(void *, void *);
C_MODE_END
#endif
......@@ -208,6 +208,9 @@ void wt_thd_destroy(WT_THD *thd)
{
DBUG_ENTER("wt_thd_destroy");
if (thd->my_resources.buffer == 0)
DBUG_VOID_RETURN; /* nothing to do */
DBUG_ASSERT(thd->my_resources.elements == 0);
delete_dynamic(&thd->my_resources);
lf_hash_put_pins(thd->pins);
......@@ -447,6 +450,9 @@ int wt_thd_will_wait_for(WT_THD *thd, WT_THD *blocker, WT_RESOURCE_ID *resid)
DBUG_PRINT("wt", ("enter: thd=%s, blocker=%s, resid=%llu",
thd->name, blocker->name, resid->value.num));
if (unlikely(thd->my_resources.buffer == 0))
wt_thd_init(thd);
if (thd->waiting_for == 0)
{
uint keylen;
......@@ -647,7 +653,7 @@ void wt_thd_release(WT_THD *thd, WT_RESOURCE_ID *resid)
}
}
}
DBUG_ASSERT(!resid);
if (!resid)
reset_dynamic(&thd->my_resources);
DBUG_VOID_RETURN;
}
......
......@@ -26,6 +26,7 @@
#include "mysqld_suffix.h"
#include "mysys_err.h"
#include "events.h"
#include <waiting_threads.h>
#include "../storage/myisam/ha_myisam.h"
......@@ -1240,6 +1241,7 @@ void clean_up(bool print_message)
if (tc_log)
tc_log->close();
xid_cache_free();
wt_end();
delete_elements(&key_caches, (void (*)(const char*, uchar*)) free_key_cache);
multi_keycache_free();
free_status_vars();
......@@ -3665,6 +3667,8 @@ static int init_server_components()
if (table_cache_init() | table_def_init() | hostname_cache_init())
unireg_abort(1);
wt_init();
query_cache_result_size_limit(query_cache_limit);
query_cache_set_min_res_unit(query_cache_min_res_unit);
query_cache_init();
......
......@@ -829,6 +829,7 @@ void THD::cleanup(void)
lock=locked_tables; locked_tables=0;
close_thread_tables(this);
}
wt_thd_destroy(&transaction.wt);
mysql_ha_cleanup(this);
delete_dynamic(&user_var_events);
hash_free(&user_vars);
......@@ -874,12 +875,12 @@ THD::~THD()
#endif
stmt_map.reset(); /* close all prepared statements */
DBUG_ASSERT(lock_info.n_cursors == 0);
if (!cleanup_done)
cleanup();
ha_close_connection(this);
plugin_thdvar_cleanup(this);
if (!cleanup_done)
cleanup();
DBUG_PRINT("info", ("freeing security context"));
main_security_ctx.destroy();
safeFree(db);
......
......@@ -22,6 +22,7 @@
#include "log.h"
#include "rpl_tblmap.h"
#include <waiting_threads.h>
class Relay_log_info;
......@@ -1327,6 +1328,7 @@ public:
THD_TRANS stmt; // Trans for current statement
bool on; // see ha_enable_transaction()
XID_STATE xid_state;
WT_THD wt;
Rows_log_event *m_pending_rows_event;
/*
......
......@@ -2267,7 +2267,7 @@ int ha_maria::external_lock(THD *thd, int lock_type)
/* Start of new statement */
if (!trn) /* no transaction yet - open it now */
{
trn= trnman_new_trn(& thd->mysys_var->mutex, & thd->mysys_var->suspend);
trn= trnman_new_trn(& thd->transaction.wt);
if (unlikely(!trn))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
THD_TRN= trn;
......@@ -2457,7 +2457,7 @@ int ha_maria::implicit_commit(THD *thd, bool new_trn)
tables may be under LOCK TABLES, and so they will start the next
statement assuming they have a trn (see ha_maria::start_stmt()).
*/
trn= trnman_new_trn(& thd->mysys_var->mutex, & thd->mysys_var->suspend);
trn= trnman_new_trn(& thd->transaction.wt);
/* This is just a commit, tables stay locked if they were: */
trnman_reset_locked_tables(trn, locked_tables);
THD_TRN= trn;
......
......@@ -106,9 +106,7 @@ int maria_begin(MARIA_HA *info)
if (info->s->now_transactional)
{
TRN *trn;
struct st_my_thread_var *mysys_var= my_thread_var;
trn= trnman_new_trn(&mysys_var->mutex, &mysys_var->suspend);
TRN *trn= trnman_new_trn(0);
if (unlikely(!trn))
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
......
......@@ -221,13 +221,13 @@ int maria_write(MARIA_HA *info, uchar *record)
rc.type= &ma_rc_dup_unique;
rc.value.ptr= blocker; /* TODO savepoint id when we'll have them */
res= wt_thd_will_wait_for(& info->trn->wt, & blocker->wt, & rc);
res= wt_thd_will_wait_for(info->trn->wt, blocker->wt, & rc);
if (res != WT_OK)
{
pthread_mutex_unlock(& blocker->state_lock);
goto err;
}
res=wt_thd_cond_timedwait(& info->trn->wt, & blocker->state_lock);
res=wt_thd_cond_timedwait(info->trn->wt, & blocker->state_lock);
pthread_mutex_unlock(& blocker->state_lock);
if (res != WT_OK)
goto err;
......
......@@ -462,7 +462,7 @@ typedef struct st_maria_block_scan
struct st_maria_handler
{
MARIA_SHARE *s; /* Shared between open:s */
struct st_transaction *trn; /* Pointer to active transaction */
struct st_ma_transaction *trn; /* Pointer to active transaction */
MARIA_STATUS_INFO *state, state_save;
MARIA_STATUS_INFO *state_start; /* State at start of transaction */
MARIA_ROW cur_row; /* The active row that we just read */
......@@ -761,7 +761,6 @@ extern char *maria_data_root;
extern uchar maria_zero_string[];
extern my_bool maria_inited, maria_in_ha_maria;
extern HASH maria_stored_state;
extern WT_RESOURCE_TYPE ma_rc_dup_unique;
/* This is used by _ma_calc_xxx_key_length och _ma_store_key */
typedef struct st_maria_s_param
......
......@@ -81,6 +81,16 @@ void trnman_reset_locked_tables(TRN *trn, uint locked_tables)
trn->locked_tables= locked_tables;
}
static void wt_thd_release_self(TRN *trn)
{
if (trn->wt)
{
WT_RESOURCE_ID rc;
rc.type= &ma_rc_dup_unique;
rc.value.ptr= trn;
wt_thd_release(trn->wt, & rc);
}
}
static my_bool
default_trnman_end_trans_hook(TRN *trn __attribute__ ((unused)),
......@@ -114,8 +124,6 @@ int trnman_init(TrID initial_trid)
{
DBUG_ENTER("trnman_init");
wt_init(); /* FIXME this should be done in the server, not in the engine! */
short_trid_to_active_trn= (TRN **)my_malloc(SHORT_TRID_MAX*sizeof(TRN*),
MYF(MY_WME|MY_ZEROFILL));
if (unlikely(!short_trid_to_active_trn))
......@@ -182,7 +190,6 @@ void trnman_destroy()
TRN *trn= pool;
pool= pool->next;
pthread_mutex_destroy(&trn->state_lock);
wt_thd_destroy(&trn->wt);
my_free((void *)trn, MYF(0));
}
lf_hash_destroy(&trid_to_trn);
......@@ -193,8 +200,6 @@ void trnman_destroy()
my_free((void *)(short_trid_to_active_trn+1), MYF(0));
short_trid_to_active_trn= NULL;
wt_end();
DBUG_VOID_RETURN;
}
......@@ -243,7 +248,7 @@ static uint get_short_trid(TRN *trn)
mutex and cond will be used for lock waits
*/
TRN *trnman_new_trn(pthread_mutex_t *mutex, pthread_cond_t *cond)
TRN *trnman_new_trn(WT_THD *wt)
{
int res;
TRN *trn;
......@@ -291,7 +296,7 @@ TRN *trnman_new_trn(pthread_mutex_t *mutex, pthread_cond_t *cond)
}
trnman_allocated_transactions++;
pthread_mutex_init(&trn->state_lock, MY_MUTEX_INIT_FAST);
wt_thd_init(&trn->wt);
trn->wt= wt;
}
trn->pins= lf_hash_get_pins(&trid_to_trn);
if (!trn->pins)
......@@ -418,7 +423,7 @@ my_bool trnman_end_trn(TRN *trn, my_bool commit)
{
pthread_mutex_lock(&trn->state_lock);
trn->commit_trid= global_trid_generator;
wt_thd_release_all(& trn->wt);
wt_thd_release_self(trn);
pthread_mutex_unlock(&trn->state_lock);
trn->next= &committed_list_max;
......@@ -457,11 +462,6 @@ my_bool trnman_end_trn(TRN *trn, my_bool commit)
/* ignore OOM. it's harmless, and we can do nothing here anyway */
(void)lf_hash_delete(&trid_to_trn, pins, &t->trid, sizeof(TrID));
pthread_mutex_lock(&trn->state_lock);
trn->short_id= 0;
wt_thd_release_all(& trn->wt);
pthread_mutex_unlock(&trn->state_lock);
trnman_free_trn(t);
}
......@@ -489,6 +489,13 @@ void trnman_free_trn(TRN *trn)
*/
union { TRN *trn; void *v; } tmp;
pthread_mutex_lock(&trn->state_lock);
trn->short_id= 0;
wt_thd_release_self(trn);
trn->wt= 0; /* just in case */
pthread_mutex_unlock(&trn->state_lock);
tmp.trn= pool;
my_atomic_rwlock_wrlock(&LOCK_pool);
......@@ -759,7 +766,7 @@ TRN *trnman_recreate_trn_from_recovery(uint16 shortid, TrID longid)
TRN *trn;
DBUG_ASSERT(maria_in_recovery && !maria_multi_threaded);
global_trid_generator= longid-1; /* force a correct trid in the new trn */
if (unlikely((trn= trnman_new_trn(NULL, NULL)) == NULL))
if (unlikely((trn= trnman_new_trn(NULL)) == NULL))
return NULL;
/* deallocate excessive allocations of trnman_new_trn() */
global_trid_generator= old_trid_generator;
......
......@@ -21,7 +21,6 @@ C_MODE_START
#include <lf.h>
#include "trnman_public.h"
#include "ma_loghandler_lsn.h"
#include <waiting_threads.h>
/*
trid - 6 uchar transaction identifier. Assigned when a transaction
......@@ -42,10 +41,10 @@ C_MODE_START
commit_trid happen under this mutex.
*/
struct st_transaction
struct st_ma_transaction
{
LF_PINS *pins;
WT_THD wt;
WT_THD *wt;
pthread_mutex_t state_lock;
void *used_tables; /* Tables used by transaction */
TRN *next, *prev;
......@@ -58,6 +57,8 @@ struct st_transaction
#define TRANSACTION_LOGGED_LONG_ID ULL(0x8000000000000000)
extern WT_RESOURCE_TYPE ma_rc_dup_unique;
C_MODE_END
#endif
......
......@@ -24,10 +24,11 @@
#define _trnman_public_h
#include "ma_loghandler_lsn.h"
#include <waiting_threads.h>
C_MODE_START
typedef uint64 TrID; /* our TrID is 6 bytes */
typedef struct st_transaction TRN;
typedef struct st_ma_transaction TRN;
#define SHORT_TRID_MAX 65535
......@@ -38,7 +39,7 @@ extern my_bool (*trnman_end_trans_hook)(TRN *trn, my_bool commit,
int trnman_init(TrID);
void trnman_destroy(void);
TRN *trnman_new_trn(pthread_mutex_t *, pthread_cond_t *);
TRN *trnman_new_trn(WT_THD *wt);
my_bool trnman_end_trn(TRN *trn, my_bool commit);
#define trnman_commit_trn(T) trnman_end_trn(T, TRUE)
#define trnman_abort_trn(T) trnman_end_trn(T, FALSE)
......
......@@ -38,25 +38,17 @@ pthread_handler_t test_trnman(void *arg)
{
uint x, y, i, n;
TRN *trn[MAX_ITER];
pthread_mutex_t mutexes[MAX_ITER];
pthread_cond_t conds[MAX_ITER];
int m= (*(int *)arg);
my_thread_init();
for (i= 0; i < MAX_ITER; i++)
{
pthread_mutex_init(&mutexes[i], MY_MUTEX_INIT_FAST);
pthread_cond_init(&conds[i], 0);
}
for (x= ((int)(intptr)(&m)); m > 0; )
{
y= x= (x*LL(3628273133) + LL(1500450271)) % LL(9576890767); /* three prime numbers */
m-= n= x % MAX_ITER;
for (i= 0; i < n; i++)
{
trn[i]= trnman_new_trn(&mutexes[i], &conds[i]);
trn[i]= trnman_new_trn(0);
if (!trn[i])
{
diag("trnman_new_trn() failed");
......@@ -69,11 +61,6 @@ pthread_handler_t test_trnman(void *arg)
trnman_end_trn(trn[i], y & 1);
}
}
for (i= 0; i < MAX_ITER; i++)
{
pthread_mutex_destroy(&mutexes[i]);
pthread_cond_destroy(&conds[i]);
}
pthread_mutex_lock(&rt_mutex);
rt_num_threads--;
pthread_mutex_unlock(&rt_mutex);
......@@ -118,7 +105,7 @@ void run_test(const char *test, pthread_handler handler, int n, int m)
i= trnman_can_read_from(trn[T1], trid[T2]); \
ok(i == RES, "trn" #T1 " %s read from trn" #T2, i ? "can" : "cannot")
#define start_transaction(T) \
trn[T]= trnman_new_trn(&mutexes[T], &conds[T]); \
trn[T]= trnman_new_trn(0); \
trid[T]= trn[T]->trid
#define commit(T) trnman_commit_trn(trn[T])
#define abort(T) trnman_abort_trn(trn[T])
......@@ -128,16 +115,8 @@ void test_trnman_read_from()
{
TRN *trn[Ntrns];
TrID trid[Ntrns];
pthread_mutex_t mutexes[Ntrns];
pthread_cond_t conds[Ntrns];
int i;
for (i= 0; i < Ntrns; i++)
{
pthread_mutex_init(&mutexes[i], MY_MUTEX_INIT_FAST);
pthread_cond_init(&conds[i], 0);
}
start_transaction(0); /* start trn1 */
start_transaction(1); /* start trn2 */
ok_read_from(1, 0, 0);
......@@ -153,11 +132,6 @@ void test_trnman_read_from()
ok_read_from(3, 1, 0);
commit(3); /* commit trn5 */
for (i= 0; i < Ntrns; i++)
{
pthread_mutex_destroy(&mutexes[i]);
pthread_cond_destroy(&conds[i]);
}
}
int main(int argc __attribute__((unused)), char **argv)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment