Skip to content

Commit d0fcdb1

Browse files
committed
Bug#36554026 Ndb : optimized_node_selection control and behaviour issues
Extend testing of the ndb_optimized_node_selection variable in MySQL Server, and fix a few issues. ndb_optimized_node_selection MTR test is extended to give some coverage of : - Table types : Read Primary, Read Backup, Fully Replicated - O_N_S variants : 0,1,2,3 Surfaced issues : - Reading and Setting optimized_node_selection option on (one of) the ndb_cluster_connection objects and/or on the session's Ndb object with implications for thread safety, testability, behaviour - Two Round-robin unhinted transaction issues : - Proximity ignoring Round-robin algorithm logic error resulting in imbalance. - Proximity info used in some cases even when optimized_node_selection is disabled. Changes - Avoid setting/clearing ndb_cluster_connection optimized_node_selection value - Rename the variable to indicate that it's a connection default - Modify NdbApi level code to consistently use the Ndb session object's value for decisions - Modify ha_ndbcluster code to consistently set the Ndb object's optimized_node_selection value prior to starting a transaction - Fix Round-robin bugs. Change-Id: I7205837bc3672110490d23c2470a95a963237e3d
1 parent e6248f5 commit d0fcdb1

File tree

9 files changed

+356
-13
lines changed

9 files changed

+356
-13
lines changed

