File: rpl_info_table.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (874 lines) | stat: -rw-r--r-- 27,730 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/* Copyright (c) 2010, 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 "sql/rpl_info_table.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "m_ctype.h"
#include "m_string.h"
#include "my_base.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_loglevel.h"
#include "my_sys.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql/thread_type.h"
#include "mysqld_error.h"
#include "sql/dynamic_ids.h"  // Server_ids
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/key.h"
#include "sql/log.h"
#include "sql/log_event.h"
#include "sql/psi_memory_key.h"
#include "sql/query_options.h"
#include "sql/rpl_info_table_access.h"  // Rpl_info_table_access
#include "sql/rpl_info_values.h"        // Rpl_info_values
#include "sql/sql_class.h"              // THD
#include "sql/system_variables.h"
#include "sql/table.h"
#include "sql_string.h"
#include "thr_lock.h"

Rpl_info_table::Rpl_info_table(uint nparam, const char *param_schema,
                               const char *param_table,
                               const uint param_n_pk_fields,
                               const uint *param_pk_field_indexes,
                               MY_BITMAP const *nullable_bitmap)
    : Rpl_info_handler(nparam, nullable_bitmap), is_transactional(false) {
  str_schema.str = str_table.str = nullptr;
  str_schema.length = str_table.length = 0;

  size_t schema_length = strlen(param_schema);
  if ((str_schema.str = (char *)my_malloc(key_memory_Rpl_info_table,
                                          schema_length + 1, MYF(0)))) {
    str_schema.length = schema_length;
    strmake(str_schema.str, param_schema, schema_length);
  }

  size_t table_length = strlen(param_table);
  if ((str_table.str = (char *)my_malloc(key_memory_Rpl_info_table,
                                         table_length + 1, MYF(0)))) {
    str_table.length = table_length;
    strmake(str_table.str, param_table, table_length);
  }

  if ((description = (char *)my_malloc(key_memory_Rpl_info_table,
                                       str_schema.length + str_table.length + 2,
                                       MYF(0)))) {
    char *pos = my_stpcpy(description, param_schema);
    pos = my_stpcpy(pos, ".");
    pos = my_stpcpy(pos, param_table);
  }

  m_n_pk_fields = param_n_pk_fields;
  m_pk_field_indexes = param_pk_field_indexes;

  access = new Rpl_info_table_access();
}

Rpl_info_table::~Rpl_info_table() {
  delete access;

  my_free(description);

  my_free(str_table.str);

  my_free(str_schema.str);
}

int Rpl_info_table::do_init_info() { return do_init_info(FIND_KEY, 0); }

int Rpl_info_table::do_init_info(uint instance) {
  return do_init_info(FIND_KEY, instance);
}

int Rpl_info_table::do_init_info(enum_find_method method, uint instance) {
  int error = 1;
  enum enum_return_id res = FOUND_ID;
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;

  DBUG_TRACE;

  THD *thd = access->create_thd();

  saved_mode = thd->variables.sql_mode;
  ulonglong saved_options = thd->variables.option_bits;
  thd->variables.option_bits &= ~OPTION_BIN_LOG;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_WRITE,
                         &table, &backup))
    goto end;

  if (verify_table_primary_key_fields(table)) goto end;

  /*
    Points the cursor at the row to be read according to the
    keys.
  */
  switch (method) {
    case FIND_KEY:
      res = access->find_info(field_values, table);
      break;

    case FIND_SCAN:
      res = access->scan_info(table, instance);
      break;

    default:
      assert(0);
      break;
  }

  if (res == FOUND_ID) {
    /*
      Reads the information stored in the rpl_info table into a
      set of variables. If there is a failure, an error is returned.
    */
    if (access->load_info_values(get_number_info(), table->field, field_values))
      goto end;
  }
  error = (res == ERROR_ID);
end:
  /*
    Unlocks and closes the rpl_info table.
  */
  error = access->close_table(thd, table, &backup, error) || error;
  thd->variables.sql_mode = saved_mode;
  thd->variables.option_bits = saved_options;
  access->drop_thd(thd);
  return error;
}

