-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathreplication_observers_example.cc
1212 lines (979 loc) · 39 KB
/
replication_observers_example.cc
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2014, 2024, 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 */
/*
This plugin serves as an example for all those who which to use the new
Hooks installed by Replication in order to capture:
- Transaction progress
- Server state
*/
#include <assert.h>
#include <mysql/components/my_service.h>
#include <mysql/components/services/log_builtins.h>
#include <mysql/group_replication_priv.h>
#include <mysql/plugin.h>
#include <mysql/service_rpl_transaction_ctx.h>
#include <mysqld_error.h>
#include <sys/types.h>
#include "plugin/replication_observers_example/gr_message_service_example.h"
#include "plugin/replication_observers_example/src/binlog/service/iterator/tests/pfs.h"
#include "plugin/replication_observers_example/src/binlog/service/iterator/tests/status_vars.h"
#include <mysql/components/services/ongoing_transaction_query_service.h>
#include "my_dbug.h"
#include "my_inttypes.h"
#include "sql/current_thd.h"
#include "sql/sql_class.h"
#include "string_with_len.h"
static MYSQL_PLUGIN plugin_info_ptr;
static SERVICE_TYPE(registry) *reg_srv = nullptr;
SERVICE_TYPE(log_builtins) *log_bi = nullptr;
SERVICE_TYPE(log_builtins_string) *log_bs = nullptr;
int validate_plugin_server_requirements(Trans_param *param);
int test_channel_service_interface_initialization();
int test_channel_service_interface();
int test_channel_service_interface_io_thread();
bool test_channel_service_interface_is_io_stopping();
bool test_channel_service_interface_is_sql_stopping();
bool test_channel_service_interface_relay_log_renamed();
bool test_server_count_transactions();
/*
Will register the number of calls to each method of Server state
*/
static int before_handle_connection_call = 0;
static int before_recovery_call = 0;
static int after_engine_recovery_call = 0;
static int after_recovery_call = 0;
static int before_server_shutdown_call = 0;
static int after_server_shutdown_call = 0;
static int after_dd_upgrade_call = 0;
static bool thread_aborted = false;
static void dump_server_state_calls() {
if (before_handle_connection_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:before_handle_connection");
}
if (before_recovery_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:before_recovery");
}
if (after_engine_recovery_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:after_engine_recovery");
}
if (after_recovery_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:after_recovery");
}
if (before_server_shutdown_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:before_server_shutdown");
}
if (after_server_shutdown_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:after_server_shutdown");
}
}
/*
DBMS lifecycle events observers.
*/
static int before_handle_connection(Server_state_param *) {
before_handle_connection_call++;
return 0;
}
static int before_recovery(Server_state_param *) {
before_recovery_call++;
return 0;
}
static int after_engine_recovery(Server_state_param *) {
after_engine_recovery_call++;
return 0;
}
static int after_recovery(Server_state_param *) {
after_recovery_call++;
return 0;
}
static int before_server_shutdown(Server_state_param *) {
before_server_shutdown_call++;
return 0;
}
static int after_server_shutdown(Server_state_param *) {
after_server_shutdown_call++;
return 0;
}
static int after_dd_upgrade(Server_state_param *) {
after_dd_upgrade_call++;
return 0;
}
Server_state_observer server_state_observer = {
sizeof(Server_state_observer),
before_handle_connection, // before the client connect the node
before_recovery, // before_recovery
after_engine_recovery, // after engine recovery
after_recovery, // after_recovery
before_server_shutdown, // before shutdown
after_server_shutdown, // after shutdown
after_dd_upgrade, // after DD upgrade from 5.7 to 8.0
};
static int trans_before_dml_call = 0;
static int trans_before_commit_call = 0;
static int trans_before_rollback_call = 0;
static int trans_after_commit_call = 0;
static int trans_after_rollback_call = 0;
static int trans_begin_call = 0;
static void dump_transaction_calls() {
if (trans_before_dml_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:trans_before_dml");
}
if (trans_before_commit_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:trans_before_commit");
}
if (trans_before_rollback_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:trans_before_rollback");
}
if (trans_after_commit_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:trans_after_commit");
}
if (trans_after_rollback_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:trans_after_rollback");
}
}
/*
Transaction lifecycle events observers.
*/
static int trans_before_dml(Trans_param *, int &out_val [[maybe_unused]]) {
trans_before_dml_call++;
DBUG_EXECUTE_IF("cause_failure_in_before_dml_hook", out_val = 1;);
DBUG_EXECUTE_IF("validate_replication_observers_plugin_server_channels",
test_channel_service_interface(););
DBUG_EXECUTE_IF(
"validate_replication_observers_plugin_server_channel_io_thread",
test_channel_service_interface_io_thread(););
DBUG_EXECUTE_IF("validate_replication_observers_plugin_server_channels_init",
test_channel_service_interface_initialization(););
DBUG_EXECUTE_IF("validate_replication_observers_plugin_server_is_io_stopping",
test_channel_service_interface_is_io_stopping(););
DBUG_EXECUTE_IF(
"validate_replication_observers_plugin_server_is_sql_stopping",
test_channel_service_interface_is_sql_stopping(););
DBUG_EXECUTE_IF(
"validate_replication_observers_plugin_server_relay_log_renamed",
test_channel_service_interface_relay_log_renamed(););
DBUG_EXECUTE_IF("validate_replication_observers_plugin_counts_transactions",
test_server_count_transactions(););
return 0;
}
typedef enum enum_before_commit_test_cases {
NEGATIVE_CERTIFICATION,
POSITIVE_CERTIFICATION_WITH_GTID,
POSITIVE_CERTIFICATION_WITHOUT_GTID,
INVALID_CERTIFICATION_OUTCOME
} before_commit_test_cases;
#ifndef NDEBUG
static int before_commit_tests(Trans_param *param,
before_commit_test_cases test_case) {
mysql::gtid::Tsid fake_tsid;
rpl_sidno fake_sidno;
rpl_gno fake_gno;
Transaction_termination_ctx transaction_termination_ctx;
memset(&transaction_termination_ctx, 0, sizeof(transaction_termination_ctx));
transaction_termination_ctx.m_thread_id = param->thread_id;
[[maybe_unused]] std::size_t tsid_chars = 0;
switch (test_case) {
case NEGATIVE_CERTIFICATION:
transaction_termination_ctx.m_rollback_transaction = true;
transaction_termination_ctx.m_generated_gtid = false;
transaction_termination_ctx.m_sidno = -1;
transaction_termination_ctx.m_gno = -1;
break;
case POSITIVE_CERTIFICATION_WITH_GTID:
std::ignore =
fake_tsid.from_cstring("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
fake_sidno = get_sidno_from_global_tsid_map(fake_tsid);
fake_gno = get_last_executed_gno(fake_sidno);
fake_gno++;
transaction_termination_ctx.m_rollback_transaction = false;
transaction_termination_ctx.m_generated_gtid = true;
transaction_termination_ctx.m_sidno = fake_sidno;
transaction_termination_ctx.m_gno = fake_gno;
break;
case POSITIVE_CERTIFICATION_WITHOUT_GTID:
transaction_termination_ctx.m_rollback_transaction = false;
transaction_termination_ctx.m_generated_gtid = false;
transaction_termination_ctx.m_sidno = 0;
transaction_termination_ctx.m_gno = 0;
break;
case INVALID_CERTIFICATION_OUTCOME:
transaction_termination_ctx.m_rollback_transaction = true;
transaction_termination_ctx.m_generated_gtid = true;
transaction_termination_ctx.m_sidno = -1;
transaction_termination_ctx.m_gno = -1;
default:
break;
}
if (set_transaction_ctx(transaction_termination_ctx)) {
LogPluginErrMsg(
ERROR_LEVEL, ER_LOG_PRINTF_MSG,
"Unable to update transaction context service on server, thread_id: %u",
param->thread_id);
return 1;
}
return 0;
}
#endif
static int trans_before_commit(Trans_param *param [[maybe_unused]]) {
trans_before_commit_call++;
DBUG_EXECUTE_IF("force_error_on_before_commit_listener", return 1;);
DBUG_EXECUTE_IF("force_negative_certification_outcome",
return before_commit_tests(param, NEGATIVE_CERTIFICATION););
DBUG_EXECUTE_IF(
"force_positive_certification_outcome_without_gtid",
return before_commit_tests(param, POSITIVE_CERTIFICATION_WITHOUT_GTID););
DBUG_EXECUTE_IF(
"force_positive_certification_outcome_with_gtid",
return before_commit_tests(param, POSITIVE_CERTIFICATION_WITH_GTID););
DBUG_EXECUTE_IF(
"force_invalid_certification_outcome",
return before_commit_tests(param, INVALID_CERTIFICATION_OUTCOME););
return 0;
}
static int trans_before_rollback(Trans_param *) {
trans_before_rollback_call++;
return 0;
}
static int trans_after_commit(Trans_param *) {
DBUG_EXECUTE_IF("bgc_after_after_commit_stage", {
const char act[] = "now wait_for continue_commit";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
});
trans_after_commit_call++;
return 0;
}
static int trans_after_rollback(Trans_param *param [[maybe_unused]]) {
trans_after_rollback_call++;
DBUG_EXECUTE_IF("validate_replication_observers_plugin_server_requirements",
return validate_plugin_server_requirements(param););
return 0;
}
static int trans_begin(Trans_param *param [[maybe_unused]],
int &out_val [[maybe_unused]]) {
trans_begin_call++;
return 0;
}
Trans_observer trans_observer = {
sizeof(Trans_observer),
trans_before_dml, trans_before_commit, trans_before_rollback,
trans_after_commit, trans_after_rollback, trans_begin,
};
/*
Binlog relay IO events observers.
*/
static int binlog_relay_thread_start_call = 0;
static int binlog_relay_thread_stop_call = 0;
static int binlog_relay_applier_start_call = 0;
static int binlog_relay_applier_stop_call = 0;
static int binlog_relay_before_request_transmit_call = 0;
static int binlog_relay_after_read_event_call = 0;
static int binlog_relay_after_queue_event_call = 0;
static int binlog_relay_after_reset_slave_call = 0;
static void dump_binlog_relay_calls() {
if (binlog_relay_thread_start_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_thread_start");
}
if (binlog_relay_thread_stop_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_thread_stop");
}
if (binlog_relay_applier_start_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_applier_start");
}
if (binlog_relay_applier_stop_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_applier_stop");
}
if (binlog_relay_before_request_transmit_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_"
"before_request_transmit");
}
if (binlog_relay_after_read_event_call) {
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_after_read_event");
}
if (binlog_relay_after_queue_event_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_"
"after_queue_event");
}
if (binlog_relay_after_reset_slave_call) {
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:binlog_relay_"
"after_reset_slave");
}
}
static int binlog_relay_thread_start(Binlog_relay_IO_param *) {
binlog_relay_thread_start_call++;
return 0;
}
static int binlog_relay_thread_stop(Binlog_relay_IO_param *) {
binlog_relay_thread_stop_call++;
return 0;
}
int binlog_relay_applier_start(Binlog_relay_IO_param *) {
binlog_relay_applier_start_call++;
return 0;
}
static int binlog_relay_applier_stop(Binlog_relay_IO_param *, bool aborted) {
binlog_relay_applier_stop_call++;
thread_aborted = aborted;
return 0;
}
static int binlog_relay_before_request_transmit(Binlog_relay_IO_param *,
uint32) {
binlog_relay_before_request_transmit_call++;
return 0;
}
static int binlog_relay_after_read_event(Binlog_relay_IO_param *, const char *,
unsigned long, const char **,
unsigned long *) {
binlog_relay_after_read_event_call++;
return 0;
}
static int binlog_relay_after_queue_event(Binlog_relay_IO_param *, const char *,
unsigned long, uint32) {
binlog_relay_after_queue_event_call++;
return 0;
}
static int binlog_relay_after_reset_slave(Binlog_relay_IO_param *) {
binlog_relay_after_reset_slave_call++;
return 0;
}
static int binlog_relay_applier_log_event(Binlog_relay_IO_param *,
Trans_param *, int &) {
return 0;
}
Binlog_relay_IO_observer relay_io_observer = {
sizeof(Binlog_relay_IO_observer),
binlog_relay_thread_start,
binlog_relay_thread_stop,
binlog_relay_applier_start,
binlog_relay_applier_stop,
binlog_relay_before_request_transmit,
binlog_relay_after_read_event,
binlog_relay_after_queue_event,
binlog_relay_after_reset_slave,
binlog_relay_applier_log_event};
/*
Validate plugin requirements on server code.
This function is mainly to ensure that any change on server code
will not break Group Replication requirements.
*/
int validate_plugin_server_requirements(Trans_param *param) {
int success = 0;
/*
Instantiate a Gtid_log_event without a THD parameter.
*/
mysql::gtid::Tsid fake_tsid;
std::ignore = fake_tsid.from_cstring("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
const rpl_sidno fake_sidno = get_sidno_from_global_tsid_map(fake_tsid);
const rpl_gno fake_gno = get_last_executed_gno(fake_sidno) + 1;
const Gtid gtid = {fake_sidno, fake_gno};
mysql::gtid::Tag_plain empty_tag;
empty_tag.clear();
Gtid_specification gtid_spec = {ASSIGNED_GTID, gtid, empty_tag};
Gtid_log_event *gle =
new Gtid_log_event(param->server_id, true, 0, 1, true, 0, 0, gtid_spec,
UNKNOWN_SERVER_VERSION, UNKNOWN_SERVER_VERSION);
if (gle->is_valid())
success++;
else
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"replication_observers_example_plugin:validate_"
"plugin_server_requirements:"
" failed to instantiate a Gtid_log_event");
delete gle;
/*
Instantiate a anonymous Gtid_log_event without a THD parameter.
*/
const Gtid_specification anonymous_gtid_spec = {ANONYMOUS_GTID, gtid,
empty_tag};
gle = new Gtid_log_event(param->server_id, true, 0, 1, true, 0, 0,
anonymous_gtid_spec, UNKNOWN_SERVER_VERSION,
UNKNOWN_SERVER_VERSION);
if (gle->is_valid())
success++;
else
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"replication_observers_example_plugin:validate_"
"plugin_server_requirements:"
" failed to instantiate a anonymous Gtid_log_event");
delete gle;
/*
Instantiate a Transaction_context_log_event.
*/
Transaction_context_log_event *tcle = new Transaction_context_log_event(
param->server_uuid, true, param->thread_id, false);
if (tcle->is_valid()) {
Gtid_set *snapshot_version = tcle->get_snapshot_version();
const size_t snapshot_version_len = snapshot_version->get_encoded_length();
uchar *snapshot_version_buf =
(uchar *)my_malloc(PSI_NOT_INSTRUMENTED, snapshot_version_len, MYF(0));
snapshot_version->encode(snapshot_version_buf);
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"snapshot version is '%s'", snapshot_version_buf);
my_free(snapshot_version_buf);
success++;
} else
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"replication_observers_example_plugin:validate_plugin_server_"
"requirements:"
" failed to instantiate a Transaction_context_log_event");
delete tcle;
/*
Instantiate a View_Change_log_event.
*/
View_change_log_event *vcle =
new View_change_log_event(const_cast<char *>("1421867646:1"));
if (vcle->is_valid()) {
success++;
} else
LogPluginErr(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"replication_observers_example_plugin:validate_"
"plugin_server_requirements:"
" failed to instantiate a View_change_log_event");
delete vcle;
/*
include/mysql/group_replication_priv.h exported functions.
*/
my_thread_attr_t *thread_attr = get_connection_attrib();
char *hostname, *uuid;
uint port, admin_port;
unsigned int server_version;
get_server_parameters(&hostname, &port, &uuid, &server_version, &admin_port);
Trans_context_info startup_pre_reqs;
get_server_startup_prerequirements(startup_pre_reqs);
// check the server is initialized by checking if the default channel exists
const bool server_engine_ready = channel_is_active("", CHANNEL_NO_THD);
uchar *encoded_gtid_executed = nullptr;
size_t length;
get_server_encoded_gtid_executed(&encoded_gtid_executed, &length);
#if !defined(NDEBUG)
char *encoded_gtid_executed_string =
encoded_gtid_set_to_string(encoded_gtid_executed, length);
#endif
if (thread_attr != nullptr && hostname != nullptr && uuid != nullptr &&
port > 0 && startup_pre_reqs.gtid_mode == 3 && server_engine_ready &&
encoded_gtid_executed != nullptr
#if !defined(NDEBUG)
&& encoded_gtid_executed_string != nullptr
#endif
)
success++;
else
LogPluginErr(
INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"replication_observers_example_plugin:validate_plugin_server_"
"requirements:"
" failed to invoke group_replication_priv.h exported functions");
#if !defined(NDEBUG)
my_free(encoded_gtid_executed_string);
#endif
my_free(encoded_gtid_executed);
/*
Log number of successful validations.
*/
LogPluginErrMsg(INFORMATION_LEVEL, ER_LOG_PRINTF_MSG,
"\nreplication_observers_example_plugin:validate_"
"plugin_server_requirements=%d",
success);
return 0;
}
int test_channel_service_interface_initialization() {
const int error = initialize_channel_service_interface();
assert(error);
return error;
}
int test_channel_service_interface() {
// The initialization method should return OK
int error = initialize_channel_service_interface();
assert(!error);
// Test channel creation
char interface_channel[] = "example_channel";
Channel_creation_info info;
initialize_channel_creation_info(&info);
error = channel_create(interface_channel, &info);
assert(!error);
// Assert the channel exists
bool exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(exists);
// Check that a non existing channel is declared as such
char dummy_channel[] = "dummy_channel";
exists = channel_is_active(dummy_channel, CHANNEL_NO_THD);
assert(!exists);
// Test that we cannot create a empty named channel (the default channel)
char empty_interface_channel[] = "";
initialize_channel_creation_info(&info);
error = channel_create(empty_interface_channel, &info);
assert(error == RPL_CHANNEL_SERVICE_DEFAULT_CHANNEL_CREATION_ERROR);
// Start the applier thread (since it does not need an external server)
Channel_connection_info connection_info;
initialize_channel_connection_info(&connection_info);
error = channel_start(interface_channel, &connection_info,
CHANNEL_APPLIER_THREAD, true);
assert(!error);
// Assert that the applier thread is running
bool running = channel_is_active(interface_channel, CHANNEL_APPLIER_THREAD);
assert(running);
// Wait for execution of events (none in this case so it should return OK)
error = channel_wait_until_apply_queue_applied(interface_channel, 100000);
assert(!error);
// Get the last delivered gno (should be 0)
mysql::gtid::Tsid fake_tsid;
std::ignore = fake_tsid.from_cstring("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
const rpl_sidno fake_sidno = get_sidno_from_global_tsid_map(fake_tsid);
rpl_gno gno = channel_get_last_delivered_gno(interface_channel, fake_sidno);
assert(gno == 0);
// Check that for non existing channels it returns the corresponding error
gno = channel_get_last_delivered_gno(dummy_channel, fake_sidno);
assert(gno == RPL_CHANNEL_SERVICE_CHANNEL_DOES_NOT_EXISTS_ERROR);
// Extract the applier id
long unsigned int *applier_id = nullptr;
channel_get_thread_id(interface_channel, CHANNEL_APPLIER_THREAD, &applier_id);
assert(*applier_id > 0);
my_free(applier_id);
assert(binlog_relay_applier_stop_call == 0);
// Stop the channel applier
error = channel_stop(interface_channel, 3, 10000);
assert(!error);
// Repeat the stop to check it goes ok
error = channel_stop(interface_channel, 3, 10000);
assert(!error);
assert(binlog_relay_applier_stop_call > 0);
assert(!thread_aborted);
// Assert that the applier thread is not running
running = channel_is_active(interface_channel, CHANNEL_APPLIER_THREAD);
assert(!running);
// Purge the channel and assert all is OK
error = channel_purge_queue(interface_channel, true);
assert(!error);
// Assert the channel is not there.
exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(!exists);
// Check that a queue in an empty channel will fail.
char empty_event[] = "";
error = channel_queue_packet(dummy_channel, empty_event, 0);
assert(error);
// Test a multi thread channel
info.channel_mts_parallel_type = CHANNEL_MTS_PARALLEL_TYPE_LOGICAL_CLOCK;
info.channel_mts_parallel_workers = 3;
error = channel_create(interface_channel, &info);
assert(!error);
// Assert the channel exists
exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(exists);
error = channel_start(interface_channel, &connection_info,
CHANNEL_APPLIER_THREAD, true);
assert(!error);
// Extract the applier ids
applier_id = nullptr;
const int num_appliers = channel_get_thread_id(
interface_channel, CHANNEL_APPLIER_THREAD, &applier_id);
assert(num_appliers == 4);
unsigned long thread_id = 0;
for (int i = 0; i < num_appliers; i++) {
thread_id = applier_id[i];
assert(thread_id > 0);
}
my_free(applier_id);
// Stop the channel applier
error = channel_stop(interface_channel, 3, 10000);
assert(!error);
// Purge the channel and assert all is OK
error = channel_purge_queue(interface_channel, true);
assert(!error);
// Assert the channel is not there.
exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(!exists);
// Test the method to extract credentials - first a non existing channel
std::string username, password;
error = channel_get_credentials(dummy_channel, username, password);
assert(error == RPL_CHANNEL_SERVICE_CHANNEL_DOES_NOT_EXISTS_ERROR);
// Now get channel credentials, after setting them
char dummy_user[] = "user";
char dummy_pass[] = "pass";
info.user = dummy_user;
info.password = dummy_pass;
error = channel_create(interface_channel, &info);
assert(!error);
error = channel_get_credentials(interface_channel, username, password);
assert(!error);
assert(strcmp(dummy_user, username.c_str()) == 0);
assert(strcmp(dummy_pass, password.c_str()) == 0);
return (error && exists && running && gno && num_appliers && thread_id);
}
int test_channel_service_interface_io_thread() {
// The initialization method should return OK
int error = initialize_channel_service_interface();
assert(!error);
char interface_channel[] = "example_channel";
// Assert the channel exists
const bool exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(exists);
// Assert that the receiver is running
bool running = channel_is_active(interface_channel, CHANNEL_RECEIVER_THREAD);
assert(running);
// Extract the receiver id
long unsigned int *thread_id = nullptr;
const int num_threads = channel_get_thread_id(
interface_channel, CHANNEL_RECEIVER_THREAD, &thread_id);
assert(num_threads == 1);
assert(*thread_id > 0);
my_free(thread_id);
// Get the I/O thread retrieved GTID set
char *retrieved_gtid_set;
error =
channel_get_retrieved_gtid_set(interface_channel, &retrieved_gtid_set);
assert(!error);
assert(strlen(retrieved_gtid_set) > 0);
my_free(retrieved_gtid_set);
// Check that the applier thread is waiting for events to be queued.
const int is_waiting = channel_is_applier_waiting(interface_channel);
assert(is_waiting == 1);
// Stop the channel
error = channel_stop(interface_channel, 3, 10000);
assert(!error);
// Assert that the receiver thread is not running
running = channel_is_active(interface_channel, CHANNEL_RECEIVER_THREAD);
assert(!running);
return (error && exists && running && num_threads && is_waiting);
}
bool test_channel_service_interface_is_io_stopping() {
// The initialization method should return OK
bool error = initialize_channel_service_interface();
assert(!error);
// Initialize the channel to be used with the channel service interface
char interface_channel[] = "example_channel";
Channel_creation_info info;
initialize_channel_creation_info(&info);
error = channel_create(interface_channel, &info);
assert(!error);
// Reset the I/O stop counter
binlog_relay_thread_stop_call = 0;
// Unregister the thread stop hook
error = unregister_binlog_relay_io_observer(&relay_io_observer,
(void *)plugin_info_ptr);
assert(!error);
// Start the I/O thread
Channel_connection_info connection_info;
initialize_channel_connection_info(&connection_info);
error = channel_start(interface_channel, &connection_info,
CHANNEL_RECEIVER_THREAD, true);
assert(!error);
// Assert the channel exists
const bool exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(exists);
// Wait until I/O thread reached the error and is going to stop
DBUG_EXECUTE_IF("pause_after_io_thread_stop_hook", {
const char act[] =
"now "
"WAIT_FOR reached_stopping_io_thread";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
// Register the thread stop hook again
error = register_binlog_relay_io_observer(&relay_io_observer,
(void *)plugin_info_ptr);
assert(!error);
// Assert that the receiver is stopping
const bool io_stopping =
channel_is_stopping(interface_channel, CHANNEL_RECEIVER_THREAD);
assert(io_stopping);
// Assert that the receiver is running
const bool io_running =
channel_is_active(interface_channel, CHANNEL_RECEIVER_THREAD);
assert(io_running);
// Signal to make the MTR test case to start monitoring the I/O thread
DBUG_EXECUTE_IF("pause_after_io_thread_stop_hook", {
const char act[] =
"now "
"SIGNAL reached_io_thread_started";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
DBUG_EXECUTE_IF("pause_after_io_thread_stop_hook", {
const char act[] = "now SIGNAL continue_to_stop_io_thread";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
// The plug-in has missed the stop
assert(binlog_relay_thread_stop_call == 0);
return (error | exists | io_stopping | io_running);
}
bool test_channel_service_interface_is_sql_stopping() {
// The initialization method should return OK
bool error = initialize_channel_service_interface();
assert(!error);
// Initialize the channel to be used with the channel service interface
char interface_channel[] = "example_channel";
Channel_creation_info info;
initialize_channel_creation_info(&info);
error = channel_create(interface_channel, &info);
assert(!error);
// Assert the channel exists
const bool exists = channel_is_active(interface_channel, CHANNEL_NO_THD);
assert(exists);
// Unregister the thread stop hook
error = unregister_binlog_relay_io_observer(&relay_io_observer,
(void *)plugin_info_ptr);
assert(!error);
// Start the I/O thread
Channel_connection_info connection_info;
initialize_channel_connection_info(&connection_info);
error = channel_start(interface_channel, &connection_info,
CHANNEL_RECEIVER_THREAD, true);
assert(!error);
// Start the SQL thread
error = channel_start(interface_channel, &connection_info,
CHANNEL_APPLIER_THREAD, true);
assert(!error);
// Wait until SQL thread reached the error and is going to stop
DBUG_EXECUTE_IF("pause_after_sql_thread_stop_hook", {
const char act[] =
"now "
"WAIT_FOR reached_stopping_sql_thread";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
// Register the thread stop hook again
error = register_binlog_relay_io_observer(&relay_io_observer,
(void *)plugin_info_ptr);
assert(!error);
// Assert that the applier is stopping
const bool sql_stopping =
channel_is_stopping(interface_channel, CHANNEL_APPLIER_THREAD);
assert(sql_stopping);
// Assert that the applier is running
const bool sql_running =
channel_is_active(interface_channel, CHANNEL_APPLIER_THREAD);
assert(sql_running);
// Signal to make the MTR test case to start monitoring the SQL thread
DBUG_EXECUTE_IF("pause_after_sql_thread_stop_hook", {
const char act[] =
"now "
"SIGNAL reached_sql_thread_started";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
DBUG_EXECUTE_IF("pause_after_sql_thread_stop_hook", {
const char act[] = "now SIGNAL continue_to_stop_sql_thread";
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act)));
};);
// The plug-in has missed the stop
assert(binlog_relay_applier_stop_call == 0);
return (error | exists | sql_stopping | sql_running);
}
bool test_channel_service_interface_relay_log_renamed() {
// The initialization method should return OK
bool error = initialize_channel_service_interface();
assert(!error);
// Initialize the channel to be used with the channel service interface
char interface_channel[] = "example_channel";
char channel_hostname[] = "127.0.0.1";
char channel_user[] = "root";
Channel_creation_info info;