Commit f76a17e3 authored by Jacob Mathew's avatar Jacob Mathew

MDEV-7914: spider/bg.ha, spider/bg.ha_part crash server sporadically in buildbot

The crash occurs when a thread that is closing its connection attempts to
access Spider transaction information when another thread has freed that memory
while processing Spider plugin deinit.  This occurs because Spider does not
adjust the plugin's reference count when it sets a transaction information
pointer for the plugin.

The fix I implemented changes the way Spider sets the transaction information
pointer to use thd_set_ha_data() so that Spider's plugin reference counter is
adjusted as well.

Author:
  Jacob Mathew.

Reviewer:
  Kentoku Shiba.

Merged From:
  Commit ab9d420d on branch 10.2
parent 0bd2b802
......@@ -712,7 +712,7 @@ int ha_spider::close()
}
}
if (!thd || !*thd_ha_data(thd, spider_hton_ptr))
if (!thd || !thd_get_ha_data(thd, spider_hton_ptr))
{
for (roop_count = 0; roop_count < (int) share->link_count; roop_count++)
conns[roop_count] = NULL;
......
......@@ -19,7 +19,7 @@
#define spider_bulk_malloc(A,B,C,...) \
spider_bulk_alloc_mem(A,B,__func__,__FILE__,__LINE__,C,__VA_ARGS__)
#define spider_current_trx \
(current_thd ? ((SPIDER_TRX *) *thd_ha_data(current_thd, spider_hton_ptr)) : NULL)
(current_thd ? ((SPIDER_TRX *) thd_get_ha_data(current_thd, spider_hton_ptr)) : NULL)
#define init_calc_mem(A) init_mem_calc(A,__func__,__FILE__,__LINE__)
......
......@@ -41,6 +41,27 @@
#include "spd_direct_sql.h"
#include "spd_malloc.h"
inline MYSQL_THD spider_create_thd()
{
THD *thd;
my_thread_init();
if (!(thd = new THD()))
my_thread_end();
else
{
#ifdef HAVE_PSI_INTERFACE
mysql_thread_set_psi_id(thd->thread_id);
#endif
thd->thread_stack = (char *) &thd;
thd->store_globals();
}
return thd;
}
inline void spider_destroy_thd(MYSQL_THD thd)
{
delete thd;
}
ulong *spd_db_att_thread_id;
#ifdef SPIDER_XID_USES_xid_cache_iterate
#else
......@@ -5994,7 +6015,7 @@ int spider_close_connection(
SPIDER_CONN *conn;
SPIDER_TRX *trx;
DBUG_ENTER("spider_close_connection");
if (!(trx = (SPIDER_TRX*) *thd_ha_data(thd, spider_hton_ptr)))
if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)))
DBUG_RETURN(0); /* transaction is not started */
trx->tmp_spider->conns = &conn;
......@@ -6049,6 +6070,7 @@ int spider_db_done(
void *p
) {
int roop_count;
bool do_delete_thd;
THD *thd = current_thd, *tmp_thd;
SPIDER_CONN *conn;
SPIDER_INIT_ERROR_TABLE *spider_init_error_table;
......@@ -6056,6 +6078,18 @@ int spider_db_done(
SPIDER_LGTM_TBLHND_SHARE *lgtm_tblhnd_share;
DBUG_ENTER("spider_db_done");
/* Begin Spider plugin deinit */
if (thd)
do_delete_thd = FALSE;
else
{
/* Create a thread for Spider plugin deinit */
thd = spider_create_thd();
if (!thd)
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
do_delete_thd = TRUE;
}
#ifndef WITHOUT_SPIDER_BG_SEARCH
spider_free_trx(spider_global_trx, TRUE);
#endif
......@@ -6098,21 +6132,22 @@ int spider_db_done(
pthread_mutex_destroy(&spider_udf_table_mon_mutexes[roop_count]);
spider_free(NULL, spider_udf_table_mon_mutexes, MYF(0));
if (thd && thd_sql_command(thd) == SQLCOM_UNINSTALL_PLUGIN) {
pthread_mutex_lock(&spider_allocated_thds_mutex);
while ((tmp_thd = (THD *) my_hash_element(&spider_allocated_thds, 0)))
pthread_mutex_lock(&spider_allocated_thds_mutex);
while ((tmp_thd = (THD *) my_hash_element(&spider_allocated_thds, 0)))
{
SPIDER_TRX *trx = (SPIDER_TRX *)
thd_get_ha_data(tmp_thd, spider_hton_ptr);
if (trx)
{
SPIDER_TRX *trx = (SPIDER_TRX *) *thd_ha_data(tmp_thd, spider_hton_ptr);
if (trx)
{
DBUG_ASSERT(tmp_thd == trx->thd);
spider_free_trx(trx, FALSE);
*thd_ha_data(tmp_thd, spider_hton_ptr) = (void *) NULL;
} else
my_hash_delete(&spider_allocated_thds, (uchar *) tmp_thd);
DBUG_ASSERT(tmp_thd == trx->thd);
spider_free_trx(trx, FALSE);
thd_set_ha_data(tmp_thd, spider_hton_ptr, NULL);
}
pthread_mutex_unlock(&spider_allocated_thds_mutex);
else
my_hash_delete(&spider_allocated_thds, (uchar *) tmp_thd);
}
pthread_mutex_unlock(&spider_allocated_thds_mutex);
#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
pthread_mutex_lock(&spider_hs_w_conn_mutex);
while ((conn = (SPIDER_CONN*) my_hash_element(&spider_hs_w_conn_hash, 0)))
......@@ -6261,6 +6296,11 @@ int spider_db_done(
spider_current_alloc_mem[roop_count] ? "NG" : "OK"
));
}
/* End Spider plugin deinit */
if (do_delete_thd)
spider_destroy_thd(thd);
/*
DBUG_ASSERT(0);
*/
......
......@@ -1147,7 +1147,7 @@ SPIDER_TRX *spider_get_trx(
if (
!thd ||
!(trx = (SPIDER_TRX*) *thd_ha_data(thd, spider_hton_ptr))
!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr))
) {
DBUG_PRINT("info",("spider create new trx"));
if (!(trx = (SPIDER_TRX *)
......@@ -1185,7 +1185,7 @@ SPIDER_TRX *spider_get_trx(
goto error_init_hash;
spider_alloc_calc_mem_init(trx->trx_conn_hash, 151);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_conn_hash,
trx->trx_conn_hash.array.max_element *
trx->trx_conn_hash.array.size_of_element);
......@@ -1197,7 +1197,7 @@ SPIDER_TRX *spider_get_trx(
goto error_init_another_hash;
spider_alloc_calc_mem_init(trx->trx_another_conn_hash, 152);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_another_conn_hash,
trx->trx_another_conn_hash.array.max_element *
trx->trx_another_conn_hash.array.size_of_element);
......@@ -1210,7 +1210,7 @@ SPIDER_TRX *spider_get_trx(
goto error_hs_r_init_hash;
spider_alloc_calc_mem_init(trx->trx_hs_r_conn_hash, 153);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_hs_r_conn_hash,
trx->trx_hs_r_conn_hash.array.max_element *
trx->trx_hs_r_conn_hash.array.size_of_element);
......@@ -1222,7 +1222,7 @@ SPIDER_TRX *spider_get_trx(
goto error_hs_w_init_hash;
spider_alloc_calc_mem_init(trx->trx_hs_w_conn_hash, 154);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_hs_w_conn_hash,
trx->trx_hs_w_conn_hash.array.max_element *
trx->trx_hs_w_conn_hash.array.size_of_element);
......@@ -1236,7 +1236,7 @@ SPIDER_TRX *spider_get_trx(
goto error_direct_hs_r_init_hash;
spider_alloc_calc_mem_init(trx->trx_direct_hs_r_conn_hash, 155);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_direct_hs_r_conn_hash,
trx->trx_direct_hs_r_conn_hash.array.max_element *
trx->trx_direct_hs_r_conn_hash.array.size_of_element);
......@@ -1248,7 +1248,7 @@ SPIDER_TRX *spider_get_trx(
goto error_direct_hs_w_init_hash;
spider_alloc_calc_mem_init(trx->trx_direct_hs_w_conn_hash, 156);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_direct_hs_w_conn_hash,
trx->trx_direct_hs_w_conn_hash.array.max_element *
trx->trx_direct_hs_w_conn_hash.array.size_of_element);
......@@ -1261,7 +1261,7 @@ SPIDER_TRX *spider_get_trx(
goto error_init_alter_hash;
spider_alloc_calc_mem_init(trx->trx_alter_table_hash, 157);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_alter_table_hash,
trx->trx_alter_table_hash.array.max_element *
trx->trx_alter_table_hash.array.size_of_element);
......@@ -1273,7 +1273,7 @@ SPIDER_TRX *spider_get_trx(
goto error_init_trx_ha_hash;
spider_alloc_calc_mem_init(trx->trx_ha_hash, 158);
spider_alloc_calc_mem(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_ha_hash,
trx->trx_ha_hash.array.max_element *
trx->trx_ha_hash.array.size_of_element);
......@@ -1395,7 +1395,7 @@ SPIDER_TRX *spider_get_trx(
pthread_mutex_unlock(&spider_allocated_thds_mutex);
trx->registed_allocated_thds = TRUE;
}
*thd_ha_data(thd, spider_hton_ptr) = (void *) trx;
thd_set_ha_data(thd, spider_hton_ptr, trx);
}
}
......@@ -1441,7 +1441,7 @@ SPIDER_TRX *spider_get_trx(
my_hash_free(&trx->trx_ha_hash);
error_init_trx_ha_hash:
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_alter_table_hash_id,
trx->trx_alter_table_hash.array.max_element *
trx->trx_alter_table_hash.array.size_of_element);
......@@ -1449,14 +1449,14 @@ SPIDER_TRX *spider_get_trx(
error_init_alter_hash:
#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_direct_hs_w_conn_hash_id,
trx->trx_direct_hs_w_conn_hash.array.max_element *
trx->trx_direct_hs_w_conn_hash.array.size_of_element);
my_hash_free(&trx->trx_direct_hs_w_conn_hash);
error_direct_hs_w_init_hash:
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_direct_hs_r_conn_hash_id,
trx->trx_direct_hs_r_conn_hash.array.max_element *
trx->trx_direct_hs_r_conn_hash.array.size_of_element);
......@@ -1465,14 +1465,14 @@ SPIDER_TRX *spider_get_trx(
#endif
#if defined(HS_HAS_SQLCOM) && defined(HAVE_HANDLERSOCKET)
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_hs_w_conn_hash_id,
trx->trx_hs_w_conn_hash.array.max_element *
trx->trx_hs_w_conn_hash.array.size_of_element);
my_hash_free(&trx->trx_hs_w_conn_hash);
error_hs_w_init_hash:
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_hs_r_conn_hash_id,
trx->trx_hs_r_conn_hash.array.max_element *
trx->trx_hs_r_conn_hash.array.size_of_element);
......@@ -1480,14 +1480,14 @@ SPIDER_TRX *spider_get_trx(
error_hs_r_init_hash:
#endif
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_another_conn_hash_id,
trx->trx_another_conn_hash.array.max_element *
trx->trx_another_conn_hash.array.size_of_element);
my_hash_free(&trx->trx_another_conn_hash);
error_init_another_hash:
spider_free_mem_calc(
thd ? ((SPIDER_TRX *) *thd_ha_data(thd, spider_hton_ptr)) : NULL,
thd ? ((SPIDER_TRX *) thd_get_ha_data(thd, spider_hton_ptr)) : NULL,
trx->trx_conn_hash_id,
trx->trx_conn_hash.array.max_element *
trx->trx_conn_hash.array.size_of_element);
......@@ -1526,7 +1526,7 @@ int spider_free_trx(
if (need_lock)
pthread_mutex_unlock(&spider_allocated_thds_mutex);
}
*thd_ha_data(trx->thd, spider_hton_ptr) = (void *) NULL;
thd_set_ha_data(trx->thd, spider_hton_ptr, NULL);
}
spider_free_trx_alloc(trx);
spider_merge_mem_calc(trx, TRUE);
......@@ -3298,7 +3298,7 @@ int spider_commit(
SPIDER_CONN *conn;
DBUG_ENTER("spider_commit");
if (!(trx = (SPIDER_TRX*) *thd_ha_data(thd, spider_hton_ptr)))
if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)))
DBUG_RETURN(0); /* transaction is not started */
#ifdef HA_CAN_BULK_ACCESS
......@@ -3388,7 +3388,7 @@ int spider_rollback(
SPIDER_CONN *conn;
DBUG_ENTER("spider_rollback");
if (!(trx = (SPIDER_TRX*) *thd_ha_data(thd, spider_hton_ptr)))
if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)))
DBUG_RETURN(0); /* transaction is not started */
#ifdef HA_CAN_BULK_ACCESS
......@@ -3463,7 +3463,7 @@ int spider_xa_prepare(
if (all || (!thd_test_options(thd, OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN)))
{
if (!(trx = (SPIDER_TRX*) *thd_ha_data(thd, spider_hton_ptr)))
if (!(trx = (SPIDER_TRX*) thd_get_ha_data(thd, spider_hton_ptr)))
DBUG_RETURN(0); /* transaction is not started */
DBUG_PRINT("info",("spider trx_start=%s",
......
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