int Rpl_info_table::do_flush_info(const bool force) {
  int error = 1;
  enum enum_return_id res = FOUND_ID;
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;

  DBUG_TRACE;

  if (!(force || (sync_period && ++(sync_counter) >= sync_period))) return 0;

  THD *thd = access->create_thd();

  sync_counter = 0;
  saved_mode = thd->variables.sql_mode;
  ulonglong saved_options = thd->variables.option_bits;
  thd->variables.option_bits &= ~OPTION_BIN_LOG;
  thd->is_operating_substatement_implicitly = true;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_WRITE,
                         &table, &backup))
    goto end;

  /*
    Points the cursor at the row to be read according to the
    keys. If the row is not found an error is reported.
  */
  if ((res = access->find_info(field_values, table)) == NOT_FOUND_ID) {
    /*
      Prepares the information to be stored before calling ha_write_row.
    */
    empty_record(table);
    if (access->store_info_values(get_number_info(), table->field,
                                  field_values))
      goto end;

    /*
      Inserts a new row into rpl_info table.
    */
    if ((error = table->file->ha_write_row(table->record[0]))) {
      table->file->print_error(error, MYF(0));
      /*
        This makes sure that the error is 1 and not the status returned
        by the handler.
      */
      error = 1;
      goto end;
    }
    error = 0;
  } else if (res == FOUND_ID) {
    /*
      Prepares the information to be stored before calling ha_update_row.
    */
    store_record(table, record[1]);
    if (access->store_info_values(get_number_info(), table->field,
                                  field_values))
      goto end;

    /*
      Updates a row in the rpl_info table.
    */
    if ((error =
             table->file->ha_update_row(table->record[1], table->record[0])) &&
        error != HA_ERR_RECORD_IS_THE_SAME) {
      table->file->print_error(error, MYF(0));
      /*
        This makes sure that the error is 1 and not the status returned
        by the handler.
      */
      error = 1;
      goto end;
    }
    error = 0;
  }

end:
  DBUG_EXECUTE_IF("mta_debug_concurrent_access", {
    while (thd->system_thread == SYSTEM_THREAD_SLAVE_WORKER &&
           mta_debug_concurrent_access < 2 && mta_debug_concurrent_access > 0) {
      DBUG_PRINT("mts", ("Waiting while locks are acquired to show "
                         "concurrency in mts: %u %u\n",
                         mta_debug_concurrent_access, thd->thread_id()));
      my_sleep(6000000);
    }
  };);

  /*
    Unlocks and closes the rpl_info table.
  */
  error = access->close_table(thd, table, &backup, error) || error;
  thd->is_operating_substatement_implicitly = false;
  thd->variables.sql_mode = saved_mode;
  thd->variables.option_bits = saved_options;
  access->drop_thd(thd);
  return error;
}

int Rpl_info_table::do_remove_info() { return do_clean_info(); }

int Rpl_info_table::do_clean_info() {
  int error = 1;
  enum enum_return_id res = FOUND_ID;
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;

  DBUG_TRACE;

  THD *thd = access->create_thd();

  saved_mode = thd->variables.sql_mode;
  ulonglong saved_options = thd->variables.option_bits;
  thd->variables.option_bits &= ~OPTION_BIN_LOG;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_WRITE,
                         &table, &backup))
    goto end;

  /*
    Points the cursor at the row to be deleted according to the
    keys. If the row is not found, the execution proceeds normally.
  */
  if ((res = access->find_info(field_values, table)) == FOUND_ID) {
    /*
      Deletes a row in the rpl_info table.
    */
    if ((error = table->file->ha_delete_row(table->record[0]))) {
      table->file->print_error(error, MYF(0));
      goto end;
    }
  }
  error = (res == ERROR_ID);
end:
  /*
    Unlocks and closes the rpl_info table.
  */
  error = access->close_table(thd, table, &backup, error) || error;
  thd->variables.sql_mode = saved_mode;
  thd->variables.option_bits = saved_options;
  access->drop_thd(thd);
  return error;
}