mysql-test/suite/ndb/r/ndb_optimized_node_selection.result

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,215 @@ ndb_optimized_node_selection 3
2323
show variables like 'ndb_optimized_node_selection';
2424
Variable_name Value
2525
ndb_optimized_node_selection 1
26-
set global ndb_optimized_node_selection=3;
26+
SET @@global.ndb_optimized_node_selection = @global_start_value;
2727
SELECT @@global.ndb_optimized_node_selection;
2828
@@global.ndb_optimized_node_selection
2929
3
30+
Test behaviours within each variant
31+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=READ_BACKUP=0";
32+
insert into t1 values (0,0), (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9);
33+
create table basecounts (node_id int primary key, transcount int) engine=innodb;
34+
set ndb_optimized_node_selection=0;
35+
show variables like 'ndb_optimized_node_selection';
36+
Variable_name Value
37+
ndb_optimized_node_selection 0
38+
READ PRIMARY o-n-s 0 case Hinted autocommit select by pk
39+
node_id balance
40+
1 HALF
41+
2 HALF
42+
READ PRIMARY o-n-s 0 case Unhinted table scan
43+
node_id balance
44+
1 HALF
45+
2 HALF
46+
READ PRIMARY o-n-s 0 case Autocommit update by pk
47+
node_id balance
48+
1 HALF
49+
2 HALF
50+
set ndb_optimized_node_selection=1;
51+
show variables like 'ndb_optimized_node_selection';
52+
Variable_name Value
53+
ndb_optimized_node_selection 1
54+
READ PRIMARY o-n-s 1 case Hinted autocommit select by pk
55+
node_id balance
56+
1 HALF
57+
2 HALF
58+
READ PRIMARY o-n-s 1 case Unhinted table scan
59+
node_id balance
60+
1 HALF
61+
2 HALF
62+
READ PRIMARY o-n-s 1 case Autocommit update by pk
63+
node_id balance
64+
1 HALF
65+
2 HALF
66+
set ndb_optimized_node_selection=2;
67+
show variables like 'ndb_optimized_node_selection';
68+
Variable_name Value
69+
ndb_optimized_node_selection 2
70+
READ PRIMARY o-n-s 2 case Hinted autocommit select by pk
71+
node_id balance
72+
1 NONE
73+
2 ALL
74+
READ PRIMARY o-n-s 2 case Unhinted table scan
75+
node_id balance
76+
1 HALF
77+
2 HALF
78+
READ PRIMARY o-n-s 2 case Autocommit update by pk
79+
node_id balance
80+
1 NONE
81+
2 ALL
82+
set ndb_optimized_node_selection=3;
83+
show variables like 'ndb_optimized_node_selection';
84+
Variable_name Value
85+
ndb_optimized_node_selection 3
86+
READ PRIMARY o-n-s 3 case Hinted autocommit select by pk
87+
node_id balance
88+
1 NONE
89+
2 ALL
90+
READ PRIMARY o-n-s 3 case Unhinted table scan
91+
node_id balance
92+
1 HALF
93+
2 HALF
94+
READ PRIMARY o-n-s 3 case Autocommit update by pk
95+
node_id balance
96+
1 NONE
97+
2 ALL
98+
drop table basecounts;
99+
drop table t1;
100+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=READ_BACKUP=1";
101+
insert into t1 values (0,0), (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9);
102+
create table basecounts (node_id int primary key, transcount int) engine=innodb;
103+
set ndb_optimized_node_selection=0;
104+
show variables like 'ndb_optimized_node_selection';
105+
Variable_name Value
106+
ndb_optimized_node_selection 0
107+
READ BACKUP o-n-s 0 case Hinted autocommit select by pk
108+
node_id balance
109+
1 HALF
110+
2 HALF
111+
READ BACKUP o-n-s 0 case Unhinted table scan
112+
node_id balance
113+
1 HALF
114+
2 HALF
115+
READ BACKUP o-n-s 0 case Autocommit update by pk
116+
node_id balance
117+
1 HALF
118+
2 HALF
119+
set ndb_optimized_node_selection=1;
120+
show variables like 'ndb_optimized_node_selection';
121+
Variable_name Value
122+
ndb_optimized_node_selection 1
123+
READ BACKUP o-n-s 1 case Hinted autocommit select by pk
124+
node_id balance
125+
1 HALF
126+
2 HALF
127+
READ BACKUP o-n-s 1 case Unhinted table scan
128+
node_id balance
129+
1 HALF
130+
2 HALF
131+
READ BACKUP o-n-s 1 case Autocommit update by pk
132+
node_id balance
133+
1 HALF
134+
2 HALF
135+
set ndb_optimized_node_selection=2;
136+
show variables like 'ndb_optimized_node_selection';
137+
Variable_name Value
138+
ndb_optimized_node_selection 2
139+
READ BACKUP o-n-s 2 case Hinted autocommit select by pk
140+
node_id balance
141+
1 HALF
142+
2 HALF
143+
READ BACKUP o-n-s 2 case Unhinted table scan
144+
node_id balance
145+
1 HALF
146+
2 HALF
147+
READ BACKUP o-n-s 2 case Autocommit update by pk
148+
node_id balance
149+
1 HALF
150+
2 HALF
151+
set ndb_optimized_node_selection=3;
152+
show variables like 'ndb_optimized_node_selection';
153+
Variable_name Value
154+
ndb_optimized_node_selection 3
155+
READ BACKUP o-n-s 3 case Hinted autocommit select by pk
156+
node_id balance
157+
1 HALF
158+
2 HALF
159+
READ BACKUP o-n-s 3 case Unhinted table scan
160+
node_id balance
161+
1 HALF
162+
2 HALF
163+
READ BACKUP o-n-s 3 case Autocommit update by pk
164+
node_id balance
165+
1 HALF
166+
2 HALF
167+
drop table basecounts;
168+
drop table t1;
169+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=FULLY_REPLICATED=1";
170+
insert into t1 values (0,0), (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9);
171+
create table basecounts (node_id int primary key, transcount int) engine=innodb;
172+
set ndb_optimized_node_selection=0;
173+
show variables like 'ndb_optimized_node_selection';
174+
Variable_name Value
175+
ndb_optimized_node_selection 0
176+
FULLY REPLICATED o-n-s 0 case Hinted autocommit select by pk
177+
node_id balance
178+
1 HALF
179+
2 HALF
180+
FULLY REPLICATED o-n-s 0 case Unhinted table scan
181+
node_id balance
182+
1 HALF
183+
2 HALF
184+
FULLY REPLICATED o-n-s 0 case Autocommit update by pk
185+
node_id balance
186+
1 HALF
187+
2 HALF
188+
set ndb_optimized_node_selection=1;
189+
show variables like 'ndb_optimized_node_selection';
190+
Variable_name Value
191+
ndb_optimized_node_selection 1
192+
FULLY REPLICATED o-n-s 1 case Hinted autocommit select by pk
193+
node_id balance
194+
1 HALF
195+
2 HALF
196+
FULLY REPLICATED o-n-s 1 case Unhinted table scan
197+
node_id balance
198+
1 HALF
199+
2 HALF
200+
FULLY REPLICATED o-n-s 1 case Autocommit update by pk
201+
node_id balance
202+
1 HALF
203+
2 HALF
204+
set ndb_optimized_node_selection=2;
205+
show variables like 'ndb_optimized_node_selection';
206+
Variable_name Value
207+
ndb_optimized_node_selection 2
208+
FULLY REPLICATED o-n-s 2 case Hinted autocommit select by pk
209+
node_id balance
210+
1 HALF
211+
2 HALF
212+
FULLY REPLICATED o-n-s 2 case Unhinted table scan
213+
node_id balance
214+
1 HALF
215+
2 HALF
216+
FULLY REPLICATED o-n-s 2 case Autocommit update by pk
217+
node_id balance
218+
1 HALF
219+
2 HALF
220+
set ndb_optimized_node_selection=3;
221+
show variables like 'ndb_optimized_node_selection';
222+
Variable_name Value
223+
ndb_optimized_node_selection 3
224+
FULLY REPLICATED o-n-s 3 case Hinted autocommit select by pk
225+
node_id balance
226+
1 HALF
227+
2 HALF
228+
FULLY REPLICATED o-n-s 3 case Unhinted table scan
229+
node_id balance
230+
1 HALF
231+
2 HALF
232+
FULLY REPLICATED o-n-s 3 case Autocommit update by pk
233+
node_id balance
234+
1 HALF
235+
2 HALF
236+
drop table basecounts;
237+
drop table t1;

