-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.c
271 lines (246 loc) · 5.85 KB
/
connection.c
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
#include "jimcore_internal.h"
#include <mysql.h>
#include <mysqld_error.h>
char *host = "dogfood";
char *user = "dogfs";
char *passwd = "";
char *db = "dogfs";
char *unix_socket = NULL;
static unsigned port = 3306;
static int flags = 0;
#define NSEC_PER_SEC (1000 * 1000 * 1000)
#define NSEC_PER_MSEC (1000 * 1000)
void
connection_close(connection_t *c)
{
int n = SET_COUNT(stmts);
for (int i = 0; i < n; ++i) {
if (c->stmts[i].s) {
if (mysql_stmt_close(c->stmts[i].s))
vlog("error closing statement");
}
}
mysql_close(c->c);
free(c);
}
static bool
connection_statement(connection_t *c, struct stmt_def *def)
{
MYSQL_STMT *s = mysql_stmt_init(c->c);
if (!s)
return false;;
int r = mysql_stmt_prepare(s, def->query, strlen(def->query));
if (r) {
vlog("Error preparing `%s': %s", def->query, mysql_error(c->c));
mysql_stmt_close(s);
return true;
}
c->stmts[def->id].s = s;
c->stmts[def->id].def = def;;
return false;
}
static connection_t *
connection_create(MYSQL *m)
{
int n = SET_COUNT(stmts);
size_t bytes = sizeof(connection_t) + sizeof(statement_t) * n;
connection_t *c = malloc(bytes);
memset(c, 0, bytes);
c->c = m;
return c;
}
static bool
connection_init_statements(connection_t *c)
{
struct stmt_def **defptr;
SET_FOREACH(defptr, stmts) {
if (connection_statement(c, *defptr))
return false;
}
return true;
}
connection_t *
connection_open_raw(void)
{
MYSQL *c = mysql_init(NULL);
if (mysql_real_connect(c, host, user, passwd, db,
port, unix_socket, flags)) {
vvlog("Opened mysql connection");
return connection_create(c);
}
vlog("Error connecting to database: %s", mysql_error(c));
mysql_close(c);
return NULL;
}
connection_t *
connection_open(void)
{
connection_t *c = connection_open_raw();
if (!c) {
return NULL;
} else if (!connection_init_statements(c)) {
connection_close(c);
return NULL;
} else {
return c;
}
}
static struct timespec
gettime(void)
{
struct timespec tv;
clock_gettime(CLOCK_MONOTONIC, &tv);
return tv;
}
static int
statement_error(statement_t *s)
{
counter_inc(&s->def->errors);
int err = mysql_stmt_errno(s->s);
if (err == ER_DUP_ENTRY) {
return -EEXIST;
} else {
vlog("query error for `%s`: %s",
s->def->query, mysql_stmt_error(s->s));
return -ETRANSIENT;
}
}
/* Return time difference in ns */
static unsigned long
timespec_sub_ns(struct timespec *a, struct timespec *b)
{
unsigned long ns = (a->tv_sec - b->tv_sec) * NSEC_PER_SEC;
ns += a->tv_nsec;
ns -= b->tv_nsec;
return ns;
}
int
_connection_execute(connection_t *c, int id, MYSQL_BIND *params,
statement_t **s_out)
{
int r;
struct timespec start = gettime();
c->statement_count++;
statement_t *s = &c->stmts[id];
counter_inc(&s->def->calls);
if (mysql_stmt_bind_param(s->s, params))
r = statement_error(s);
else if (mysql_stmt_execute(s->s))
r = statement_error(s);
else
r = 0;
struct timespec finish = gettime();
counter_add(&s->def->runtime_ns, timespec_sub_ns(&finish, &start));
*s_out = s;
return r;
}
void
connection_error(connection_t *c)
{
c->errored = true;
}
void
statement_release(statement_t *s)
{
if (mysql_stmt_free_result(s->s))
vlog("error freeing result");
}
static void
connection_init(void)
{
int i = 0;
struct stmt_def **sptr;
SET_FOREACH(sptr, stmts) {
struct stmt_def *s = *sptr;
s->id = i++;
counter_init(&s->calls);
counter_init(&s->errors);
counter_init(&s->runtime_ns);
}
}
SET_ENTRY(init_hooks, connection_init);
bool
statement_bind_result(statement_t *s, MYSQL_BIND *row)
{
if (mysql_stmt_bind_result(s->s, row)) {
vlog("cannot bind result for `%s': %s",
s->def->query, mysql_stmt_error(s->s));
return true;
}
return false;
}
int
statement_fetch(statement_t *s)
{
int r = mysql_stmt_fetch(s->s);
if (r == 1)
vlog("cannot fetch row for `%s': %s",
s->def->query, mysql_stmt_error(s->s));
return r;
}
int
connection_begin(connection_t *c)
{
return run_statement(c, "begin", NULL);
}
int
connection_commit(connection_t *c)
{
return run_statement(c, "commit", NULL);
}
int
connection_rollback(connection_t *c)
{
return run_statement(c, "rollback", NULL);
}
int
connection_commit_or_rollback(connection_t *c, int res)
{
if (res < 0)
connection_rollback(c);
else {
int x = connection_commit(c);
if (x < 0)
res = x;
}
return res;
}
int
check_update(statement_t *s)
{
if (mysql_stmt_affected_rows(s->s) == 0)
return -ENOENT;
return 0;
}
/* Order by runtime, largest first. */
static int
stmt_def_cmp(const void *x, const void *y)
{
struct stmt_def *a = *(struct stmt_def**)x;
struct stmt_def *b = *(struct stmt_def**)y;
if (counter_value(&a->runtime_ns) > counter_value(&b->runtime_ns))
return -1;
if (counter_value(&a->runtime_ns) < counter_value(&b->runtime_ns))
return 1;
return 0;
}
static void
statements_dump_stats(void)
{
log("Prepared statements:");
int n = SET_COUNT(stmts);
struct stmt_def *stmts[n];
struct stmt_def **defptr;
int i = 0;
SET_FOREACH(defptr, stmts) {
stmts[i++] = *defptr;
}
qsort(stmts, n, sizeof(struct stmt_def *), stmt_def_cmp);
for (i = 0; i < n; ++i) {
struct stmt_def *s = stmts[i];
log(" %s [%lu msec/%ld calls/%ld errors]",
s->query, counter_value(&s->runtime_ns) / NSEC_PER_MSEC,
counter_value(&s->calls), counter_value(&s->errors));
}
}
SET_ENTRY(debug_hooks, statements_dump_stats);