/**
   Removes records belonging to the channel_name parameter's channel.

   @param nparam             number of fields in the table
   @param param_schema       schema name
   @param param_table        table name
   @param channel_name       channel name
   @param nullable_bitmap    fields allowed to be null.

   @return 0   on success
           1   when a failure happens
*/
int Rpl_info_table::do_reset_info(uint nparam, const char *param_schema,
                                  const char *param_table,
                                  const char *channel_name,
                                  MY_BITMAP const *nullable_bitmap) {
  int error = 0;
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;
  Rpl_info_table *info = nullptr;
  THD *thd = nullptr;
  int handler_error = 0;

  DBUG_TRACE;

  if (!(info = new Rpl_info_table(nparam, param_schema, param_table, 0, nullptr,
                                  nullable_bitmap)))
    return 1;

  thd = info->access->create_thd();
  saved_mode = thd->variables.sql_mode;
  ulonglong saved_options = thd->variables.option_bits;
  thd->variables.option_bits &= ~OPTION_BIN_LOG;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (info->access->open_table(thd, to_lex_cstring(info->str_schema),
                               to_lex_cstring(info->str_table),
                               info->get_number_info(), TL_WRITE, &table,
                               &backup)) {
    error = 1;
    goto end;
  }

  if (!(handler_error = table->file->ha_index_init(0, true))) {
    KEY *key_info = table->key_info;

    /*
      Currently this method is used only for Worker info table
      resetting.
      todo: for another table in future, consider to make use of the
      passed parameter to locate the lookup key.
    */
    assert(strcmp(info->str_table.str, "slave_worker_info") == 0);

    if (info->verify_table_primary_key_fields(table)) {
      error = 1;
      table->file->ha_index_end();
      goto end;
    }

    uint fieldnr = key_info->key_part[0].fieldnr - 1;
    table->field[fieldnr]->store(channel_name, strlen(channel_name),
                                 &my_charset_bin);
    uint key_len = key_info->key_part[0].store_length;

    uchar key[MAX_KEY_LENGTH];
    key_copy(key, table->record[0], table->key_info,
             table->key_info->key_length);
    if (!(handler_error = table->file->ha_index_read_map(
              table->record[0], key, (key_part_map)1, HA_READ_KEY_EXACT))) {
      do {
        if ((handler_error = table->file->ha_delete_row(table->record[0])))
          break;
      } while (!(handler_error = table->file->ha_index_next_same(
                     table->record[0], key, key_len)));
      if (handler_error != HA_ERR_END_OF_FILE) error = 1;
    } else {
      /*
        Being reset table can be even empty, and that's benign.
      */
      if (handler_error != HA_ERR_KEY_NOT_FOUND) error = 1;
    }

    if (error) table->file->print_error(handler_error, MYF(0));
    table->file->ha_index_end();
  }
end:
  /*
    Unlocks and closes the rpl_info table.
  */
  error = info->access->close_table(thd, table, &backup, error) || error;
  thd->variables.sql_mode = saved_mode;
  thd->variables.option_bits = saved_options;
  info->access->drop_thd(thd);
  delete info;
  return error;
}

enum_return_check Rpl_info_table::do_check_info() {
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;
  enum_return_check return_check = ERROR_CHECKING_REPOSITORY;

  DBUG_TRACE;

  THD *thd = access->create_thd();
  saved_mode = thd->variables.sql_mode;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_READ,
                         &table, &backup)) {
    LogErr(WARNING_LEVEL, ER_RPL_CANT_OPEN_INFO_TABLE, str_schema.str,
           str_table.str);

    return_check = ERROR_CHECKING_REPOSITORY;
    goto end;
  }

  /*
    Points the cursor at the row to be read according to the
    keys.
  */
  if (access->find_info(field_values, table) != FOUND_ID) {
    /*
       We cannot simply call my_error here because it does not
       really means that there was a failure but only that the
       record was not found.
    */
    return_check = REPOSITORY_DOES_NOT_EXIST;
    goto end;
  }
  return_check = REPOSITORY_EXISTS;

