-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathha_ndbcluster_connection.cc
306 lines (277 loc) · 8.41 KB
/
ha_ndbcluster_connection.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
/*
Copyright (C) 2000-2003 MySQL AB
All rights reserved. Use is subject to license terms.
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
*/
#include "ha_ndbcluster_glue.h"
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
#include <ndbapi/NdbApi.hpp>
#include <portlib/NdbTick.h>
#include "ha_ndbcluster_connection.h"
Ndb* g_ndb= NULL;
Ndb_cluster_connection* g_ndb_cluster_connection= NULL;
static Ndb_cluster_connection **g_pool= NULL;
static uint g_pool_alloc= 0;
static uint g_pool_pos= 0;
static pthread_mutex_t g_pool_mutex;
/*
Global flag in ndbapi to specify if api should wait to connect
until dict cache is clean.
Set to 1 below to not wait, as ndb handler makes sure that no
old ndb objects are used.
*/
extern int global_flag_skip_waiting_for_clean_cache;
int
ndbcluster_connect(int (*connect_callback)(void),
ulong wait_connected,
uint connection_pool_size,
bool optimized_node_select,
const char* connect_string,
uint force_nodeid)
{
NDB_TICKS end_time;
#ifndef EMBEDDED_LIBRARY
const char mysqld_name[]= "mysqld";
#else
const char mysqld_name[]= "libmysqld";
#endif
int res;
DBUG_ENTER("ndbcluster_connect");
DBUG_PRINT("enter", ("connect_string: %s, force_nodeid: %d",
connect_string, force_nodeid));
global_flag_skip_waiting_for_clean_cache= 1;
g_ndb_cluster_connection=
new Ndb_cluster_connection(connect_string, force_nodeid);
if (!g_ndb_cluster_connection)
{
sql_print_error("NDB: failed to allocate global ndb cluster connection");
DBUG_PRINT("error", ("Ndb_cluster_connection(%s)", connect_string));
my_errno= HA_ERR_OUT_OF_MEM;
DBUG_RETURN(-1);
}
{
char buf[128];
my_snprintf(buf, sizeof(buf), "%s --server-id=%lu",
mysqld_name, server_id);
g_ndb_cluster_connection->set_name(buf);
}
g_ndb_cluster_connection->set_optimized_node_selection(optimized_node_select);
// Create a Ndb object to open the connection to NDB
if ( (g_ndb= new Ndb(g_ndb_cluster_connection, "sys")) == 0 )
{
sql_print_error("NDB: failed to allocate global ndb object");
DBUG_PRINT("error", ("failed to create global ndb object"));
my_errno= HA_ERR_OUT_OF_MEM;
DBUG_RETURN(-1);
}
if (g_ndb->init() != 0)
{
DBUG_PRINT("error", ("%d message: %s",
g_ndb->getNdbError().code,
g_ndb->getNdbError().message));
DBUG_RETURN(-1);
}
/* Connect to management server */
end_time= NdbTick_CurrentMillisecond();
end_time+= 1000 * wait_connected;
while ((res= g_ndb_cluster_connection->connect(0,0,0)) == 1)
{
if (NdbTick_CurrentMillisecond() > end_time)
break;
do_retry_sleep(100);
if (abort_loop)
DBUG_RETURN(-1);
}
{
g_pool_alloc= connection_pool_size;
g_pool= (Ndb_cluster_connection**)
my_malloc(g_pool_alloc * sizeof(Ndb_cluster_connection*),
MYF(MY_WME | MY_ZEROFILL));
pthread_mutex_init(&g_pool_mutex,
MY_MUTEX_INIT_FAST);
g_pool[0]= g_ndb_cluster_connection;
for (uint i= 1; i < g_pool_alloc; i++)
{
if ((g_pool[i]=
new Ndb_cluster_connection(connect_string,
g_ndb_cluster_connection)) == 0)
{
sql_print_error("NDB[%u]: failed to allocate cluster connect object",
i);
DBUG_PRINT("error",("Ndb_cluster_connection[%u](%s)",
i, connect_string));
DBUG_RETURN(-1);
}
{
char buf[128];
my_snprintf(buf, sizeof(buf), "%s --server-id=%lu (connection %u)",
mysqld_name, server_id, i+1);
g_pool[i]->set_name(buf);
}
g_pool[i]->set_optimized_node_selection(optimized_node_select);
}
}
if (res == 0)
{
connect_callback();
for (uint i= 0; i < g_pool_alloc; i++)
{
int node_id= g_pool[i]->node_id();
if (node_id == 0)
{
// not connected to mgmd yet, try again
g_pool[i]->connect(0,0,0);
if (g_pool[i]->node_id() == 0)
{
sql_print_warning("NDB[%u]: starting connect thread", i);
g_pool[i]->start_connect_thread();
continue;
}
node_id= g_pool[i]->node_id();
}
DBUG_PRINT("info",
("NDBCLUSTER storage engine (%u) at %s on port %d", i,
g_pool[i]->get_connected_host(),
g_pool[i]->get_connected_port()));
NDB_TICKS now_time;
do
{
res= g_pool[i]->wait_until_ready(1, 1);
now_time= NdbTick_CurrentMillisecond();
} while (res != 0 && now_time < end_time);
const char *msg= 0;
if (res == 0)
{
msg= "all storage nodes connected";
}
else if (res > 0)
{
msg= "some storage nodes connected";
}
else if (res < 0)
{
msg= "no storage nodes connected (timed out)";
}
sql_print_information("NDB[%u]: NodeID: %d, %s",
i, node_id, msg);
}
}
else if (res == 1)
{
for (uint i= 0; i < g_pool_alloc; i++)
{
if (g_pool[i]->
start_connect_thread(i == 0 ? connect_callback : NULL))
{
sql_print_error("NDB[%u]: failed to start connect thread", i);
DBUG_PRINT("error", ("g_ndb_cluster_connection->start_connect_thread()"));
DBUG_RETURN(-1);
}
}
#ifndef DBUG_OFF
{
char buf[1024];
DBUG_PRINT("info",
("NDBCLUSTER storage engine not started, "
"will connect using %s",
g_ndb_cluster_connection->
get_connectstring(buf,sizeof(buf))));
}
#endif
}
else
{
DBUG_ASSERT(res == -1);
DBUG_PRINT("error", ("permanent error"));
sql_print_error("NDB: error (%u) %s",
g_ndb_cluster_connection->get_latest_error(),
g_ndb_cluster_connection->get_latest_error_msg());
DBUG_RETURN(-1);
}
DBUG_RETURN(0);
}
void ndbcluster_disconnect(void)
{
DBUG_ENTER("ndbcluster_disconnect");
if (g_ndb)
delete g_ndb;
g_ndb= NULL;
{
if (g_pool)
{
/* first in pool is the main one, wait with release */
for (uint i= 1; i < g_pool_alloc; i++)
{
if (g_pool[i])
delete g_pool[i];
}
my_free((uchar*) g_pool, MYF(MY_ALLOW_ZERO_PTR));
pthread_mutex_destroy(&g_pool_mutex);
g_pool= 0;
}
g_pool_alloc= 0;
g_pool_pos= 0;
}
if (g_ndb_cluster_connection)
delete g_ndb_cluster_connection;
g_ndb_cluster_connection= NULL;
DBUG_VOID_RETURN;
}
Ndb_cluster_connection *ndb_get_cluster_connection()
{
pthread_mutex_lock(&g_pool_mutex);
Ndb_cluster_connection *connection= g_pool[g_pool_pos];
g_pool_pos++;
if (g_pool_pos == g_pool_alloc)
g_pool_pos= 0;
pthread_mutex_unlock(&g_pool_mutex);
return connection;
}
ulonglong ndb_get_latest_trans_gci()
{
ulonglong val= *g_ndb_cluster_connection->get_latest_trans_gci();
for (uint i= 1; i < g_pool_alloc; i++)
{
ulonglong tmp= *g_pool[i]->get_latest_trans_gci();
if (tmp > val)
val= tmp;
}
return val;
}
void ndb_set_latest_trans_gci(ulonglong val)
{
for (uint i= 0; i < g_pool_alloc; i++)
{
*g_pool[i]->get_latest_trans_gci()= val;
}
}
int ndb_has_node_id(uint id)
{
for (uint i= 0; i < g_pool_alloc; i++)
{
if (id == g_pool[i]->node_id())
return 1;
}
return 0;
}
void ndb_get_connection_stats(Uint64* statsArr)
{
Uint64 connectionStats[ Ndb::NumClientStatistics ];
memset(statsArr, 0, sizeof(connectionStats));
for (uint i=0; i < g_pool_alloc; i++)
{
g_pool[i]->collect_client_stats(connectionStats, Ndb::NumClientStatistics);
for (Uint32 s=0; s < Ndb::NumClientStatistics; s++)
statsArr[s]+= connectionStats[s];
}
}
#endif /* WITH_NDBCLUSTER_STORAGE_ENGINE */