mariadb/storage/federatedx/federatedx_txn.cc

429 lines
8.6 KiB
C++
Raw Normal View History

/*
Copyright (c) 2007, Antony T Curtis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Neither the name of FederatedX nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2011-04-25 17:22:25 +02:00
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
2011-04-25 17:22:25 +02:00
#define MYSQL_SERVER 1
MDEV-5120 Test suite test maria-no-logging fails The reason for the failure was a bug in an include file on debian that causes 'struct stat' to have different sized depending on the environment. This patch fixes so that we always include my_global.h or my_config.h before we include any other files. Other things: - Removed #include <my_global.h> in some include files; Better to always do this at the top level to have as few "always-include-this-file-first' files as possible. - Removed usage of some include files that where already included by my_global.h or by other files. client/mysql_plugin.c: Use my_global.h first client/mysqlslap.c: Remove duplicated include files extra/comp_err.c: Remove duplicated include files include/m_string.h: Remove duplicated include files include/maria.h: Remove duplicated include files libmysqld/emb_qcache.cc: Use my_global.h first plugin/semisync/semisync.h: Use my_pthread.h first sql/datadict.cc: Use my_global.h first sql/debug_sync.cc: Use my_global.h first sql/derror.cc: Use my_global.h first sql/des_key_file.cc: Use my_global.h first sql/discover.cc: Use my_global.h first sql/event_data_objects.cc: Use my_global.h first sql/event_db_repository.cc: Use my_global.h first sql/event_parse_data.cc: Use my_global.h first sql/event_queue.cc: Use my_global.h first sql/event_scheduler.cc: Use my_global.h first sql/events.cc: Use my_global.h first sql/field.cc: Use my_global.h first Remove duplicated include files sql/field_conv.cc: Use my_global.h first sql/filesort.cc: Use my_global.h first Remove duplicated include files sql/gstream.cc: Use my_global.h first sql/ha_ndbcluster.cc: Use my_global.h first sql/ha_ndbcluster_binlog.cc: Use my_global.h first sql/ha_ndbcluster_cond.cc: Use my_global.h first sql/ha_partition.cc: Use my_global.h first sql/handler.cc: Use my_global.h first sql/hash_filo.cc: Use my_global.h first sql/hostname.cc: Use my_global.h first sql/init.cc: Use my_global.h first sql/item.cc: Use my_global.h first sql/item_buff.cc: Use my_global.h first sql/item_cmpfunc.cc: Use my_global.h first sql/item_create.cc: Use my_global.h first sql/item_geofunc.cc: Use my_global.h first sql/item_inetfunc.cc: Use my_global.h first sql/item_row.cc: Use my_global.h first sql/item_strfunc.cc: Use my_global.h first sql/item_subselect.cc: Use my_global.h first sql/item_sum.cc: Use my_global.h first sql/item_timefunc.cc: Use my_global.h first sql/item_xmlfunc.cc: Use my_global.h first sql/key.cc: Use my_global.h first sql/lock.cc: Use my_global.h first sql/log.cc: Use my_global.h first sql/log_event.cc: Use my_global.h first sql/log_event_old.cc: Use my_global.h first sql/mf_iocache.cc: Use my_global.h first sql/mysql_install_db.cc: Remove duplicated include files sql/mysqld.cc: Remove duplicated include files sql/net_serv.cc: Remove duplicated include files sql/opt_range.cc: Use my_global.h first sql/opt_subselect.cc: Use my_global.h first sql/opt_sum.cc: Use my_global.h first sql/parse_file.cc: Use my_global.h first sql/partition_info.cc: Use my_global.h first sql/procedure.cc: Use my_global.h first sql/protocol.cc: Use my_global.h first sql/records.cc: Use my_global.h first sql/records.h: Don't include my_global.h Better to do this at the upper level sql/repl_failsafe.cc: Use my_global.h first sql/rpl_filter.cc: Use my_global.h first sql/rpl_gtid.cc: Use my_global.h first sql/rpl_handler.cc: Use my_global.h first sql/rpl_injector.cc: Use my_global.h first sql/rpl_record.cc: Use my_global.h first sql/rpl_record_old.cc: Use my_global.h first sql/rpl_reporting.cc: Use my_global.h first sql/rpl_rli.cc: Use my_global.h first sql/rpl_tblmap.cc: Use my_global.h first sql/rpl_utility.cc: Use my_global.h first sql/set_var.cc: Added comment sql/slave.cc: Use my_global.h first sql/sp.cc: Use my_global.h first sql/sp_cache.cc: Use my_global.h first sql/sp_head.cc: Use my_global.h first sql/sp_pcontext.cc: Use my_global.h first sql/sp_rcontext.cc: Use my_global.h first sql/spatial.cc: Use my_global.h first sql/sql_acl.cc: Use my_global.h first sql/sql_admin.cc: Use my_global.h first sql/sql_analyse.cc: Use my_global.h first sql/sql_audit.cc: Use my_global.h first sql/sql_base.cc: Use my_global.h first sql/sql_binlog.cc: Use my_global.h first sql/sql_bootstrap.cc: Use my_global.h first Use my_global.h first sql/sql_cache.cc: Use my_global.h first sql/sql_class.cc: Use my_global.h first sql/sql_client.cc: Use my_global.h first sql/sql_connect.cc: Use my_global.h first sql/sql_crypt.cc: Use my_global.h first sql/sql_cursor.cc: Use my_global.h first sql/sql_db.cc: Use my_global.h first sql/sql_delete.cc: Use my_global.h first sql/sql_derived.cc: Use my_global.h first sql/sql_do.cc: Use my_global.h first sql/sql_error.cc: Use my_global.h first sql/sql_explain.cc: Use my_global.h first sql/sql_expression_cache.cc: Use my_global.h first sql/sql_handler.cc: Use my_global.h first sql/sql_help.cc: Use my_global.h first sql/sql_insert.cc: Use my_global.h first sql/sql_lex.cc: Use my_global.h first sql/sql_load.cc: Use my_global.h first sql/sql_locale.cc: Use my_global.h first sql/sql_manager.cc: Use my_global.h first sql/sql_parse.cc: Use my_global.h first sql/sql_partition.cc: Use my_global.h first sql/sql_plugin.cc: Added comment sql/sql_prepare.cc: Use my_global.h first sql/sql_priv.h: Added error if we use this before including my_global.h This check is here becasue so many files includes sql_priv.h first. sql/sql_profile.cc: Use my_global.h first sql/sql_reload.cc: Use my_global.h first sql/sql_rename.cc: Use my_global.h first sql/sql_repl.cc: Use my_global.h first sql/sql_select.cc: Use my_global.h first sql/sql_servers.cc: Use my_global.h first sql/sql_show.cc: Added comment sql/sql_signal.cc: Use my_global.h first sql/sql_statistics.cc: Use my_global.h first sql/sql_table.cc: Use my_global.h first sql/sql_tablespace.cc: Use my_global.h first sql/sql_test.cc: Use my_global.h first sql/sql_time.cc: Use my_global.h first sql/sql_trigger.cc: Use my_global.h first sql/sql_udf.cc: Use my_global.h first sql/sql_union.cc: Use my_global.h first sql/sql_update.cc: Use my_global.h first sql/sql_view.cc: Use my_global.h first sql/sys_vars.cc: Added comment sql/table.cc: Use my_global.h first sql/thr_malloc.cc: Use my_global.h first sql/transaction.cc: Use my_global.h first sql/uniques.cc: Use my_global.h first sql/unireg.cc: Use my_global.h first sql/unireg.h: Removed inclusion of my_global.h storage/archive/ha_archive.cc: Added comment storage/blackhole/ha_blackhole.cc: Use my_global.h first storage/csv/ha_tina.cc: Use my_global.h first storage/csv/transparent_file.cc: Use my_global.h first storage/federated/ha_federated.cc: Use my_global.h first storage/federatedx/federatedx_io.cc: Use my_global.h first storage/federatedx/federatedx_io_mysql.cc: Use my_global.h first storage/federatedx/federatedx_io_null.cc: Use my_global.h first storage/federatedx/federatedx_txn.cc: Use my_global.h first storage/heap/ha_heap.cc: Use my_global.h first storage/innobase/handler/handler0alter.cc: Use my_global.h first storage/maria/ha_maria.cc: Use my_global.h first storage/maria/unittest/ma_maria_log_cleanup.c: Remove duplicated include files storage/maria/unittest/test_file.c: Added comment storage/myisam/ha_myisam.cc: Move sql_plugin.h first as this includes my_global.h storage/myisammrg/ha_myisammrg.cc: Use my_global.h first storage/oqgraph/oqgraph_thunk.cc: Use my_config.h and my_global.h first One could not include my_global.h before oqgraph_thunk.h (don't know why) storage/spider/ha_spider.cc: Use my_global.h first storage/spider/hs_client/config.cpp: Use my_global.h first storage/spider/hs_client/escape.cpp: Use my_global.h first storage/spider/hs_client/fatal.cpp: Use my_global.h first storage/spider/hs_client/hstcpcli.cpp: Use my_global.h first storage/spider/hs_client/socket.cpp: Use my_global.h first storage/spider/hs_client/string_util.cpp: Use my_global.h first storage/spider/spd_conn.cc: Use my_global.h first storage/spider/spd_copy_tables.cc: Use my_global.h first storage/spider/spd_db_conn.cc: Use my_global.h first storage/spider/spd_db_handlersocket.cc: Use my_global.h first storage/spider/spd_db_mysql.cc: Use my_global.h first storage/spider/spd_db_oracle.cc: Use my_global.h first storage/spider/spd_direct_sql.cc: Use my_global.h first storage/spider/spd_i_s.cc: Use my_global.h first storage/spider/spd_malloc.cc: Use my_global.h first storage/spider/spd_param.cc: Use my_global.h first storage/spider/spd_ping_table.cc: Use my_global.h first storage/spider/spd_sys_table.cc: Use my_global.h first storage/spider/spd_table.cc: Use my_global.h first storage/spider/spd_trx.cc: Use my_global.h first storage/xtradb/handler/handler0alter.cc: Use my_global.h first storage/xtradb/handler/i_s.cc: Use my_global.h first
2014-09-30 19:31:14 +02:00
#include <my_global.h>
2011-04-25 17:22:25 +02:00
#include "sql_priv.h"
#include "ha_federatedx.h"
#include "m_string.h"
2011-04-25 17:22:25 +02:00
#include "table.h"
#include "sql_servers.h"
federatedx_txn::federatedx_txn()
: txn_list(0), savepoint_level(0), savepoint_stmt(0), savepoint_next(0)
{
DBUG_ENTER("federatedx_txn::federatedx_txn");
DBUG_VOID_RETURN;
}
federatedx_txn::~federatedx_txn()
{
DBUG_ENTER("federatedx_txn::~federatedx_txn");
DBUG_ASSERT(!txn_list);
DBUG_VOID_RETURN;
}
void federatedx_txn::close(FEDERATEDX_SERVER *server)
{
uint count= 0;
federatedx_io *io, **iop;
DBUG_ENTER("federatedx_txn::close");
DBUG_ASSERT(!server->use_count);
DBUG_PRINT("info",("use count: %u connections: %u",
server->use_count, server->io_count));
for (iop= &txn_list; (io= *iop);)
{
if (io->server != server)
iop= &io->txn_next;
else
{
*iop= io->txn_next;
io->txn_next= NULL;
io->busy= FALSE;
io->idle_next= server->idle_list;
server->idle_list= io;
}
}
while ((io= server->idle_list))
{
server->idle_list= io->idle_next;
delete io;
count++;
}
DBUG_PRINT("info",("closed %u connections, txn_list: %s", count,
txn_list ? "active": "empty"));
DBUG_VOID_RETURN;
}
int federatedx_txn::acquire(FEDERATEDX_SHARE *share, void *thd,
bool readonly, federatedx_io **ioptr)
{
federatedx_io *io;
FEDERATEDX_SERVER *server= share->s;
DBUG_ENTER("federatedx_txn::acquire");
DBUG_ASSERT(ioptr && server);
if (!(io= *ioptr))
{
/* check to see if we have an available IO connection */
for (io= txn_list; io; io= io->txn_next)
if (io->server == server)
break;
if (!io)
{
/* check to see if there are any unowned IO connections */
mysql_mutex_lock(&server->mutex);
if ((io= server->idle_list))
{
server->idle_list= io->idle_next;
io->idle_next= NULL;
}
else
io= federatedx_io::construct(&server->mem_root, server);
io->txn_next= txn_list;
txn_list= io;
mysql_mutex_unlock(&server->mutex);
}
if (io->busy)
*io->owner_ptr= NULL;
io->busy= TRUE;
io->owner_ptr= ioptr;
io->set_thd(thd);
}
DBUG_ASSERT(io->busy && io->server == server);
io->readonly&= readonly;
DBUG_RETURN((*ioptr= io) ? 0 : -1);
}
void federatedx_txn::release(federatedx_io **ioptr)
{
federatedx_io *io;
DBUG_ENTER("federatedx_txn::release");
DBUG_ASSERT(ioptr);
if ((io= *ioptr))
{
/* mark as available for reuse in this transaction */
io->busy= FALSE;
*ioptr= NULL;
DBUG_PRINT("info", ("active: %d autocommit: %d",
io->active, io->is_autocommit()));
if (io->is_autocommit())
{
io->set_thd(NULL);
io->active= FALSE;
}
}
release_scan();
DBUG_VOID_RETURN;
}
void federatedx_txn::release_scan()
{
uint count= 0, returned= 0;
federatedx_io *io, **pio;
DBUG_ENTER("federatedx_txn::release_scan");
/* return any inactive and idle connections to the server */
for (pio= &txn_list; (io= *pio); count++)
{
if (io->active || io->busy)
pio= &io->txn_next;
else
{
FEDERATEDX_SERVER *server= io->server;
/* unlink from list of connections bound to the transaction */
*pio= io->txn_next;
io->txn_next= NULL;
/* reset some values */
io->readonly= TRUE;
mysql_mutex_lock(&server->mutex);
io->idle_next= server->idle_list;
server->idle_list= io;
mysql_mutex_unlock(&server->mutex);
returned++;
}
}
DBUG_PRINT("info",("returned %u of %u connections(s)", returned, count));
DBUG_VOID_RETURN;
}
bool federatedx_txn::txn_begin()
{
ulong level= 0;
DBUG_ENTER("federatedx_txn::txn_begin");
if (savepoint_next == 0)
{
savepoint_next++;
savepoint_level= savepoint_stmt= 0;
sp_acquire(&level);
}
DBUG_RETURN(level == 1);
}
int federatedx_txn::txn_commit()
{
int error= 0;
federatedx_io *io;
DBUG_ENTER("federatedx_txn::txn_commit");
if (savepoint_next)
{
DBUG_ASSERT(savepoint_stmt != 1);
for (io= txn_list; io; io= io->txn_next)
{
int rc= 0;
if (io->active)
rc= io->commit();
else
io->rollback();
if (io->active && rc)
error= -1;
io->reset();
}
release_scan();
savepoint_next= savepoint_stmt= savepoint_level= 0;
}
DBUG_RETURN(error);
}
int federatedx_txn::txn_rollback()
{
int error= 0;
federatedx_io *io;
DBUG_ENTER("federatedx_txn::txn_commit");
if (savepoint_next)
{
DBUG_ASSERT(savepoint_stmt != 1);
for (io= txn_list; io; io= io->txn_next)
{
int rc= io->rollback();
if (io->active && rc)
error= -1;
io->reset();
}
release_scan();
savepoint_next= savepoint_stmt= savepoint_level= 0;
}
DBUG_RETURN(error);
}
bool federatedx_txn::sp_acquire(ulong *sp)
{
bool rc= FALSE;
federatedx_io *io;
DBUG_ENTER("federatedx_txn::sp_acquire");
DBUG_ASSERT(sp && savepoint_next);
*sp= savepoint_level= savepoint_next++;
for (io= txn_list; io; io= io->txn_next)
{
if (io->readonly)
continue;
io->savepoint_set(savepoint_level);
rc= TRUE;
}
DBUG_RETURN(rc);
}
int federatedx_txn::sp_rollback(ulong *sp)
{
ulong level, new_level= savepoint_level;
federatedx_io *io;
DBUG_ENTER("federatedx_txn::sp_rollback");
DBUG_ASSERT(sp && savepoint_next && *sp && *sp <= savepoint_level);
for (io= txn_list; io; io= io->txn_next)
{
if (io->readonly)
continue;
if ((level= io->savepoint_rollback(*sp)) < new_level)
new_level= level;
}
savepoint_level= new_level;
DBUG_RETURN(0);
}
int federatedx_txn::sp_release(ulong *sp)
{
ulong level, new_level= savepoint_level;
federatedx_io *io;
DBUG_ENTER("federatedx_txn::sp_release");
DBUG_ASSERT(sp && savepoint_next && *sp && *sp <= savepoint_level);
for (io= txn_list; io; io= io->txn_next)
{
if (io->readonly)
continue;
if ((level= io->savepoint_release(*sp)) < new_level)
new_level= level;
}
savepoint_level= new_level;
*sp= 0;
DBUG_RETURN(0);
}
bool federatedx_txn::stmt_begin()
{
bool result= FALSE;
DBUG_ENTER("federatedx_txn::stmt_begin");
if (!savepoint_stmt)
{
if (!savepoint_next)
{
savepoint_next++;
savepoint_level= savepoint_stmt= 0;
}
result= sp_acquire(&savepoint_stmt);
}
DBUG_RETURN(result);
}
int federatedx_txn::stmt_commit()
{
int result= 0;
DBUG_ENTER("federatedx_txn::stmt_commit");
if (savepoint_stmt == 1)
{
savepoint_stmt= 0;
result= txn_commit();
}
else
if (savepoint_stmt)
result= sp_release(&savepoint_stmt);
DBUG_RETURN(result);
}
int federatedx_txn::stmt_rollback()
{
int result= 0;
DBUG_ENTER("federated:txn::stmt_rollback");
if (savepoint_stmt == 1)
{
savepoint_stmt= 0;
result= txn_rollback();
}
else
if (savepoint_stmt)
{
result= sp_rollback(&savepoint_stmt);
sp_release(&savepoint_stmt);
}
DBUG_RETURN(result);
}
void federatedx_txn::stmt_autocommit()
{
federatedx_io *io;
DBUG_ENTER("federatedx_txn::stmt_autocommit");
for (io= txn_list; savepoint_stmt && io; io= io->txn_next)
{
if (io->readonly)
continue;
io->savepoint_restrict(savepoint_stmt);
}
DBUG_VOID_RETURN;
}