forked from sysown/proxysql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MySQL_Logger.cpp
387 lines (339 loc) · 9.15 KB
/
MySQL_Logger.cpp
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
#include <fstream>
#include "proxysql.h"
#include "cpp.h"
#include <dirent.h>
#include <libgen.h>
static uint8_t mysql_encode_length(uint64_t len, unsigned char *hd) {
if (len < 251) return 1;
if (len < 65536) { if (hd) { *hd=0xfc; }; return 3; }
if (len < 16777216) { if (hd) { *hd=0xfd; }; return 4; }
if (hd) { *hd=0xfe; }
return 9;
}
static inline int write_encoded_length(unsigned char *p, uint64_t val, uint8_t len, char prefix) {
if (len==1) {
*p=(char)val;
return 1;
}
*p=prefix;
p++;
memcpy(p,&val,len-1);
return len;
}
MySQL_Event::MySQL_Event (uint32_t _thread_id, char * _username, char * _schemaname , uint64_t _start_time , uint64_t _end_time , uint64_t _query_digest, char *_client, size_t _client_len) {
thread_id=_thread_id;
username=_username;
schemaname=_schemaname;
start_time=_start_time;
end_time=_end_time;
query_digest=_query_digest;
client=_client;
client_len=_client_len;
et=PROXYSQL_QUERY;
hid=UINT64_MAX;
server=NULL;
}
void MySQL_Event::set_query(const char *ptr, int len) {
query_ptr=(char *)ptr;
query_len=len;
}
void MySQL_Event::set_server(int _hid, const char *ptr, int len) {
server=(char *)ptr;
server_len=len;
hid=_hid;
}
uint64_t MySQL_Event::write(std::fstream *f) {
uint64_t total_bytes=0;
switch (et) {
case PROXYSQL_QUERY:
total_bytes=write_query(f);
break;
default:
break;
}
return total_bytes;
}
uint64_t MySQL_Event::write_query(std::fstream *f) {
uint64_t total_bytes=0;
total_bytes+=1; // et
total_bytes+=mysql_encode_length(thread_id, NULL);
username_len=strlen(username);
total_bytes+=mysql_encode_length(username_len,NULL)+username_len;
schemaname_len=strlen(schemaname);
total_bytes+=mysql_encode_length(schemaname_len,NULL)+schemaname_len;
total_bytes+=mysql_encode_length(client_len,NULL)+client_len;
total_bytes+=mysql_encode_length(hid, NULL);
if (hid!=UINT64_MAX) {
total_bytes+=mysql_encode_length(server_len,NULL)+server_len;
}
total_bytes+=mysql_encode_length(start_time,NULL);
total_bytes+=mysql_encode_length(end_time,NULL);
total_bytes+=mysql_encode_length(query_digest,NULL);
total_bytes+=mysql_encode_length(query_len,NULL)+query_len;
// write total length , fixed size
f->write((const char *)&total_bytes,sizeof(uint64_t));
//char prefix;
uint8_t len;
f->write((char *)&et,1);
len=mysql_encode_length(thread_id,buf);
write_encoded_length(buf,thread_id,len,buf[0]);
f->write((char *)buf,len);
len=mysql_encode_length(username_len,buf);
write_encoded_length(buf,username_len,len,buf[0]);
f->write((char *)buf,len);
f->write(username,username_len);
len=mysql_encode_length(schemaname_len,buf);
write_encoded_length(buf,schemaname_len,len,buf[0]);
f->write((char *)buf,len);
f->write(schemaname,schemaname_len);
len=mysql_encode_length(client_len,buf);
write_encoded_length(buf,client_len,len,buf[0]);
f->write((char *)buf,len);
f->write(client,client_len);
len=mysql_encode_length(hid,buf);
write_encoded_length(buf,hid,len,buf[0]);
f->write((char *)buf,len);
if (hid!=UINT64_MAX) {
len=mysql_encode_length(server_len,buf);
write_encoded_length(buf,server_len,len,buf[0]);
f->write((char *)buf,len);
f->write(server,server_len);
}
len=mysql_encode_length(start_time,buf);
write_encoded_length(buf,start_time,len,buf[0]);
f->write((char *)buf,len);
len=mysql_encode_length(end_time,buf);
write_encoded_length(buf,end_time,len,buf[0]);
f->write((char *)buf,len);
len=mysql_encode_length(query_digest,buf);
write_encoded_length(buf,query_digest,len,buf[0]);
f->write((char *)buf,len);
len=mysql_encode_length(query_len,buf);
write_encoded_length(buf,query_len,len,buf[0]);
f->write((char *)buf,len);
if (query_len) {
f->write(query_ptr,query_len);
}
return total_bytes;
}
extern Query_Processor *GloQPro;
MySQL_Logger::MySQL_Logger() {
enabled=false;
base_filename=NULL;
datadir=NULL;
base_filename=strdup((char *)"");
#ifdef PROXYSQL_LOGGER_PTHREAD_MUTEX
pthread_mutex_init(&wmutex,NULL);
#else
spinlock_rwlock_init(&rwlock);
#endif
logfile=NULL;
log_file_id=0;
max_log_file_size=100*1024*1024;
};
MySQL_Logger::~MySQL_Logger() {
if (datadir) {
free(datadir);
}
free(base_filename);
};
void MySQL_Logger::wrlock() {
#ifdef PROXYSQL_LOGGER_PTHREAD_MUTEX
pthread_mutex_lock(&wmutex);
#else
spin_wrlock(&rwlock);
#endif
};
void MySQL_Logger::wrunlock() {
#ifdef PROXYSQL_LOGGER_PTHREAD_MUTEX
pthread_mutex_unlock(&wmutex);
#else
spin_wrunlock(&rwlock);
#endif
};
void MySQL_Logger::flush_log() {
if (enabled==false) return;
wrlock();
flush_log_unlocked();
wrunlock();
}
void MySQL_Logger::close_log_unlocked() {
if (logfile) {
logfile->flush();
logfile->close();
delete logfile;
logfile=NULL;
}
}
void MySQL_Logger::flush_log_unlocked() {
if (enabled==false) return;
close_log_unlocked();
open_log_unlocked();
}
void MySQL_Logger::open_log_unlocked() {
log_file_id=find_next_id();
if (log_file_id!=0) {
log_file_id=find_next_id()+1;
} else {
log_file_id++;
}
char *filen=NULL;
if (base_filename[0]=='/') { // absolute path
filen=(char *)malloc(strlen(base_filename)+10);
sprintf(filen,"%s.%08d",base_filename,log_file_id);
} else { // relative path
filen=(char *)malloc(strlen(datadir)+strlen(base_filename)+10);
sprintf(filen,"%s/%s.%08d",datadir,base_filename,log_file_id);
}
logfile=new std::fstream();
logfile->exceptions ( std::ofstream::failbit | std::ofstream::badbit );
try {
logfile->open(filen , std::ios::out | std::ios::binary);
proxy_info("Starting new mysql log file %s\n", filen);
}
catch (std::ofstream::failure e) {
proxy_error("Error creating new mysql log file %s\n", filen);
delete logfile;
logfile=NULL;
}
free(filen);
};
void MySQL_Logger::set_base_filename() {
// if filename is the same, return
wrlock();
max_log_file_size=mysql_thread___eventslog_filesize;
if (strcmp(base_filename,mysql_thread___eventslog_filename)==0) {
wrunlock();
return;
}
// close current log
close_log_unlocked();
// set file id to 0 , so that find_next_id() will be called
log_file_id=0;
free(base_filename);
base_filename=strdup(mysql_thread___eventslog_filename);
if (strlen(base_filename)) {
enabled=true;
open_log_unlocked();
} else {
enabled=false;
}
wrunlock();
}
void MySQL_Logger::set_datadir(char *s) {
datadir=strdup(s);
flush_log();
};
void MySQL_Logger::log_request(MySQL_Session *sess, MySQL_Data_Stream *myds) {
if (enabled==false) return;
if (logfile==NULL) return;
MySQL_Connection_userinfo *ui=sess->client_myds->myconn->userinfo;
uint64_t curtime_real=realtime_time();
uint64_t curtime_mono=sess->thread->curtime;
int cl=0;
char *ca=(char *)""; // default
if (sess->client_myds->addr.addr) {
ca=sess->client_myds->addr.addr;
}
cl+=strlen(ca);
if (cl && sess->client_myds->addr.port) {
ca=(char *)malloc(cl+8);
sprintf(ca,"%s:%d",sess->client_myds->addr.addr,sess->client_myds->addr.port);
}
cl=strlen(ca);
MySQL_Event me(sess->thread_session_id,ui->username,ui->schemaname,
sess->CurrentQuery.start_time + curtime_real - curtime_mono,
sess->CurrentQuery.end_time + curtime_real - curtime_mono,
GloQPro->get_digest(&sess->CurrentQuery.QueryParserArgs),
ca, cl
);
char *c=(char *)sess->CurrentQuery.QueryPointer;
if (c) {
me.set_query(c,sess->CurrentQuery.QueryLength);
} else {
me.set_query("",0);
}
int sl=0;
char *sa=(char *)""; // default
if (myds) {
if (myds->myconn) {
sa=myds->myconn->parent->address;
}
}
sl+=strlen(sa);
if (sl && myds->myconn->parent->port) {
sa=(char *)malloc(sl+8);
sprintf(sa,"%s:%d", myds->myconn->parent->address, myds->myconn->parent->port);
}
sl=strlen(sa);
if (sl) {
int hid=-1;
hid=myds->myconn->parent->myhgc->hid;
me.set_server(hid,sa,sl);
}
wrlock();
me.write(logfile);
unsigned long curpos=logfile->tellp();
if (curpos > max_log_file_size) {
flush_log_unlocked();
}
wrunlock();
if (cl && sess->client_myds->addr.port) {
free(ca);
}
if (sl && myds->myconn->parent->port) {
free(sa);
}
}
void MySQL_Logger::flush() {
wrlock();
if (logfile) {
logfile->flush();
}
wrunlock();
}
unsigned int MySQL_Logger::find_next_id() {
int maxidx=0;
DIR *dir;
struct dirent *ent;
char *eval_filename = NULL;
char *eval_dirname = NULL;
char *eval_pathname = NULL;
assert(base_filename);
if (base_filename[0] == '/') {
eval_pathname = strdup(base_filename);
eval_filename = basename(eval_pathname);
eval_dirname = dirname(eval_pathname);
} else {
assert(datadir);
eval_filename = strdup(base_filename);
eval_dirname = strdup(datadir);
}
size_t efl=strlen(eval_filename);
if ((dir = opendir(eval_dirname)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
if (strlen(ent->d_name)==efl+9) {
if (strncmp(ent->d_name,eval_filename,efl)==0) {
if (ent->d_name[efl]=='.') {
int idx=atoi(ent->d_name+efl+1);
if (idx>maxidx) maxidx=idx;
}
}
}
}
closedir (dir);
if (base_filename[0] != '/') {
free(eval_dirname);
free(eval_filename);
}
if (eval_pathname) {
free(eval_pathname);
}
return maxidx;
} else {
/* could not open directory */
proxy_error("Unable to open datadir: %s\n", eval_dirname);
exit(EXIT_FAILURE);
}
return 0;
}