mysql-test/suite/ndb/t/ndb_optimized_node_selection.test

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,116 @@ show variables like 'ndb_optimized_node_selection';
2525
--connection another_con
2626
show variables like 'ndb_optimized_node_selection';
2727

28+
--connection default
2829
# reset
29-
set global ndb_optimized_node_selection=3;
30-
# does not work for some reason
31-
#SET @@global.ndb_optimized_node_selection = @global_start_value;
30+
SET @@global.ndb_optimized_node_selection = @global_start_value;
3231
SELECT @@global.ndb_optimized_node_selection;
32+
33+
--echo Test behaviours within each variant
34+
35+
--let $dist=0
36+
37+
while ($dist < 3)
38+
{
39+
if ($dist == 0)
40+
{
41+
--let $distname=READ PRIMARY
42+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=READ_BACKUP=0";
43+
}
44+
if ($dist == 1)
45+
{
46+
--let $distname=READ BACKUP
47+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=READ_BACKUP=1";
48+
}
49+
if ($dist == 2)
50+
{
51+
--let $distname=FULLY REPLICATED
52+
create table t1 (a int primary key, b int) engine=ndb comment="NDB_TABLE=FULLY_REPLICATED=1";
53+
}
54+
55+
insert into t1 values (0,0), (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9);
56+
57+
create table basecounts (node_id int primary key, transcount int) engine=innodb;
58+
59+
--let $val=0
60+
61+
while ($val < 4)
62+
{
63+
--eval set ndb_optimized_node_selection=$val
64+
show variables like 'ndb_optimized_node_selection';
65+
66+
--let $case=0
67+
while ($case < 3)
68+
{
69+
--disable_query_log
70+
--disable_result_log
71+
# Initial query ideally doing any e.g. stats fetches
72+
select * from test.t1 where a=0;
73+
74+
delete from basecounts;
75+
insert into basecounts
76+
select node_id, sum(val)
77+
from ndbinfo.counters
78+
where counter_name="TRANSACTIONS"
79+
group by node_id;
80+
81+
--let $iter=10
82+
--let $rpt=$iter
83+
while ($rpt)
84+
{
85+
if ($case == 0)
86+
{
87+
--let $casename=Hinted autocommit select by pk
88+
select * from test.t1 where a=0;
89+
}
90+
if ($case == 1)
91+
{
92+
--let $casename=Unhinted table scan
93+
begin;
94+
select * from test.t1;
95+
select * from test.t1 where a=0;
96+
rollback;
97+
}
98+
if ($case == 2)
99+
{
100+
--let $casename=Autocommit update by pk
101+
update test.t1 set b=2 where a=0;
102+
}
103+
--dec $rpt
104+
}
105+
--enable_result_log
106+
107+
# Query here works out per-node TRANSACTIONS count diff around ops
108+
# That shows the 'hinting' footprint
109+
# if statement is used to map loosely to ALL/HALF/NONE to handle
110+
# non determinism due to other activity in the cluster during the
111+
# test.
112+
--echo $distname o-n-s $val case $casename
113+
eval select x.node_id,
114+
if (x.transcount >= $iter, "ALL",
115+
if (x.transcount <= 1, "NONE",
116+
if (x.transcount BETWEEN (($iter/2)-1)
117+
AND (($iter/2)+1),
118+
"HALF", "ERROR"))) as balance
119+
from (
120+
select a.node_id, b.transcount - a.transcount as transcount
121+
from basecounts a
122+
join
123+
(select node_id, sum(val) as transcount
124+
from ndbinfo.counters
125+
where counter_name="TRANSACTIONS"
126+
group by node_id) b
127+
on a.node_id = b.node_id) x;
128+
129+
--enable_query_log
130+
131+
--inc $case
132+
}
133+
134+
--inc $val
135+
}
136+
drop table basecounts;
137+
drop table t1;
138+
139+
--inc $dist
140+
}

