-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjeap.c
780 lines (670 loc) · 16.2 KB
/
jeap.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#define _BSD_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
#ifdef __gnu_linux__
#include <jemalloc/jemalloc.h>
#endif
#ifdef __FREEBSD__
#include <malloc_np.h>
#endif
#define NWORKER 0x4
#ifdef DEBUG
volatile int log_level = 2;
#define debug(...) do { if (log_level <= 0) fprintf(stderr, __VA_ARGS__); } while (0)
#define info(...) do { if (log_level <= 1) fprintf(stderr, __VA_ARGS__); } while (0)
#define warning(...) do { if (log_level <= 2) fprintf(stderr, __VA_ARGS__); } while (0)
#else
#define debug(...) do {} while (0)
#define info(...) do {} while (0)
#define warning(...) do {} while (0)
#endif
#define MAX_LENGTH 0x1000
#define HTABLE_SIZE 0x1000
typedef enum {
CMD_NONE = 0, // abstract command
CMD_SET,
CMD_ADD,
CMD_REPLACE,
CMD_APPEND,
CMD_PREPEND,
CMD_CAS,
CMD_STORE, // abstract command
CMD_GET,
CMD_GETS = CMD_GET,
CMD_DELETE,
CMD_INCR,
CMD_DECR,
CMD_STAT,
CMD_0OPS,
CMD_MAX = 0x10, // abstract command
} cmd_t;
typedef struct job_s {
int id;
int flags;
cmd_t cmd;
int noreply;
char *key;
char *value;
char *error;
size_t length;
time_t exptime;
uint64_t casunique;
int done;
pthread_cond_t cond;
pthread_mutex_t lock;
} job_t;
typedef struct item_s {
struct item_s *next;
struct item_s *prev;
char *key;
char *value;
size_t length;
time_t exptime;
int dead;
int flags;
// uint64_t casunique;
} item_t;
typedef struct entry_s {
struct item_s *next;
struct item_s *prev;
uint32_t hash;
pthread_mutex_t lock;
} entry_t;
typedef void (*handler_t)(job_t *);
handler_t handlers[CMD_MAX];
entry_t htable[HTABLE_SIZE];
pthread_cond_t cond;
pthread_mutex_t lock;
volatile int worker_all;
volatile int job_done;
volatile int job_all;
volatile job_t *cur_job;
volatile int stopped;
pthread_t workers[NWORKER];
#ifdef DEBUG
volatile int trap_cnt = 0;
void trap(int stop) {
// trap_cnt++;
if (stop)
__asm__ volatile ( "int $0x3" );
else {
trap_cnt++;
// log_level = 0;
}
}
#endif
void destroy_job(job_t *job) {
debug("destroy %p\n", job);
if (job->key) {
free(job->key);
job->key = NULL;
}
if (job->cmd < CMD_STORE && job->value) {
free(job->value);
job->value = NULL;
job->length = 0;
}
free(job);
}
job_t *get_job(int wid) {
job_t *ret = NULL;
int waiting = 1;
while (waiting == 1) {
pthread_mutex_lock(&lock);
if (stopped) {
pthread_mutex_unlock(&lock);
break;
}
worker_all++; // add a worker to list
debug("worker #%d waiting\n", wid);
pthread_cond_wait(&cond, &lock);
if (cur_job){
ret = *(job_t **)&cur_job;
cur_job = NULL; // get a new job
waiting = 0;
debug("worker #%d get a new job\n", wid);
} else if (stopped) {
waiting = -1;
info("worker #%d retired\n", wid);
}
worker_all--;
pthread_mutex_unlock(&lock);
}
return ret;
}
void put_job(job_t *job) {
int waiting;
while (1) {
pthread_mutex_lock(&lock);
if (worker_all > 0 && cur_job == NULL && job_all - job_done < NWORKER) {
cur_job = job;
job->id = ++job_all;
pthread_mutex_init(&job->lock, NULL);
pthread_cond_init(&job->cond, NULL);
waiting = 0;
info("put a new job #%d (%p)\n", job->id, job);
// pthread_cond_signal(&cond);
pthread_cond_broadcast(&cond);
} else {
waiting = 1;
// debug("pending job (%p)\n", job);
}
pthread_mutex_unlock(&lock);
// sched_yield();
if (waiting) {
// sleep(1);
// sched_yield();
} else {
break;
}
}
}
void done_job(job_t *job) {
pthread_mutex_lock(&job->lock);
job->done = 1;
pthread_cond_signal(&job->cond);
pthread_mutex_unlock(&job->lock);
sched_yield();
pthread_mutex_lock(&lock);
job_done++;
pthread_mutex_unlock(&lock);
}
void worker(int wid) {
job_t *job = NULL;
while ((job = get_job(wid)) != NULL) {
handler_t h = handlers[job->cmd];
info("worker #%d get job #%d (cmd = %d)\n", wid, job->id, job->cmd);
if (h == NULL) {
job->error = "NOT_IMPLEMENT";
} else {
h(job);
}
done_job(job);
if (job->noreply) {
#ifdef DEBUG
if (job->error && 0)
warning("%s\n", job->error);
#endif
destroy_job(job);
}
}
}
int recvn(int fd, char *buf, int n) {
int i;
for (i = 0; i < n; ) {
int r = read(fd, &buf[i], n - i);
if (r == -1) {
break;
}
i += r;
}
return i;
}
int recvline(int fd, char *buf, int n) {
int i;
for (i = 0; i < n; i++) {
int r = read(fd, &buf[i], 1);
if (r == -1) {
break;
} else if (r == 1 && (buf[i] == '\n'))
break;
}
buf[i] = '\0';
if (i > 1 && buf[i - 1] == '\r')
buf[--i] = '\0';
return i;
}
void sendn(int fd, char *buf, int n) {
write(fd, buf, n);
write(fd, "\r\n", 2);
}
void sendline(int fd, char *buf) {
write(fd, buf, strlen(buf));
write(fd, "\r\n", 2);
}
uint32_t hash(char *s) {
uint32_t c = 0;
if (!s)
return 0;
for (uint8_t *a = (uint8_t *)s; *a; a++)
c = c * 31 + *a;
return c % HTABLE_SIZE;
}
// safe version
char *_strdup(const char *s) {
if (s == NULL)
return NULL;
return strdup(s);
}
void *_memcpy(void *dest, const void *src, size_t n) {
if (n == 0 || src == NULL)
return dest;
return memcpy(dest, src, n);
}
int _strcmp(const char *s1, const char *s2) {
if (s1 == NULL || s2 == NULL)
return s1 == s2 ? 0 : 1;
return strcmp(s1, s2);
}
void _free(void *ptr) {
if (ptr != NULL)
free(ptr);
}
void fatal(char *s) {
sendline(1, s);
exit(1);
}
void _unlink(item_t *i) {
item_t *p = i->prev;
item_t *n = i->next;
if (p->next != i || n->prev != i)
fatal("SERVER_ERROR corrupted item");
p->next = n;
n->prev = p;
_free(i);
}
item_t *lookup(entry_t *e, char *k) {
// must hold entry lock
item_t *ret = NULL;
item_t *next = NULL;
time_t t = time(NULL);
for (item_t *i = e->next; i != (item_t *)e; i = next) {
if (i->prev->next != i || i->next->prev != i) {
fatal("SERVER_ERROR corrupted item");
}
next = i->next;
if (i->exptime && i->exptime < t) {
// expired
_unlink(i);
continue;
}
if (!_strcmp(i->key, k)) {
ret = i;
break;
}
}
return ret;
}
char *insert(entry_t *e, item_t *i) {
// must hold entry lock
// linklist check
item_t *h = e->next;
if (h->prev != (item_t *)e)
fatal("SERVER_ERROR corrupted entry");
e->next = i;
h->prev = i;
i->next = h;
i->prev = (item_t *)e;
}
void handle_set(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL) { // create a new one
i = malloc(sizeof(item_t));
memset(i, 0, sizeof(item_t));
i->value = job->value;
job->value = NULL;
i->length = job->length;
i->exptime = job->exptime;
i->dead = 0;
i->flags = job->flags;
i->key = _strdup(job->key);
insert(&htable[h], i);
} else if (job->length == i->length) {
// shortcut
_memcpy(i->value, job->value, i->length);
i->exptime = job->exptime;
i->dead = 0;
i->flags = job->flags;
} else { // update one item
_free(i->value);
i->value = job->value;
job->value = NULL;
i->length = job->length;
i->exptime = job->exptime;
i->dead = 0;
i->flags = job->flags;
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_add(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL) {
i = malloc(sizeof(item_t));
memset(i, 0, sizeof(item_t));
i->value = job->value;
job->value = NULL;
i->length = job->length;
i->exptime = job->exptime;
i->dead = 0;
i->flags = job->flags;
i->key = _strdup(job->key);
insert(&htable[h], i);
} else {
job->error = "EXISTS";
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_replace(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL || i->dead) {
job->error = "NOT_FOUND";
} else {
_free(i->value);
i->value = job->value;
job->value = NULL;
i->length = job->length;
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_append(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL || i->dead) {
job->error = "NOT_FOUND";
} else if (job->length != 0) {
char *value = malloc(job->length + i->length);
if (i->value == NULL)
job->error = "NOT_STORED";
else {
_memcpy(value, i->value, i->length);
_memcpy(&value[i->length], job->value, job->length);
_free(i->value);
i->value = value;
i->length += job->length;
}
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_prepend(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL || i->dead) {
job->error = "NOT_FOUND";
} else if (job->length != 0) {
char *value = malloc(job->length + i->length);
if (i->value == NULL)
job->error = "NOT_STORED";
else {
_memcpy(value, job->value, job->length);
_memcpy(&value[job->length], i->value, i->length);
_free(i->value);
i->value = value;
i->length += job->length;
}
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_get(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL || i->dead) {
job->error = "NOT_FOUND";
} else {
job->value = i->value;
job->length = i->length;
job->flags = i->flags;
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_delete(job_t *job) {
uint32_t h = hash(job->key);
item_t *i;
pthread_mutex_lock(&htable[h].lock);
i = lookup(&htable[h], job->key);
if (i == NULL || (i->dead && job->exptime > i->exptime)) {
job->error = "NOT_FOUND";
} else {
i->dead = 1;
i->exptime = job->exptime;
}
pthread_mutex_unlock(&htable[h].lock);
}
void handle_stat(job_t *job) {
job->error = "jeapcached v0.095";
}
void handle_0ops(job_t *job) {
#ifdef DEBUG
printf("malloc(%lu) = %p\r\n", sizeof(item_t), malloc(sizeof(item_t)));
#else
job->error = "NO_BACKDOOR";
#endif
}
void init() {
long i;
debug("init\n");
handlers[CMD_SET] = handle_set;
handlers[CMD_ADD] = handle_add;
handlers[CMD_REPLACE] = handle_replace;
handlers[CMD_APPEND] = handle_append;
handlers[CMD_PREPEND] = handle_prepend;
handlers[CMD_GET] = handle_get;
handlers[CMD_DELETE] = handle_delete;
handlers[CMD_STAT] = handle_stat;
handlers[CMD_0OPS] = handle_0ops;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&lock, NULL);
stopped = 0;
cur_job = NULL;
job_done = 0;
job_all = 0;
worker_all = 0;
for (i = 0; i < NWORKER; i++) {
pthread_create(&workers[i], NULL, (void *)worker, (void *)i);
}
memset(htable, 0, sizeof(htable));
for (i = 0; i < HTABLE_SIZE; i++) {
htable[i].prev = htable[i].next = (item_t *)&htable[i];
pthread_mutex_init(&htable[i].lock, NULL);
}
}
void fini() {
int i;
debug("fini\n");
for (i = 0; i < NWORKER; i++)
pthread_join(workers[i], NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&lock);
// TODO destroy more locks
}
void stop() {
int waiting = 1;
debug("stop\n");
do {
pthread_mutex_lock(&lock);
if (worker_all) {
stopped = 1;
pthread_cond_broadcast(&cond);
} else {
waiting = 0;
}
pthread_mutex_unlock(&lock);
} while (waiting);
// hide error messages of main thread :)
exit(0);
}
void test_job() {
int i = 0;
getchar();
for (i = 0; i < 10; i++) {
job_t *job = malloc(sizeof(job_t));
job->cmd = i * i;
put_job(job);
}
}
job_t *parse_args(char *buf) {
char command[0x10];
char key[0x100];
uint64_t args[5];
int scanned;
job_t *job = (job_t *)malloc(sizeof(job_t));
memset(job, 0, sizeof(job_t));
if (sscanf(buf, "%10s %250s%n", command, key, &scanned) == 2) {
job->key = _strdup(key);
if (!strcasecmp(command, "get") || !strcasecmp(command, "gets")) {
job->cmd = CMD_GET;
} else if (!strcasecmp(command, "delete")) {
job->cmd = CMD_DELETE;
int argc = sscanf(&buf[scanned], "%ld %d", &job->exptime, &job->noreply);
if (job->exptime == 0)
job->exptime = 1;
else
job->exptime += time(NULL);
} else if (!strcasecmp(command, "stat")) {
job->cmd = CMD_STAT;
} else if (!strcasecmp(command, "incr") || !strcasecmp(command, "decr")) {
int argc = sscanf(&buf[scanned], "%ld %d", (uint64_t *)&job->value, &job->noreply);
if (command[0] == 'i' || command[0] == 'I')
job->cmd = CMD_INCR;
else
job->cmd = CMD_DECR;
} else if (!strcasecmp(command, "set")
|| !strcasecmp(command, "add")
|| !strcasecmp(command, "replace")
|| !strcasecmp(command, "append")
|| !strcasecmp(command, "prepend")
|| !strcasecmp(command, "cas")) {
int argc = sscanf(&buf[scanned], "%ld %ld %ld %ld %ld", &args[0], &args[1], &args[2], &args[3], &args[4]);
switch (command[0]) {
case 's':
case 'S':
job->cmd = CMD_SET;
break;
case 'r':
case 'R':
job->cmd = CMD_REPLACE;
break;
case 'p':
case 'P':
job->cmd = CMD_PREPEND;
break;
case 'c':
case 'C':
job->cmd = CMD_CAS;
break;
default:
if (command[1] == 'd' || command[1] == 'D')
job->cmd = CMD_ADD;
else
job->cmd = CMD_APPEND;
}
if (argc == 1) {
job->length = args[0];
} else if (argc == 2) {
job->flags = args[0];
job->length = args[1];
} else if (argc >= 3) {
job->flags = args[0];
job->exptime = args[1];
job->length = args[2];
if (argc == 4)
job->noreply = args[3];
else { // argc == 5
job->casunique = args[3];
job->noreply = args[4];
}
} else { // argc == 0
job->error = "NOT_STORED";
}
} else if (!strcasecmp(command, "0ops")) {
job->cmd = CMD_0OPS;
} else {
job->error = "ERROR";
}
// sanity checks
if (!job->cmd) {
job->error = "ERROR";
} else if(job->length > MAX_LENGTH) {
job->error = "NOT_STORED";
}
} else {
job->error = "ERROR";
}
return job;
}
void handler() {
char buf[0x200];
job_t *job;
while (recvline(0, buf, 0x100)) {
job = parse_args(buf);
if (!job->error) {
debug("got a job: cmd = %d\n", job->cmd);
// read length for a store command
if (job->cmd < CMD_STORE) {
if ((job->value = malloc(job->length)) == NULL) {
job->error = "NOT_STORED";
goto error;
}
debug("reading %ld bytes => %p\n", job->length, job->value);
recvn(0, job->value, job->length);
}
if (handlers[job->cmd] == NULL) {
job->error = "NOT_IMPLEMENT";
goto error;
}
if (job->key == NULL) {
job->error = "NOT_STORED";
goto error;
}
put_job(job);
// wait until the job is done
if (!job->noreply) {
pthread_mutex_lock(&job->lock);
if (!job->done)
pthread_cond_wait(&job->cond, &job->lock);
pthread_mutex_unlock(&job->lock);
}
if (!job->noreply && !job->error) {
// handle some normal result
if (job->cmd < CMD_STORE)
sendline(1, "STORED");
else if (job->cmd == CMD_GET) {
snprintf(buf, sizeof(buf), "VALUE %s %u %lu", job->key, job->flags, job->length);
sendline(1, buf);
sendn(1, job->value, job->length);
} else if (job->cmd == CMD_DELETE) {
sendline(1, "DELETED");
}
}
}
error:
if (!job->noreply) {
if (job->error)
sendline(1, job->error);
destroy_job(job);
}
}
}
int main(int argc, char* argv[]) {
#ifdef DEBUG
if (getenv("NODEBUG"))
fclose(stderr);
#endif
if (argc > 1) {
alarm(atoi(argv[1]));
}
init();
handler();
stop();
fini();
return 0;
}