-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.c
642 lines (562 loc) · 18.1 KB
/
runtime.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
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
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "logfile.h"
#include "mem.h"
#include "zzmalloc.h"
#include "synclog.h"
#include "server.h"
#include "dumpfile.h"
#include "wthread.h"
#include "common.h"
#include "utils.h"
#include "sslave.h"
#include "myconfig.h"
#include "runtime.h"
/**
* Apply log records to hash table.
*
* @param logname sync log file pathname
*/
static int
load_synclog(char *logname, unsigned int dumplogver, unsigned int dumplogpos)
{
int ret;
unsigned int binlogver;
int have_key = 0;
int ffd = open(logname, O_RDONLY);
if (-1 == ffd) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("open file %s error! %s\n", logname, errbuf);
MEMLINK_EXIT;
}
int len = lseek(ffd, 0, SEEK_END);
char *addr = mmap(NULL, len, PROT_READ, MAP_SHARED, ffd, 0);
if (addr == MAP_FAILED) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("synclog mmap error: %s\n", errbuf);
MEMLINK_EXIT;
}
unsigned int indexlen = *(unsigned int*)(addr + SYNCLOG_HEAD_LEN - sizeof(int));
char *data = addr + SYNCLOG_HEAD_LEN + indexlen * sizeof(int);
char *enddata = addr + len;
binlogver = *(unsigned int *)(addr + sizeof(short));
DINFO("binlogver: %d, dumplogver: %d\n", binlogver, dumplogver);
if (binlogver == dumplogver) {
int *indxdata = (int *)(addr + SYNCLOG_HEAD_LEN);
int pos;
if (dumplogpos < SYNCLOG_INDEXNUM) {
pos = indxdata[dumplogpos];
} else {
pos = 0;
}
DINFO("dumplogpos: %d, pos: %d\n", dumplogpos, pos);
if (pos == 0 && dumplogpos != 0) {
DINFO("indxdata[dumplogpos - 1]=%d\n", indxdata[dumplogpos - 1]);
if (indxdata[dumplogpos - 1] != 0) {
data = addr + indxdata[dumplogpos - 1];
have_key = 1;
}else{
//g_runtime->slave->logver = dumplogver;
//g_runtime->slave->logline = dumplogpos;
munmap(addr, len);
close(ffd);
return dumplogpos;
}
}else{
if (pos != 0) {
data = addr + pos;
}
}
}
unsigned int blen;
unsigned int logver = 0, logline = 0, count = 0;
while (data < enddata) {
//blen = *(unsigned short*)(data + SYNCPOS_LEN);
blen = *(unsigned int*)(data + SYNCPOS_LEN);
memcpy(&logver, data, sizeof(int));
memcpy(&logline, data + sizeof(int), sizeof(int));
DINFO("logver: %d, logline: %d\n", logver, logline);
if (enddata < data + SYNCPOS_LEN + blen + sizeof(int)) {
DERROR("synclog end error: %s, skip\n", logname);
//MEMLINK_EXIT;
break;
}
DINFO("command, len:%d\n", blen);
DINFO("have_key: %d\n", have_key);
if (have_key == 0) {
ret = wdata_apply(data + SYNCPOS_LEN, blen + sizeof(int), MEMLINK_NO_LOG, NULL);
if (ret != 0) {
DERROR("wdata_apply log error: %d\n", ret);
MEMLINK_EXIT;
}
ret = syncmem_write(g_runtime->syncmem, data + SYNCPOS_LEN, blen + sizeof(int), logver, logline);
if (ret != 0) {
DERROR("syncmem_write error: %d\n", ret);
MEMLINK_EXIT;
}
count ++;
}
data += SYNCPOS_LEN + blen + sizeof(int);
}
/*
if (g_cf->role == ROLE_SLAVE) {
g_runtime->slave->logver = logver;
g_runtime->slave->logline = logline;
if (dumplogver == binlogver) {
g_runtime->slave->binlog_index = count + dumplogpos;
}else{
g_runtime->slave->binlog_index = count;
}
}
*/
munmap(addr, len);
close(ffd);
return count;
}
static int
load_data()
{
int ret;
int havedump = 0;
char filename[PATH_MAX];
char dumpfileok[PATH_MAX];
struct timeval start, end;
snprintf(filename, PATH_MAX, "%s/dump.dat", g_cf->datadir);
// check dumpfile exist
/*
ret = stat(filename, &stbuf);
if (ret == -1 && errno == ENOENT) {
DINFO("not found dumpfile: %s\n", filename);
}*/
ret = isfile(filename);
if (ret == 0) {
DINFO("node fount dumpfile: %s\n", filename);
snprintf(dumpfileok, PATH_MAX, "%s/dump.dat.ok", g_cf->datadir);
if (!isfile(dumpfileok)) {
ret = 0;
} else {
rename(dumpfileok, filename);
ret = 1;
}
}
// have dumpfile, load
if (ret == 1) {
havedump = 1;
DINFO("try load dumpfile ...\n");
ret = dumpfile_load(g_runtime->ht, filename, 1);
if (ret < 0) {
DERROR("dumpfile_load error: %d\n", ret);
MEMLINK_EXIT;
return -1;
}
}
int n;
int i;
char logname[PATH_MAX];
int logids[10000] = {0};
DINFO("havedump: %d\n", havedump);
n = synclog_scan_binlog(logids, 10000);
if (n < 0) {
DERROR("get binlog error! %d\n", n);
return -1;
}
DINFO("dumplogver: %d, n: %d\n", g_runtime->dumplogver, n);
//没有dump.dat也没有dump.dat.ok,且本地binlog不是从1开始记录的
if (havedump == 0) {
/*
if (logids[0] == 0 && g_runtime->logver != 1) {//本地只有bin.log,且bin.log的logver不为1
DERROR("no dump.dat, binlog version must start from 1. logver: %d\n", g_runtime->logver);
MEMLINK_EXIT;
} else {
if (logids[0] != 1 ) {//本地有一系列的bin.log.xxx,但是logver不是从1开始的
DERROR("no dump.dat, binlog version must start from 1. binlog version start: %d\n", logids[0]);
MEMLINK_EXIT;
}
}
*/
if (!(logids[0] == 1 || g_runtime->logver == 1)) {
DERROR("no dump.dat, binlog version must start from 1. binlog version start: %d\n", logids[0]);
MEMLINK_EXIT;
}
}
unsigned int count = 0;
gettimeofday(&start, NULL);
for (i = 0; i < n; i++) {
if (logids[i] < g_runtime->dumplogver) {
continue;
}
//snprintf(logname, PATH_MAX, "%s/data/bin.log.%d", g_runtime->home, logids[i]);
snprintf(logname, PATH_MAX, "%s/bin.log.%d", g_cf->datadir, logids[i]);
DINFO("load synclog: %s\n", logname);
count += load_synclog(logname, g_runtime->dumplogver, g_runtime->dumplogpos);
}
//snprintf(logname, PATH_MAX, "%s/data/bin.log", g_runtime->home);
snprintf(logname, PATH_MAX, "%s/bin.log", g_cf->datadir);
DINFO("load synclog: %s\n", logname);
count += load_synclog(logname, g_runtime->dumplogver, g_runtime->dumplogpos);
gettimeofday(&end, NULL);
DNOTE("load bin.log time: %u us\n", timediff(&start, &end));
if (havedump == 0) {
dumpfile(g_runtime->ht);
}
DNOTE("load binlog %u ok!\n", count);
return 0;
}
static int
load_data_slave()
{
int ret;
//struct stat stbuf;
int havedump = 0;
int load_master_dump = 0;
char dump_filename[PATH_MAX];
char master_filename[PATH_MAX];
char dumpfileok[PATH_MAX];
snprintf(dump_filename, PATH_MAX, "%s/dump.dat", g_cf->datadir);
snprintf(master_filename, PATH_MAX, "%s/dump.master.dat", g_cf->datadir);
if (!isfile(dump_filename)) {
snprintf(dumpfileok, PATH_MAX, "%s/dump.dat.ok", g_cf->datadir);
if(isfile(dumpfileok)) {
rename(dumpfileok, dump_filename);
}
}
// check dumpfile exist
if (isfile(dump_filename) == 0) {
if (isfile(master_filename)) {
// load dump.master.dat
DINFO("try load master dumpfile ...\n");
ret = dumpfile_load(g_runtime->ht, master_filename, 0);
if (ret < 0) {
DERROR("dumpfile_load error: %d\n", ret);
MEMLINK_EXIT;
return -1;
}
//SSlave *slave = g_runtime->slave;
unsigned int logver, logline;
dumpfile_logver(master_filename, &logver, &logline);
load_master_dump = 1;
//如果要加载dump.master.dat,清空本地binlog
synclog_clean(logver, logline);
}
}else{
// have dumpfile, load
havedump = 1;
DINFO("try load dumpfile ...\n");
ret = dumpfile_load(g_runtime->ht, dump_filename, 1);
if (ret < 0) {
DERROR("dumpfile_load error: %d\n", ret);
MEMLINK_EXIT;
return -1;
}
}
int n;
char logname[PATH_MAX];
int logids[10000] = {0};
unsigned int count = 0;
n = synclog_scan_binlog(logids, 10000);
if (n < 0) {
DERROR("get binlog error! %d\n", n);
return -1;
}
DINFO("dumplogver: %d, n: %d\n", g_runtime->dumplogver, n);
if (load_master_dump == 0) {
//没有dump.dat也没有dump.dat.ok,且本地binlog不是从1开始记录的
if (havedump == 0) {
/*
if (logids[0] == 0 && g_runtime->logver != 1) {//本地只有bin.log,且bin.log的logver不为1
DERROR("no dump.dat, binlog version must start from 1. logver: %d\n", g_runtime->logver);
MEMLINK_EXIT;
} else {
if (logids[0] != 1 ) {//本地有一系列的bin.log.xxx,但是logver不是从1开始的
DERROR("no dump.dat, binlog version must start from 1. binlog version start: %d\n", logids[0]);
MEMLINK_EXIT;
}
}
*/
if (!(logids[0] == 1 || g_runtime->logver == 1)) {
DERROR("no dump.dat, binlog version must start from 1. binlog version start: %d\n", logids[0]);
MEMLINK_EXIT;
}
}
int i;
DINFO("load binlog ...\n");
for (i = 0; i < n; i++) {
if (logids[i] < g_runtime->dumplogver) {
continue;
}
snprintf(logname, PATH_MAX, "%s/bin.log.%d", g_cf->datadir, logids[i]);
DINFO("load synclog: %s\n", logname);
ret = load_synclog(logname, g_runtime->dumplogver, g_runtime->dumplogpos);
/*
if (ret > 0 && g_cf->role == ROLE_SLAVE) {
g_runtime->slave->binlog_ver = logids[i];
}
*/
count += ret;
}
snprintf(logname, PATH_MAX, "%s/bin.log", g_cf->datadir);
DINFO("load synclog: %s\n", logname);
ret = load_synclog(logname, g_runtime->dumplogver, g_runtime->dumplogpos);
/*
if (ret > 0 && g_cf->role == ROLE_SLAVE) {
g_runtime->slave->binlog_ver = g_runtime->synclog->version;
}
*/
count += ret;
}
if (havedump == 0) {
dumpfile(g_runtime->ht);
}
/*
if (load_master_dump == 1) {
g_runtime->slave->binlog_ver = g_runtime->logver;
}
*/
//DINFO("======== slave binlog_ver:%d, binlog_index:%d, logver:%d, logline:%d, dump_logver:%d, dumpsize:%lld, dumpfile_size:%lld\n", g_runtime->slave->binlog_ver, g_runtime->slave->binlog_index, g_runtime->slave->logver, g_runtime->slave->logline, g_runtime->slave->dump_logver, g_runtime->slave->dumpsize, g_runtime->slave->dumpfile_size);
DNOTE("load binlog %u ok!\n", count);
return 0;
}
Runtime *g_runtime;
Runtime*
runtime_create_common(char *pgname)
{
Runtime *rt = (Runtime*)zz_malloc(sizeof(Runtime));
if (NULL == rt) {
DERROR("malloc Runtime error!\n");
MEMLINK_EXIT;
return NULL;
}
memset(rt, 0, sizeof(Runtime));
g_runtime = rt;
rt->memlink_start = time(NULL);
if (realpath(pgname, rt->home) == NULL) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("realpath error: %s\n", errbuf);
MEMLINK_EXIT;
}
char *last = strrchr(rt->home, '/');
if (last != NULL) {
*last = 0;
}
DINFO("home: %s\n", rt->home);
int ret;
// create data and log dir
if (!isdir(g_cf->datadir)) {
ret = mkdir(g_cf->datadir, 0744);
if (ret == -1) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("create dir %s error! %s\n", g_cf->datadir, errbuf);
MEMLINK_EXIT;
}
}
ret = pthread_mutex_init(&rt->mutex, NULL);
if (ret != 0) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("pthread_mutex_init error: %s\n", errbuf);
MEMLINK_EXIT;
return NULL;
}
DINFO("mutex init ok!\n");
ret = pthread_mutex_init(&rt->mutex_mem, NULL);
if (ret != 0) {
char errbuf[1024];
strerror_r(errno, errbuf, 1024);
DERROR("pthread_mutex_init error: %s\n", errbuf);
MEMLINK_EXIT;
return NULL;
}
DINFO("mutex_mem init ok!\n");
rt->mpool = mempool_create();
if (NULL == rt->mpool) {
DERROR("mempool create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("mempool create ok!\n");
rt->ht = hashtable_create();
if (NULL == rt->ht) {
DERROR("hashtable_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("hashtable create ok!\n");
rt->syncmem = syncmem_create();
if (NULL == rt->syncmem) {
DERROR("syncmem_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("syncmem create ok!\n");
/*rt->server = mainserver_create();
if (NULL == rt->server) {
DERROR("mainserver_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("main thread create ok!\n");
*/
rt->synclog = synclog_create();
if (NULL == rt->synclog) {
DERROR("synclog_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("synclog open ok!\n");
DINFO("synclog index_pos:%d, pos:%d\n", g_runtime->synclog->index_pos, g_runtime->synclog->pos);
return rt;
}
Runtime*
runtime_create_slave(char *pgname, char *conffile)
{
Runtime *rt;
rt = runtime_create_common(pgname);
if (NULL == rt) {
return rt;
}
snprintf(rt->conffile, PATH_MAX, "%s", conffile);
int ret = load_data_slave();
if (ret < 0) {
DERROR("load_data error: %d\n", ret);
MEMLINK_EXIT;
return NULL;
}
DINFO("load_data ok!\n");
rt->server = mainserver_create();
if (NULL == rt->server) {
DERROR("mainserver_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("main thread create ok!\n");
rt->slave = sslave_create();
if (NULL == rt->slave) {
DERROR("sslave_create error!\n");
MEMLINK_EXIT;
}
DINFO("sslave thread create ok!\n");
rt->sthread = sthread_create();
if (NULL == rt->sthread) {
DERROR("sthread_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("sync thread create ok!\n");
rt->wthread = wthread_create();
if (NULL == rt->wthread) {
DERROR("wthread_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("write thread create ok!\n");
sslave_go(rt->slave);
DNOTE("create slave runtime ok!\n");
return rt;
}
Runtime*
runtime_create_master(char *pgname, char *conffile)
{
Runtime* rt;// = runtime_init(pgname);
rt = runtime_create_common(pgname);
if (NULL == rt) {
return rt;
}
snprintf(rt->conffile, PATH_MAX, "%s", conffile);
int ret = load_data();
if (ret < 0) {
DERROR("load_data error: %d\n", ret);
MEMLINK_EXIT;
return NULL;
}
DINFO("load_data ok!\n");
rt->server = mainserver_create();
if (NULL == rt->server) {
DERROR("mainserver_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("main thread create ok!\n");
rt->wthread = wthread_create();
if (NULL == rt->wthread) {
DERROR("wthread_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("write thread create ok!\n");
rt->sthread = sthread_create();
if (NULL == rt->sthread) {
DERROR("sthread_create error!\n");
MEMLINK_EXIT;
return NULL;
}
DINFO("sync thread create ok!\n");
DNOTE("create master Runtime ok!\n");
return rt;
}
void
runtime_destroy(Runtime *rt)
{
if (NULL == rt)
return;
pthread_mutex_destroy(&rt->mutex);
zz_free(rt);
}
int
conn_check_max(Conn *conn)
{
int rconn = 0;
int i;
for (i = 0; i < g_cf->thread_num; i++) {
rconn += g_runtime->server->threads[i].conns;
}
rconn++; // add self
int allconn = rconn + g_runtime->wthread->conns + g_runtime->sthread->conns;
DINFO("check conn: %d, %d\n", allconn, g_cf->max_conn);
if (allconn > g_cf->max_conn) {
return MEMLINK_ERR_CONN_TOO_MANY;
}
if (conn->port == g_cf->read_port) {
DINFO("check read conn: %d, %d\n", rconn, g_cf->max_read_conn);
if (g_cf->max_read_conn > 0 && rconn > g_cf->max_read_conn) {
return MEMLINK_ERR_CONN_TOO_MANY;
}
}else if (conn->port == g_cf->write_port) {
DINFO("check write conn: %d, %d\n", g_runtime->wthread->conns, g_cf->max_write_conn);
if (g_cf->max_write_conn > 0 && g_runtime->wthread->conns > g_cf->max_write_conn) {
return MEMLINK_ERR_CONN_TOO_MANY;
}
}else if (conn->port == g_cf->sync_port) {
DINFO("check sync conn: %d, %d\n", g_runtime->sthread->conns, g_cf->max_write_conn);
if (g_cf->max_sync_conn > 0 && g_runtime->sthread->conns > g_cf->max_sync_conn) {
return MEMLINK_ERR_CONN_TOO_MANY;
}
}
return MEMLINK_OK;
}
int
mem_used_inc(long long size)
{
pthread_mutex_lock(&g_runtime->mutex_mem);
g_runtime->mem_used += size;
pthread_mutex_unlock(&g_runtime->mutex_mem);
return 0;
}
int
mem_used_dec(long long size)
{
pthread_mutex_lock(&g_runtime->mutex_mem);
g_runtime->mem_used -= size;
pthread_mutex_unlock(&g_runtime->mutex_mem);
return 0;
}