forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha_ndbcluster_binlog.cc
7836 lines (6827 loc) · 244 KB
/
ha_ndbcluster_binlog.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) 2006, 2017, Oracle and/or its affiliates. All rights reserved.
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 also distributed 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 included with MySQL.
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/ha_ndbcluster_binlog.h"
#include <mysql/psi/mysql_thread.h>
#include "my_dbug.h"
#include "my_thread.h"
#include "mysql/plugin.h"
#include "sql/binlog.h"
#include "sql/dd/types/abstract_table.h" // dd::enum_table_type
#include "sql/derror.h" // ER_THD
#include "sql/ha_ndbcluster.h"
#include "sql/ha_ndbcluster_connection.h"
#include "sql/log_event.h" // my_strmov_quoted_identifier
// tablename_to_filename
#include "sql/mysqld.h" // opt_bin_log
#include "sql/mysqld_thd_manager.h" // Global_THD_manager
#include "sql/ndb_binlog_client.h"
#include "sql/ndb_bitmap.h"
#include "sql/ndb_dd.h"
#include "sql/ndb_dd_client.h"
#include "sql/ndb_dd_table.h"
#include "sql/ndb_global_schema_lock.h"
#include "sql/ndb_global_schema_lock_guard.h"
#include "sql/ndb_local_connection.h"
#include "sql/ndb_log.h"
#include "sql/ndb_name_util.h"
#include "sql/ndb_ndbapi_util.h"
#include "sql/ndb_sleep.h"
#include "sql/ndb_table_guard.h"
#include "sql/ndb_tdc.h"
#include "sql/ndb_thd.h"
#include "sql/rpl_injector.h"
#include "sql/rpl_slave.h"
#include "sql/sql_lex.h"
#include "sql/sql_table.h" // build_table_filename,
#include "sql/thd_raii.h"
#include "sql/transaction.h"
#include "storage/ndb/include/ndbapi/NdbDictionary.hpp"
#include "storage/ndb/include/ndbapi/ndb_cluster_connection.hpp"
typedef NdbDictionary::Event NDBEVENT;
typedef NdbDictionary::Object NDBOBJ;
typedef NdbDictionary::Column NDBCOL;
typedef NdbDictionary::Table NDBTAB;
typedef NdbDictionary::Dictionary NDBDICT;
extern bool opt_ndb_log_orig;
extern bool opt_ndb_log_bin;
extern bool opt_ndb_log_update_as_write;
extern bool opt_ndb_log_updated_only;
extern bool opt_ndb_log_update_minimal;
extern bool opt_ndb_log_binlog_index;
extern bool opt_ndb_log_apply_status;
extern st_ndb_slave_state g_ndb_slave_state;
extern bool opt_ndb_log_transaction_id;
extern bool log_bin_use_v1_row_events;
extern bool opt_ndb_log_empty_update;
extern bool opt_ndb_clear_apply_status;
bool ndb_log_empty_epochs(void);
void ndb_index_stat_restart();
#include "sql/ha_ndbcluster_tables.h"
#include "sql/ndb_anyvalue.h"
#include "sql/ndb_binlog_extra_row_info.h"
#include "sql/ndb_binlog_thread.h"
#include "sql/ndb_dist_priv_util.h"
#include "sql/ndb_event_data.h"
#include "sql/ndb_repl_tab.h"
#include "sql/ndb_schema_dist.h"
#include "sql/ndb_schema_object.h"
extern Ndb_cluster_connection* g_ndb_cluster_connection;
/*
Timeout for syncing schema events between
mysql servers, and between mysql server and the binlog
*/
static const int DEFAULT_SYNC_TIMEOUT= 120;
/* Column numbers in the ndb_binlog_index table */
enum Ndb_binlog_index_cols
{
NBICOL_START_POS = 0
,NBICOL_START_FILE = 1
,NBICOL_EPOCH = 2
,NBICOL_NUM_INSERTS = 3
,NBICOL_NUM_UPDATES = 4
,NBICOL_NUM_DELETES = 5
,NBICOL_NUM_SCHEMAOPS = 6
/* Following colums in schema 'v2' */
,NBICOL_ORIG_SERVERID = 7
,NBICOL_ORIG_EPOCH = 8
,NBICOL_GCI = 9
/* Following columns in schema 'v3' */
,NBICOL_NEXT_POS = 10
,NBICOL_NEXT_FILE = 11
};
class Mutex_guard
{
public:
Mutex_guard(mysql_mutex_t &mutex) : m_mutex(mutex)
{
mysql_mutex_lock(&m_mutex);
}
~Mutex_guard()
{
mysql_mutex_unlock(&m_mutex);
}
private:
mysql_mutex_t &m_mutex;
};
/*
Mutex and condition used for interacting between client sql thread
and injector thread
- injector_data_mutex protects global data maintained
by the injector thread and accessed by any client thread.
- injector_event_mutex, protects injector thread pollEvents()
and concurrent create and drop of events from client threads.
It also protects injector_ndb and schema_ndb which are the Ndb
objects used for the above create/drop/pollEvents()
Rational for splitting these into two separate mutexes, is that
the injector_event_mutex is held for 10ms across pollEvents().
That could (almost) block access to the shared binlog injector data,
like ndb_binlog_is_read_only().
*/
static mysql_mutex_t injector_event_mutex;
static mysql_mutex_t injector_data_mutex;
static mysql_cond_t injector_data_cond;
/*
NOTE:
Several of the ndb_binlog* variables use a 'relaxed locking' schema.
Such a variable is only modified by the 'injector_thd' thread,
but could be read by any 'thd'. Thus:
- Any update of such a variable need a mutex lock.
- Reading such a variable outside of the injector_thd need the mutex.
However, it should be safe to read the variable within the injector_thd
without holding the mutex! (As there are no other threads updating it)
*/
/*
Flag showing if the ndb binlog should be created, if so == true
false if not
*/
bool ndb_binlog_running= false;
static bool ndb_binlog_tables_inited= false; //injector_data_mutex, relaxed
static bool ndb_binlog_is_ready= false; //injector_data_mutex, relaxed
bool
ndb_binlog_is_read_only(void)
{
/*
Could be called from any client thread. Need a mutex to
protect ndb_binlog_tables_inited and ndb_binlog_is_ready.
*/
Mutex_guard injector_g(injector_data_mutex);
if (!ndb_binlog_tables_inited)
{
/* the ndb_* system tables not setup yet */
return true;
}
if (ndb_binlog_running && !ndb_binlog_is_ready)
{
/*
The binlog thread is supposed to write to binlog
but not ready (still initializing or has lost connection)
*/
return true;
}
return false;
}
static THD *injector_thd= NULL;
/*
Global reference to ndb injector thd object.
Used mainly by the binlog index thread, but exposed to the client sql
thread for one reason; to setup the events operations for a table
to enable ndb injector thread receiving events.
Must therefore always be used with a surrounding
mysql_mutex_lock(&injector_event_mutex), when create/dropEventOperation
*/
static Ndb *injector_ndb= NULL; //Need injector_event_mutex
static Ndb *schema_ndb= NULL; //Need injector_event_mutex
static int ndbcluster_binlog_inited= 0;
/* NDB Injector thread (used for binlog creation) */
static ulonglong ndb_latest_applied_binlog_epoch= 0;
static ulonglong ndb_latest_handled_binlog_epoch= 0;
static ulonglong ndb_latest_received_binlog_epoch= 0;
NDB_SHARE *ndb_apply_status_share= NULL;
static NDB_SHARE *ndb_schema_share= NULL; //Need injector_data_mutex
extern bool opt_log_slave_updates;
static bool g_ndb_log_slave_updates;
static bool g_injector_v1_warning_emitted = false;
static void remove_all_event_operations(Ndb *s_ndb, Ndb *i_ndb);
bool ndb_schema_dist_is_ready(void)
{
Mutex_guard schema_share_g(injector_data_mutex);
if (ndb_schema_share)
return true;
DBUG_PRINT("info", ("ndb schema dist not ready"));
return false;
}
static void run_query(THD *thd, char *buf, char *end,
const int *no_print_error)
{
/*
NOTE! Don't use this function for new implementation, backward
compat. only
*/
Ndb_local_connection mysqld(thd);
/*
Run the query, suppress some errors from being printed
to log and ignore any error returned
*/
(void)mysqld.raw_run_query(buf, (end - buf),
no_print_error);
}
bool
Ndb_binlog_client::create_event_data(NDB_SHARE *share,
const dd::Table *table_def,
Ndb_event_data **event_data) const
{
DBUG_ENTER("Ndb_binlog_client::create_event_data");
DBUG_ASSERT(table_def);
DBUG_ASSERT(event_data);
Ndb_event_data* new_event_data =
Ndb_event_data::create_event_data(m_thd, share,
share->db, share->table_name,
share->key_string(), injector_thd,
table_def);
if (!new_event_data)
DBUG_RETURN(false);
// Return the newly created event_data to caller
*event_data = new_event_data;
DBUG_RETURN(true);
}
static int
get_ndb_blobs_value(TABLE* table, NdbValue* value_array,
uchar*& buffer, uint& buffer_size,
my_ptrdiff_t ptrdiff)
{
DBUG_ENTER("get_ndb_blobs_value");
// Field has no field number so cannot use TABLE blob_field
// Loop twice, first only counting total buffer size
for (int loop= 0; loop <= 1; loop++)
{
uint32 offset= 0;
for (uint i= 0; i < table->s->fields; i++)
{
Field *field= table->field[i];
NdbValue value= value_array[i];
if (! (field->flags & BLOB_FLAG && field->stored_in_db))
continue;
if (value.blob == NULL)
{
DBUG_PRINT("info",("[%u] skipped", i));
continue;
}
Field_blob *field_blob= (Field_blob *)field;
NdbBlob *ndb_blob= value.blob;
int isNull;
if (ndb_blob->getNull(isNull) != 0)
DBUG_RETURN(-1);
if (isNull == 0) {
Uint64 len64= 0;
if (ndb_blob->getLength(len64) != 0)
DBUG_RETURN(-1);
// Align to Uint64
uint32 size= Uint32(len64);
if (size % 8 != 0)
size+= 8 - size % 8;
if (loop == 1)
{
uchar *buf= buffer + offset;
uint32 len= buffer_size - offset; // Size of buf
if (ndb_blob->readData(buf, len) != 0)
DBUG_RETURN(-1);
DBUG_PRINT("info", ("[%u] offset: %u buf: 0x%lx len=%u [ptrdiff=%d]",
i, offset, (long) buf, len, (int)ptrdiff));
DBUG_ASSERT(len == len64);
// Ugly hack assumes only ptr needs to be changed
field_blob->set_ptr_offset(ptrdiff, len, buf);
}
offset+= size;
}
else if (loop == 1) // undefined or null
{
// have to set length even in this case
uchar *buf= buffer + offset; // or maybe NULL
uint32 len= 0;
field_blob->set_ptr_offset(ptrdiff, len, buf);
DBUG_PRINT("info", ("[%u] isNull=%d", i, isNull));
}
}
if (loop == 0 && offset > buffer_size)
{
my_free(buffer);
buffer_size= 0;
DBUG_PRINT("info", ("allocate blobs buffer size %u", offset));
buffer= (uchar*) my_malloc(PSI_INSTRUMENT_ME, offset, MYF(MY_WME));
if (buffer == NULL)
{
ndb_log_error("get_ndb_blobs_value, my_malloc(%u) failed", offset);
DBUG_RETURN(-1);
}
buffer_size= offset;
}
}
DBUG_RETURN(0);
}
/*****************************************************************
functions called from master sql client threads
****************************************************************/
/*
called in mysql_show_binlog_events and reset_logs to make sure we wait for
all events originating from the 'thd' to arrive in the binlog.
'thd' is expected to be non-NULL.
Wait for the epoch in which the last transaction of the 'thd' is a part of.
Wait a maximum of 30 seconds.
*/
static void ndbcluster_binlog_wait(THD *thd)
{
if (ndb_binlog_running)
{
DBUG_ENTER("ndbcluster_binlog_wait");
DBUG_ASSERT(thd);
DBUG_ASSERT(thd_sql_command(thd) == SQLCOM_SHOW_BINLOG_EVENTS ||
thd_sql_command(thd) == SQLCOM_FLUSH ||
thd_sql_command(thd) == SQLCOM_RESET);
/*
Binlog Injector should not wait for itself
*/
if (thd->system_thread == SYSTEM_THREAD_NDBCLUSTER_BINLOG)
DBUG_VOID_RETURN;
Thd_ndb *thd_ndb = get_thd_ndb(thd);
if (!thd_ndb)
{
/*
thd has not interfaced with ndb before
so there is no need for waiting
*/
DBUG_VOID_RETURN;
}
const char *save_info = thd->proc_info;
thd->proc_info = "Waiting for ndbcluster binlog update to "
"reach current position";
/*
Highest epoch that a transaction against Ndb has received
as part of commit processing *in this thread*. This is a
per-session 'most recent change' indicator.
*/
const Uint64 session_last_committed_epoch =
thd_ndb->m_last_commit_epoch_session;
/*
* Wait until the last committed epoch from the session enters Binlog.
* Break any possible deadlock after 30s.
*/
int count = 30;
mysql_mutex_lock(&injector_data_mutex);
const Uint64 start_handled_epoch = ndb_latest_handled_binlog_epoch;
while (!thd->killed && count && ndb_binlog_running &&
(ndb_latest_handled_binlog_epoch == 0 ||
ndb_latest_handled_binlog_epoch < session_last_committed_epoch))
{
count--;
struct timespec abstime;
set_timespec(&abstime, 1);
mysql_cond_timedwait(&injector_data_cond, &injector_data_mutex, &abstime);
}
mysql_mutex_unlock(&injector_data_mutex);
if (count == 0)
{
ndb_log_warning("Thread id %u timed out (30s) waiting for epoch %u/%u "
"to be handled. Progress : %u/%u -> %u/%u.",
thd->thread_id(),
Uint32((session_last_committed_epoch >> 32) & 0xffffffff),
Uint32(session_last_committed_epoch & 0xffffffff),
Uint32((start_handled_epoch >> 32) & 0xffffffff),
Uint32(start_handled_epoch & 0xffffffff),
Uint32((ndb_latest_handled_binlog_epoch >> 32) & 0xffffffff),
Uint32(ndb_latest_handled_binlog_epoch & 0xffffffff));
// Fail on wait/deadlock timeout in debug compile
DBUG_ASSERT(false);
}
thd->proc_info= save_info;
DBUG_VOID_RETURN;
}
}
/*
Called from MYSQL_BIN_LOG::reset_logs in log.cc when binlog is emptied
*/
static int ndbcluster_reset_logs(THD *thd)
{
if (!ndb_binlog_running)
return 0;
/* only reset master should reset logs */
if (!((thd->lex->sql_command == SQLCOM_RESET) &&
(thd->lex->type & REFRESH_MASTER)))
return 0;
DBUG_ENTER("ndbcluster_reset_logs");
/*
Wait for all events originating from this mysql server has
reached the binlog before continuing to reset
*/
ndbcluster_binlog_wait(thd);
/*
Truncate mysql.ndb_binlog_index table, if table does not
exist ignore the error as it is a "consistent" behavior
*/
Ndb_local_connection mysqld(thd);
const bool ignore_no_such_table = true;
if(mysqld.truncate_table(STRING_WITH_LEN("mysql"),
STRING_WITH_LEN("ndb_binlog_index"),
ignore_no_such_table))
{
// Failed to truncate table
DBUG_RETURN(1);
}
DBUG_RETURN(0);
}
/*
Setup THD object
'Inspired' from ha_ndbcluster.cc : ndb_util_thread_func
*/
THD *
ndb_create_thd(char * stackptr)
{
DBUG_ENTER("ndb_create_thd");
THD * thd= new THD; /* note that constructor of THD uses DBUG_ */
if (thd == 0)
{
DBUG_RETURN(0);
}
THD_CHECK_SENTRY(thd);
thd->thread_stack= stackptr; /* remember where our stack is */
if (thd->store_globals())
{
delete thd;
DBUG_RETURN(0);
}
thd->init_query_mem_roots();
thd->set_command(COM_DAEMON);
thd->system_thread= SYSTEM_THREAD_NDBCLUSTER_BINLOG;
thd->get_protocol_classic()->set_client_capabilities(0);
thd->lex->start_transaction_opt= 0;
thd->security_context()->skip_grants();
CHARSET_INFO *charset_connection= get_charset_by_csname("utf8",
MY_CS_PRIMARY,
MYF(MY_WME));
thd->variables.character_set_client= charset_connection;
thd->variables.character_set_results= charset_connection;
thd->variables.collation_connection= charset_connection;
thd->update_charset();
DBUG_RETURN(thd);
}
/*
Called from MYSQL_BIN_LOG::purge_logs in log.cc when the binlog "file"
is removed
*/
static int
ndbcluster_binlog_index_purge_file(THD *passed_thd, const char *file)
{
int stack_base = 0;
int error = 0;
DBUG_ENTER("ndbcluster_binlog_index_purge_file");
DBUG_PRINT("enter", ("file: %s", file));
if (!ndb_binlog_running || (passed_thd && passed_thd->slave_thread))
DBUG_RETURN(0);
/**
* This function cannot safely reuse the passed thd object
* due to the variety of places from which it is called.
* new/delete one...yuck!
*/
THD* my_thd;
if ((my_thd = ndb_create_thd((char*)&stack_base) /* stack ptr */) == 0)
{
/**
* TODO return proper error code here,
* BUT! return code is not (currently) checked in
* log.cc : purge_index_entry() so we settle for warning printout
*/
ndb_log_warning("Unable to purge " NDB_REP_DB "." NDB_REP_TABLE
" File=%s (failed to setup thd)", file);
DBUG_RETURN(0);
}
/*
delete rows from mysql.ndb_binlog_index table for the given
filename, if table does not exist ignore the error as it
is a "consistent" behavior
*/
Ndb_local_connection mysqld(my_thd);
const bool ignore_no_such_table = true;
// Set needed isolation level to be independent from server settings
my_thd->variables.transaction_isolation= ISO_REPEATABLE_READ;
// Turn autocommit on
// This is needed to ensure calls to mysqld.delete_rows commits.
my_thd->variables.option_bits&= ~OPTION_NOT_AUTOCOMMIT;
// Ensure that file paths on Windows are not modified by parser
my_thd->variables.sql_mode|= MODE_NO_BACKSLASH_ESCAPES;
if(mysqld.delete_rows(STRING_WITH_LEN("mysql"),
STRING_WITH_LEN("ndb_binlog_index"),
ignore_no_such_table,
"File='", file, "'", NULL))
{
// Failed to delete rows from table
error = 1;
}
delete my_thd;
if (passed_thd)
{
/* Relink passed THD with this thread */
passed_thd->store_globals();
}
DBUG_RETURN(error);
}
// Determine if privilege tables are distributed, ie. stored in NDB
bool
Ndb_dist_priv_util::priv_tables_are_in_ndb(THD* thd)
{
bool distributed= false;
Ndb_dist_priv_util dist_priv;
DBUG_ENTER("ndbcluster_distributed_privileges");
Ndb *ndb= check_ndb_in_thd(thd);
if (!ndb)
DBUG_RETURN(false); // MAGNUS, error message?
if (ndb->setDatabaseName(dist_priv.database()) != 0)
DBUG_RETURN(false);
const char* table_name;
while((table_name= dist_priv.iter_next_table()))
{
DBUG_PRINT("info", ("table_name: %s", table_name));
Ndb_table_guard ndbtab_g(ndb->getDictionary(), table_name);
const NDBTAB *ndbtab= ndbtab_g.get_table();
if (ndbtab)
{
distributed= true;
}
else if (distributed)
{
ndb_log_error("Inconsistency detected in distributed "
"privilege tables. Table '%s.%s' is not distributed",
dist_priv.database(), table_name);
DBUG_RETURN(false);
}
}
DBUG_RETURN(distributed);
}
/*
ndbcluster_binlog_log_query
- callback function installed in handlerton->binlog_log_query
- called by MySQL Server in places where no other handlerton
function exists which can be used to notify about changes
- used by ndbcluster to detect when
-- databases are created or altered
-- privilege tables have been modified
*/
static void
ndbcluster_binlog_log_query(handlerton*, THD *thd,
enum_binlog_command binlog_command,
const char *query, uint query_length,
const char *db, const char *table_name)
{
DBUG_ENTER("ndbcluster_binlog_log_query");
DBUG_PRINT("enter", ("db: %s table_name: %s query: %s",
db, table_name, query));
if (DBUG_EVALUATE_IF("ndb_binlog_random_tableid", true, false))
{
/**
* Simulate behaviour immediately after mysql_main() init:
* We do *not* set the random seed, which according to 'man rand'
* is equivalent of setting srand(1). In turn this will result
* in the same sequence of random numbers being produced on all mysqlds.
*/
srand(1);
}
enum SCHEMA_OP_TYPE type;
/**
* Don't have any table_id/_version to uniquely identify the
* schema operation. Set the special values 0/0 which allows
* ndbcluster_log_schema_op() to produce its own unique ids.
*/
const uint32 table_id= 0, table_version= 0;
switch (binlog_command)
{
case LOGCOM_CREATE_DB:
DBUG_PRINT("info", ("New database '%s' created", db));
type= SOT_CREATE_DB;
break;
case LOGCOM_ALTER_DB:
DBUG_PRINT("info", ("The database '%s' was altered", db));
type= SOT_ALTER_DB;
break;
case LOGCOM_ACL_NOTIFY:
DBUG_PRINT("info", ("Privilege tables have been modified"));
type= SOT_GRANT;
if (!Ndb_dist_priv_util::priv_tables_are_in_ndb(thd))
{
DBUG_VOID_RETURN;
}
/*
NOTE! Grant statements with db set to NULL is very rare but
may be provoked by for example dropping the currently selected
database. Since ndbcluster_log_schema_op does not allow
db to be NULL(can't create a key for the ndb_schem_object nor
writeNULL to ndb_schema), the situation is salvaged by setting db
to the constant string "mysql" which should work in most cases.
Interestingly enough this "hack" has the effect that grant statements
are written to the remote binlog in same format as if db would have
been NULL.
*/
if (!db)
db = "mysql";
break;
default:
DBUG_PRINT("info", ("Ignoring binlog_log_query notification"));
DBUG_VOID_RETURN;
break;
}
ndbcluster_log_schema_op(thd, query, query_length,
db, table_name, table_id, table_version, type,
NULL, NULL);
DBUG_VOID_RETURN;
}
// Instantiate Ndb_binlog_thread component
static Ndb_binlog_thread ndb_binlog_thread;
/*
End use of the NDB Cluster binlog
- wait for binlog thread to shutdown
*/
int ndbcluster_binlog_end()
{
DBUG_ENTER("ndbcluster_binlog_end");
if (ndbcluster_binlog_inited)
{
ndbcluster_binlog_inited= 0;
ndb_binlog_thread.stop();
ndb_binlog_thread.deinit();
mysql_mutex_destroy(&injector_event_mutex);
mysql_mutex_destroy(&injector_data_mutex);
mysql_cond_destroy(&injector_data_cond);
}
DBUG_RETURN(0);
}
/*****************************************************************
functions called from slave sql client threads
****************************************************************/
static void ndbcluster_reset_slave(THD *thd)
{
if (!ndb_binlog_running)
return;
DBUG_ENTER("ndbcluster_reset_slave");
/*
delete all rows from mysql.ndb_apply_status table
- if table does not exist ignore the error as it
is a consistent behavior
*/
if (opt_ndb_clear_apply_status)
{
Ndb_local_connection mysqld(thd);
const bool ignore_no_such_table = true;
if(mysqld.delete_rows(STRING_WITH_LEN("mysql"),
STRING_WITH_LEN("ndb_apply_status"),
ignore_no_such_table,
NULL))
{
// Failed to delete rows from table
}
}
g_ndb_slave_state.atResetSlave();
// pending fix for bug#59844 will make this function return int
DBUG_VOID_RETURN;
}
static int ndbcluster_binlog_func(handlerton*, THD *thd,
enum_binlog_func fn,
void *arg)
{
DBUG_ENTER("ndbcluster_binlog_func");
int res= 0;
switch(fn)
{
case BFN_RESET_LOGS:
res= ndbcluster_reset_logs(thd);
break;
case BFN_RESET_SLAVE:
ndbcluster_reset_slave(thd);
break;
case BFN_BINLOG_WAIT:
ndbcluster_binlog_wait(thd);
break;
case BFN_BINLOG_END:
res= ndbcluster_binlog_end();
break;
case BFN_BINLOG_PURGE_FILE:
res= ndbcluster_binlog_index_purge_file(thd, (const char *)arg);
break;
}
DBUG_RETURN(res);
}
/*
Initialize the binlog part of the ndb handlerton
*/
void ndbcluster_binlog_init(handlerton* h)
{
h->binlog_func= ndbcluster_binlog_func;
h->binlog_log_query= ndbcluster_binlog_log_query;
}
/*
ndb_notify_tables_writable
Called to notify any waiting threads that Ndb tables are
now writable
*/
static void ndb_notify_tables_writable()
{
mysql_mutex_lock(&ndbcluster_mutex);
ndb_setup_complete= 1;
mysql_cond_broadcast(&ndbcluster_cond);
mysql_mutex_unlock(&ndbcluster_mutex);
}
static int
ndb_create_table_from_engine(THD *thd,
const char *schema_name,
const char *table_name,
bool force_overwrite = false)
{
DBUG_ENTER("ndb_create_table_from_engine");
DBUG_PRINT("enter", ("schema_name: %s, table_name: %s",
schema_name, table_name));
Thd_ndb* thd_ndb = get_thd_ndb(thd);
Ndb* ndb = thd_ndb->ndb;
NDBDICT* dict = ndb->getDictionary();
if (ndb->setDatabaseName(schema_name))
{
DBUG_PRINT("error", ("Failed to set database name of Ndb object"));
DBUG_RETURN(false);
}
Ndb_table_guard ndbtab_g(dict, table_name);
const NDBTAB *tab= ndbtab_g.get_table();
if (!tab)
{
// Could not open the table from NDB
const NdbError err= dict->getNdbError();
if (err.code == 709 || err.code == 723)
{
// Got the normal 'No such table existed'
DBUG_PRINT("info", ("No such table, error: %u", err.code));
DBUG_RETURN(709);
}
// Got an unexpected error
DBUG_PRINT("error", ("Got unexpected error when trying to open table "
"from NDB, error %u", err.code));
DBUG_ASSERT(false); // Catch in debug
DBUG_RETURN(1);
}
DBUG_PRINT("info", ("Found table '%s'", table_name));
dd::sdi_t sdi;
{
Uint32 version;
void* unpacked_data;
Uint32 unpacked_len;
const int get_result =
tab->getExtraMetadata(version,
&unpacked_data, &unpacked_len);
if (get_result != 0)
{
DBUG_PRINT("error", ("Could not get extra metadata, error: %d",
get_result));
DBUG_RETURN(10);
}
if (version != 2)
{
free(unpacked_data);
DBUG_PRINT("error", ("Found extra metadata with unsupported "
"version: %d", version));
DBUG_RETURN(11);
}
sdi.assign(static_cast<const char*>(unpacked_data), unpacked_len);
free(unpacked_data);
}
// Found table, now install it in DD
Ndb_dd_client dd_client(thd);
// First acquire exclusive MDL lock on schema and table
if (!dd_client.mdl_locks_acquire_exclusive(schema_name, table_name))
{
DBUG_RETURN(12);
}
if (!dd_client.install_table(schema_name, table_name,
sdi,
tab->getObjectId(), tab->getObjectVersion(),
force_overwrite))
{
DBUG_RETURN(13);
}
const dd::Table* table_def;
if (!dd_client.get_table(schema_name, table_name, &table_def))
{
DBUG_RETURN(14);
}
// Check if binlogging should be setup for this table
if (ndbcluster_binlog_setup_table(thd, ndb,
schema_name, table_name,
table_def))
{
DBUG_RETURN(37);
}
dd_client.commit();
DBUG_RETURN(0);
}
/**
Utility class encapsulating the code which setup the 'ndb binlog thread'
to be "connected" to the cluster.
This involves:
- synchronizing the local mysqld data dictionary with that in NDB
- subscribing to changes that happen in NDB, thus allowing:
-- local Data Dictionary to be kept in synch
-- changes in NDB to be written to binlog
*/
class Ndb_binlog_setup {
THD* const m_thd;
Thd_ndb* const m_thd_ndb;
/*
NDB has no representation of the database schema objects, but
the mysql.ndb_schema table contains the latest schema operations
done via a mysqld, and thus reflects databases created/dropped/altered.
This function tries to restore the correct state w.r.t created databases
using the information in that table.
*/
static
int find_all_databases(THD *thd, Thd_ndb* thd_ndb)
{
Ndb *ndb= thd_ndb->ndb;
NDBDICT *dict= ndb->getDictionary();
NdbTransaction *trans= NULL;
NdbError ndb_error;
int retries= 100;
int retry_sleep= 30; /* 30 milliseconds, transaction */
DBUG_ENTER("Ndb_binlog_setup::find_all_databases");
/*
Function should only be called while ndbcluster_global_schema_lock
is held, to ensure that ndb_schema table is not being updated while
scanning.
*/
if (!thd_ndb->has_required_global_schema_lock("Ndb_binlog_setup::find_all_databases"))
DBUG_RETURN(1);
ndb->setDatabaseName(NDB_REP_DB);
Thd_ndb::Options_guard thd_ndb_options(thd_ndb);
thd_ndb_options.set(Thd_ndb::IS_SCHEMA_DIST_PARTICIPANT);
while (1)
{
char db_buffer[FN_REFLEN];
char *db= db_buffer+1;