File: item_gtid_func.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (344 lines) | stat: -rw-r--r-- 10,669 bytes parent folder | download
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   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, version 2.0, 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 "item_gtid_func.h"

#include <algorithm>

#include "include/scope_guard.h"  //Scope_guard
#include "sql/derror.h"           // ER_THD
#include "sql/rpl_mi.h"           // Master_info
#include "sql/rpl_msr.h"          // channel_map
#include "sql/sql_class.h"        // THD
#include "sql/sql_lex.h"

using std::max;

bool Item_wait_for_executed_gtid_set::itemize(Parse_context *pc, Item **res) {
  if (skip_itemize(res)) return false;
  if (super::itemize(pc, res)) return true;
  /*
    It is unsafe because the return value depends on timing. If the timeout
    happens, the return value is different from the one in which the function
    returns with success.
  */
  pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
  pc->thd->lex->safe_to_cache_query = false;
  return false;
}

/**
  Wait until the given gtid_set is found in the executed gtid_set independent
  of the slave threads.
*/
longlong Item_wait_for_executed_gtid_set::val_int() {
  DBUG_TRACE;
  assert(fixed);
  THD *thd = current_thd;

  String *gtid_text = args[0]->val_str(&value);
  if (gtid_text == nullptr) {
    /*
      Usually, an argument that is NULL causes an SQL function to return NULL,
      however since this is a function with side-effects, a NULL value is
      treated as an error.
    */
    if (!thd->is_error()) {
      my_error(ER_MALFORMED_GTID_SET_SPECIFICATION, MYF(0), "NULL");
    }
    return error_int();
  }

  double timeout = 0;
  if (arg_count > 1) {
    timeout = args[1]->val_real();
    if (args[1]->null_value || timeout < 0) {
      if (!thd->is_error()) {
        my_error(ER_WRONG_ARGUMENTS, MYF(0), "WAIT_FOR_EXECUTED_GTID_SET.");
      }
      return error_int();
    }
  }

  // Waiting for a GTID in a slave thread could cause the slave to
  // hang/deadlock.
  // @todo: Return error instead of NULL
  if (thd->slave_thread) {
    return error_int();
  }

  Gtid_set wait_for_gtid_set(global_sid_map, nullptr);

  Checkable_rwlock::Guard global_sid_lock_guard(*global_sid_lock,
                                                Checkable_rwlock::READ_LOCK);
  if (global_gtid_mode.get() == Gtid_mode::OFF) {
    my_error(ER_GTID_MODE_OFF, MYF(0), "use WAIT_FOR_EXECUTED_GTID_SET");
    return error_int();
  }
  gtid_state->begin_gtid_wait();
  Scope_guard x([&] { gtid_state->end_gtid_wait(); });
  if (wait_for_gtid_set.add_gtid_text(gtid_text->c_ptr_safe()) !=
      RETURN_STATUS_OK) {
    // Error has already been generated.
    return error_int();
  }

  // Cannot wait for a GTID that the thread owns since that would
  // immediately deadlock.
  if (thd->owned_gtid.sidno > 0 &&
      wait_for_gtid_set.contains_gtid(thd->owned_gtid)) {
    char buf[Gtid::MAX_TEXT_LENGTH + 1];
    thd->owned_gtid.to_string(global_sid_map, buf);
    my_error(ER_CANT_WAIT_FOR_EXECUTED_GTID_SET_WHILE_OWNING_A_GTID, MYF(0),
             buf);
    return error_int();
  }

  bool result = gtid_state->wait_for_gtid_set(thd, &wait_for_gtid_set, timeout);

  null_value = false;
  return result;
}

Item_master_gtid_set_wait::Item_master_gtid_set_wait(const POS &pos, Item *a)
    : Item_int_func(pos, a) {
  null_on_null = false;
  push_deprecated_warn(current_thd, "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS",
                       "WAIT_FOR_EXECUTED_GTID_SET");
}

Item_master_gtid_set_wait::Item_master_gtid_set_wait(const POS &pos, Item *a,
                                                     Item *b)
    : Item_int_func(pos, a, b) {
  null_on_null = false;
  push_deprecated_warn(current_thd, "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS",
                       "WAIT_FOR_EXECUTED_GTID_SET");
}

Item_master_gtid_set_wait::Item_master_gtid_set_wait(const POS &pos, Item *a,
                                                     Item *b, Item *c)
    : Item_int_func(pos, a, b, c) {
  null_on_null = false;
  push_deprecated_warn(current_thd, "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS",
                       "WAIT_FOR_EXECUTED_GTID_SET");
}

bool Item_master_gtid_set_wait::itemize(Parse_context *pc, Item **res) {
  if (skip_itemize(res)) return false;
  if (super::itemize(pc, res)) return true;
  pc->thd->lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
  pc->thd->lex->safe_to_cache_query = false;
  return false;
}