end:
  /*
    Unlocks and closes the rpl_info table.
  */
  access->close_table(thd, table, &backup,
                      return_check == ERROR_CHECKING_REPOSITORY);
  thd->variables.sql_mode = saved_mode;
  access->drop_thd(thd);
  return return_check;
}

enum_return_check Rpl_info_table::do_check_info(uint instance) {
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;
  enum_return_check return_check = ERROR_CHECKING_REPOSITORY;

  DBUG_TRACE;

  THD *thd = access->create_thd();
  saved_mode = thd->variables.sql_mode;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_READ,
                         &table, &backup)) {
    LogErr(WARNING_LEVEL, ER_RPL_CANT_OPEN_INFO_TABLE, str_schema.str,
           str_table.str);

    return_check = ERROR_CHECKING_REPOSITORY;
    goto end;
  }

  if (verify_table_primary_key_fields(table)) {
    return_check = ERROR_CHECKING_REPOSITORY;
    goto end;
  }

  /*
    Points the cursor at the row to be read according to the
    keys.
  */
  if (access->scan_info(table, instance) != FOUND_ID) {
    /*
       We cannot simply call my_error here because it does not
       really means that there was a failure but only that the
       record was not found.
    */
    return_check = REPOSITORY_DOES_NOT_EXIST;
    goto end;
  }
  return_check = REPOSITORY_EXISTS;

end:
  /*
    Unlocks and closes the rpl_info table.
  */
  access->close_table(thd, table, &backup,
                      return_check == ERROR_CHECKING_REPOSITORY);
  thd->variables.sql_mode = saved_mode;
  access->drop_thd(thd);
  return return_check;
}

bool Rpl_info_table::do_count_info(uint nparam, const char *param_schema,
                                   const char *param_table,
                                   MY_BITMAP const *nullable_bitmap,
                                   ulonglong *counter) {
  int error = 1;
  TABLE *table = nullptr;
  sql_mode_t saved_mode;
  Open_tables_backup backup;
  Rpl_info_table *info = nullptr;
  THD *thd = nullptr;

  DBUG_TRACE;

  if (!(info = new Rpl_info_table(nparam, param_schema, param_table, 0, nullptr,
                                  nullable_bitmap)))
    return true;

  thd = info->access->create_thd();
  saved_mode = thd->variables.sql_mode;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (info->access->open_table(thd, to_lex_cstring(info->str_schema),
                               to_lex_cstring(info->str_table),
                               info->get_number_info(), TL_READ, &table,
                               &backup)) {
    /*
      We cannot simply print out a warning message at this
      point because this may represent a bootstrap.
    */
    error = 0;
    goto end;
  }

  /*
    Counts entries in the rpl_info table.
  */
  if (info->access->count_info(table, counter)) {
    LogErr(WARNING_LEVEL, ER_RPL_CANT_SCAN_INFO_TABLE, info->str_schema.str,
           info->str_table.str);
    goto end;
  }
  error = 0;

end:
  /*
    Unlocks and closes the rpl_info table.
  */
  error = info->access->close_table(thd, table, &backup, error) || error;
  thd->variables.sql_mode = saved_mode;
  info->access->drop_thd(thd);
  delete info;
  return error;
}

std::pair<bool, bool> Rpl_info_table::table_in_use(
    uint nparam, const char *param_schema, const char *param_table,
    MY_BITMAP const *nullable_bitmap) {
  bool error{false};
  bool empty{false};

  TABLE *table = nullptr;
  Open_tables_backup backup;
  Rpl_info_table *info = nullptr;

  DBUG_TRACE;

  if (!(info = new Rpl_info_table(nparam, param_schema, param_table, 0, nullptr,
                                  nullable_bitmap)))
    return std::make_pair(true, false);

  THD *thd = info->access->create_thd();
  sql_mode_t saved_mode = thd->variables.sql_mode;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (!info->access->open_table(thd, to_lex_cstring(info->str_schema),
                                to_lex_cstring(info->str_table),
                                info->get_number_info(), TL_READ, &table,
                                &backup)) {
    /*
      Check if table is in use
    */
    std::tie(error, empty) = info->access->is_table_in_use(table);
    if (error) {
      LogErr(WARNING_LEVEL, ER_RPL_CANT_SCAN_INFO_TABLE, info->str_schema.str,
             info->str_table.str);
    }
  }
  /* else:
    We cannot simply print out a warning message at this
    point because this may represent a bootstrap.
  */

  // Unlocks and closes the rpl_info table.
  error = info->access->close_table(thd, table, &backup, error) || error;
  thd->variables.sql_mode = saved_mode;
  info->access->drop_thd(thd);
  delete info;
  return std::make_pair(error != 0, empty);
}

