forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prot.c
2048 lines (1738 loc) · 52.4 KB
/
prot.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <stdarg.h>
#include "dat.h"
/* job body cannot be greater than this many bytes long */
size_t job_data_size_limit = JOB_DATA_SIZE_LIMIT_DEFAULT;
#define NAME_CHARS \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefghijklmnopqrstuvwxyz" \
"0123456789-+/;.$_()"
#define CMD_PUT "put "
#define CMD_PEEKJOB "peek "
#define CMD_PEEK_READY "peek-ready"
#define CMD_PEEK_DELAYED "peek-delayed"
#define CMD_PEEK_BURIED "peek-buried"
#define CMD_RESERVE "reserve"
#define CMD_RESERVE_TIMEOUT "reserve-with-timeout "
#define CMD_DELETE "delete "
#define CMD_RELEASE "release "
#define CMD_BURY "bury "
#define CMD_KICK "kick "
#define CMD_JOBKICK "kick-job "
#define CMD_TOUCH "touch "
#define CMD_STATS "stats"
#define CMD_JOBSTATS "stats-job "
#define CMD_USE "use "
#define CMD_WATCH "watch "
#define CMD_IGNORE "ignore "
#define CMD_LIST_TUBES "list-tubes"
#define CMD_LIST_TUBE_USED "list-tube-used"
#define CMD_LIST_TUBES_WATCHED "list-tubes-watched"
#define CMD_STATS_TUBE "stats-tube "
#define CMD_QUIT "quit"
#define CMD_PAUSE_TUBE "pause-tube"
#define CONSTSTRLEN(m) (sizeof(m) - 1)
#define CMD_PEEK_READY_LEN CONSTSTRLEN(CMD_PEEK_READY)
#define CMD_PEEK_DELAYED_LEN CONSTSTRLEN(CMD_PEEK_DELAYED)
#define CMD_PEEK_BURIED_LEN CONSTSTRLEN(CMD_PEEK_BURIED)
#define CMD_PEEKJOB_LEN CONSTSTRLEN(CMD_PEEKJOB)
#define CMD_RESERVE_LEN CONSTSTRLEN(CMD_RESERVE)
#define CMD_RESERVE_TIMEOUT_LEN CONSTSTRLEN(CMD_RESERVE_TIMEOUT)
#define CMD_DELETE_LEN CONSTSTRLEN(CMD_DELETE)
#define CMD_RELEASE_LEN CONSTSTRLEN(CMD_RELEASE)
#define CMD_BURY_LEN CONSTSTRLEN(CMD_BURY)
#define CMD_KICK_LEN CONSTSTRLEN(CMD_KICK)
#define CMD_JOBKICK_LEN CONSTSTRLEN(CMD_JOBKICK)
#define CMD_TOUCH_LEN CONSTSTRLEN(CMD_TOUCH)
#define CMD_STATS_LEN CONSTSTRLEN(CMD_STATS)
#define CMD_JOBSTATS_LEN CONSTSTRLEN(CMD_JOBSTATS)
#define CMD_USE_LEN CONSTSTRLEN(CMD_USE)
#define CMD_WATCH_LEN CONSTSTRLEN(CMD_WATCH)
#define CMD_IGNORE_LEN CONSTSTRLEN(CMD_IGNORE)
#define CMD_LIST_TUBES_LEN CONSTSTRLEN(CMD_LIST_TUBES)
#define CMD_LIST_TUBE_USED_LEN CONSTSTRLEN(CMD_LIST_TUBE_USED)
#define CMD_LIST_TUBES_WATCHED_LEN CONSTSTRLEN(CMD_LIST_TUBES_WATCHED)
#define CMD_STATS_TUBE_LEN CONSTSTRLEN(CMD_STATS_TUBE)
#define CMD_PAUSE_TUBE_LEN CONSTSTRLEN(CMD_PAUSE_TUBE)
#define MSG_FOUND "FOUND"
#define MSG_NOTFOUND "NOT_FOUND\r\n"
#define MSG_RESERVED "RESERVED"
#define MSG_DEADLINE_SOON "DEADLINE_SOON\r\n"
#define MSG_TIMED_OUT "TIMED_OUT\r\n"
#define MSG_DELETED "DELETED\r\n"
#define MSG_RELEASED "RELEASED\r\n"
#define MSG_BURIED "BURIED\r\n"
#define MSG_KICKED "KICKED\r\n"
#define MSG_TOUCHED "TOUCHED\r\n"
#define MSG_BURIED_FMT "BURIED %"PRIu64"\r\n"
#define MSG_INSERTED_FMT "INSERTED %"PRIu64"\r\n"
#define MSG_NOT_IGNORED "NOT_IGNORED\r\n"
#define MSG_NOTFOUND_LEN CONSTSTRLEN(MSG_NOTFOUND)
#define MSG_DELETED_LEN CONSTSTRLEN(MSG_DELETED)
#define MSG_TOUCHED_LEN CONSTSTRLEN(MSG_TOUCHED)
#define MSG_RELEASED_LEN CONSTSTRLEN(MSG_RELEASED)
#define MSG_BURIED_LEN CONSTSTRLEN(MSG_BURIED)
#define MSG_KICKED_LEN CONSTSTRLEN(MSG_KICKED)
#define MSG_NOT_IGNORED_LEN CONSTSTRLEN(MSG_NOT_IGNORED)
#define MSG_OUT_OF_MEMORY "OUT_OF_MEMORY\r\n"
#define MSG_INTERNAL_ERROR "INTERNAL_ERROR\r\n"
#define MSG_DRAINING "DRAINING\r\n"
#define MSG_BAD_FORMAT "BAD_FORMAT\r\n"
#define MSG_UNKNOWN_COMMAND "UNKNOWN_COMMAND\r\n"
#define MSG_EXPECTED_CRLF "EXPECTED_CRLF\r\n"
#define MSG_JOB_TOO_BIG "JOB_TOO_BIG\r\n"
#define STATE_WANTCOMMAND 0
#define STATE_WANTDATA 1
#define STATE_SENDJOB 2
#define STATE_SENDWORD 3
#define STATE_WAIT 4
#define STATE_BITBUCKET 5
#define STATE_CLOSE 6
#define OP_UNKNOWN 0
#define OP_PUT 1
#define OP_PEEKJOB 2
#define OP_RESERVE 3
#define OP_DELETE 4
#define OP_RELEASE 5
#define OP_BURY 6
#define OP_KICK 7
#define OP_STATS 8
#define OP_JOBSTATS 9
#define OP_PEEK_BURIED 10
#define OP_USE 11
#define OP_WATCH 12
#define OP_IGNORE 13
#define OP_LIST_TUBES 14
#define OP_LIST_TUBE_USED 15
#define OP_LIST_TUBES_WATCHED 16
#define OP_STATS_TUBE 17
#define OP_PEEK_READY 18
#define OP_PEEK_DELAYED 19
#define OP_RESERVE_TIMEOUT 20
#define OP_TOUCH 21
#define OP_QUIT 22
#define OP_PAUSE_TUBE 23
#define OP_JOBKICK 24
#define TOTAL_OPS 25
#define STATS_FMT "---\n" \
"current-jobs-urgent: %u\n" \
"current-jobs-ready: %u\n" \
"current-jobs-reserved: %u\n" \
"current-jobs-delayed: %u\n" \
"current-jobs-buried: %u\n" \
"cmd-put: %" PRIu64 "\n" \
"cmd-peek: %" PRIu64 "\n" \
"cmd-peek-ready: %" PRIu64 "\n" \
"cmd-peek-delayed: %" PRIu64 "\n" \
"cmd-peek-buried: %" PRIu64 "\n" \
"cmd-reserve: %" PRIu64 "\n" \
"cmd-reserve-with-timeout: %" PRIu64 "\n" \
"cmd-delete: %" PRIu64 "\n" \
"cmd-release: %" PRIu64 "\n" \
"cmd-use: %" PRIu64 "\n" \
"cmd-watch: %" PRIu64 "\n" \
"cmd-ignore: %" PRIu64 "\n" \
"cmd-bury: %" PRIu64 "\n" \
"cmd-kick: %" PRIu64 "\n" \
"cmd-touch: %" PRIu64 "\n" \
"cmd-stats: %" PRIu64 "\n" \
"cmd-stats-job: %" PRIu64 "\n" \
"cmd-stats-tube: %" PRIu64 "\n" \
"cmd-list-tubes: %" PRIu64 "\n" \
"cmd-list-tube-used: %" PRIu64 "\n" \
"cmd-list-tubes-watched: %" PRIu64 "\n" \
"cmd-pause-tube: %" PRIu64 "\n" \
"job-timeouts: %" PRIu64 "\n" \
"total-jobs: %" PRIu64 "\n" \
"max-job-size: %zu\n" \
"current-tubes: %zu\n" \
"current-connections: %u\n" \
"current-producers: %u\n" \
"current-workers: %u\n" \
"current-waiting: %u\n" \
"total-connections: %u\n" \
"pid: %ld\n" \
"version: %s\n" \
"rusage-utime: %d.%06d\n" \
"rusage-stime: %d.%06d\n" \
"uptime: %u\n" \
"binlog-oldest-index: %d\n" \
"binlog-current-index: %d\n" \
"binlog-records-migrated: %" PRId64 "\n" \
"binlog-records-written: %" PRId64 "\n" \
"binlog-max-size: %d\n" \
"id: %s\n" \
"hostname: %s\n" \
"\r\n"
#define STATS_TUBE_FMT "---\n" \
"name: %s\n" \
"current-jobs-urgent: %u\n" \
"current-jobs-ready: %u\n" \
"current-jobs-reserved: %u\n" \
"current-jobs-delayed: %u\n" \
"current-jobs-buried: %u\n" \
"total-jobs: %" PRIu64 "\n" \
"current-using: %u\n" \
"current-watching: %u\n" \
"current-waiting: %u\n" \
"cmd-delete: %" PRIu64 "\n" \
"cmd-pause-tube: %u\n" \
"pause: %" PRIu64 "\n" \
"pause-time-left: %" PRId64 "\n" \
"\r\n"
#define STATS_JOB_FMT "---\n" \
"id: %" PRIu64 "\n" \
"tube: %s\n" \
"state: %s\n" \
"pri: %u\n" \
"age: %" PRId64 "\n" \
"delay: %" PRId64 "\n" \
"ttr: %" PRId64 "\n" \
"time-left: %" PRId64 "\n" \
"file: %d\n" \
"reserves: %u\n" \
"timeouts: %u\n" \
"releases: %u\n" \
"buries: %u\n" \
"kicks: %u\n" \
"\r\n"
/* this number is pretty arbitrary */
#define BUCKET_BUF_SIZE 1024
static char bucket[BUCKET_BUF_SIZE];
static uint ready_ct = 0;
static struct stats global_stat = {0, 0, 0, 0, 0};
static tube default_tube;
static int drain_mode = 0;
static int64 started_at;
enum {
NumIdBytes = 8
};
static char id[NumIdBytes * 2 + 1]; // hex-encoded len of NumIdBytes
static struct utsname node_info;
static uint64 op_ct[TOTAL_OPS], timeout_ct = 0;
static Conn *dirty;
static const char * op_names[] = {
"<unknown>",
CMD_PUT,
CMD_PEEKJOB,
CMD_RESERVE,
CMD_DELETE,
CMD_RELEASE,
CMD_BURY,
CMD_KICK,
CMD_STATS,
CMD_JOBSTATS,
CMD_PEEK_BURIED,
CMD_USE,
CMD_WATCH,
CMD_IGNORE,
CMD_LIST_TUBES,
CMD_LIST_TUBE_USED,
CMD_LIST_TUBES_WATCHED,
CMD_STATS_TUBE,
CMD_PEEK_READY,
CMD_PEEK_DELAYED,
CMD_RESERVE_TIMEOUT,
CMD_TOUCH,
CMD_QUIT,
CMD_PAUSE_TUBE,
CMD_JOBKICK,
};
static job remove_buried_job(job j);
static int
buried_job_p(tube t)
{
return job_list_any_p(&t->buried);
}
static void
reply(Conn *c, char *line, int len, int state)
{
if (!c) return;
connwant(c, 'w');
c->next = dirty;
dirty = c;
c->reply = line;
c->reply_len = len;
c->reply_sent = 0;
c->state = state;
if (verbose >= 2) {
printf(">%d reply %.*s\n", c->sock.fd, len-2, line);
}
}
static void
protrmdirty(Conn *c)
{
Conn *x, *newdirty = NULL;
while (dirty) {
x = dirty;
dirty = dirty->next;
x->next = NULL;
if (x != c) {
x->next = newdirty;
newdirty = x;
}
}
dirty = newdirty;
}
#define reply_msg(c,m) reply((c),(m),CONSTSTRLEN(m),STATE_SENDWORD)
#define reply_serr(c,e) (twarnx("server error: %s",(e)),\
reply_msg((c),(e)))
static void
reply_line(Conn*, int, const char*, ...)
__attribute__((format(printf, 3, 4)));
static void
reply_line(Conn *c, int state, const char *fmt, ...)
{
int r;
va_list ap;
va_start(ap, fmt);
r = vsnprintf(c->reply_buf, LINE_BUF_SIZE, fmt, ap);
va_end(ap);
/* Make sure the buffer was big enough. If not, we have a bug. */
if (r >= LINE_BUF_SIZE) return reply_serr(c, MSG_INTERNAL_ERROR);
return reply(c, c->reply_buf, r, state);
}
static void
reply_job(Conn *c, job j, const char *word)
{
/* tell this connection which job to send */
c->out_job = j;
c->out_job_sent = 0;
return reply_line(c, STATE_SENDJOB, "%s %"PRIu64" %u\r\n",
word, j->r.id, j->r.body_size - 2);
}
Conn *
remove_waiting_conn(Conn *c)
{
tube t;
size_t i;
if (!conn_waiting(c)) return NULL;
c->type &= ~CONN_TYPE_WAITING;
global_stat.waiting_ct--;
for (i = 0; i < c->watch.used; i++) {
t = c->watch.items[i];
t->stat.waiting_ct--;
ms_remove(&t->waiting, c);
}
return c;
}
static void
reserve_job(Conn *c, job j)
{
j->r.deadline_at = nanoseconds() + j->r.ttr;
global_stat.reserved_ct++; /* stats */
j->tube->stat.reserved_ct++;
j->r.reserve_ct++;
j->r.state = Reserved;
job_insert(&c->reserved_jobs, j);
j->reserver = c;
c->pending_timeout = -1;
if (c->soonest_job && j->r.deadline_at < c->soonest_job->r.deadline_at) {
c->soonest_job = j;
}
return reply_job(c, j, MSG_RESERVED);
}
static job
next_eligible_job(int64 now)
{
tube t;
size_t i;
job j = NULL, candidate;
for (i = 0; i < tubes.used; i++) {
t = tubes.items[i];
if (t->pause) {
if (t->deadline_at > now) continue;
t->pause = 0;
}
if (t->waiting.used && t->ready.len) {
candidate = t->ready.data[0];
if (!j || job_pri_less(candidate, j)) {
j = candidate;
}
}
}
return j;
}
static void
process_queue()
{
job j;
int64 now = nanoseconds();
while ((j = next_eligible_job(now))) {
heapremove(&j->tube->ready, j->heap_index);
ready_ct--;
if (j->r.pri < URGENT_THRESHOLD) {
global_stat.urgent_ct--;
j->tube->stat.urgent_ct--;
}
reserve_job(remove_waiting_conn(ms_take(&j->tube->waiting)), j);
}
}
static job
delay_q_peek()
{
int i;
tube t;
job j = NULL, nj;
for (i = 0; i < tubes.used; i++) {
t = tubes.items[i];
if (t->delay.len == 0) {
continue;
}
nj = t->delay.data[0];
if (!j || nj->r.deadline_at < j->r.deadline_at) j = nj;
}
return j;
}
static int
enqueue_job(Server *s, job j, int64 delay, char update_store)
{
int r;
j->reserver = NULL;
if (delay) {
j->r.deadline_at = nanoseconds() + delay;
r = heapinsert(&j->tube->delay, j);
if (!r) return 0;
j->r.state = Delayed;
} else {
r = heapinsert(&j->tube->ready, j);
if (!r) return 0;
j->r.state = Ready;
ready_ct++;
if (j->r.pri < URGENT_THRESHOLD) {
global_stat.urgent_ct++;
j->tube->stat.urgent_ct++;
}
}
if (update_store) {
if (!walwrite(&s->wal, j)) {
return 0;
}
walmaint(&s->wal);
}
process_queue();
return 1;
}
static int
bury_job(Server *s, job j, char update_store)
{
int z;
if (update_store) {
z = walresvupdate(&s->wal, j);
if (!z) return 0;
j->walresv += z;
}
job_insert(&j->tube->buried, j);
global_stat.buried_ct++;
j->tube->stat.buried_ct++;
j->r.state = Buried;
j->reserver = NULL;
j->r.bury_ct++;
if (update_store) {
if (!walwrite(&s->wal, j)) {
return 0;
}
walmaint(&s->wal);
}
return 1;
}
void
enqueue_reserved_jobs(Conn *c)
{
int r;
job j;
while (job_list_any_p(&c->reserved_jobs)) {
j = job_remove(c->reserved_jobs.next);
r = enqueue_job(c->srv, j, 0, 0);
if (r < 1) bury_job(c->srv, j, 0);
global_stat.reserved_ct--;
j->tube->stat.reserved_ct--;
c->soonest_job = NULL;
}
}
static job
delay_q_take()
{
job j = delay_q_peek();
if (!j) {
return 0;
}
heapremove(&j->tube->delay, j->heap_index);
return j;
}
static int
kick_buried_job(Server *s, job j)
{
int r;
int z;
z = walresvupdate(&s->wal, j);
if (!z) return 0;
j->walresv += z;
remove_buried_job(j);
j->r.kick_ct++;
r = enqueue_job(s, j, 0, 1);
if (r == 1) return 1;
/* ready queue is full, so bury it */
bury_job(s, j, 0);
return 0;
}
static uint
get_delayed_job_ct()
{
tube t;
size_t i;
uint count = 0;
for (i = 0; i < tubes.used; i++) {
t = tubes.items[i];
count += t->delay.len;
}
return count;
}
static int
kick_delayed_job(Server *s, job j)
{
int r;
int z;
z = walresvupdate(&s->wal, j);
if (!z) return 0;
j->walresv += z;
heapremove(&j->tube->delay, j->heap_index);
j->r.kick_ct++;
r = enqueue_job(s, j, 0, 1);
if (r == 1) return 1;
/* ready queue is full, so delay it again */
r = enqueue_job(s, j, j->r.delay, 0);
if (r == 1) return 0;
/* last resort */
bury_job(s, j, 0);
return 0;
}
/* return the number of jobs successfully kicked */
static uint
kick_buried_jobs(Server *s, tube t, uint n)
{
uint i;
for (i = 0; (i < n) && buried_job_p(t); ++i) {
kick_buried_job(s, t->buried.next);
}
return i;
}
/* return the number of jobs successfully kicked */
static uint
kick_delayed_jobs(Server *s, tube t, uint n)
{
uint i;
for (i = 0; (i < n) && (t->delay.len > 0); ++i) {
kick_delayed_job(s, (job)t->delay.data[0]);
}
return i;
}
static uint
kick_jobs(Server *s, tube t, uint n)
{
if (buried_job_p(t)) return kick_buried_jobs(s, t, n);
return kick_delayed_jobs(s, t, n);
}
static job
remove_buried_job(job j)
{
if (!j || j->r.state != Buried) return NULL;
j = job_remove(j);
if (j) {
global_stat.buried_ct--;
j->tube->stat.buried_ct--;
}
return j;
}
static job
remove_delayed_job(job j)
{
if (!j || j->r.state != Delayed) return NULL;
heapremove(&j->tube->delay, j->heap_index);
return j;
}
static job
remove_ready_job(job j)
{
if (!j || j->r.state != Ready) return NULL;
heapremove(&j->tube->ready, j->heap_index);
ready_ct--;
if (j->r.pri < URGENT_THRESHOLD) {
global_stat.urgent_ct--;
j->tube->stat.urgent_ct--;
}
return j;
}
static void
enqueue_waiting_conn(Conn *c)
{
tube t;
size_t i;
global_stat.waiting_ct++;
c->type |= CONN_TYPE_WAITING;
for (i = 0; i < c->watch.used; i++) {
t = c->watch.items[i];
t->stat.waiting_ct++;
ms_append(&t->waiting, c);
}
}
static job
find_reserved_job_in_conn(Conn *c, job j)
{
return (j && j->reserver == c && j->r.state == Reserved) ? j : NULL;
}
static job
touch_job(Conn *c, job j)
{
j = find_reserved_job_in_conn(c, j);
if (j) {
j->r.deadline_at = nanoseconds() + j->r.ttr;
c->soonest_job = NULL;
}
return j;
}
static job
peek_job(uint64 id)
{
return job_find(id);
}
static void
check_err(Conn *c, const char *s)
{
if (errno == EAGAIN) return;
if (errno == EINTR) return;
if (errno == EWOULDBLOCK) return;
twarn("%s", s);
c->state = STATE_CLOSE;
return;
}
/* Scan the given string for the sequence "\r\n" and return the line length.
* Always returns at least 2 if a match is found. Returns 0 if no match. */
static int
scan_line_end(const char *s, int size)
{
char *match;
match = memchr(s, '\r', size - 1);
if (!match) return 0;
/* this is safe because we only scan size - 1 chars above */
if (match[1] == '\n') return match - s + 2;
return 0;
}
static int
cmd_len(Conn *c)
{
return scan_line_end(c->cmd, c->cmd_read);
}
/* parse the command line */
static int
which_cmd(Conn *c)
{
#define TEST_CMD(s,c,o) if (strncmp((s), (c), CONSTSTRLEN(c)) == 0) return (o);
TEST_CMD(c->cmd, CMD_PUT, OP_PUT);
TEST_CMD(c->cmd, CMD_PEEKJOB, OP_PEEKJOB);
TEST_CMD(c->cmd, CMD_PEEK_READY, OP_PEEK_READY);
TEST_CMD(c->cmd, CMD_PEEK_DELAYED, OP_PEEK_DELAYED);
TEST_CMD(c->cmd, CMD_PEEK_BURIED, OP_PEEK_BURIED);
TEST_CMD(c->cmd, CMD_RESERVE_TIMEOUT, OP_RESERVE_TIMEOUT);
TEST_CMD(c->cmd, CMD_RESERVE, OP_RESERVE);
TEST_CMD(c->cmd, CMD_DELETE, OP_DELETE);
TEST_CMD(c->cmd, CMD_RELEASE, OP_RELEASE);
TEST_CMD(c->cmd, CMD_BURY, OP_BURY);
TEST_CMD(c->cmd, CMD_KICK, OP_KICK);
TEST_CMD(c->cmd, CMD_JOBKICK, OP_JOBKICK);
TEST_CMD(c->cmd, CMD_TOUCH, OP_TOUCH);
TEST_CMD(c->cmd, CMD_JOBSTATS, OP_JOBSTATS);
TEST_CMD(c->cmd, CMD_STATS_TUBE, OP_STATS_TUBE);
TEST_CMD(c->cmd, CMD_STATS, OP_STATS);
TEST_CMD(c->cmd, CMD_USE, OP_USE);
TEST_CMD(c->cmd, CMD_WATCH, OP_WATCH);
TEST_CMD(c->cmd, CMD_IGNORE, OP_IGNORE);
TEST_CMD(c->cmd, CMD_LIST_TUBES_WATCHED, OP_LIST_TUBES_WATCHED);
TEST_CMD(c->cmd, CMD_LIST_TUBE_USED, OP_LIST_TUBE_USED);
TEST_CMD(c->cmd, CMD_LIST_TUBES, OP_LIST_TUBES);
TEST_CMD(c->cmd, CMD_QUIT, OP_QUIT);
TEST_CMD(c->cmd, CMD_PAUSE_TUBE, OP_PAUSE_TUBE);
return OP_UNKNOWN;
}
/* Copy up to body_size trailing bytes into the job, then the rest into the cmd
* buffer. If c->in_job exists, this assumes that c->in_job->body is empty.
* This function is idempotent(). */
static void
fill_extra_data(Conn *c)
{
int extra_bytes, job_data_bytes = 0, cmd_bytes;
if (!c->sock.fd) return; /* the connection was closed */
if (!c->cmd_len) return; /* we don't have a complete command */
/* how many extra bytes did we read? */
extra_bytes = c->cmd_read - c->cmd_len;
/* how many bytes should we put into the job body? */
if (c->in_job) {
job_data_bytes = min(extra_bytes, c->in_job->r.body_size);
memcpy(c->in_job->body, c->cmd + c->cmd_len, job_data_bytes);
c->in_job_read = job_data_bytes;
} else if (c->in_job_read) {
/* we are in bit-bucket mode, throwing away data */
job_data_bytes = min(extra_bytes, c->in_job_read);
c->in_job_read -= job_data_bytes;
}
/* how many bytes are left to go into the future cmd? */
cmd_bytes = extra_bytes - job_data_bytes;
memmove(c->cmd, c->cmd + c->cmd_len + job_data_bytes, cmd_bytes);
c->cmd_read = cmd_bytes;
c->cmd_len = 0; /* we no longer know the length of the new command */
}
static void
_skip(Conn *c, int n, char *line, int len)
{
/* Invert the meaning of in_job_read while throwing away data -- it
* counts the bytes that remain to be thrown away. */
c->in_job = 0;
c->in_job_read = n;
fill_extra_data(c);
if (c->in_job_read == 0) return reply(c, line, len, STATE_SENDWORD);
c->reply = line;
c->reply_len = len;
c->reply_sent = 0;
c->state = STATE_BITBUCKET;
return;
}
#define skip(c,n,m) (_skip(c,n,m,CONSTSTRLEN(m)))
static void
enqueue_incoming_job(Conn *c)
{
int r;
job j = c->in_job;
c->in_job = NULL; /* the connection no longer owns this job */
c->in_job_read = 0;
/* check if the trailer is present and correct */
if (memcmp(j->body + j->r.body_size - 2, "\r\n", 2)) {
job_free(j);
return reply_msg(c, MSG_EXPECTED_CRLF);
}
if (verbose >= 2) {
printf("<%d job %"PRIu64"\n", c->sock.fd, j->r.id);
}
if (drain_mode) {
job_free(j);
return reply_serr(c, MSG_DRAINING);
}
if (j->walresv) return reply_serr(c, MSG_INTERNAL_ERROR);
j->walresv = walresvput(&c->srv->wal, j);
if (!j->walresv) return reply_serr(c, MSG_OUT_OF_MEMORY);
/* we have a complete job, so let's stick it in the pqueue */
r = enqueue_job(c->srv, j, j->r.delay, 1);
if (r < 0) return reply_serr(c, MSG_INTERNAL_ERROR);
global_stat.total_jobs_ct++;
j->tube->stat.total_jobs_ct++;
if (r == 1) return reply_line(c, STATE_SENDWORD, MSG_INSERTED_FMT, j->r.id);
/* out of memory trying to grow the queue, so it gets buried */
bury_job(c->srv, j, 0);
reply_line(c, STATE_SENDWORD, MSG_BURIED_FMT, j->r.id);
}
static uint
uptime()
{
return (nanoseconds() - started_at) / 1000000000;
}
static int
fmt_stats(char *buf, size_t size, void *x)
{
int whead = 0, wcur = 0;
Server *srv;
struct rusage ru = {{0, 0}, {0, 0}};
srv = x;
if (srv->wal.head) {
whead = srv->wal.head->seq;
}
if (srv->wal.cur) {
wcur = srv->wal.cur->seq;
}
getrusage(RUSAGE_SELF, &ru); /* don't care if it fails */
return snprintf(buf, size, STATS_FMT,
global_stat.urgent_ct,
ready_ct,
global_stat.reserved_ct,
get_delayed_job_ct(),
global_stat.buried_ct,
op_ct[OP_PUT],
op_ct[OP_PEEKJOB],
op_ct[OP_PEEK_READY],
op_ct[OP_PEEK_DELAYED],
op_ct[OP_PEEK_BURIED],
op_ct[OP_RESERVE],
op_ct[OP_RESERVE_TIMEOUT],
op_ct[OP_DELETE],
op_ct[OP_RELEASE],
op_ct[OP_USE],
op_ct[OP_WATCH],
op_ct[OP_IGNORE],
op_ct[OP_BURY],
op_ct[OP_KICK],
op_ct[OP_TOUCH],
op_ct[OP_STATS],
op_ct[OP_JOBSTATS],
op_ct[OP_STATS_TUBE],
op_ct[OP_LIST_TUBES],
op_ct[OP_LIST_TUBE_USED],
op_ct[OP_LIST_TUBES_WATCHED],
op_ct[OP_PAUSE_TUBE],
timeout_ct,
global_stat.total_jobs_ct,
job_data_size_limit,
tubes.used,
count_cur_conns(),
count_cur_producers(),
count_cur_workers(),
global_stat.waiting_ct,
count_tot_conns(),
(long) getpid(),
version,
(int) ru.ru_utime.tv_sec, (int) ru.ru_utime.tv_usec,
(int) ru.ru_stime.tv_sec, (int) ru.ru_stime.tv_usec,
uptime(),
whead,
wcur,
srv->wal.nmig,
srv->wal.nrec,
srv->wal.filesize,
id,
node_info.nodename);
}
/* Read a priority value from the given buffer and place it in pri.
* Update end to point to the address after the last character consumed.
* Pri and end can be NULL. If they are both NULL, read_pri() will do the
* conversion and return the status code but not update any values. This is an
* easy way to check for errors.
* If end is NULL, read_pri will also check that the entire input string was
* consumed and return an error code otherwise.
* Return 0 on success, or nonzero on failure.
* If a failure occurs, pri and end are not modified. */
static int
read_pri(uint *pri, const char *buf, char **end)
{
char *tend;
uint tpri;
errno = 0;
while (buf[0] == ' ') buf++;
if (buf[0] < '0' || '9' < buf[0]) return -1;
tpri = strtoul(buf, &tend, 10);
if (tend == buf) return -1;
if (errno && errno != ERANGE) return -1;
if (!end && tend[0] != '\0') return -1;
if (pri) *pri = tpri;
if (end) *end = tend;
return 0;
}
/* Read a delay value from the given buffer and place it in delay.
* The interface and behavior are analogous to read_pri(). */
static int
read_delay(int64 *delay, const char *buf, char **end)
{
int r;
uint delay_sec;
r = read_pri(&delay_sec, buf, end);
if (r) return r;
*delay = ((int64) delay_sec) * 1000000000;
return 0;
}
/* Read a timeout value from the given buffer and place it in ttr.
* The interface and behavior are the same as in read_delay(). */
static int
read_ttr(int64 *ttr, const char *buf, char **end)
{
return read_delay(ttr, buf, end);
}
/* Read a tube name from the given buffer moving the buffer to the name start */
static int
read_tube_name(char **tubename, char *buf, char **end)
{
size_t len;
while (buf[0] == ' ') buf++;
len = strspn(buf, NAME_CHARS);
if (len == 0) return -1;
if (tubename) *tubename = buf;
if (end) *end = buf + len;
return 0;