1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/* Copyright 2018 Codership Oy <info@codership.com>
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "my_global.h"
#include "wsrep_storage_service.h"
#include "wsrep_trans_observer.h" /* wsrep_open() */
#include "wsrep_schema.h"
#include "wsrep_binlog.h"
#include "sql_class.h"
#include "mysqld.h" /* next_query_id() */
#include "slave.h" /* opt_log_slave_updates() */
#include "transaction.h" /* trans_commit(), trans_rollback() */
/*
Temporarily enable wsrep on thd
*/
class Wsrep_on
{
public:
Wsrep_on(THD* thd)
: m_thd(thd)
, m_wsrep_on(thd->variables.wsrep_on)
{
thd->variables.wsrep_on= TRUE;
}
~Wsrep_on()
{
m_thd->variables.wsrep_on= m_wsrep_on;
}
private:
THD* m_thd;
my_bool m_wsrep_on;
};
Wsrep_storage_service::Wsrep_storage_service(THD* thd)
: wsrep::storage_service()
, wsrep::high_priority_context(thd->wsrep_cs())
, m_thd(thd)
{
thd->security_ctx->skip_grants();
thd->system_thread= SYSTEM_THREAD_SLAVE_SQL;
/* No binlogging */
/* No general log */
thd->variables.option_bits |= OPTION_LOG_OFF;
/* Read committed isolation to avoid gap locking */
thd->variables.tx_isolation = ISO_READ_COMMITTED;
/* Keep wsrep on to enter commit ordering hooks */
thd->variables.wsrep_on= 1;
thd->wsrep_skip_locking= true;
wsrep_open(thd);
wsrep_before_command(thd);
}
Wsrep_storage_service::~Wsrep_storage_service()
{
wsrep_after_command_ignore_result(m_thd);
wsrep_close(m_thd);
m_thd->wsrep_skip_locking= false;
}
int Wsrep_storage_service::start_transaction(const wsrep::ws_handle& ws_handle)
{
DBUG_ENTER("Wsrep_storage_service::start_transaction");
DBUG_ASSERT(m_thd == current_thd);
DBUG_PRINT("info", ("Wsrep_storage_service::start_transcation(%llu, %p)",
m_thd->thread_id, m_thd));
m_thd->set_wsrep_next_trx_id(ws_handle.transaction_id().get());
DBUG_RETURN(m_thd->wsrep_cs().start_transaction(
wsrep::transaction_id(m_thd->wsrep_next_trx_id())) ||
trans_begin(m_thd, MYSQL_START_TRANS_OPT_READ_WRITE));
}
void Wsrep_storage_service::adopt_transaction(const wsrep::transaction& transaction)
{
DBUG_ENTER("Wsrep_Storage_server::adopt_transaction");
DBUG_ASSERT(m_thd == current_thd);
m_thd->wsrep_cs().adopt_transaction(transaction);
trans_begin(m_thd, MYSQL_START_TRANS_OPT_READ_WRITE);
DBUG_VOID_RETURN;
}
int Wsrep_storage_service::append_fragment(const wsrep::id& server_id,
wsrep::transaction_id transaction_id,
int flags,
const wsrep::const_buffer& data,
const wsrep::xid& xid WSREP_UNUSED)
{
DBUG_ENTER("Wsrep_storage_service::append_fragment");
DBUG_ASSERT(m_thd == current_thd);
DBUG_PRINT("info", ("Wsrep_storage_service::append_fragment(%llu, %p)",
m_thd->thread_id, m_thd));
int ret= wsrep_schema->append_fragment(m_thd,
server_id,
transaction_id,
wsrep::seqno(-1),
flags,
data);
DBUG_RETURN(ret);
}
int Wsrep_storage_service::update_fragment_meta(const wsrep::ws_meta& ws_meta)
{
DBUG_ENTER("Wsrep_storage_service::update_fragment_meta");
DBUG_ASSERT(m_thd == current_thd);
DBUG_PRINT("info", ("Wsrep_storage_service::update_fragment_meta(%llu, %p)",
m_thd->thread_id, m_thd));
int ret= wsrep_schema->update_fragment_meta(m_thd, ws_meta);
DBUG_RETURN(ret);
}
int Wsrep_storage_service::remove_fragments()
{
DBUG_ENTER("Wsrep_storage_service::remove_fragments");
DBUG_ASSERT(m_thd == current_thd);
int ret= wsrep_schema->remove_fragments(m_thd,
m_thd->wsrep_trx().server_id(),
m_thd->wsrep_trx().id(),
m_thd->wsrep_sr().fragments());
DBUG_RETURN(ret);
}
int Wsrep_storage_service::commit(const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta)
{
DBUG_ENTER("Wsrep_storage_service::commit");
DBUG_ASSERT(m_thd == current_thd);
DBUG_PRINT("info", ("Wsrep_storage_service::commit(%llu, %p)",
m_thd->thread_id, m_thd));
WSREP_DEBUG("Storage service commit: %llu, %lld",
ws_meta.transaction_id().get(), ws_meta.seqno().get());
int ret= 0;
const bool is_ordered= !ws_meta.seqno().is_undefined();
if (is_ordered)
{
ret= m_thd->wsrep_cs().prepare_for_ordering(ws_handle, ws_meta, true);
}
ret= ret || trans_commit(m_thd);
if (!is_ordered)
{
/* Wsrep commit was not ordered so it does not go through commit time
hooks and remains active. Roll it back to make cleanup happen
in after_applying() call. */
m_thd->wsrep_cs().before_rollback();
m_thd->wsrep_cs().after_rollback();
}
else if (ret)
{
/* Commit failed, this probably means that the parent SR transaction
was BF aborted. Roll back out of order, the parent
transaction will release commit order after it has rolled back. */
m_thd->wsrep_cs().prepare_for_ordering(wsrep::ws_handle(),
wsrep::ws_meta(),
false);
trans_rollback(m_thd);
}
m_thd->wsrep_cs().after_applying();
m_thd->release_transactional_locks();
DBUG_RETURN(ret);
}
int Wsrep_storage_service::rollback(const wsrep::ws_handle& ws_handle,
const wsrep::ws_meta& ws_meta)
{
DBUG_ENTER("Wsrep_storage_service::rollback");
DBUG_ASSERT(m_thd == current_thd);
DBUG_PRINT("info", ("Wsrep_storage_service::rollback(%llu, %p)",
m_thd->thread_id, m_thd));
int ret= (m_thd->wsrep_cs().prepare_for_ordering(
ws_handle, ws_meta, false) ||
trans_rollback(m_thd));
m_thd->wsrep_cs().after_applying();
m_thd->release_transactional_locks();
DBUG_RETURN(ret);
}
void Wsrep_storage_service::store_globals()
{
wsrep_store_threadvars(m_thd);
}
void Wsrep_storage_service::reset_globals()
{
wsrep_reset_threadvars(m_thd);
}
|