longlong Item_master_gtid_set_wait::val_int() {
  assert(fixed);
  DBUG_TRACE;
  THD *thd = current_thd;

  String *gtid = args[0]->val_str(&gtid_value);
  if (gtid == nullptr) {
    if (!thd->is_error()) {
      my_error(ER_WRONG_ARGUMENTS, MYF(0),
               "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.");
    }
    return error_int();
  }

  double timeout = 0;
  if (arg_count > 1) {
    timeout = args[1]->val_real();
    if (args[1]->null_value || timeout < 0) {
      if (!thd->is_error()) {
        my_error(ER_WRONG_ARGUMENTS, MYF(0),
                 "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.");
      }
      return error_int();
    }
  }

  if (thd->slave_thread) {
    return error_int();
  }

  Master_info *mi = nullptr;
  channel_map.rdlock();

  /* If replication channel is mentioned */
  if (arg_count > 2) {
    String *channel_str = args[2]->val_str(&channel_value);
    if (channel_str == nullptr) {
      channel_map.unlock();
      if (!thd->is_error()) {
        my_error(ER_WRONG_ARGUMENTS, MYF(0),
                 "WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.");
      }
      return error_int();
    }
    mi = channel_map.get_mi(channel_str->c_ptr_safe());
  } else {
    if (channel_map.get_num_instances() > 1) {
      channel_map.unlock();
      mi = nullptr;
      my_error(ER_REPLICA_MULTIPLE_CHANNELS_CMD, MYF(0));
      return error_int();
    } else
      mi = channel_map.get_default_channel_mi();
  }

  if ((mi != nullptr) &&
      mi->rli->m_assign_gtids_to_anonymous_transactions_info.get_type() >
          Assign_gtids_to_anonymous_transactions_info::enum_type::AGAT_OFF) {
    my_error(ER_CANT_SET_ANONYMOUS_TO_GTID_AND_WAIT_UNTIL_SQL_THD_AFTER_GTIDS,
             MYF(0));
    channel_map.unlock();
    return error_int();
  }
  if (global_gtid_mode.get() == Gtid_mode::OFF) {
    channel_map.unlock();
    return error_int();
  }
  gtid_state->begin_gtid_wait();

  if (mi != nullptr) mi->inc_reference();

  channel_map.unlock();

  bool null_result = false;
  int event_count = 0;

  if (mi != nullptr && mi->rli != nullptr) {
    event_count = mi->rli->wait_for_gtid_set(thd, gtid, timeout);
    if (event_count == -2) {
      null_result = true;
    }
  } else {
    /*
      Replication has not been set up, we should return NULL;
     */
    null_result = true;
  }
  if (mi != nullptr) mi->dec_reference();

  gtid_state->end_gtid_wait();

  null_value = false;
  return null_result ? error_int() : event_count;
}

/**
  Return 1 if both arguments are Gtid_sets and the first is a subset
  of the second.  Generate an error if any of the arguments is not a
  Gtid_set.
*/
longlong Item_func_gtid_subset::val_int() {
  DBUG_TRACE;
  assert(fixed);

  // Evaluate strings without lock
  String *string1 = args[0]->val_str(&buf1);
  if (string1 == nullptr) {
    return error_int();
  }
  String *string2 = args[1]->val_str(&buf2);
  if (string2 == nullptr) {
    return error_int();
  }

  const char *charp1 = string1->c_ptr_safe();
  assert(charp1 != nullptr);
  const char *charp2 = string2->c_ptr_safe();
  assert(charp2 != nullptr);
  int ret = 1;
  enum_return_status status;

  Sid_map sid_map(nullptr /*no rwlock*/);
  // compute sets while holding locks
  const Gtid_set sub_set(&sid_map, charp1, &status);
  if (status == RETURN_STATUS_OK) {
    const Gtid_set super_set(&sid_map, charp2, &status);
    if (status == RETURN_STATUS_OK) {
      ret = sub_set.is_subset(&super_set) ? 1 : 0;
    }
  }

  null_value = false;
  return ret;
}

bool Item_func_gtid_subtract::resolve_type(THD *thd) {
  if (param_type_is_default(thd, 0, -1)) return true;

  collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
  /*
    In the worst case, the string grows after subtraction. This
    happens when a GTID in args[0] is split by a GTID in args[1],
    e.g., UUID:1-6 minus UUID:3-4 becomes UUID:1-2,5-6.  The worst
    case is UUID:1-100 minus UUID:9, where the two characters ":9" in
    args[1] yield the five characters "-8,10" in the result.
  */
  set_data_type_string(
      args[0]->max_length +
      max<ulonglong>(args[1]->max_length - binary_log::Uuid::TEXT_LENGTH, 0) *
          5 / 2);
  return false;
}

String *Item_func_gtid_subtract::val_str_ascii(String *str) {
  DBUG_TRACE;
  assert(fixed);

  String *str1 = args[0]->val_str_ascii(&buf1);
  if (str1 == nullptr) {
    return error_str();
  }
  String *str2 = args[1]->val_str_ascii(&buf2);
  if (str2 == nullptr) {
    return error_str();
  }

  const char *charp1 = str1->c_ptr_safe();
  assert(charp1 != nullptr);
  const char *charp2 = str2->c_ptr_safe();
  assert(charp2 != nullptr);

  enum_return_status status;

  Sid_map sid_map(nullptr /*no rwlock*/);
  // compute sets while holding locks
  Gtid_set set1(&sid_map, charp1, &status);
  if (status == RETURN_STATUS_OK) {
    Gtid_set set2(&sid_map, charp2, &status);
    size_t length;
    // subtract, save result, return result
    if (status == RETURN_STATUS_OK) {
      set1.remove_gtid_set(&set2);
      if (!str->mem_realloc((length = set1.get_string_length()) + 1)) {
        set1.to_string(str->ptr());
        str->length(length);
        null_value = false;
        return str;
      }
    }
  }
  return error_str();
}