-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathha_ndbcluster.cc
17122 lines (15367 loc) · 510 KB
/
ha_ndbcluster.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) 2004, 2015, 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 as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
@brief
This file defines the NDB Cluster handler: the interface between
MySQL and NDB Cluster
*/
#include "ha_ndbcluster_glue.h"
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
#include "ha_ndbcluster.h"
#include <ndbapi/NdbApi.hpp>
#include <util/Bitmask.hpp>
#include <ndbapi/NdbIndexStat.hpp>
#include <ndbapi/NdbInterpretedCode.hpp>
#include "../storage/ndb/src/ndbapi/NdbQueryBuilder.hpp"
#include "../storage/ndb/src/ndbapi/NdbQueryOperation.hpp"
#include "ha_ndbcluster_binlog.h"
#include "ha_ndbcluster_push.h"
#include "ha_ndbcluster_cond.h"
#include "ha_ndbcluster_tables.h"
#include "ha_ndbcluster_connection.h"
#include "ndb_thd.h"
#include "ndb_table_guard.h"
#include "ndb_global_schema_lock.h"
#include "ndb_global_schema_lock_guard.h"
#ifndef NDB_WITHOUT_JOIN_PUSHDOWN
#include "abstract_query_plan.h"
#endif
#include "ndb_dist_priv_util.h"
#include "ha_ndb_index_stat.h"
#include <mysql/plugin.h>
#include <ndb_version.h>
#include "ndb_mi.h"
// ndb interface initialization/cleanup
extern "C" void ndb_init_internal();
extern "C" void ndb_end_internal();
static const int DEFAULT_PARALLELISM= 0;
static const ha_rows DEFAULT_AUTO_PREFETCH= 32;
static const ulong ONE_YEAR_IN_SECONDS= (ulong) 3600L*24L*365L;
ulong opt_ndb_extra_logging;
static ulong opt_ndb_wait_connected;
ulong opt_ndb_wait_setup;
static ulong opt_ndb_cache_check_time;
static uint opt_ndb_cluster_connection_pool;
static char* opt_ndb_index_stat_option;
static char* opt_ndb_connectstring;
static uint opt_ndb_nodeid;
static MYSQL_THDVAR_UINT(
autoincrement_prefetch_sz, /* name */
PLUGIN_VAR_RQCMDARG,
"Specify number of autoincrement values that are prefetched.",
NULL, /* check func. */
NULL, /* update func. */
1, /* default */
1, /* min */
65535, /* max */
0 /* block */
);
static MYSQL_THDVAR_BOOL(
force_send, /* name */
PLUGIN_VAR_OPCMDARG,
"Force send of buffers to ndb immediately without waiting for "
"other threads.",
NULL, /* check func. */
NULL, /* update func. */
1 /* default */
);
static MYSQL_THDVAR_BOOL(
use_exact_count, /* name */
PLUGIN_VAR_OPCMDARG,
"Use exact records count during query planning and for fast "
"select count(*), disable for faster queries.",
NULL, /* check func. */
NULL, /* update func. */
0 /* default */
);
static MYSQL_THDVAR_BOOL(
use_transactions, /* name */
PLUGIN_VAR_OPCMDARG,
"Use transactions for large inserts, if enabled then large "
"inserts will be split into several smaller transactions",
NULL, /* check func. */
NULL, /* update func. */
1 /* default */
);
static MYSQL_THDVAR_BOOL(
use_copying_alter_table, /* name */
PLUGIN_VAR_OPCMDARG,
"Force ndbcluster to always copy tables at alter table (should "
"only be used if on-line alter table fails).",
NULL, /* check func. */
NULL, /* update func. */
0 /* default */
);
static MYSQL_THDVAR_UINT(
optimized_node_selection, /* name */
PLUGIN_VAR_OPCMDARG,
"Select nodes for transactions in a more optimal way.",
NULL, /* check func. */
NULL, /* update func. */
3, /* default */
0, /* min */
3, /* max */
0 /* block */
);
static MYSQL_THDVAR_ULONG(
batch_size, /* name */
PLUGIN_VAR_RQCMDARG,
"Batch size in bytes.",
NULL, /* check func. */
NULL, /* update func. */
32768, /* default */
0, /* min */
ONE_YEAR_IN_SECONDS, /* max */
0 /* block */
);
static MYSQL_THDVAR_ULONG(
optimization_delay, /* name */
PLUGIN_VAR_RQCMDARG,
"For optimize table, specifies the delay in milliseconds "
"for each batch of rows sent.",
NULL, /* check func. */
NULL, /* update func. */
10, /* default */
0, /* min */
100000, /* max */
0 /* block */
);
#if NDB_VERSION_D < NDB_MAKE_VERSION(7,2,0)
#define DEFAULT_NDB_INDEX_STAT_ENABLE FALSE
#else
#define DEFAULT_NDB_INDEX_STAT_ENABLE TRUE
#endif
static MYSQL_THDVAR_BOOL(
index_stat_enable, /* name */
PLUGIN_VAR_OPCMDARG,
"Use ndb index statistics in query optimization.",
NULL, /* check func. */
NULL, /* update func. */
DEFAULT_NDB_INDEX_STAT_ENABLE /* default */
);
static MYSQL_THDVAR_ULONG(
index_stat_cache_entries, /* name */
PLUGIN_VAR_NOCMDARG,
"Obsolete (ignored and will be removed later).",
NULL, /* check func. */
NULL, /* update func. */
32, /* default */
0, /* min */
ULONG_MAX, /* max */
0 /* block */
);
static MYSQL_THDVAR_ULONG(
index_stat_update_freq, /* name */
PLUGIN_VAR_NOCMDARG,
"Obsolete (ignored and will be removed later).",
NULL, /* check func. */
NULL, /* update func. */
20, /* default */
0, /* min */
ULONG_MAX, /* max */
0 /* block */
);
static MYSQL_THDVAR_BOOL(
table_no_logging, /* name */
PLUGIN_VAR_NOCMDARG,
"",
NULL, /* check func. */
NULL, /* update func. */
FALSE /* default */
);
static MYSQL_THDVAR_BOOL(
table_temporary, /* name */
PLUGIN_VAR_NOCMDARG,
"",
NULL, /* check func. */
NULL, /* update func. */
FALSE /* default */
);
static MYSQL_THDVAR_UINT(
blob_read_batch_bytes, /* name */
PLUGIN_VAR_RQCMDARG,
"Specifies the bytesize large Blob reads "
"should be batched into. 0 == No limit.",
NULL, /* check func */
NULL, /* update func */
65536, /* default */
0, /* min */
UINT_MAX, /* max */
0 /* block */
);
static MYSQL_THDVAR_UINT(
blob_write_batch_bytes, /* name */
PLUGIN_VAR_RQCMDARG,
"Specifies the bytesize large Blob writes "
"should be batched into. 0 == No limit.",
NULL, /* check func */
NULL, /* update func */
65536, /* default */
0, /* min */
UINT_MAX, /* max */
0 /* block */
);
static MYSQL_THDVAR_UINT(
deferred_constraints, /* name */
PLUGIN_VAR_RQCMDARG,
"Specified that constraints should be checked deferred (when supported)",
NULL, /* check func */
NULL, /* update func */
0, /* default */
0, /* min */
1, /* max */
0 /* block */
);
#if NDB_VERSION_D < NDB_MAKE_VERSION(7,2,0)
#define DEFAULT_NDB_JOIN_PUSHDOWN FALSE
#else
#define DEFAULT_NDB_JOIN_PUSHDOWN TRUE
#endif
static MYSQL_THDVAR_BOOL(
join_pushdown, /* name */
PLUGIN_VAR_OPCMDARG,
"Enable pushing down of join to datanodes",
NULL, /* check func. */
NULL, /* update func. */
DEFAULT_NDB_JOIN_PUSHDOWN /* default */
);
/*
Required in index_stat.cc but available only from here
thanks to use of top level anonymous structs.
*/
bool ndb_index_stat_get_enable(THD *thd)
{
const bool value = THDVAR(thd, index_stat_enable);
return value;
}
static int ndbcluster_end(handlerton *hton, ha_panic_function flag);
static bool ndbcluster_show_status(handlerton *hton, THD*,
stat_print_fn *,
enum ha_stat_type);
static int ndbcluster_alter_tablespace(handlerton *hton,
THD* thd,
st_alter_tablespace *info);
static int ndbcluster_fill_files_table(handlerton *hton,
THD *thd,
TABLE_LIST *tables,
Item *cond);
#if MYSQL_VERSION_ID >= 50501
/**
Used to fill in INFORMATION_SCHEMA* tables.
@param hton handle to the handlerton structure
@param thd the thread/connection descriptor
@param[in,out] tables the information schema table that is filled up
@param cond used for conditional pushdown to storage engine
@param schema_table_idx the table id that distinguishes the type of table
@return Operation status
*/
static int
ndbcluster_fill_is_table(handlerton *hton, THD *thd, TABLE_LIST *tables,
Item *cond, enum enum_schema_tables schema_table_idx)
{
if (schema_table_idx == SCH_FILES)
return ndbcluster_fill_files_table(hton, thd, tables, cond);
return 0;
}
#endif
handlerton *ndbcluster_hton;
static handler *ndbcluster_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root)
{
return new (mem_root) ha_ndbcluster(hton, table);
}
static uint
ndbcluster_partition_flags()
{
return (HA_CAN_PARTITION | HA_CAN_UPDATE_PARTITION_KEY |
HA_CAN_PARTITION_UNIQUE | HA_USE_AUTO_PARTITION);
}
#ifndef NDB_WITHOUT_ONLINE_ALTER
static uint
ndbcluster_alter_table_flags(uint flags)
{
if (flags & ALTER_DROP_PARTITION)
return 0;
else
return (HA_PARTITION_FUNCTION_SUPPORTED);
}
#else
static uint
ndbcluster_alter_table_flags(uint flags)
{
const uint f=
HA_PARTITION_FUNCTION_SUPPORTED |
0;
if (flags & Alter_info::ALTER_DROP_PARTITION)
return 0;
return f;
}
#endif
#define NDB_AUTO_INCREMENT_RETRIES 100
#define BATCH_FLUSH_SIZE (32768)
/*
Room for 10 instruction words, two labels (@ 2words/label)
+ 2 extra words for the case of resolve_size == 8
*/
#define MAX_CONFLICT_INTERPRETED_PROG_SIZE 16
static int ndb_to_mysql_error(const NdbError *ndberr);
#define ERR_PRINT(err) \
DBUG_PRINT("error", ("%d message: %s", err.code, err.message))
#define ERR_RETURN(err) \
{ \
const NdbError& tmp= err; \
DBUG_RETURN(ndb_to_mysql_error(&tmp)); \
}
#define ERR_BREAK(err, code) \
{ \
const NdbError& tmp= err; \
code= ndb_to_mysql_error(&tmp); \
break; \
}
#define ERR_SET(err, code) \
{ \
const NdbError& tmp= err; \
code= ndb_to_mysql_error(&tmp); \
}
static int ndbcluster_inited= 0;
int ndbcluster_terminating= 0;
/*
Indicator and CONDVAR used to delay client and slave
connections until Ndb has Binlog setup
(bug#46955)
*/
int ndb_setup_complete= 0;
pthread_cond_t COND_ndb_setup_complete; // Signal with ndbcluster_mutex
extern Ndb* g_ndb;
uchar g_node_id_map[max_ndb_nodes];
/// Handler synchronization
pthread_mutex_t ndbcluster_mutex;
/// Table lock handling
HASH ndbcluster_open_tables;
static uchar *ndbcluster_get_key(NDB_SHARE *share, size_t *length,
my_bool not_used __attribute__((unused)));
static void modify_shared_stats(NDB_SHARE *share,
Ndb_local_table_statistics *local_stat);
static int ndb_get_table_statistics(THD *thd, ha_ndbcluster*, bool, Ndb*,
const NdbRecord *, struct Ndb_statistics *,
bool have_lock= FALSE,
uint part_id= ~(uint)0);
THD *injector_thd= 0;
// Util thread variables
pthread_t ndb_util_thread;
int ndb_util_thread_running= 0;
pthread_mutex_t LOCK_ndb_util_thread;
pthread_cond_t COND_ndb_util_thread;
pthread_cond_t COND_ndb_util_ready;
pthread_handler_t ndb_util_thread_func(void *arg);
// Index stats thread variables
pthread_t ndb_index_stat_thread;
int ndb_index_stat_thread_running= 0;
pthread_mutex_t LOCK_ndb_index_stat_thread;
pthread_cond_t COND_ndb_index_stat_thread;
pthread_cond_t COND_ndb_index_stat_ready;
pthread_mutex_t ndb_index_stat_list_mutex;
pthread_mutex_t ndb_index_stat_stat_mutex;
pthread_cond_t ndb_index_stat_stat_cond;
pthread_handler_t ndb_index_stat_thread_func(void *arg);
extern void ndb_index_stat_free(NDB_SHARE *share);
extern void ndb_index_stat_end();
/* Status variables shown with 'show status like 'Ndb%' */
struct st_ndb_status g_ndb_status;
long g_ndb_status_index_stat_cache_query = 0;
long g_ndb_status_index_stat_cache_clean = 0;
long long g_event_data_count = 0;
long long g_event_nondata_count = 0;
long long g_event_bytes_count = 0;
static long long g_slave_api_client_stats[Ndb::NumClientStatistics];
static long long g_server_api_client_stats[Ndb::NumClientStatistics];
void
update_slave_api_stats(Ndb* ndb)
{
for (Uint32 i=0; i < Ndb::NumClientStatistics; i++)
g_slave_api_client_stats[i] = ndb->getClientStat(i);
}
st_ndb_slave_state g_ndb_slave_state;
st_ndb_slave_state::st_ndb_slave_state()
: current_conflict_defined_op_count(0),
current_master_server_epoch(0),
current_max_rep_epoch(0),
max_rep_epoch(0),
sql_run_id(~Uint32(0))
{
memset(current_violation_count, 0, sizeof(current_violation_count));
memset(total_violation_count, 0, sizeof(total_violation_count));
};
void
st_ndb_slave_state::atTransactionAbort()
{
/* Reset current-transaction counters + state */
memset(current_violation_count, 0, sizeof(current_violation_count));
current_conflict_defined_op_count = 0;
current_max_rep_epoch = 0;
}
void
st_ndb_slave_state::atTransactionCommit()
{
/* Merge committed transaction counters into total state
* Then reset current transaction counters
*/
for (int i=0; i < CFT_NUMBER_OF_CFTS; i++)
{
total_violation_count[i]+= current_violation_count[i];
current_violation_count[i] = 0;
}
current_conflict_defined_op_count = 0;
if (current_max_rep_epoch > max_rep_epoch)
{
DBUG_PRINT("info", ("Max replicated epoch increases from %llu to %llu",
max_rep_epoch,
current_max_rep_epoch));
max_rep_epoch = current_max_rep_epoch;
}
current_max_rep_epoch = 0;
}
void
st_ndb_slave_state::atApplyStatusWrite(Uint32 master_server_id,
Uint32 row_server_id,
Uint64 row_epoch,
bool is_row_server_id_local)
{
if (row_server_id == master_server_id)
{
/*
WRITE_ROW to ndb_apply_status injected by MySQLD
immediately upstream of us.
Record epoch
*/
current_master_server_epoch = row_epoch;
assert(! is_row_server_id_local);
}
else if (is_row_server_id_local)
{
DBUG_PRINT("info", ("Recording application of local server %u epoch %llu "
" which is %s.",
row_server_id, row_epoch,
(row_epoch > g_ndb_slave_state.current_max_rep_epoch)?
" new highest." : " older than previously applied"));
if (row_epoch > current_max_rep_epoch)
{
/*
Store new highest epoch in thdvar. If we commit successfully
then this can become the new global max
*/
current_max_rep_epoch = row_epoch;
}
}
}
void
st_ndb_slave_state::atResetSlave()
{
/* Reset the Maximum replicated epoch vars
* on slave reset
* No need to touch the sql_run_id as that
* will increment if the slave is started
* again.
*/
current_max_rep_epoch = 0;
max_rep_epoch = 0;
}
static int check_slave_state(THD* thd)
{
DBUG_ENTER("check_slave_state");
#ifdef HAVE_NDB_BINLOG
if (!thd->slave_thread)
DBUG_RETURN(0);
const Uint32 runId = ndb_mi_get_slave_run_id();
DBUG_PRINT("info", ("Slave SQL thread run id is %u",
runId));
if (unlikely(runId != g_ndb_slave_state.sql_run_id))
{
DBUG_PRINT("info", ("Slave run id changed from %u, "
"treating as Slave restart",
g_ndb_slave_state.sql_run_id));
g_ndb_slave_state.sql_run_id = runId;
/* Always try to load the Max Replicated Epoch info
* first.
* Could be made optional if it's a problem
*/
{
/*
Load highest replicated epoch from a local
MySQLD from the cluster.
*/
DBUG_PRINT("info", ("Loading applied epoch information from %s",
NDB_APPLY_TABLE));
NdbError ndb_error;
Uint64 highestAppliedEpoch = 0;
do
{
Ndb* ndb= check_ndb_in_thd(thd);
NDBDICT* dict= ndb->getDictionary();
NdbTransaction* trans= NULL;
ndb->setDatabaseName(NDB_REP_DB);
Ndb_table_guard ndbtab_g(dict, NDB_APPLY_TABLE);
const NDBTAB* ndbtab= ndbtab_g.get_table();
if (unlikely(ndbtab == NULL))
{
ndb_error = dict->getNdbError();
break;
}
trans= ndb->startTransaction();
if (unlikely(trans == NULL))
{
ndb_error = ndb->getNdbError();
break;
}
do
{
NdbScanOperation* sop = trans->getNdbScanOperation(ndbtab);
if (unlikely(sop == NULL))
{
ndb_error = trans->getNdbError();
break;
}
const Uint32 server_id_col_num = 0;
const Uint32 epoch_col_num = 1;
NdbRecAttr* server_id_ra = 0;
NdbRecAttr* epoch_ra = 0;
if (unlikely((sop->readTuples(NdbOperation::LM_CommittedRead) != 0) ||
((server_id_ra = sop->getValue(server_id_col_num)) == NULL) ||
((epoch_ra = sop->getValue(epoch_col_num)) == NULL)))
{
ndb_error = sop->getNdbError();
break;
}
if (trans->execute(NdbTransaction::Commit))
{
ndb_error = trans->getNdbError();
break;
}
int rc = 0;
while (0 == (rc= sop->nextResult(true)))
{
Uint32 serverid = server_id_ra->u_32_value();
Uint64 epoch = epoch_ra->u_64_value();
if ((serverid == ::server_id) ||
(ndb_mi_get_ignore_server_id(serverid)))
{
highestAppliedEpoch = MAX(epoch, highestAppliedEpoch);
}
}
if (rc != 1)
{
ndb_error = sop->getNdbError();
break;
}
} while (0);
trans->close();
} while(0);
if (ndb_error.code != 0)
{
sql_print_warning("NDB Slave : Could not determine maximum replicated epoch from %s.%s "
"at Slave start, error %u %s",
NDB_REP_DB,
NDB_APPLY_TABLE,
ndb_error.code, ndb_error.message);
}
/*
Set Global status variable to the Highest Applied Epoch from
the Cluster DB.
If none was found, this will be zero.
*/
g_ndb_slave_state.max_rep_epoch = highestAppliedEpoch;
sql_print_information("NDB Slave : MaxReplicatedEpoch set to %llu (%u/%u) at Slave start",
g_ndb_slave_state.max_rep_epoch,
(Uint32)(g_ndb_slave_state.max_rep_epoch >> 32),
(Uint32)(g_ndb_slave_state.max_rep_epoch & 0xffffffff));
} // Load highest replicated epoch
} // New Slave SQL thread run id
#endif
DBUG_RETURN(0);
}
static int update_status_variables(Thd_ndb *thd_ndb,
st_ndb_status *ns,
Ndb_cluster_connection *c)
{
ns->connected_port= c->get_connected_port();
ns->connected_host= c->get_connected_host();
if (ns->cluster_node_id != (int) c->node_id())
{
ns->cluster_node_id= c->node_id();
if (&g_ndb_status == ns && g_ndb_cluster_connection == c)
sql_print_information("NDB: NodeID is %lu, management server '%s:%lu'",
ns->cluster_node_id, ns->connected_host,
ns->connected_port);
}
ns->number_of_replicas= 0;
{
int n= c->get_no_ready();
ns->number_of_ready_data_nodes= n > 0 ? n : 0;
}
ns->number_of_data_nodes= c->no_db_nodes();
ns->connect_count= c->get_connect_count();
if (thd_ndb)
{
ns->execute_count= thd_ndb->m_execute_count;
ns->scan_count= thd_ndb->m_scan_count;
ns->pruned_scan_count= thd_ndb->m_pruned_scan_count;
ns->sorted_scan_count= thd_ndb->m_sorted_scan_count;
ns->pushed_queries_defined= thd_ndb->m_pushed_queries_defined;
ns->pushed_queries_dropped= thd_ndb->m_pushed_queries_dropped;
ns->pushed_queries_executed= thd_ndb->m_pushed_queries_executed;
ns->pushed_reads= thd_ndb->m_pushed_reads;
for (int i= 0; i < MAX_NDB_NODES; i++)
{
ns->transaction_no_hint_count[i]= thd_ndb->m_transaction_no_hint_count[i];
ns->transaction_hint_count[i]= thd_ndb->m_transaction_hint_count[i];
}
for (int i=0; i < Ndb::NumClientStatistics; i++)
{
ns->api_client_stats[i] = thd_ndb->ndb->getClientStat(i);
}
ns->schema_locks_count= thd_ndb->schema_locks_count;
}
return 0;
}
/* Helper macro for definitions of NdbApi status variables */
#define NDBAPI_COUNTERS(NAME_SUFFIX, ARRAY_LOCATION) \
{"api_wait_exec_complete_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::WaitExecCompleteCount ], \
SHOW_LONGLONG}, \
{"api_wait_scan_result_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::WaitScanResultCount ], \
SHOW_LONGLONG}, \
{"api_wait_meta_request_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::WaitMetaRequestCount ], \
SHOW_LONGLONG}, \
{"api_wait_nanos_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::WaitNanosCount ], \
SHOW_LONGLONG}, \
{"api_bytes_sent_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::BytesSentCount ], \
SHOW_LONGLONG}, \
{"api_bytes_received_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::BytesRecvdCount ], \
SHOW_LONGLONG}, \
{"api_trans_start_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TransStartCount ], \
SHOW_LONGLONG}, \
{"api_trans_commit_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TransCommitCount ], \
SHOW_LONGLONG}, \
{"api_trans_abort_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TransAbortCount ], \
SHOW_LONGLONG}, \
{"api_trans_close_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TransCloseCount ], \
SHOW_LONGLONG}, \
{"api_pk_op_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::PkOpCount ], \
SHOW_LONGLONG}, \
{"api_uk_op_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::UkOpCount ], \
SHOW_LONGLONG}, \
{"api_table_scan_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TableScanCount ], \
SHOW_LONGLONG}, \
{"api_range_scan_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::RangeScanCount ], \
SHOW_LONGLONG}, \
{"api_pruned_scan_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::PrunedScanCount ], \
SHOW_LONGLONG}, \
{"api_scan_batch_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::ScanBatchCount ], \
SHOW_LONGLONG}, \
{"api_read_row_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::ReadRowCount ], \
SHOW_LONGLONG}, \
{"api_trans_local_read_row_count" NAME_SUFFIX, \
(char*) ARRAY_LOCATION[ Ndb::TransLocalReadRowCount ], \
SHOW_LONGLONG}
SHOW_VAR ndb_status_variables_dynamic[]= {
{"cluster_node_id", (char*) &g_ndb_status.cluster_node_id, SHOW_LONG},
{"config_from_host", (char*) &g_ndb_status.connected_host, SHOW_CHAR_PTR},
{"config_from_port", (char*) &g_ndb_status.connected_port, SHOW_LONG},
//{"number_of_replicas", (char*) &g_ndb_status.number_of_replicas, SHOW_LONG},
{"number_of_data_nodes",(char*) &g_ndb_status.number_of_data_nodes, SHOW_LONG},
{"number_of_ready_data_nodes",
(char*) &g_ndb_status.number_of_ready_data_nodes, SHOW_LONG},
{"connect_count", (char*) &g_ndb_status.connect_count, SHOW_LONG},
{"execute_count", (char*) &g_ndb_status.execute_count, SHOW_LONG},
{"scan_count", (char*) &g_ndb_status.scan_count, SHOW_LONG},
{"pruned_scan_count", (char*) &g_ndb_status.pruned_scan_count, SHOW_LONG},
{"schema_locks_count", (char*) &g_ndb_status.schema_locks_count, SHOW_LONG},
NDBAPI_COUNTERS("_session", &g_ndb_status.api_client_stats),
{"sorted_scan_count", (char*) &g_ndb_status.sorted_scan_count, SHOW_LONG},
{"pushed_queries_defined", (char*) &g_ndb_status.pushed_queries_defined,
SHOW_LONG},
{"pushed_queries_dropped", (char*) &g_ndb_status.pushed_queries_dropped,
SHOW_LONG},
{"pushed_queries_executed", (char*) &g_ndb_status.pushed_queries_executed,
SHOW_LONG},
{"pushed_reads", (char*) &g_ndb_status.pushed_reads, SHOW_LONG},
{NullS, NullS, SHOW_LONG}
};
SHOW_VAR ndb_status_conflict_variables[]= {
{"fn_max", (char*) &g_ndb_slave_state.total_violation_count[CFT_NDB_MAX], SHOW_LONGLONG},
{"fn_old", (char*) &g_ndb_slave_state.total_violation_count[CFT_NDB_OLD], SHOW_LONGLONG},
{"fn_max_del_win", (char*) &g_ndb_slave_state.total_violation_count[CFT_NDB_MAX_DEL_WIN], SHOW_LONGLONG},
{"fn_epoch", (char*) &g_ndb_slave_state.total_violation_count[CFT_NDB_EPOCH], SHOW_LONGLONG},
{NullS, NullS, SHOW_LONG}
};
SHOW_VAR ndb_status_injector_variables[]= {
{"api_event_data_count_injector", (char*) &g_event_data_count, SHOW_LONGLONG},
{"api_event_nondata_count_injector", (char*) &g_event_nondata_count, SHOW_LONGLONG},
{"api_event_bytes_count_injector", (char*) &g_event_bytes_count, SHOW_LONGLONG},
{NullS, NullS, SHOW_LONG}
};
SHOW_VAR ndb_status_slave_variables[]= {
NDBAPI_COUNTERS("_slave", &g_slave_api_client_stats),
{"slave_max_replicated_epoch", (char*) &g_ndb_slave_state.max_rep_epoch, SHOW_LONGLONG},
{NullS, NullS, SHOW_LONG}
};
SHOW_VAR ndb_status_server_client_stat_variables[]= {
NDBAPI_COUNTERS("", &g_server_api_client_stats),
{"api_event_data_count",
(char*) &g_server_api_client_stats[ Ndb::DataEventsRecvdCount ],
SHOW_LONGLONG},
{"api_event_nondata_count",
(char*) &g_server_api_client_stats[ Ndb::NonDataEventsRecvdCount ],
SHOW_LONGLONG},
{"api_event_bytes_count",
(char*) &g_server_api_client_stats[ Ndb::EventBytesRecvdCount ],
SHOW_LONGLONG},
{NullS, NullS, SHOW_LONG}
};
static int show_ndb_server_api_stats(THD *thd, SHOW_VAR *var, char *buff)
{
/* This function is called when SHOW STATUS / INFO_SCHEMA wants
* to see one of our status vars
* We use this opportunity to :
* 1) Update the globals with current values
* 2) Return an array of var definitions, pointing to
* the updated globals
*/
ndb_get_connection_stats((Uint64*) &g_server_api_client_stats[0]);
var->type= SHOW_ARRAY;
var->value= (char*) ndb_status_server_client_stat_variables;
return 0;
}
SHOW_VAR ndb_status_index_stat_variables[]= {
{"cache_query", (char*) &g_ndb_status_index_stat_cache_query, SHOW_LONG},
{"cache_clean", (char*) &g_ndb_status_index_stat_cache_clean, SHOW_LONG},
{NullS, NullS, SHOW_LONG}
};
#ifndef NDB_WITHOUT_JOIN_PUSHDOWN
static int ndbcluster_make_pushed_join(handlerton *, THD*,AQP::Join_plan*);
#endif
/*
Error handling functions
*/
/* Note for merge: old mapping table, moved to storage/ndb/ndberror.c */
static int ndb_to_mysql_error(const NdbError *ndberr)
{
/* read the mysql mapped error code */
int error= ndberr->mysql_code;
switch (error)
{
/* errors for which we do not add warnings, just return mapped error code
*/
case HA_ERR_NO_SUCH_TABLE:
case HA_ERR_KEY_NOT_FOUND:
return error;
/* Mapping missing, go with the ndb error code*/
case -1:
error= ndberr->code;
break;
/* Mapping exists, go with the mapped code */
default:
break;
}
/*
If we don't abort directly on warnings push a warning
with the internal error information
*/
if (!current_thd->abort_on_warning)
{
/*
Push the NDB error message as warning
- Used to be able to use SHOW WARNINGS toget more info on what the error is
- Used by replication to see if the error was temporary
*/
if (ndberr->status == NdbError::TemporaryError)
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_GET_TEMPORARY_ERRMSG, ER(ER_GET_TEMPORARY_ERRMSG),
ndberr->code, ndberr->message, "NDB");
else
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
ER_GET_ERRMSG, ER(ER_GET_ERRMSG),
ndberr->code, ndberr->message, "NDB");
}
return error;
}
#ifdef HAVE_NDB_BINLOG
/* Write conflicting row to exceptions table. */
static int write_conflict_row(NDB_SHARE *share,
NdbTransaction *trans,
const uchar *row,
NdbError& err)
{
DBUG_ENTER("write_conflict_row");
/* get exceptions table */
NDB_CONFLICT_FN_SHARE *cfn_share= share->m_cfn_share;
const NDBTAB *ex_tab= cfn_share->m_ex_tab;
DBUG_ASSERT(ex_tab != NULL);
/* get insert op */
NdbOperation *ex_op= trans->getNdbOperation(ex_tab);
if (ex_op == NULL)
{
err= trans->getNdbError();
DBUG_RETURN(-1);
}
if (ex_op->insertTuple() == -1)
{
err= ex_op->getNdbError();
DBUG_RETURN(-1);
}
{
uint32 server_id= (uint32)::server_id;
uint32 master_server_id= (uint32) ndb_mi_get_master_server_id();
uint64 master_epoch= (uint64) g_ndb_slave_state.current_master_server_epoch;
uint32 count= (uint32)++(cfn_share->m_count);
if (ex_op->setValue((Uint32)0, (const char *)&(server_id)) ||
ex_op->setValue((Uint32)1, (const char *)&(master_server_id)) ||
ex_op->setValue((Uint32)2, (const char *)&(master_epoch)) ||
ex_op->setValue((Uint32)3, (const char *)&(count)))
{
err= ex_op->getNdbError();
DBUG_RETURN(-1);
}
}
/* copy primary keys */
{
const int fixed_cols= 4;
int nkey= cfn_share->m_pk_cols;
int k;
for (k= 0; k < nkey; k++)
{
DBUG_ASSERT(row != NULL);
const uchar* data= row + cfn_share->m_offset[k];
if (ex_op->setValue((Uint32)(fixed_cols + k), (const char*)data) == -1)
{
err= ex_op->getNdbError();
DBUG_RETURN(-1);
}
}
}
DBUG_RETURN(0);
}
#endif