Update which binlog a job refers to.
[beanstalkd.git] / prot.c
blob3e349d0ca7ff985e9cb32f65daa8bb2bd4d5d3ba
1 /* prot.c - protocol implementation */
3 /* Copyright (C) 2007 Keith Rarick and Philotic Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <sys/resource.h>
25 #include <sys/uio.h>
26 #include <stdarg.h>
28 #include "stat.h"
29 #include "prot.h"
30 #include "pq.h"
31 #include "ms.h"
32 #include "job.h"
33 #include "tube.h"
34 #include "conn.h"
35 #include "util.h"
36 #include "net.h"
37 #include "binlog.h"
38 #include "version.h"
40 /* job body cannot be greater than this many bytes long */
41 size_t job_data_size_limit = JOB_DATA_SIZE_LIMIT_DEFAULT;
43 #define NAME_CHARS \
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
45 "abcdefghijklmnopqrstuvwxyz" \
46 "0123456789-+/;.$()"
48 #define CMD_PUT "put "
49 #define CMD_PEEKJOB "peek "
50 #define CMD_PEEK_READY "peek-ready"
51 #define CMD_PEEK_DELAYED "peek-delayed"
52 #define CMD_PEEK_BURIED "peek-buried"
53 #define CMD_RESERVE "reserve"
54 #define CMD_RESERVE_TIMEOUT "reserve-with-timeout "
55 #define CMD_DELETE "delete "
56 #define CMD_RELEASE "release "
57 #define CMD_BURY "bury "
58 #define CMD_KICK "kick "
59 #define CMD_STATS "stats"
60 #define CMD_JOBSTATS "stats-job "
61 #define CMD_USE "use "
62 #define CMD_WATCH "watch "
63 #define CMD_IGNORE "ignore "
64 #define CMD_LIST_TUBES "list-tubes"
65 #define CMD_LIST_TUBE_USED "list-tube-used"
66 #define CMD_LIST_TUBES_WATCHED "list-tubes-watched"
67 #define CMD_STATS_TUBE "stats-tube "
69 #define CONSTSTRLEN(m) (sizeof(m) - 1)
71 #define CMD_PEEK_READY_LEN CONSTSTRLEN(CMD_PEEK_READY)
72 #define CMD_PEEK_DELAYED_LEN CONSTSTRLEN(CMD_PEEK_DELAYED)
73 #define CMD_PEEK_BURIED_LEN CONSTSTRLEN(CMD_PEEK_BURIED)
74 #define CMD_PEEKJOB_LEN CONSTSTRLEN(CMD_PEEKJOB)
75 #define CMD_RESERVE_LEN CONSTSTRLEN(CMD_RESERVE)
76 #define CMD_RESERVE_TIMEOUT_LEN CONSTSTRLEN(CMD_RESERVE_TIMEOUT)
77 #define CMD_DELETE_LEN CONSTSTRLEN(CMD_DELETE)
78 #define CMD_RELEASE_LEN CONSTSTRLEN(CMD_RELEASE)
79 #define CMD_BURY_LEN CONSTSTRLEN(CMD_BURY)
80 #define CMD_KICK_LEN CONSTSTRLEN(CMD_KICK)
81 #define CMD_STATS_LEN CONSTSTRLEN(CMD_STATS)
82 #define CMD_JOBSTATS_LEN CONSTSTRLEN(CMD_JOBSTATS)
83 #define CMD_USE_LEN CONSTSTRLEN(CMD_USE)
84 #define CMD_WATCH_LEN CONSTSTRLEN(CMD_WATCH)
85 #define CMD_IGNORE_LEN CONSTSTRLEN(CMD_IGNORE)
86 #define CMD_LIST_TUBES_LEN CONSTSTRLEN(CMD_LIST_TUBES)
87 #define CMD_LIST_TUBE_USED_LEN CONSTSTRLEN(CMD_LIST_TUBE_USED)
88 #define CMD_LIST_TUBES_WATCHED_LEN CONSTSTRLEN(CMD_LIST_TUBES_WATCHED)
89 #define CMD_STATS_TUBE_LEN CONSTSTRLEN(CMD_STATS_TUBE)
91 #define MSG_FOUND "FOUND"
92 #define MSG_NOTFOUND "NOT_FOUND\r\n"
93 #define MSG_RESERVED "RESERVED"
94 #define MSG_DEADLINE_SOON "DEADLINE_SOON\r\n"
95 #define MSG_TIMED_OUT "TIMED_OUT\r\n"
96 #define MSG_DELETED "DELETED\r\n"
97 #define MSG_RELEASED "RELEASED\r\n"
98 #define MSG_BURIED "BURIED\r\n"
99 #define MSG_BURIED_FMT "BURIED %llu\r\n"
100 #define MSG_INSERTED_FMT "INSERTED %llu\r\n"
101 #define MSG_NOT_IGNORED "NOT_IGNORED\r\n"
103 #define MSG_NOTFOUND_LEN CONSTSTRLEN(MSG_NOTFOUND)
104 #define MSG_DELETED_LEN CONSTSTRLEN(MSG_DELETED)
105 #define MSG_RELEASED_LEN CONSTSTRLEN(MSG_RELEASED)
106 #define MSG_BURIED_LEN CONSTSTRLEN(MSG_BURIED)
107 #define MSG_NOT_IGNORED_LEN CONSTSTRLEN(MSG_NOT_IGNORED)
109 #define MSG_OUT_OF_MEMORY "OUT_OF_MEMORY\r\n"
110 #define MSG_INTERNAL_ERROR "INTERNAL_ERROR\r\n"
111 #define MSG_DRAINING "DRAINING\r\n"
112 #define MSG_BAD_FORMAT "BAD_FORMAT\r\n"
113 #define MSG_UNKNOWN_COMMAND "UNKNOWN_COMMAND\r\n"
114 #define MSG_EXPECTED_CRLF "EXPECTED_CRLF\r\n"
115 #define MSG_JOB_TOO_BIG "JOB_TOO_BIG\r\n"
117 #define STATE_WANTCOMMAND 0
118 #define STATE_WANTDATA 1
119 #define STATE_SENDJOB 2
120 #define STATE_SENDWORD 3
121 #define STATE_WAIT 4
122 #define STATE_BITBUCKET 5
124 #define OP_UNKNOWN 0
125 #define OP_PUT 1
126 #define OP_PEEKJOB 2
127 #define OP_RESERVE 3
128 #define OP_DELETE 4
129 #define OP_RELEASE 5
130 #define OP_BURY 6
131 #define OP_KICK 7
132 #define OP_STATS 8
133 #define OP_JOBSTATS 9
134 #define OP_PEEK_BURIED 10
135 #define OP_USE 11
136 #define OP_WATCH 12
137 #define OP_IGNORE 13
138 #define OP_LIST_TUBES 14
139 #define OP_LIST_TUBE_USED 15
140 #define OP_LIST_TUBES_WATCHED 16
141 #define OP_STATS_TUBE 17
142 #define OP_PEEK_READY 18
143 #define OP_PEEK_DELAYED 19
144 #define OP_RESERVE_TIMEOUT 20
145 #define TOTAL_OPS 21
147 #define STATS_FMT "---\n" \
148 "current-jobs-urgent: %u\n" \
149 "current-jobs-ready: %u\n" \
150 "current-jobs-reserved: %u\n" \
151 "current-jobs-delayed: %u\n" \
152 "current-jobs-buried: %u\n" \
153 "cmd-put: %llu\n" \
154 "cmd-peek: %llu\n" \
155 "cmd-peek-ready: %llu\n" \
156 "cmd-peek-delayed: %llu\n" \
157 "cmd-peek-buried: %llu\n" \
158 "cmd-reserve: %llu\n" \
159 "cmd-reserve-with-timeout: %llu\n" \
160 "cmd-delete: %llu\n" \
161 "cmd-release: %llu\n" \
162 "cmd-use: %llu\n" \
163 "cmd-watch: %llu\n" \
164 "cmd-ignore: %llu\n" \
165 "cmd-bury: %llu\n" \
166 "cmd-kick: %llu\n" \
167 "cmd-stats: %llu\n" \
168 "cmd-stats-job: %llu\n" \
169 "cmd-stats-tube: %llu\n" \
170 "cmd-list-tubes: %llu\n" \
171 "cmd-list-tube-used: %llu\n" \
172 "cmd-list-tubes-watched: %llu\n" \
173 "job-timeouts: %llu\n" \
174 "total-jobs: %llu\n" \
175 "max-job-size: %zu\n" \
176 "current-tubes: %zu\n" \
177 "current-connections: %u\n" \
178 "current-producers: %u\n" \
179 "current-workers: %u\n" \
180 "current-waiting: %u\n" \
181 "total-connections: %u\n" \
182 "pid: %u\n" \
183 "version: %s\n" \
184 "rusage-utime: %d.%06d\n" \
185 "rusage-stime: %d.%06d\n" \
186 "uptime: %u\n" \
187 "\r\n"
189 #define STATS_TUBE_FMT "---\n" \
190 "name: %s\n" \
191 "current-jobs-urgent: %u\n" \
192 "current-jobs-ready: %u\n" \
193 "current-jobs-reserved: %u\n" \
194 "current-jobs-delayed: %u\n" \
195 "current-jobs-buried: %u\n" \
196 "total-jobs: %llu\n" \
197 "current-using: %u\n" \
198 "current-watching: %u\n" \
199 "current-waiting: %u\n" \
200 "\r\n"
202 #define JOB_STATS_FMT "---\n" \
203 "id: %llu\n" \
204 "tube: %s\n" \
205 "state: %s\n" \
206 "pri: %u\n" \
207 "age: %u\n" \
208 "delay: %u\n" \
209 "ttr: %u\n" \
210 "time-left: %u\n" \
211 "timeouts: %u\n" \
212 "releases: %u\n" \
213 "buries: %u\n" \
214 "kicks: %u\n" \
215 "\r\n"
217 /* this number is pretty arbitrary */
218 #define BUCKET_BUF_SIZE 1024
220 static char bucket[BUCKET_BUF_SIZE];
222 static unsigned int ready_ct = 0;
223 static struct stats global_stat = {0, 0, 0, 0, 0};
225 static tube default_tube;
227 static int drain_mode = 0;
228 static time_t start_time;
229 static unsigned long long int op_ct[TOTAL_OPS], timeout_ct = 0;
232 /* Doubly-linked list of connections with at least one reserved job. */
233 static struct conn running = { &running, &running, 0 };
235 #ifdef DEBUG
236 static const char * op_names[] = {
237 "<unknown>",
238 CMD_PUT,
239 CMD_PEEKJOB,
240 CMD_RESERVE,
241 CMD_DELETE,
242 CMD_RELEASE,
243 CMD_BURY,
244 CMD_KICK,
245 CMD_STATS,
246 CMD_JOBSTATS,
247 CMD_PEEK_BURIED,
248 CMD_USE,
249 CMD_WATCH,
250 CMD_IGNORE,
251 CMD_LIST_TUBES,
252 CMD_LIST_TUBE_USED,
253 CMD_LIST_TUBES_WATCHED,
254 CMD_STATS_TUBE,
255 CMD_PEEK_READY,
256 CMD_PEEK_DELAYED,
257 CMD_RESERVE_TIMEOUT
259 #endif
261 static int
262 buried_job_p(tube t)
264 return job_list_any_p(&t->buried);
267 static void
268 reply(conn c, const char *line, int len, int state)
270 int r;
272 if (!c) return;
274 r = conn_update_evq(c, EV_WRITE | EV_PERSIST);
275 if (r == -1) return twarnx("conn_update_evq() failed"), conn_close(c);
277 c->reply = line;
278 c->reply_len = len;
279 c->reply_sent = 0;
280 c->state = state;
281 dprintf("sending reply: %.*s", len, line);
284 #define reply_msg(c,m) reply((c),(m),CONSTSTRLEN(m),STATE_SENDWORD)
286 #define reply_serr(c,e) (twarnx("server error: %s",(e)),\
287 reply_msg((c),(e)))
289 static void
290 reply_line(conn c, int state, const char *fmt, ...)
292 int r;
293 va_list ap;
295 va_start(ap, fmt);
296 r = vsnprintf(c->reply_buf, LINE_BUF_SIZE, fmt, ap);
297 va_end(ap);
299 /* Make sure the buffer was big enough. If not, we have a bug. */
300 if (r >= LINE_BUF_SIZE) return reply_serr(c, MSG_INTERNAL_ERROR);
302 return reply(c, c->reply_buf, r, state);
305 static void
306 reply_job(conn c, job j, const char *word)
308 /* tell this connection which job to send */
309 c->out_job = j;
310 c->out_job_sent = 0;
312 return reply_line(c, STATE_SENDJOB, "%s %llu %u\r\n",
313 word, j->id, j->body_size - 2);
316 conn
317 remove_waiting_conn(conn c)
319 tube t;
320 size_t i;
322 if (!conn_waiting(c)) return NULL;
324 c->type &= ~CONN_TYPE_WAITING;
325 global_stat.waiting_ct--;
326 for (i = 0; i < c->watch.used; i++) {
327 t = c->watch.items[i];
328 t->stat.waiting_ct--;
329 ms_remove(&t->waiting, c);
331 return c;
334 static void
335 reserve_job(conn c, job j)
337 j->deadline = time(NULL) + j->ttr;
338 global_stat.reserved_ct++; /* stats */
339 j->tube->stat.reserved_ct++;
340 conn_insert(&running, c);
341 j->state = JOB_STATE_RESERVED;
342 job_insert(&c->reserved_jobs, j);
343 j->reserver=c;
344 if (c->soonest_job && j->deadline < c->soonest_job->deadline) {
345 c->soonest_job = j;
347 return reply_job(c, j, MSG_RESERVED);
350 static job
351 next_eligible_job()
353 tube t;
354 size_t i;
355 job j = NULL, candidate;
357 dprintf("tubes.used = %zu\n", tubes.used);
358 for (i = 0; i < tubes.used; i++) {
359 t = tubes.items[i];
360 dprintf("for %s t->waiting.used=%zu t->ready.used=%d\n",
361 t->name, t->waiting.used, t->ready.used);
362 if (t->waiting.used && t->ready.used) {
363 candidate = pq_peek(&t->ready);
364 if (!j || candidate->id < j->id) j = candidate;
366 dprintf("i = %zu, tubes.used = %zu\n", i, tubes.used);
369 return j;
372 static void
373 process_queue()
375 job j;
377 dprintf("processing queue\n");
378 while ((j = next_eligible_job())) {
379 dprintf("got eligible job %llu in %s\n", j->id, j->tube->name);
380 j = pq_take(&j->tube->ready);
381 ready_ct--;
382 if (j->pri < URGENT_THRESHOLD) {
383 global_stat.urgent_ct--;
384 j->tube->stat.urgent_ct--;
386 reserve_job(remove_waiting_conn(ms_take(&j->tube->waiting)), j);
390 static job
391 delay_q_peek()
393 int i;
394 tube t;
395 job j = NULL, nj;
397 for (i = 0; i < tubes.used; i++) {
398 t = tubes.items[i];
399 nj = pq_peek(&t->delay);
400 if (!nj) continue;
401 if (!j || nj->deadline < j->deadline) j = nj;
404 return j;
407 static void
408 set_main_delay_timeout()
410 job j;
412 set_main_timeout((j = delay_q_peek()) ? j->deadline : 0);
415 static int
416 enqueue_job(job j, unsigned int delay)
418 int r;
420 j->reserver = NULL;
421 if (delay) {
422 j->deadline = time(NULL) + delay;
423 r = pq_give(&j->tube->delay, j);
424 if (!r) return 0;
425 j->state = JOB_STATE_DELAYED;
426 set_main_delay_timeout();
427 } else {
428 r = pq_give(&j->tube->ready, j);
429 if (!r) return 0;
430 j->state = JOB_STATE_READY;
431 ready_ct++;
432 if (j->pri < URGENT_THRESHOLD) {
433 global_stat.urgent_ct++;
434 j->tube->stat.urgent_ct++;
437 binlog_write_job(j);
438 process_queue();
439 return 1;
442 static void
443 bury_job(job j)
445 job_insert(&j->tube->buried, j);
446 global_stat.buried_ct++;
447 j->tube->stat.buried_ct++;
448 j->state = JOB_STATE_BURIED;
449 j->reserver=NULL;
450 j->bury_ct++;
451 binlog_write_job(j);
454 void
455 enqueue_reserved_jobs(conn c)
457 int r;
458 job j;
460 while (job_list_any_p(&c->reserved_jobs)) {
461 j = job_remove(c->reserved_jobs.next);
462 r = enqueue_job(j, 0);
463 if (!r) bury_job(j);
464 global_stat.reserved_ct--;
465 j->tube->stat.reserved_ct--;
466 c->soonest_job = NULL;
467 if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
471 static job
472 delay_q_take()
474 job j = delay_q_peek();
475 return j ? pq_take(&j->tube->delay) : NULL;
478 static job
479 remove_this_buried_job(job j)
481 j = job_remove(j);
482 if (j) {
483 global_stat.buried_ct--;
484 j->tube->stat.buried_ct--;
486 return j;
489 static int
490 kick_buried_job(tube t)
492 int r;
493 job j;
495 if (!buried_job_p(t)) return 0;
496 j = remove_this_buried_job(t->buried.next);
497 j->kick_ct++;
498 r = enqueue_job(j, 0);
499 if (r) return 1;
501 /* ready queue is full, so bury it */
502 bury_job(j);
503 return 0;
506 static unsigned int
507 get_delayed_job_ct()
509 tube t;
510 size_t i;
511 unsigned int count = 0;
513 for (i = 0; i < tubes.used; i++) {
514 t = tubes.items[i];
515 count += pq_used(&t->delay);
517 return count;
520 static int
521 kick_delayed_job(tube t)
523 int r;
524 job j;
526 j = pq_take(&t->delay);
527 if (!j) return 0;
528 j->kick_ct++;
529 r = enqueue_job(j, 0);
530 if (r) return 1;
532 /* ready queue is full, so delay it again */
533 r = enqueue_job(j, j->delay);
534 if (r) return 0;
536 /* last resort */
537 bury_job(j);
538 return 0;
541 /* return the number of jobs successfully kicked */
542 static unsigned int
543 kick_buried_jobs(tube t, unsigned int n)
545 unsigned int i;
546 for (i = 0; (i < n) && kick_buried_job(t); ++i);
547 return i;
550 /* return the number of jobs successfully kicked */
551 static unsigned int
552 kick_delayed_jobs(tube t, unsigned int n)
554 unsigned int i;
555 for (i = 0; (i < n) && kick_delayed_job(t); ++i);
556 return i;
559 static unsigned int
560 kick_jobs(tube t, unsigned int n)
562 if (buried_job_p(t)) return kick_buried_jobs(t, n);
563 return kick_delayed_jobs(t, n);
566 static job
567 find_buried_job(unsigned long long int id)
569 job j = job_find(id);
570 return (j && j->state == JOB_STATE_BURIED) ? j : NULL;
573 static job
574 remove_buried_job(unsigned long long int id)
576 return remove_this_buried_job(find_buried_job(id));
579 static void
580 enqueue_waiting_conn(conn c)
582 tube t;
583 size_t i;
585 global_stat.waiting_ct++;
586 c->type |= CONN_TYPE_WAITING;
587 for (i = 0; i < c->watch.used; i++) {
588 t = c->watch.items[i];
589 t->stat.waiting_ct++;
590 ms_append(&t->waiting, c);
594 static job
595 find_reserved_job_in_conn(conn c, unsigned long long int id)
597 job j = job_find(id);
598 return (j && j->reserver == c && j->state == JOB_STATE_RESERVED) ? j : NULL;
601 static job
602 peek_job(unsigned long long int id)
604 return job_find(id);
607 static void
608 check_err(conn c, const char *s)
610 if (errno == EAGAIN) return;
611 if (errno == EINTR) return;
612 if (errno == EWOULDBLOCK) return;
614 twarn("%s", s);
615 conn_close(c);
616 return;
619 /* Scan the given string for the sequence "\r\n" and return the line length.
620 * Always returns at least 2 if a match is found. Returns 0 if no match. */
621 static int
622 scan_line_end(const char *s, int size)
624 char *match;
626 match = memchr(s, '\r', size - 1);
627 if (!match) return 0;
629 /* this is safe because we only scan size - 1 chars above */
630 if (match[1] == '\n') return match - s + 2;
632 return 0;
635 static int
636 cmd_len(conn c)
638 return scan_line_end(c->cmd, c->cmd_read);
641 /* parse the command line */
642 static int
643 which_cmd(conn c)
645 #define TEST_CMD(s,c,o) if (strncmp((s), (c), CONSTSTRLEN(c)) == 0) return (o);
646 TEST_CMD(c->cmd, CMD_PUT, OP_PUT);
647 TEST_CMD(c->cmd, CMD_PEEKJOB, OP_PEEKJOB);
648 TEST_CMD(c->cmd, CMD_PEEK_READY, OP_PEEK_READY);
649 TEST_CMD(c->cmd, CMD_PEEK_DELAYED, OP_PEEK_DELAYED);
650 TEST_CMD(c->cmd, CMD_PEEK_BURIED, OP_PEEK_BURIED);
651 TEST_CMD(c->cmd, CMD_RESERVE_TIMEOUT, OP_RESERVE_TIMEOUT);
652 TEST_CMD(c->cmd, CMD_RESERVE, OP_RESERVE);
653 TEST_CMD(c->cmd, CMD_DELETE, OP_DELETE);
654 TEST_CMD(c->cmd, CMD_RELEASE, OP_RELEASE);
655 TEST_CMD(c->cmd, CMD_BURY, OP_BURY);
656 TEST_CMD(c->cmd, CMD_KICK, OP_KICK);
657 TEST_CMD(c->cmd, CMD_JOBSTATS, OP_JOBSTATS);
658 TEST_CMD(c->cmd, CMD_STATS_TUBE, OP_STATS_TUBE);
659 TEST_CMD(c->cmd, CMD_STATS, OP_STATS);
660 TEST_CMD(c->cmd, CMD_USE, OP_USE);
661 TEST_CMD(c->cmd, CMD_WATCH, OP_WATCH);
662 TEST_CMD(c->cmd, CMD_IGNORE, OP_IGNORE);
663 TEST_CMD(c->cmd, CMD_LIST_TUBES_WATCHED, OP_LIST_TUBES_WATCHED);
664 TEST_CMD(c->cmd, CMD_LIST_TUBE_USED, OP_LIST_TUBE_USED);
665 TEST_CMD(c->cmd, CMD_LIST_TUBES, OP_LIST_TUBES);
666 return OP_UNKNOWN;
669 /* Copy up to body_size trailing bytes into the job, then the rest into the cmd
670 * buffer. If c->in_job exists, this assumes that c->in_job->body is empty.
671 * This function is idempotent(). */
672 static void
673 fill_extra_data(conn c)
675 int extra_bytes, job_data_bytes = 0, cmd_bytes;
677 if (!c->fd) return; /* the connection was closed */
678 if (!c->cmd_len) return; /* we don't have a complete command */
680 /* how many extra bytes did we read? */
681 extra_bytes = c->cmd_read - c->cmd_len;
683 /* how many bytes should we put into the job body? */
684 if (c->in_job) {
685 job_data_bytes = min(extra_bytes, c->in_job->body_size);
686 memcpy(c->in_job->body, c->cmd + c->cmd_len, job_data_bytes);
687 c->in_job_read = job_data_bytes;
688 } else if (c->in_job_read) {
689 /* we are in bit-bucket mode, throwing away data */
690 job_data_bytes = min(extra_bytes, c->in_job_read);
691 c->in_job_read -= job_data_bytes;
694 /* how many bytes are left to go into the future cmd? */
695 cmd_bytes = extra_bytes - job_data_bytes;
696 memmove(c->cmd, c->cmd + c->cmd_len + job_data_bytes, cmd_bytes);
697 c->cmd_read = cmd_bytes;
698 c->cmd_len = 0; /* we no longer know the length of the new command */
701 static void
702 enqueue_incoming_job(conn c)
704 int r;
705 job j = c->in_job;
707 c->in_job = NULL; /* the connection no longer owns this job */
708 c->in_job_read = 0;
710 /* check if the trailer is present and correct */
711 if (memcmp(j->body + j->body_size - 2, "\r\n", 2)) {
712 job_free(j);
713 return reply_msg(c, MSG_EXPECTED_CRLF);
716 if (drain_mode) {
717 job_free(j);
718 return reply_serr(c, MSG_DRAINING);
721 /* we have a complete job, so let's stick it in the pqueue */
722 r = enqueue_job(j, j->delay);
723 op_ct[OP_PUT]++; /* stats */
724 global_stat.total_jobs_ct++;
725 j->tube->stat.total_jobs_ct++;
727 if (r) return reply_line(c, STATE_SENDWORD, MSG_INSERTED_FMT, j->id);
729 /* out of memory trying to grow the queue, so it gets buried */
730 bury_job(j);
731 reply_line(c, STATE_SENDWORD, MSG_BURIED_FMT, j->id);
734 static unsigned int
735 uptime()
737 return time(NULL) - start_time;
740 static int
741 fmt_stats(char *buf, size_t size, void *x)
743 struct rusage ru = {{0, 0}, {0, 0}};
744 getrusage(RUSAGE_SELF, &ru); /* don't care if it fails */
745 return snprintf(buf, size, STATS_FMT,
746 global_stat.urgent_ct,
747 ready_ct,
748 global_stat.reserved_ct,
749 get_delayed_job_ct(),
750 global_stat.buried_ct,
751 op_ct[OP_PUT],
752 op_ct[OP_PEEKJOB],
753 op_ct[OP_PEEK_READY],
754 op_ct[OP_PEEK_DELAYED],
755 op_ct[OP_PEEK_BURIED],
756 op_ct[OP_RESERVE],
757 op_ct[OP_RESERVE_TIMEOUT],
758 op_ct[OP_DELETE],
759 op_ct[OP_RELEASE],
760 op_ct[OP_USE],
761 op_ct[OP_WATCH],
762 op_ct[OP_IGNORE],
763 op_ct[OP_BURY],
764 op_ct[OP_KICK],
765 op_ct[OP_STATS],
766 op_ct[OP_JOBSTATS],
767 op_ct[OP_STATS_TUBE],
768 op_ct[OP_LIST_TUBES],
769 op_ct[OP_LIST_TUBE_USED],
770 op_ct[OP_LIST_TUBES_WATCHED],
771 timeout_ct,
772 global_stat.total_jobs_ct,
773 job_data_size_limit,
774 tubes.used,
775 count_cur_conns(),
776 count_cur_producers(),
777 count_cur_workers(),
778 global_stat.waiting_ct,
779 count_tot_conns(),
780 getpid(),
781 BEAN_VERSION,
782 (int) ru.ru_utime.tv_sec, (int) ru.ru_utime.tv_usec,
783 (int) ru.ru_stime.tv_sec, (int) ru.ru_stime.tv_usec,
784 uptime());
788 /* Read a priority value from the given buffer and place it in pri.
789 * Update end to point to the address after the last character consumed.
790 * Pri and end can be NULL. If they are both NULL, read_pri() will do the
791 * conversion and return the status code but not update any values. This is an
792 * easy way to check for errors.
793 * If end is NULL, read_pri will also check that the entire input string was
794 * consumed and return an error code otherwise.
795 * Return 0 on success, or nonzero on failure.
796 * If a failure occurs, pri and end are not modified. */
797 static int
798 read_pri(unsigned int *pri, const char *buf, char **end)
800 char *tend;
801 unsigned int tpri;
803 errno = 0;
804 tpri = strtoul(buf, &tend, 10);
805 if (tend == buf) return -1;
806 if (errno && errno != ERANGE) return -1;
807 if (!end && tend[0] != '\0') return -1;
809 if (pri) *pri = tpri;
810 if (end) *end = tend;
811 return 0;
814 /* Read a delay value from the given buffer and place it in delay.
815 * The interface and behavior are the same as in read_pri(). */
816 static int
817 read_delay(unsigned int *delay, const char *buf, char **end)
819 return read_pri(delay, buf, end);
822 /* Read a timeout value from the given buffer and place it in ttr.
823 * The interface and behavior are the same as in read_pri(). */
824 static int
825 read_ttr(unsigned int *ttr, const char *buf, char **end)
827 return read_pri(ttr, buf, end);
830 static void
831 wait_for_job(conn c, int timeout)
833 int r;
835 c->state = STATE_WAIT;
836 enqueue_waiting_conn(c);
838 /* Set the pending timeout to the requested timeout amount */
839 c->pending_timeout = timeout;
841 /* this conn is waiting, but we want to know if they hang up */
842 r = conn_update_evq(c, EV_READ | EV_PERSIST);
843 if (r == -1) return twarnx("update events failed"), conn_close(c);
846 typedef int(*fmt_fn)(char *, size_t, void *);
848 static void
849 do_stats(conn c, fmt_fn fmt, void *data)
851 int r, stats_len;
853 /* first, measure how big a buffer we will need */
854 stats_len = fmt(NULL, 0, data) + 16;
856 c->out_job = allocate_job(stats_len); /* fake job to hold stats data */
857 if (!c->out_job) return reply_serr(c, MSG_OUT_OF_MEMORY);
859 /* now actually format the stats data */
860 r = fmt(c->out_job->body, stats_len, data);
861 /* and set the actual body size */
862 c->out_job->body_size = r;
863 if (r > stats_len) return reply_serr(c, MSG_INTERNAL_ERROR);
865 c->out_job_sent = 0;
866 return reply_line(c, STATE_SENDJOB, "OK %d\r\n", r - 2);
869 static void
870 do_list_tubes(conn c, ms l)
872 char *buf;
873 tube t;
874 size_t i, resp_z;
876 /* first, measure how big a buffer we will need */
877 resp_z = 6; /* initial "---\n" and final "\r\n" */
878 for (i = 0; i < l->used; i++) {
879 t = l->items[i];
880 resp_z += 3 + strlen(t->name); /* including "- " and "\n" */
883 c->out_job = allocate_job(resp_z); /* fake job to hold response data */
884 if (!c->out_job) return reply_serr(c, MSG_OUT_OF_MEMORY);
886 /* now actually format the response */
887 buf = c->out_job->body;
888 buf += snprintf(buf, 5, "---\n");
889 for (i = 0; i < l->used; i++) {
890 t = l->items[i];
891 buf += snprintf(buf, 4 + strlen(t->name), "- %s\n", t->name);
893 buf[0] = '\r';
894 buf[1] = '\n';
896 c->out_job_sent = 0;
897 return reply_line(c, STATE_SENDJOB, "OK %d\r\n", resp_z - 2);
900 static int
901 fmt_job_stats(char *buf, size_t size, job j)
903 time_t t;
905 t = time(NULL);
906 return snprintf(buf, size, JOB_STATS_FMT,
907 j->id,
908 j->tube->name,
909 job_state(j),
910 j->pri,
911 (unsigned int) (t - j->creation),
912 j->delay,
913 j->ttr,
914 (unsigned int) (j->deadline - t),
915 j->timeout_ct,
916 j->release_ct,
917 j->bury_ct,
918 j->kick_ct);
921 static int
922 fmt_stats_tube(char *buf, size_t size, tube t)
924 return snprintf(buf, size, STATS_TUBE_FMT,
925 t->name,
926 t->stat.urgent_ct,
927 t->ready.used,
928 t->stat.reserved_ct,
929 pq_used(&t->delay),
930 t->stat.buried_ct,
931 t->stat.total_jobs_ct,
932 t->using_ct,
933 t->watching_ct,
934 t->stat.waiting_ct);
937 static void
938 maybe_enqueue_incoming_job(conn c)
940 job j = c->in_job;
942 /* do we have a complete job? */
943 if (c->in_job_read == j->body_size) return enqueue_incoming_job(c);
945 /* otherwise we have incomplete data, so just keep waiting */
946 c->state = STATE_WANTDATA;
949 /* j can be NULL */
950 static job
951 remove_this_reserved_job(conn c, job j)
953 j = job_remove(j);
954 if (j) {
955 global_stat.reserved_ct--;
956 j->tube->stat.reserved_ct--;
957 j->reserver=NULL;
959 c->soonest_job = NULL;
960 if (!job_list_any_p(&c->reserved_jobs)) conn_remove(c);
961 return j;
964 static job
965 remove_reserved_job(conn c, unsigned long long int id)
967 return remove_this_reserved_job(c, find_reserved_job_in_conn(c, id));
970 static int
971 name_is_ok(const char *name, size_t max)
973 size_t len = strlen(name);
974 return len > 0 && len <= max &&
975 strspn(name, NAME_CHARS) == len && name[0] != '-';
978 void
979 prot_remove_tube(tube t)
981 ms_remove(&tubes, t);
984 static void
985 dispatch_cmd(conn c)
987 int r, i, timeout = -1;
988 unsigned int count;
989 job j;
990 unsigned char type;
991 char *size_buf, *delay_buf, *ttr_buf, *pri_buf, *end_buf, *name;
992 unsigned int pri, delay, ttr, body_size;
993 unsigned long long int id;
994 tube t = NULL;
996 /* NUL-terminate this string so we can use strtol and friends */
997 c->cmd[c->cmd_len - 2] = '\0';
999 /* check for possible maliciousness */
1000 if (strlen(c->cmd) != c->cmd_len - 2) {
1001 return reply_msg(c, MSG_BAD_FORMAT);
1004 type = which_cmd(c);
1005 dprintf("got %s command: \"%s\"\n", op_names[(int) type], c->cmd);
1007 switch (type) {
1008 case OP_PUT:
1009 r = read_pri(&pri, c->cmd + 4, &delay_buf);
1010 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1012 r = read_delay(&delay, delay_buf, &ttr_buf);
1013 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1015 r = read_ttr(&ttr, ttr_buf, &size_buf);
1016 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1018 errno = 0;
1019 body_size = strtoul(size_buf, &end_buf, 10);
1020 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1022 if (body_size > job_data_size_limit) {
1023 return reply_msg(c, MSG_JOB_TOO_BIG);
1026 /* don't allow trailing garbage */
1027 if (end_buf[0] != '\0') return reply_msg(c, MSG_BAD_FORMAT);
1029 conn_set_producer(c);
1031 c->in_job = make_job(pri, delay, ttr ? : 1, body_size + 2, c->use);
1033 /* OOM? */
1034 if (!c->in_job) {
1035 /* throw away the job body and respond with OUT_OF_MEMORY */
1037 /* Invert the meaning of in_job_read while throwing away data -- it
1038 * counts the bytes that remain to be thrown away. */
1039 c->in_job_read = body_size + 2;
1040 fill_extra_data(c);
1042 if (c->in_job_read == 0) return reply_serr(c, MSG_OUT_OF_MEMORY);
1044 c->state = STATE_BITBUCKET;
1045 return;
1048 fill_extra_data(c);
1050 /* it's possible we already have a complete job */
1051 maybe_enqueue_incoming_job(c);
1053 break;
1054 case OP_PEEK_READY:
1055 /* don't allow trailing garbage */
1056 if (c->cmd_len != CMD_PEEK_READY_LEN + 2) {
1057 return reply_msg(c, MSG_BAD_FORMAT);
1059 op_ct[type]++;
1061 j = job_copy(pq_peek(&c->use->ready));
1063 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1065 reply_job(c, j, MSG_FOUND);
1066 break;
1067 case OP_PEEK_DELAYED:
1068 /* don't allow trailing garbage */
1069 if (c->cmd_len != CMD_PEEK_DELAYED_LEN + 2) {
1070 return reply_msg(c, MSG_BAD_FORMAT);
1072 op_ct[type]++;
1074 j = job_copy(pq_peek(&c->use->delay));
1076 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1078 reply_job(c, j, MSG_FOUND);
1079 break;
1080 case OP_PEEK_BURIED:
1081 /* don't allow trailing garbage */
1082 if (c->cmd_len != CMD_PEEK_BURIED_LEN + 2) {
1083 return reply_msg(c, MSG_BAD_FORMAT);
1085 op_ct[type]++;
1087 j = job_copy(buried_job_p(c->use)? j = c->use->buried.next : NULL);
1089 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1091 reply_job(c, j, MSG_FOUND);
1092 break;
1093 case OP_PEEKJOB:
1094 errno = 0;
1095 id = strtoull(c->cmd + CMD_PEEKJOB_LEN, &end_buf, 10);
1096 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1097 op_ct[type]++;
1099 /* So, peek is annoying, because some other connection might free the
1100 * job while we are still trying to write it out. So we copy it and
1101 * then free the copy when it's done sending. */
1102 j = job_copy(peek_job(id));
1104 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1106 reply_job(c, j, MSG_FOUND);
1107 break;
1108 case OP_RESERVE_TIMEOUT:
1109 errno = 0;
1110 timeout = strtol(c->cmd + CMD_RESERVE_TIMEOUT_LEN, &end_buf, 10);
1111 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1112 case OP_RESERVE: /* FALLTHROUGH */
1113 /* don't allow trailing garbage */
1114 if (type == OP_RESERVE && c->cmd_len != CMD_RESERVE_LEN + 2) {
1115 return reply_msg(c, MSG_BAD_FORMAT);
1118 op_ct[type]++;
1119 conn_set_worker(c);
1121 if (conn_has_close_deadline(c) && !conn_ready(c)) {
1122 return reply_msg(c, MSG_DEADLINE_SOON);
1125 /* try to get a new job for this guy */
1126 wait_for_job(c, timeout);
1127 process_queue();
1128 break;
1129 case OP_DELETE:
1130 errno = 0;
1131 id = strtoull(c->cmd + CMD_DELETE_LEN, &end_buf, 10);
1132 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1133 op_ct[type]++;
1135 j = remove_reserved_job(c, id) ? : remove_buried_job(id);
1137 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1139 j->state = JOB_STATE_INVALID;
1140 binlog_write_job(j);
1141 job_free(j);
1143 reply(c, MSG_DELETED, MSG_DELETED_LEN, STATE_SENDWORD);
1144 break;
1145 case OP_RELEASE:
1146 errno = 0;
1147 id = strtoull(c->cmd + CMD_RELEASE_LEN, &pri_buf, 10);
1148 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1150 r = read_pri(&pri, pri_buf, &delay_buf);
1151 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1153 r = read_delay(&delay, delay_buf, NULL);
1154 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1155 op_ct[type]++;
1157 j = remove_reserved_job(c, id);
1159 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1161 j->pri = pri;
1162 j->delay = delay;
1163 j->release_ct++;
1164 r = enqueue_job(j, delay);
1165 if (r) return reply(c, MSG_RELEASED, MSG_RELEASED_LEN, STATE_SENDWORD);
1167 /* out of memory trying to grow the queue, so it gets buried */
1168 bury_job(j);
1169 reply(c, MSG_BURIED, MSG_BURIED_LEN, STATE_SENDWORD);
1170 break;
1171 case OP_BURY:
1172 errno = 0;
1173 id = strtoull(c->cmd + CMD_BURY_LEN, &pri_buf, 10);
1174 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1176 r = read_pri(&pri, pri_buf, NULL);
1177 if (r) return reply_msg(c, MSG_BAD_FORMAT);
1178 op_ct[type]++;
1180 j = remove_reserved_job(c, id);
1182 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1184 j->pri = pri;
1185 bury_job(j);
1186 reply(c, MSG_BURIED, MSG_BURIED_LEN, STATE_SENDWORD);
1187 break;
1188 case OP_KICK:
1189 errno = 0;
1190 count = strtoul(c->cmd + CMD_KICK_LEN, &end_buf, 10);
1191 if (end_buf == c->cmd + CMD_KICK_LEN) {
1192 return reply_msg(c, MSG_BAD_FORMAT);
1194 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1196 op_ct[type]++;
1198 i = kick_jobs(c->use, count);
1200 return reply_line(c, STATE_SENDWORD, "KICKED %u\r\n", i);
1201 case OP_STATS:
1202 /* don't allow trailing garbage */
1203 if (c->cmd_len != CMD_STATS_LEN + 2) {
1204 return reply_msg(c, MSG_BAD_FORMAT);
1207 op_ct[type]++;
1209 do_stats(c, fmt_stats, NULL);
1210 break;
1211 case OP_JOBSTATS:
1212 errno = 0;
1213 id = strtoull(c->cmd + CMD_JOBSTATS_LEN, &end_buf, 10);
1214 if (errno) return reply_msg(c, MSG_BAD_FORMAT);
1216 op_ct[type]++;
1218 j = peek_job(id);
1219 if (!j) return reply(c, MSG_NOTFOUND, MSG_NOTFOUND_LEN, STATE_SENDWORD);
1221 if (!j->tube) return reply_serr(c, MSG_INTERNAL_ERROR);
1222 do_stats(c, (fmt_fn) fmt_job_stats, j);
1223 break;
1224 case OP_STATS_TUBE:
1225 name = c->cmd + CMD_STATS_TUBE_LEN;
1226 if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
1228 op_ct[type]++;
1230 t = tube_find(name);
1231 if (!t) return reply_msg(c, MSG_NOTFOUND);
1233 do_stats(c, (fmt_fn) fmt_stats_tube, t);
1234 t = NULL;
1235 break;
1236 case OP_LIST_TUBES:
1237 /* don't allow trailing garbage */
1238 if (c->cmd_len != CMD_LIST_TUBES_LEN + 2) {
1239 return reply_msg(c, MSG_BAD_FORMAT);
1242 op_ct[type]++;
1243 do_list_tubes(c, &tubes);
1244 break;
1245 case OP_LIST_TUBE_USED:
1246 /* don't allow trailing garbage */
1247 if (c->cmd_len != CMD_LIST_TUBE_USED_LEN + 2) {
1248 return reply_msg(c, MSG_BAD_FORMAT);
1251 op_ct[type]++;
1252 reply_line(c, STATE_SENDWORD, "USING %s\r\n", c->use->name);
1253 break;
1254 case OP_LIST_TUBES_WATCHED:
1255 /* don't allow trailing garbage */
1256 if (c->cmd_len != CMD_LIST_TUBES_WATCHED_LEN + 2) {
1257 return reply_msg(c, MSG_BAD_FORMAT);
1260 op_ct[type]++;
1261 do_list_tubes(c, &c->watch);
1262 break;
1263 case OP_USE:
1264 name = c->cmd + CMD_USE_LEN;
1265 if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
1266 op_ct[type]++;
1268 TUBE_ASSIGN(t, tube_find_or_make(name));
1269 if (!t) return reply_serr(c, MSG_OUT_OF_MEMORY);
1271 c->use->using_ct--;
1272 TUBE_ASSIGN(c->use, t);
1273 TUBE_ASSIGN(t, NULL);
1274 c->use->using_ct++;
1276 reply_line(c, STATE_SENDWORD, "USING %s\r\n", c->use->name);
1277 break;
1278 case OP_WATCH:
1279 name = c->cmd + CMD_WATCH_LEN;
1280 if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
1281 op_ct[type]++;
1283 TUBE_ASSIGN(t, tube_find_or_make(name));
1284 if (!t) return reply_serr(c, MSG_OUT_OF_MEMORY);
1286 r = 1;
1287 if (!ms_contains(&c->watch, t)) r = ms_append(&c->watch, t);
1288 TUBE_ASSIGN(t, NULL);
1289 if (!r) return reply_serr(c, MSG_OUT_OF_MEMORY);
1291 reply_line(c, STATE_SENDWORD, "WATCHING %d\r\n", c->watch.used);
1292 break;
1293 case OP_IGNORE:
1294 name = c->cmd + CMD_IGNORE_LEN;
1295 if (!name_is_ok(name, 200)) return reply_msg(c, MSG_BAD_FORMAT);
1296 op_ct[type]++;
1298 t = NULL;
1299 for (i = 0; i < c->watch.used; i++) {
1300 t = c->watch.items[i];
1301 if (strncmp(t->name, name, MAX_TUBE_NAME_LEN) == 0) break;
1302 t = NULL;
1305 if (t && c->watch.used < 2) return reply_msg(c, MSG_NOT_IGNORED);
1307 if (t) ms_remove(&c->watch, t); /* may free t if refcount => 0 */
1308 t = NULL;
1310 reply_line(c, STATE_SENDWORD, "WATCHING %d\r\n", c->watch.used);
1311 break;
1312 default:
1313 return reply_msg(c, MSG_UNKNOWN_COMMAND);
1317 /* There are three reasons this function may be called. We need to check for
1318 * all of them.
1320 * 1. A reserved job has run out of time.
1321 * 2. A waiting client's reserved job has entered the safety margin.
1322 * 3. A waiting client's requested timeout has occurred.
1324 * If any of these happen, we must do the appropriate thing. */
1325 static void
1326 h_conn_timeout(conn c)
1328 int r, should_timeout = 0;
1329 job j;
1331 /* Check if the client was trying to reserve a job. */
1332 if (conn_waiting(c) && conn_has_close_deadline(c)) should_timeout = 1;
1334 /* Check if any reserved jobs have run out of time. We should do this
1335 * whether or not the client is waiting for a new reservation. */
1336 while ((j = soonest_job(c))) {
1337 if (j->deadline > time(NULL)) break;
1338 timeout_ct++; /* stats */
1339 j->timeout_ct++;
1340 r = enqueue_job(remove_this_reserved_job(c, j), 0);
1341 if (!r) bury_job(j); /* there was no room in the queue, so bury it */
1342 r = conn_update_evq(c, c->evq.ev_events);
1343 if (r == -1) return twarnx("conn_update_evq() failed"), conn_close(c);
1346 if (should_timeout) {
1347 dprintf("conn_waiting(%p) = %d\n", c, conn_waiting(c));
1348 return reply_msg(remove_waiting_conn(c), MSG_DEADLINE_SOON);
1349 } else if (conn_waiting(c) && c->pending_timeout >= 0) {
1350 dprintf("conn_waiting(%p) = %d\n", c, conn_waiting(c));
1351 c->pending_timeout=-1;
1352 return reply_msg(remove_waiting_conn(c), MSG_TIMED_OUT);
1356 void
1357 enter_drain_mode(int sig)
1359 drain_mode = 1;
1362 static void
1363 do_cmd(conn c)
1365 dispatch_cmd(c);
1366 fill_extra_data(c);
1369 static void
1370 reset_conn(conn c)
1372 int r;
1374 r = conn_update_evq(c, EV_READ | EV_PERSIST);
1375 if (r == -1) return twarnx("update events failed"), conn_close(c);
1377 /* was this a peek or stats command? */
1378 if (!has_reserved_this_job(c, c->out_job)) job_free(c->out_job);
1379 c->out_job = NULL;
1381 c->reply_sent = 0; /* now that we're done, reset this */
1382 c->state = STATE_WANTCOMMAND;
1385 static void
1386 h_conn_data(conn c)
1388 int r, to_read;
1389 job j;
1390 struct iovec iov[2];
1392 switch (c->state) {
1393 case STATE_WANTCOMMAND:
1394 r = read(c->fd, c->cmd + c->cmd_read, LINE_BUF_SIZE - c->cmd_read);
1395 if (r == -1) return check_err(c, "read()");
1396 if (r == 0) return conn_close(c); /* the client hung up */
1398 c->cmd_read += r; /* we got some bytes */
1400 c->cmd_len = cmd_len(c); /* find the EOL */
1402 /* yay, complete command line */
1403 if (c->cmd_len) return do_cmd(c);
1405 /* c->cmd_read > LINE_BUF_SIZE can't happen */
1407 /* command line too long? */
1408 if (c->cmd_read == LINE_BUF_SIZE) {
1409 c->cmd_read = 0; /* discard the input so far */
1410 return reply_msg(c, MSG_BAD_FORMAT);
1413 /* otherwise we have an incomplete line, so just keep waiting */
1414 break;
1415 case STATE_BITBUCKET:
1416 /* Invert the meaning of in_job_read while throwing away data -- it
1417 * counts the bytes that remain to be thrown away. */
1418 to_read = min(c->in_job_read, BUCKET_BUF_SIZE);
1419 r = read(c->fd, bucket, to_read);
1420 if (r == -1) return check_err(c, "read()");
1421 if (r == 0) return conn_close(c); /* the client hung up */
1423 c->in_job_read -= r; /* we got some bytes */
1425 /* (c->in_job_read < 0) can't happen */
1427 if (c->in_job_read == 0) return reply_serr(c, MSG_OUT_OF_MEMORY);
1428 break;
1429 case STATE_WANTDATA:
1430 j = c->in_job;
1432 r = read(c->fd, j->body + c->in_job_read, j->body_size -c->in_job_read);
1433 if (r == -1) return check_err(c, "read()");
1434 if (r == 0) return conn_close(c); /* the client hung up */
1436 c->in_job_read += r; /* we got some bytes */
1438 /* (j->in_job_read > j->body_size) can't happen */
1440 maybe_enqueue_incoming_job(c);
1441 break;
1442 case STATE_SENDWORD:
1443 r= write(c->fd, c->reply + c->reply_sent, c->reply_len - c->reply_sent);
1444 if (r == -1) return check_err(c, "write()");
1445 if (r == 0) return conn_close(c); /* the client hung up */
1447 c->reply_sent += r; /* we got some bytes */
1449 /* (c->reply_sent > c->reply_len) can't happen */
1451 if (c->reply_sent == c->reply_len) return reset_conn(c);
1453 /* otherwise we sent an incomplete reply, so just keep waiting */
1454 break;
1455 case STATE_SENDJOB:
1456 j = c->out_job;
1458 iov[0].iov_base = (void *)(c->reply + c->reply_sent);
1459 iov[0].iov_len = c->reply_len - c->reply_sent; /* maybe 0 */
1460 iov[1].iov_base = j->body + c->out_job_sent;
1461 iov[1].iov_len = j->body_size - c->out_job_sent;
1463 r = writev(c->fd, iov, 2);
1464 if (r == -1) return check_err(c, "writev()");
1465 if (r == 0) return conn_close(c); /* the client hung up */
1467 /* update the sent values */
1468 c->reply_sent += r;
1469 if (c->reply_sent >= c->reply_len) {
1470 c->out_job_sent += c->reply_sent - c->reply_len;
1471 c->reply_sent = c->reply_len;
1474 /* (c->out_job_sent > j->body_size) can't happen */
1476 /* are we done? */
1477 if (c->out_job_sent == j->body_size) return reset_conn(c);
1479 /* otherwise we sent incomplete data, so just keep waiting */
1480 break;
1481 case STATE_WAIT: /* keep an eye out in case they hang up */
1482 /* but don't hang up just because our buffer is full */
1483 if (LINE_BUF_SIZE - c->cmd_read < 1) break;
1485 r = read(c->fd, c->cmd + c->cmd_read, LINE_BUF_SIZE - c->cmd_read);
1486 if (r == -1) return check_err(c, "read()");
1487 if (r == 0) return conn_close(c); /* the client hung up */
1488 c->cmd_read += r; /* we got some bytes */
1492 #define want_command(c) ((c)->fd && ((c)->state == STATE_WANTCOMMAND))
1493 #define cmd_data_ready(c) (want_command(c) && (c)->cmd_read)
1495 static void
1496 h_conn(const int fd, const short which, conn c)
1498 if (fd != c->fd) {
1499 twarnx("Argh! event fd doesn't match conn fd.");
1500 close(fd);
1501 return conn_close(c);
1504 switch (which) {
1505 case EV_TIMEOUT:
1506 h_conn_timeout(c);
1507 event_add(&c->evq, NULL); /* seems to be necessary */
1508 break;
1509 case EV_READ:
1510 /* fall through... */
1511 case EV_WRITE:
1512 /* fall through... */
1513 default:
1514 h_conn_data(c);
1517 while (cmd_data_ready(c) && (c->cmd_len = cmd_len(c))) do_cmd(c);
1520 static void
1521 h_delay()
1523 int r;
1524 job j;
1525 time_t t;
1527 t = time(NULL);
1528 while ((j = delay_q_peek())) {
1529 if (j->deadline > t) break;
1530 j = delay_q_take();
1531 r = enqueue_job(j, 0);
1532 if (!r) bury_job(j); /* there was no room in the queue, so bury it */
1535 set_main_delay_timeout();
1538 void
1539 h_accept(const int fd, const short which, struct event *ev)
1541 conn c;
1542 int cfd, flags, r;
1543 socklen_t addrlen;
1544 struct sockaddr addr;
1546 if (which == EV_TIMEOUT) return h_delay();
1548 addrlen = sizeof addr;
1549 cfd = accept(fd, &addr, &addrlen);
1550 if (cfd == -1) {
1551 if (errno != EAGAIN && errno != EWOULDBLOCK) twarn("accept()");
1552 if (errno == EMFILE) brake();
1553 return;
1556 flags = fcntl(cfd, F_GETFL, 0);
1557 if (flags < 0) return twarn("getting flags"), close(cfd), v();
1559 r = fcntl(cfd, F_SETFL, flags | O_NONBLOCK);
1560 if (r < 0) return twarn("setting O_NONBLOCK"), close(cfd), v();
1562 c = make_conn(cfd, STATE_WANTCOMMAND, default_tube, default_tube);
1563 if (!c) return twarnx("make_conn() failed"), close(cfd), brake();
1565 dprintf("accepted conn, fd=%d\n", cfd);
1566 r = conn_set_evq(c, EV_READ | EV_PERSIST, (evh) h_conn);
1567 if (r == -1) return twarnx("conn_set_evq() failed"), close(cfd), brake();
1570 void
1571 prot_init()
1573 start_time = time(NULL);
1574 memset(op_ct, 0, sizeof(op_ct));
1576 ms_init(&tubes, NULL, NULL);
1578 TUBE_ASSIGN(default_tube, tube_find_or_make("default"));
1579 if (!default_tube) twarnx("Out of memory during startup!");
1582 void
1583 prot_replay_binlog()
1585 struct job binlog_jobs;
1586 job j, nj;
1587 unsigned int delay;
1589 binlog_jobs.prev = binlog_jobs.next = &binlog_jobs;
1590 binlog_read(&binlog_jobs);
1592 for(j = binlog_jobs.next ; j != &binlog_jobs ; j = nj) {
1593 nj = j->next;
1594 job_remove(j);
1595 delay = 0;
1596 switch (j->state) {
1597 case JOB_STATE_BURIED:
1598 bury_job(j);
1599 break;
1600 case JOB_STATE_DELAYED:
1601 if (start_time < j->deadline) delay = j->deadline - start_time;
1602 default:
1603 enqueue_job(j,delay);