summaryrefslogtreecommitdiff
path: root/sql/wsrep_client_service.cc
blob: 994fa97db606580d1af519ba7a2ee7c3b6531b0e (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* 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 "wsrep_client_service.h"
#include "wsrep_high_priority_service.h"
#include "wsrep_applier.h" /* wsrep_apply_events() */
#include "wsrep_binlog.h"  /* wsrep_dump_rbr_buf() */
#include "wsrep_schema.h"  /* remove_fragments() */
#include "wsrep_thd.h"
#include "wsrep_xid.h"
#include "wsrep_trans_observer.h"

#include "sql_base.h"    /* close_temporary_table() */
#include "sql_class.h"   /* THD */
#include "sql_parse.h"   /* stmt_causes_implicit_commit() */
#include "rpl_filter.h"  /* binlog_filter */
#include "rpl_rli.h"     /* Relay_log_info */
#include "slave.h"   /* opt_log_slave_updates */
#include "transaction.h" /* trans_commit()... */
#include "log.h"      /* stmt_has_updated_trans_table() */
//#include "debug_sync.h"
#include "mysql/service_debug_sync.h"
namespace
{

void debug_sync_caller(THD* thd, const char* sync_point)
{
#ifdef ENABLED_DEBUG_SYNC_OUT
  debug_sync_set_action(thd, sync_point, strlen(sync_point));
#endif
#ifdef ENABLED_DEBUG_SYNC
  if (debug_sync_service) debug_sync_service(thd,sync_point,strlen(sync_point));
#endif

}
}

Wsrep_client_service::Wsrep_client_service(THD* thd,
                                           Wsrep_client_state& client_state)
  : wsrep::client_service()
  , m_thd(thd)
  , m_client_state(client_state)
{ }

void Wsrep_client_service::store_globals()
{
  DBUG_ENTER("Wsrep_client_service::store_globals");
  m_thd->store_globals();
  DBUG_VOID_RETURN;
}

void Wsrep_client_service::reset_globals()
{
  DBUG_ENTER("Wsrep_client_service::reset_globals");
  m_thd->reset_globals();
  DBUG_VOID_RETURN;
}

bool Wsrep_client_service::interrupted() const
{
  DBUG_ASSERT(m_thd == current_thd);
  mysql_mutex_lock(&m_thd->LOCK_thd_data);

  /* wsrep state can be interrupted only if THD was explicitly killed,
     for wsrep conflicts, we use deadlock error only
   */
  bool ret= (m_thd->killed != NOT_KILLED                                    &&
             m_thd->wsrep_trx().state() != wsrep::transaction::s_must_abort &&
             m_thd->wsrep_trx().state() != wsrep::transaction::s_aborting   &&
             m_thd->wsrep_trx().state() != wsrep::transaction::s_aborted);
  mysql_mutex_unlock(&m_thd->LOCK_thd_data);
  if (ret)
  {
      WSREP_DEBUG("wsrep state is interrupted, THD::killed %d trx state %d",
                  m_thd->killed,  m_thd->wsrep_trx().state());
  }
  return ret;
}

int Wsrep_client_service::prepare_data_for_replication()
{
  DBUG_ASSERT(m_thd == current_thd);
  DBUG_ENTER("Wsrep_client_service::prepare_data_for_replication");
  size_t data_len= 0;
  IO_CACHE* cache= wsrep_get_trans_cache(m_thd);

  if (cache)
  {
    m_thd->binlog_flush_pending_rows_event(true);
    if (wsrep_write_cache(m_thd, cache, &data_len))
    {
      WSREP_ERROR("rbr write fail, data_len: %zu",
                  data_len);
      // wsrep_override_error(m_thd, ER_ERROR_DURING_COMMIT);
      DBUG_RETURN(1);
    }
  }

  if (data_len == 0)
  {
    if (m_thd->get_stmt_da()->is_ok()              &&
        m_thd->get_stmt_da()->affected_rows() > 0  &&
        !binlog_filter->is_on() &&
        !m_thd->wsrep_trx().is_streaming())
    {
      WSREP_DEBUG("empty rbr buffer, query: %s, "
                  "affected rows: %llu, "
                  "changed tables: %d, "
                  "sql_log_bin: %d",
                  WSREP_QUERY(m_thd),
                  m_thd->get_stmt_da()->affected_rows(),
                  stmt_has_updated_trans_table(m_thd),
                  m_thd->variables.sql_log_bin);
    }
    else
    {
      WSREP_DEBUG("empty rbr buffer, query: %s", WSREP_QUERY(m_thd));
    }
  }
  DBUG_RETURN(0);
}


void Wsrep_client_service::cleanup_transaction()
{
  DBUG_ASSERT(m_thd == current_thd);
  if (WSREP_EMULATE_BINLOG(m_thd)) wsrep_thd_binlog_trx_reset(m_thd);
  m_thd->wsrep_affected_rows= 0;
}


int Wsrep_client_service::prepare_fragment_for_replication(wsrep::mutable_buffer& buffer)
{
  DBUG_ASSERT(m_thd == current_thd);
  THD* thd= m_thd;
  DBUG_ENTER("Wsrep_client_service::prepare_fragment_for_replication");
  IO_CACHE* cache= wsrep_get_trans_cache(thd);
  thd->binlog_flush_pending_rows_event(true);

  if (!cache)
  {
    DBUG_RETURN(0);
  }

  const my_off_t saved_pos(my_b_tell(cache));
  if (reinit_io_cache(cache, READ_CACHE, thd->wsrep_sr().bytes_certified(), 0, 0))
  {
    DBUG_RETURN(1);
  }

  int ret= 0;
  size_t total_length= 0;
  size_t length= my_b_bytes_in_cache(cache);

  if (!length)
  {
    length= my_b_fill(cache);
  }

  if (length > 0)
  {
    do
    {
      total_length+= length;
      if (total_length > wsrep_max_ws_size)
      {
        WSREP_WARN("transaction size limit (%lu) exceeded: %zu",
                   wsrep_max_ws_size, total_length);
        ret= 1;
        goto cleanup;
      }

      buffer.push_back(reinterpret_cast<const char*>(cache->read_pos),
                       reinterpret_cast<const char*>(cache->read_pos + length));
      cache->read_pos= cache->read_end;
    }
    while (cache->file >= 0 && (length= my_b_fill(cache)));
  }
  DBUG_ASSERT(total_length == buffer.size());
cleanup:
  if (reinit_io_cache(cache, WRITE_CACHE, saved_pos, 0, 0))
  {
    WSREP_WARN("Failed to reinitialize IO cache");
    ret= 1;
  }
  DBUG_RETURN(ret);
}

int Wsrep_client_service::remove_fragments()
{
  DBUG_ENTER("Wsrep_client_service::remove_fragments");
  if (wsrep_schema->remove_fragments(m_thd,
                                     Wsrep_server_state::instance().id(),
                                     m_thd->wsrep_trx().id(),
                                     m_thd->wsrep_sr().fragments()))
  {
    WSREP_DEBUG("Failed to remove fragments from SR storage for transaction "
                "%llu, %llu",
                m_thd->thread_id, m_thd->wsrep_trx().id().get());
    DBUG_RETURN(1);
  }
  DBUG_RETURN(0);
}

bool Wsrep_client_service::statement_allowed_for_streaming() const
{
  /*
    Todo: Decide if implicit commit is allowed with streaming
    replication.
    !stmt_causes_implicit_commit(m_thd, CF_IMPLICIT_COMMIT_BEGIN);
  */
  return true;
}

size_t Wsrep_client_service::bytes_generated() const
{
  IO_CACHE* cache= wsrep_get_trans_cache(m_thd);
  if (cache)
  {
    m_thd->binlog_flush_pending_rows_event(true);
    return my_b_tell(cache);
  }
  return 0;
}

void Wsrep_client_service::will_replay()
{
  DBUG_ASSERT(m_thd == current_thd);
  mysql_mutex_lock(&LOCK_wsrep_replaying);
  ++wsrep_replaying;
  mysql_mutex_unlock(&LOCK_wsrep_replaying);
}

enum wsrep::provider::status Wsrep_client_service::replay()
{
  DBUG_ASSERT(m_thd == current_thd);
  Wsrep_replayer_service replayer_service(m_thd);
  wsrep::provider& provider(m_thd->wsrep_cs().provider());
  mysql_mutex_lock(&m_thd->LOCK_thd_data);
  m_thd->killed= NOT_KILLED;
  mysql_mutex_unlock(&m_thd->LOCK_thd_data);
  enum wsrep::provider::status ret=
    provider.replay(m_thd->wsrep_trx().ws_handle(), &replayer_service);
  replayer_service.replay_status(ret);
  mysql_mutex_lock(&LOCK_wsrep_replaying);
  --wsrep_replaying;
  mysql_mutex_unlock(&LOCK_wsrep_replaying);
  return ret;
}

void Wsrep_client_service::wait_for_replayers(wsrep::unique_lock<wsrep::mutex>& lock)
{
  DBUG_ASSERT(m_thd == current_thd);
  lock.unlock();
  mysql_mutex_lock(&LOCK_wsrep_replaying);
  while (wsrep_replaying > 0)
  {
    mysql_cond_wait(&COND_wsrep_replaying, &LOCK_wsrep_replaying);
  }
  mysql_mutex_unlock(&LOCK_wsrep_replaying);
  lock.lock();
}

void Wsrep_client_service::debug_sync(const char* sync_point)
{
  DBUG_ASSERT(m_thd == current_thd);
  debug_sync_caller(m_thd, sync_point);
}

void Wsrep_client_service::debug_crash(const char* crash_point)
{
  // DBUG_ASSERT(m_thd == current_thd);
  DBUG_EXECUTE_IF(crash_point, DBUG_SUICIDE(); );
}

int Wsrep_client_service::bf_rollback()
{
  DBUG_ASSERT(m_thd == current_thd);
  DBUG_ENTER("Wsrep_client_service::rollback");

  int ret= (trans_rollback_stmt(m_thd) || trans_rollback(m_thd));
  if (m_thd->locked_tables_mode && m_thd->lock)
  {
    m_thd->locked_tables_list.unlock_locked_tables(m_thd);
    m_thd->variables.option_bits&= ~OPTION_TABLE_LOCK;
  }
  if (m_thd->global_read_lock.is_acquired())
  {
    m_thd->global_read_lock.unlock_global_read_lock(m_thd);
  }
  m_thd->mdl_context.release_transactional_locks();
  m_thd->mdl_context.release_explicit_locks();

  DBUG_RETURN(ret);
}