void Rpl_info_table::do_end_info() {}

int Rpl_info_table::do_prepare_info_for_read() {
  if (!field_values) return true;

  cursor = 0;
  prv_error = false;
  prv_get_error = Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;

  return false;
}

int Rpl_info_table::do_prepare_info_for_write() {
  return (do_prepare_info_for_read());
}

uint Rpl_info_table::do_get_rpl_info_type() { return INFO_REPOSITORY_TABLE; }

bool Rpl_info_table::do_set_info(const int pos, const char *value) {
  if (value == nullptr)
    return do_set_info(pos, nullptr);
  else {
    bitmap_clear_bit(&field_values->is_null, pos);
    return (
        field_values->value[pos].copy(value, strlen(value), &my_charset_bin));
  }
}

bool Rpl_info_table::do_set_info(const int pos, const uchar *value,
                                 const size_t size) {
  if (value == nullptr)
    return do_set_info(pos, nullptr, 0);
  else {
    bitmap_clear_bit(&field_values->is_null, pos);
    return (field_values->value[pos].copy(pointer_cast<const char *>(value),
                                          size, &my_charset_bin));
  }
}

bool Rpl_info_table::do_set_info(const int pos, const ulong value) {
  bitmap_clear_bit(&field_values->is_null, pos);
  return (field_values->value[pos].set_int(value, true, &my_charset_bin));
}

bool Rpl_info_table::do_set_info(const int pos, const int value) {
  bitmap_clear_bit(&field_values->is_null, pos);
  return (field_values->value[pos].set_int(value, false, &my_charset_bin));
}

bool Rpl_info_table::do_set_info(const int pos, const float value) {
  bitmap_clear_bit(&field_values->is_null, pos);
  return (field_values->value[pos].set_real(value, DECIMAL_NOT_SPECIFIED,
                                            &my_charset_bin));
}

bool Rpl_info_table::do_set_info(const int pos, const Server_ids *value) {
  bitmap_clear_bit(&field_values->is_null, pos);
  if (const_cast<Server_ids *>(value)->pack_dynamic_ids(
          &field_values->value[pos]))
    return true;

  return false;
}

bool Rpl_info_table::do_set_info(const int pos, const std::nullptr_t) {
  if (!this->is_field_nullable(pos)) return true;
  bitmap_set_bit(&field_values->is_null, pos);
  return false;
}

bool Rpl_info_table::do_set_info(const int pos, const std::nullptr_t,
                                 const size_t) {
  if (!this->is_field_nullable(pos)) return true;
  bitmap_set_bit(&field_values->is_null, pos);
  return false;
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, char *value, const size_t, const char *default_value) {
  if (bitmap_is_set(&field_values->is_null, pos)) {
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
  } else {
    if (field_values->value[pos].length())
      strmake(value, field_values->value[pos].c_ptr_safe(),
              field_values->value[pos].length());
    else if (default_value)
      strmake(value, default_value, strlen(default_value));
    else
      *value = '\0';
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
  }
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, uchar *value, const size_t size,
    const uchar *default_value [[maybe_unused]]) {
  if (bitmap_is_set(&field_values->is_null, pos)) {
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
  } else {
    if (field_values->value[pos].length() == size)
      return Rpl_info_handler::enum_field_get_status(
          !memcpy((char *)value, field_values->value[pos].c_ptr_safe(), size));
    return Rpl_info_handler::enum_field_get_status::FAILURE;
  }
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, ulong *value, const ulong default_value) {
  if (bitmap_is_set(&field_values->is_null, pos)) {
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
  } else {
    if (field_values->value[pos].length()) {
      *value = strtoul(field_values->value[pos].c_ptr_safe(), nullptr, 10);
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    } else if (default_value) {
      *value = default_value;
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    }

    return Rpl_info_handler::enum_field_get_status::FAILURE;
  }
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, int *value, const int default_value) {
  if (bitmap_is_set(&field_values->is_null, pos)) {
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
  } else {
    if (field_values->value[pos].length()) {
      *value = atoi(field_values->value[pos].c_ptr_safe());
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    } else if (default_value) {
      *value = default_value;
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    }

    return Rpl_info_handler::enum_field_get_status::FAILURE;
  }
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, float *value, const float default_value) {
  if (bitmap_is_set(&field_values->is_null, pos)) {
    return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_IS_NULL;
  } else {
    if (field_values->value[pos].length()) {
      if (sscanf(field_values->value[pos].c_ptr_safe(), "%f", value) != 1)
        return Rpl_info_handler::enum_field_get_status::FAILURE;
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    } else if (default_value != 0.0) {
      *value = default_value;
      return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
    }

    return Rpl_info_handler::enum_field_get_status::FAILURE;
  }
}