storage/ndb/include/ndbapi/Ndb.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,9 @@ class Ndb {
20822082

20832083
Uint64 getClientStat(Uint32 id) const;
20842084
const char *getClientStatName(Uint32 id) const;
2085+
2086+
// Set optimized node selection value for this object
2087+
void set_optimized_node_selection(int val);
20852088
#endif
20862089

20872090
private:

storage/ndb/include/ndbapi/ndb_cluster_connection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ class Ndb_cluster_connection {
373373
// Get generation of the configuration used to configure the NdbApi
374374
Uint32 get_config_generation() const;
375375

376+
// Set optimized node selection value used for Ndb objects created
377+
// from this connection.
376378
void set_optimized_node_selection(int val);
377379

378380
unsigned no_db_nodes();

storage/ndb/plugin/ha_ndbcluster.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7383,6 +7383,10 @@ void Thd_ndb::transaction_checks() {
73837383
THDVAR(thd, optimized_node_selection) =
73847384
THDVAR(nullptr, optimized_node_selection) & 1; /* using global value */
73857385
}
7386+
7387+
/* Set thread's Ndb object's optimized_node_selection (locality) value */
7388+
get_thd_ndb(thd)->ndb->set_optimized_node_selection(
7389+
THDVAR(thd, optimized_node_selection) & 1);
73867390
}
73877391

73887392
int ha_ndbcluster::start_statement(THD *thd, Thd_ndb *thd_ndb,
@@ -7671,8 +7675,6 @@ NdbTransaction *ha_ndbcluster::start_transaction(int &error) {
76717675

76727676
m_thd_ndb->transaction_checks();
76737677

7674-
const uint opti_node_select = THDVAR(table->in_use, optimized_node_selection);
7675-
m_thd_ndb->connection->set_optimized_node_selection(opti_node_select & 1);
76767678
if ((trans = m_thd_ndb->ndb->startTransaction(m_table))) {
76777679
// NOTE! No hint provided when starting transaction
76787680

storage/ndb/src/ndbapi/Ndb.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,3 +2334,7 @@ const char *Ndb::getClientStatName(Uint32 id) const {
23342334

23352335
return nullptr;
23362336
}
2337+
2338+
void Ndb::set_optimized_node_selection(int val) {
2339+
theImpl->m_optimized_node_selection = val;
2340+
}

storage/ndb/src/ndbapi/Ndbinit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ NdbImpl::NdbImpl(Ndb_cluster_connection *ndb_cluster_connection, Ndb &ndb)
236236
the_release_ind[i] = 0;
237237
}
238238
m_optimized_node_selection =
239-
m_ndb_cluster_connection.m_optimized_node_selection;
239+
m_ndb_cluster_connection.m_conn_default_optimized_node_selection;
240240

241241
forceShortRequests = false;
242242

0 commit comments

Comments
 (0)