Rpl_info_handler::enum_field_get_status Rpl_info_table::do_get_info(
    const int pos, Server_ids *value,
    const Server_ids *default_value [[maybe_unused]]) {
  if (value->unpack_dynamic_ids(field_values->value[pos].c_ptr_safe()))
    return Rpl_info_handler::enum_field_get_status::FAILURE;

  return Rpl_info_handler::enum_field_get_status::FIELD_VALUE_NOT_NULL;
}

char *Rpl_info_table::do_get_description_info() { return description; }

bool Rpl_info_table::do_is_transactional() { return is_transactional; }

bool Rpl_info_table::do_update_is_transactional() {
  bool error = true;
  sql_mode_t saved_mode;
  TABLE *table = nullptr;
  Open_tables_backup backup;

  DBUG_TRACE;

  THD *thd = access->create_thd();
  saved_mode = thd->variables.sql_mode;
  ulonglong saved_options = thd->variables.option_bits;
  thd->variables.option_bits &= ~OPTION_BIN_LOG;

  /*
    Opens and locks the rpl_info table before accessing it.
  */
  if (access->open_table(thd, to_lex_cstring(str_schema),
                         to_lex_cstring(str_table), get_number_info(), TL_READ,
                         &table, &backup))
    goto end;

  is_transactional = table->file->has_transactions();
  error = false;

end:
  error = access->close_table(thd, table, &backup, false) || error;
  thd->variables.sql_mode = saved_mode;
  thd->variables.option_bits = saved_options;
  access->drop_thd(thd);
  return error;
}

bool Rpl_info_table::verify_table_primary_key_fields(TABLE *table) {
  DBUG_TRACE;
  KEY *key_info = table->key_info;
  bool error;

  /*
    If the table has no keys or has less key fields than expected,
    it must be corrupted.
  */
  if ((error = !key_info || key_info->user_defined_key_parts == 0 ||
               (m_n_pk_fields > 0 &&
                key_info->user_defined_key_parts != m_n_pk_fields))) {
    LogErr(ERROR_LEVEL, ER_RPL_CORRUPTED_INFO_TABLE, str_schema.str,
           str_table.str);
  }

  if (!error && m_n_pk_fields && m_pk_field_indexes) {
    /*
      If any of its primary key fields are not at the expected position,
      the table must be corrupted.
    */
    for (uint idx = 0; idx < m_n_pk_fields; idx++) {
      if (key_info->key_part[idx].field !=
          table->field[m_pk_field_indexes[idx]]) {
        const char *key_field_name = key_info->key_part[idx].field->field_name;
        const char *table_field_name =
            table->field[m_pk_field_indexes[idx]]->field_name;
        LogErr(ERROR_LEVEL, ER_RPL_CORRUPTED_KEYS_IN_INFO_TABLE, str_schema.str,
               str_table.str, m_pk_field_indexes[idx], key_field_name,
               table_field_name);
        error = true;
        break;
      }
    }
  }

  return error;
}