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/>.
24 #include <sys/resource.h>
41 /* job body cannot be greater than this many bytes long */
42 size_t job_data_size_limit
= JOB_DATA_SIZE_LIMIT_DEFAULT
;
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
46 "abcdefghijklmnopqrstuvwxyz" \
49 #define CMD_PUT "put "
50 #define CMD_PEEKJOB "peek "
51 #define CMD_PEEK_READY "peek-ready"
52 #define CMD_PEEK_DELAYED "peek-delayed"
53 #define CMD_PEEK_BURIED "peek-buried"
54 #define CMD_RESERVE "reserve"
55 #define CMD_RESERVE_TIMEOUT "reserve-with-timeout "
56 #define CMD_DELETE "delete "
57 #define CMD_RELEASE "release "
58 #define CMD_BURY "bury "
59 #define CMD_KICK "kick "
60 #define CMD_TOUCH "touch "
61 #define CMD_STATS "stats"
62 #define CMD_JOBSTATS "stats-job "
63 #define CMD_USE "use "
64 #define CMD_WATCH "watch "
65 #define CMD_IGNORE "ignore "
66 #define CMD_LIST_TUBES "list-tubes"
67 #define CMD_LIST_TUBE_USED "list-tube-used"
68 #define CMD_LIST_TUBES_WATCHED "list-tubes-watched"
69 #define CMD_STATS_TUBE "stats-tube "
71 #define CONSTSTRLEN(m) (sizeof(m) - 1)
73 #define CMD_PEEK_READY_LEN CONSTSTRLEN(CMD_PEEK_READY)
74 #define CMD_PEEK_DELAYED_LEN CONSTSTRLEN(CMD_PEEK_DELAYED)
75 #define CMD_PEEK_BURIED_LEN CONSTSTRLEN(CMD_PEEK_BURIED)
76 #define CMD_PEEKJOB_LEN CONSTSTRLEN(CMD_PEEKJOB)
77 #define CMD_RESERVE_LEN CONSTSTRLEN(CMD_RESERVE)
78 #define CMD_RESERVE_TIMEOUT_LEN CONSTSTRLEN(CMD_RESERVE_TIMEOUT)
79 #define CMD_DELETE_LEN CONSTSTRLEN(CMD_DELETE)
80 #define CMD_RELEASE_LEN CONSTSTRLEN(CMD_RELEASE)
81 #define CMD_BURY_LEN CONSTSTRLEN(CMD_BURY)
82 #define CMD_KICK_LEN CONSTSTRLEN(CMD_KICK)
83 #define CMD_TOUCH_LEN CONSTSTRLEN(CMD_TOUCH)
84 #define CMD_STATS_LEN CONSTSTRLEN(CMD_STATS)
85 #define CMD_JOBSTATS_LEN CONSTSTRLEN(CMD_JOBSTATS)
86 #define CMD_USE_LEN CONSTSTRLEN(CMD_USE)
87 #define CMD_WATCH_LEN CONSTSTRLEN(CMD_WATCH)
88 #define CMD_IGNORE_LEN CONSTSTRLEN(CMD_IGNORE)
89 #define CMD_LIST_TUBES_LEN CONSTSTRLEN(CMD_LIST_TUBES)
90 #define CMD_LIST_TUBE_USED_LEN CONSTSTRLEN(CMD_LIST_TUBE_USED)
91 #define CMD_LIST_TUBES_WATCHED_LEN CONSTSTRLEN(CMD_LIST_TUBES_WATCHED)
92 #define CMD_STATS_TUBE_LEN CONSTSTRLEN(CMD_STATS_TUBE)
94 #define MSG_FOUND "FOUND"
95 #define MSG_NOTFOUND "NOT_FOUND\r\n"
96 #define MSG_RESERVED "RESERVED"
97 #define MSG_DEADLINE_SOON "DEADLINE_SOON\r\n"
98 #define MSG_TIMED_OUT "TIMED_OUT\r\n"
99 #define MSG_DELETED "DELETED\r\n"
100 #define MSG_RELEASED "RELEASED\r\n"
101 #define MSG_BURIED "BURIED\r\n"
102 #define MSG_TOUCHED "TOUCHED\r\n"
103 #define MSG_BURIED_FMT "BURIED %llu\r\n"
104 #define MSG_INSERTED_FMT "INSERTED %llu\r\n"
105 #define MSG_NOT_IGNORED "NOT_IGNORED\r\n"
107 #define MSG_NOTFOUND_LEN CONSTSTRLEN(MSG_NOTFOUND)
108 #define MSG_DELETED_LEN CONSTSTRLEN(MSG_DELETED)
109 #define MSG_TOUCHED_LEN CONSTSTRLEN(MSG_TOUCHED)
110 #define MSG_RELEASED_LEN CONSTSTRLEN(MSG_RELEASED)
111 #define MSG_BURIED_LEN CONSTSTRLEN(MSG_BURIED)
112 #define MSG_NOT_IGNORED_LEN CONSTSTRLEN(MSG_NOT_IGNORED)
114 #define MSG_OUT_OF_MEMORY "OUT_OF_MEMORY\r\n"
115 #define MSG_INTERNAL_ERROR "INTERNAL_ERROR\r\n"
116 #define MSG_DRAINING "DRAINING\r\n"
117 #define MSG_BAD_FORMAT "BAD_FORMAT\r\n"
118 #define MSG_UNKNOWN_COMMAND "UNKNOWN_COMMAND\r\n"
119 #define MSG_EXPECTED_CRLF "EXPECTED_CRLF\r\n"
120 #define MSG_JOB_TOO_BIG "JOB_TOO_BIG\r\n"
122 #define STATE_WANTCOMMAND 0
123 #define STATE_WANTDATA 1
124 #define STATE_SENDJOB 2
125 #define STATE_SENDWORD 3
127 #define STATE_BITBUCKET 5
138 #define OP_JOBSTATS 9
139 #define OP_PEEK_BURIED 10
143 #define OP_LIST_TUBES 14
144 #define OP_LIST_TUBE_USED 15
145 #define OP_LIST_TUBES_WATCHED 16
146 #define OP_STATS_TUBE 17
147 #define OP_PEEK_READY 18
148 #define OP_PEEK_DELAYED 19
149 #define OP_RESERVE_TIMEOUT 20
153 #define STATS_FMT "---\n" \
154 "current-jobs-urgent: %u\n" \
155 "current-jobs-ready: %u\n" \
156 "current-jobs-reserved: %u\n" \
157 "current-jobs-delayed: %u\n" \
158 "current-jobs-buried: %u\n" \
161 "cmd-peek-ready: %llu\n" \
162 "cmd-peek-delayed: %llu\n" \
163 "cmd-peek-buried: %llu\n" \
164 "cmd-reserve: %llu\n" \
165 "cmd-reserve-with-timeout: %llu\n" \
166 "cmd-delete: %llu\n" \
167 "cmd-release: %llu\n" \
169 "cmd-watch: %llu\n" \
170 "cmd-ignore: %llu\n" \
173 "cmd-touch: %llu\n" \
174 "cmd-stats: %llu\n" \
175 "cmd-stats-job: %llu\n" \
176 "cmd-stats-tube: %llu\n" \
177 "cmd-list-tubes: %llu\n" \
178 "cmd-list-tube-used: %llu\n" \
179 "cmd-list-tubes-watched: %llu\n" \
180 "job-timeouts: %llu\n" \
181 "total-jobs: %llu\n" \
182 "max-job-size: %zu\n" \
183 "current-tubes: %zu\n" \
184 "current-connections: %u\n" \
185 "current-producers: %u\n" \
186 "current-workers: %u\n" \
187 "current-waiting: %u\n" \
188 "total-connections: %u\n" \
191 "rusage-utime: %d.%06d\n" \
192 "rusage-stime: %d.%06d\n" \
194 "binlog-oldest-index: %s\n" \
195 "binlog-current-index: %s\n" \
196 "binlog-max-size: %zu\n" \
199 #define STATS_TUBE_FMT "---\n" \
201 "current-jobs-urgent: %u\n" \
202 "current-jobs-ready: %u\n" \
203 "current-jobs-reserved: %u\n" \
204 "current-jobs-delayed: %u\n" \
205 "current-jobs-buried: %u\n" \
206 "total-jobs: %llu\n" \
207 "current-using: %u\n" \
208 "current-watching: %u\n" \
209 "current-waiting: %u\n" \
212 #define JOB_STATS_FMT "---\n" \
228 /* this number is pretty arbitrary */
229 #define BUCKET_BUF_SIZE 1024
231 static char bucket
[BUCKET_BUF_SIZE
];
233 static unsigned int ready_ct
= 0;
234 static struct stats global_stat
= {0, 0, 0, 0, 0};
236 static tube default_tube
;
238 static int drain_mode
= 0;
239 static time_t start_time
;
240 static unsigned long long int op_ct
[TOTAL_OPS
], timeout_ct
= 0;
243 /* Doubly-linked list of connections with at least one reserved job. */
244 static struct conn running
= { &running
, &running
, 0 };
247 static const char * op_names
[] = {
264 CMD_LIST_TUBES_WATCHED
,
273 static job
remove_buried_job(job j
);
278 return job_list_any_p(&t
->buried
);
282 reply(conn c
, const char *line
, int len
, int state
)
288 r
= conn_update_evq(c
, EV_WRITE
| EV_PERSIST
);
289 if (r
== -1) return twarnx("conn_update_evq() failed"), conn_close(c
);
295 dprintf("sending reply: %.*s", len
, line
);
298 #define reply_msg(c,m) reply((c),(m),CONSTSTRLEN(m),STATE_SENDWORD)
300 #define reply_serr(c,e) (twarnx("server error: %s",(e)),\
304 reply_line(conn c
, int state
, const char *fmt
, ...)
310 r
= vsnprintf(c
->reply_buf
, LINE_BUF_SIZE
, fmt
, ap
);
313 /* Make sure the buffer was big enough. If not, we have a bug. */
314 if (r
>= LINE_BUF_SIZE
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
316 return reply(c
, c
->reply_buf
, r
, state
);
320 reply_job(conn c
, job j
, const char *word
)
322 /* tell this connection which job to send */
326 return reply_line(c
, STATE_SENDJOB
, "%s %llu %u\r\n",
327 word
, j
->id
, j
->body_size
- 2);
331 remove_waiting_conn(conn c
)
336 if (!conn_waiting(c
)) return NULL
;
338 c
->type
&= ~CONN_TYPE_WAITING
;
339 global_stat
.waiting_ct
--;
340 for (i
= 0; i
< c
->watch
.used
; i
++) {
341 t
= c
->watch
.items
[i
];
342 t
->stat
.waiting_ct
--;
343 ms_remove(&t
->waiting
, c
);
349 reserve_job(conn c
, job j
)
351 j
->deadline
= time(NULL
) + j
->ttr
;
352 global_stat
.reserved_ct
++; /* stats */
353 j
->tube
->stat
.reserved_ct
++;
355 conn_insert(&running
, c
);
356 j
->state
= JOB_STATE_RESERVED
;
357 job_insert(&c
->reserved_jobs
, j
);
359 if (c
->soonest_job
&& j
->deadline
< c
->soonest_job
->deadline
) {
362 return reply_job(c
, j
, MSG_RESERVED
);
370 job j
= NULL
, candidate
;
372 dprintf("tubes.used = %zu\n", tubes
.used
);
373 for (i
= 0; i
< tubes
.used
; i
++) {
375 dprintf("for %s t->waiting.used=%zu t->ready.used=%d\n",
376 t
->name
, t
->waiting
.used
, t
->ready
.used
);
377 if (t
->waiting
.used
&& t
->ready
.used
) {
378 candidate
= pq_peek(&t
->ready
);
379 if (!j
|| job_pri_cmp(candidate
, j
) < 0) j
= candidate
;
381 dprintf("i = %zu, tubes.used = %zu\n", i
, tubes
.used
);
392 dprintf("processing queue\n");
393 while ((j
= next_eligible_job())) {
394 dprintf("got eligible job %llu in %s\n", j
->id
, j
->tube
->name
);
395 j
= pq_take(&j
->tube
->ready
);
397 if (j
->pri
< URGENT_THRESHOLD
) {
398 global_stat
.urgent_ct
--;
399 j
->tube
->stat
.urgent_ct
--;
401 reserve_job(remove_waiting_conn(ms_take(&j
->tube
->waiting
)), j
);
412 for (i
= 0; i
< tubes
.used
; i
++) {
414 nj
= pq_peek(&t
->delay
);
416 if (!j
|| nj
->deadline
< j
->deadline
) j
= nj
;
423 set_main_delay_timeout()
427 set_main_timeout((j
= delay_q_peek()) ? j
->deadline
: 0);
431 enqueue_job(job j
, unsigned int delay
, char update_store
)
437 j
->deadline
= time(NULL
) + delay
;
438 r
= pq_give(&j
->tube
->delay
, j
);
440 j
->state
= JOB_STATE_DELAYED
;
441 set_main_delay_timeout();
443 r
= pq_give(&j
->tube
->ready
, j
);
445 j
->state
= JOB_STATE_READY
;
447 if (j
->pri
< URGENT_THRESHOLD
) {
448 global_stat
.urgent_ct
++;
449 j
->tube
->stat
.urgent_ct
++;
454 r
= binlog_write_job(j
);
463 bury_job(job j
, char update_store
)
468 z
= binlog_reserve_space_update(j
);
470 j
->reserved_binlog_space
+= z
;
473 job_insert(&j
->tube
->buried
, j
);
474 global_stat
.buried_ct
++;
475 j
->tube
->stat
.buried_ct
++;
476 j
->state
= JOB_STATE_BURIED
;
480 if (update_store
) return binlog_write_job(j
);
486 enqueue_reserved_jobs(conn c
)
491 while (job_list_any_p(&c
->reserved_jobs
)) {
492 j
= job_remove(c
->reserved_jobs
.next
);
493 r
= enqueue_job(j
, 0, 0);
494 if (r
< 1) bury_job(j
, 0);
495 global_stat
.reserved_ct
--;
496 j
->tube
->stat
.reserved_ct
--;
497 c
->soonest_job
= NULL
;
498 if (!job_list_any_p(&c
->reserved_jobs
)) conn_remove(c
);
505 job j
= delay_q_peek();
506 return j
? pq_take(&j
->tube
->delay
) : NULL
;
510 kick_buried_job(tube t
)
516 if (!buried_job_p(t
)) return 0;
517 j
= remove_buried_job(t
->buried
.next
);
519 z
= binlog_reserve_space_update(j
);
520 if (!z
) return pq_give(&t
->delay
, j
), 0; /* put it back */
521 j
->reserved_binlog_space
+= z
;
524 r
= enqueue_job(j
, 0, 1);
525 if (r
== 1) return 1;
527 /* ready queue is full, so bury it */
537 unsigned int count
= 0;
539 for (i
= 0; i
< tubes
.used
; i
++) {
541 count
+= t
->delay
.used
;
547 kick_delayed_job(tube t
)
553 j
= pq_take(&t
->delay
);
556 z
= binlog_reserve_space_update(j
);
557 if (!z
) return pq_give(&t
->delay
, j
), 0; /* put it back */
558 j
->reserved_binlog_space
+= z
;
561 r
= enqueue_job(j
, 0, 1);
562 if (r
== 1) return 1;
564 /* ready queue is full, so delay it again */
565 r
= enqueue_job(j
, j
->delay
, 0);
566 if (r
== 1) return 0;
573 /* return the number of jobs successfully kicked */
575 kick_buried_jobs(tube t
, unsigned int n
)
578 for (i
= 0; (i
< n
) && kick_buried_job(t
); ++i
);
582 /* return the number of jobs successfully kicked */
584 kick_delayed_jobs(tube t
, unsigned int n
)
587 for (i
= 0; (i
< n
) && kick_delayed_job(t
); ++i
);
592 kick_jobs(tube t
, unsigned int n
)
594 if (buried_job_p(t
)) return kick_buried_jobs(t
, n
);
595 return kick_delayed_jobs(t
, n
);
599 remove_buried_job(job j
)
601 if (!j
|| j
->state
!= JOB_STATE_BURIED
) return NULL
;
604 global_stat
.buried_ct
--;
605 j
->tube
->stat
.buried_ct
--;
611 remove_ready_job(job j
)
613 if (!j
|| j
->state
!= JOB_STATE_READY
) return NULL
;
614 j
= pq_remove(&j
->tube
->ready
, j
);
617 if (j
->pri
< URGENT_THRESHOLD
) {
618 global_stat
.urgent_ct
--;
619 j
->tube
->stat
.urgent_ct
--;
626 enqueue_waiting_conn(conn c
)
631 global_stat
.waiting_ct
++;
632 c
->type
|= CONN_TYPE_WAITING
;
633 for (i
= 0; i
< c
->watch
.used
; i
++) {
634 t
= c
->watch
.items
[i
];
635 t
->stat
.waiting_ct
++;
636 ms_append(&t
->waiting
, c
);
641 find_reserved_job_in_conn(conn c
, job j
)
643 return (j
&& j
->reserver
== c
&& j
->state
== JOB_STATE_RESERVED
) ? j
: NULL
;
647 touch_job(conn c
, job j
)
649 j
= find_reserved_job_in_conn(c
, j
);
651 j
->deadline
= time(NULL
) + j
->ttr
;
652 c
->soonest_job
= NULL
;
658 peek_job(unsigned long long int id
)
664 check_err(conn c
, const char *s
)
666 if (errno
== EAGAIN
) return;
667 if (errno
== EINTR
) return;
668 if (errno
== EWOULDBLOCK
) return;
675 /* Scan the given string for the sequence "\r\n" and return the line length.
676 * Always returns at least 2 if a match is found. Returns 0 if no match. */
678 scan_line_end(const char *s
, int size
)
682 match
= memchr(s
, '\r', size
- 1);
683 if (!match
) return 0;
685 /* this is safe because we only scan size - 1 chars above */
686 if (match
[1] == '\n') return match
- s
+ 2;
694 return scan_line_end(c
->cmd
, c
->cmd_read
);
697 /* parse the command line */
701 #define TEST_CMD(s,c,o) if (strncmp((s), (c), CONSTSTRLEN(c)) == 0) return (o);
702 TEST_CMD(c
->cmd
, CMD_PUT
, OP_PUT
);
703 TEST_CMD(c
->cmd
, CMD_PEEKJOB
, OP_PEEKJOB
);
704 TEST_CMD(c
->cmd
, CMD_PEEK_READY
, OP_PEEK_READY
);
705 TEST_CMD(c
->cmd
, CMD_PEEK_DELAYED
, OP_PEEK_DELAYED
);
706 TEST_CMD(c
->cmd
, CMD_PEEK_BURIED
, OP_PEEK_BURIED
);
707 TEST_CMD(c
->cmd
, CMD_RESERVE_TIMEOUT
, OP_RESERVE_TIMEOUT
);
708 TEST_CMD(c
->cmd
, CMD_RESERVE
, OP_RESERVE
);
709 TEST_CMD(c
->cmd
, CMD_DELETE
, OP_DELETE
);
710 TEST_CMD(c
->cmd
, CMD_RELEASE
, OP_RELEASE
);
711 TEST_CMD(c
->cmd
, CMD_BURY
, OP_BURY
);
712 TEST_CMD(c
->cmd
, CMD_KICK
, OP_KICK
);
713 TEST_CMD(c
->cmd
, CMD_TOUCH
, OP_TOUCH
);
714 TEST_CMD(c
->cmd
, CMD_JOBSTATS
, OP_JOBSTATS
);
715 TEST_CMD(c
->cmd
, CMD_STATS_TUBE
, OP_STATS_TUBE
);
716 TEST_CMD(c
->cmd
, CMD_STATS
, OP_STATS
);
717 TEST_CMD(c
->cmd
, CMD_USE
, OP_USE
);
718 TEST_CMD(c
->cmd
, CMD_WATCH
, OP_WATCH
);
719 TEST_CMD(c
->cmd
, CMD_IGNORE
, OP_IGNORE
);
720 TEST_CMD(c
->cmd
, CMD_LIST_TUBES_WATCHED
, OP_LIST_TUBES_WATCHED
);
721 TEST_CMD(c
->cmd
, CMD_LIST_TUBE_USED
, OP_LIST_TUBE_USED
);
722 TEST_CMD(c
->cmd
, CMD_LIST_TUBES
, OP_LIST_TUBES
);
726 /* Copy up to body_size trailing bytes into the job, then the rest into the cmd
727 * buffer. If c->in_job exists, this assumes that c->in_job->body is empty.
728 * This function is idempotent(). */
730 fill_extra_data(conn c
)
732 int extra_bytes
, job_data_bytes
= 0, cmd_bytes
;
734 if (!c
->fd
) return; /* the connection was closed */
735 if (!c
->cmd_len
) return; /* we don't have a complete command */
737 /* how many extra bytes did we read? */
738 extra_bytes
= c
->cmd_read
- c
->cmd_len
;
740 /* how many bytes should we put into the job body? */
742 job_data_bytes
= min(extra_bytes
, c
->in_job
->body_size
);
743 memcpy(c
->in_job
->body
, c
->cmd
+ c
->cmd_len
, job_data_bytes
);
744 c
->in_job_read
= job_data_bytes
;
745 } else if (c
->in_job_read
) {
746 /* we are in bit-bucket mode, throwing away data */
747 job_data_bytes
= min(extra_bytes
, c
->in_job_read
);
748 c
->in_job_read
-= job_data_bytes
;
751 /* how many bytes are left to go into the future cmd? */
752 cmd_bytes
= extra_bytes
- job_data_bytes
;
753 memmove(c
->cmd
, c
->cmd
+ c
->cmd_len
+ job_data_bytes
, cmd_bytes
);
754 c
->cmd_read
= cmd_bytes
;
755 c
->cmd_len
= 0; /* we no longer know the length of the new command */
759 enqueue_incoming_job(conn c
)
764 c
->in_job
= NULL
; /* the connection no longer owns this job */
767 /* check if the trailer is present and correct */
768 if (memcmp(j
->body
+ j
->body_size
- 2, "\r\n", 2)) {
770 return reply_msg(c
, MSG_EXPECTED_CRLF
);
775 return reply_serr(c
, MSG_DRAINING
);
778 if (j
->reserved_binlog_space
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
779 j
->reserved_binlog_space
= binlog_reserve_space_put(j
);
780 if (!j
->reserved_binlog_space
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
782 /* we have a complete job, so let's stick it in the pqueue */
783 r
= enqueue_job(j
, j
->delay
, 1);
784 if (r
< 0) return reply_serr(c
, MSG_INTERNAL_ERROR
);
786 op_ct
[OP_PUT
]++; /* stats */
787 global_stat
.total_jobs_ct
++;
788 j
->tube
->stat
.total_jobs_ct
++;
790 if (r
== 1) return reply_line(c
, STATE_SENDWORD
, MSG_INSERTED_FMT
, j
->id
);
792 /* out of memory trying to grow the queue, so it gets buried */
794 reply_line(c
, STATE_SENDWORD
, MSG_BURIED_FMT
, j
->id
);
800 return time(NULL
) - start_time
;
804 fmt_stats(char *buf
, size_t size
, void *x
)
806 struct rusage ru
= {{0, 0}, {0, 0}};
807 getrusage(RUSAGE_SELF
, &ru
); /* don't care if it fails */
808 return snprintf(buf
, size
, STATS_FMT
,
809 global_stat
.urgent_ct
,
811 global_stat
.reserved_ct
,
812 get_delayed_job_ct(),
813 global_stat
.buried_ct
,
816 op_ct
[OP_PEEK_READY
],
817 op_ct
[OP_PEEK_DELAYED
],
818 op_ct
[OP_PEEK_BURIED
],
820 op_ct
[OP_RESERVE_TIMEOUT
],
831 op_ct
[OP_STATS_TUBE
],
832 op_ct
[OP_LIST_TUBES
],
833 op_ct
[OP_LIST_TUBE_USED
],
834 op_ct
[OP_LIST_TUBES_WATCHED
],
836 global_stat
.total_jobs_ct
,
840 count_cur_producers(),
842 global_stat
.waiting_ct
,
846 (int) ru
.ru_utime
.tv_sec
, (int) ru
.ru_utime
.tv_usec
,
847 (int) ru
.ru_stime
.tv_sec
, (int) ru
.ru_stime
.tv_usec
,
849 binlog_oldest_index(),
850 binlog_current_index(),
855 /* Read a priority value from the given buffer and place it in pri.
856 * Update end to point to the address after the last character consumed.
857 * Pri and end can be NULL. If they are both NULL, read_pri() will do the
858 * conversion and return the status code but not update any values. This is an
859 * easy way to check for errors.
860 * If end is NULL, read_pri will also check that the entire input string was
861 * consumed and return an error code otherwise.
862 * Return 0 on success, or nonzero on failure.
863 * If a failure occurs, pri and end are not modified. */
865 read_pri(unsigned int *pri
, const char *buf
, char **end
)
871 while (buf
[0] == ' ') buf
++;
872 if (!isdigit(buf
[0])) return -1;
873 tpri
= strtoul(buf
, &tend
, 10);
874 if (tend
== buf
) return -1;
875 if (errno
&& errno
!= ERANGE
) return -1;
876 if (!end
&& tend
[0] != '\0') return -1;
878 if (pri
) *pri
= tpri
;
879 if (end
) *end
= tend
;
883 /* Read a delay value from the given buffer and place it in delay.
884 * The interface and behavior are the same as in read_pri(). */
886 read_delay(unsigned int *delay
, const char *buf
, char **end
)
888 time_t now
= time(NULL
);
889 return read_pri(delay
, buf
, end
) ? : (now
+ *delay
< now
);
892 /* Read a timeout value from the given buffer and place it in ttr.
893 * The interface and behavior are the same as in read_pri(). */
895 read_ttr(unsigned int *ttr
, const char *buf
, char **end
)
897 return read_pri(ttr
, buf
, end
);
901 wait_for_job(conn c
, int timeout
)
905 c
->state
= STATE_WAIT
;
906 enqueue_waiting_conn(c
);
908 /* Set the pending timeout to the requested timeout amount */
909 c
->pending_timeout
= timeout
;
911 /* this conn is waiting, but we want to know if they hang up */
912 r
= conn_update_evq(c
, EV_READ
| EV_PERSIST
);
913 if (r
== -1) return twarnx("update events failed"), conn_close(c
);
916 typedef int(*fmt_fn
)(char *, size_t, void *);
919 do_stats(conn c
, fmt_fn fmt
, void *data
)
923 /* first, measure how big a buffer we will need */
924 stats_len
= fmt(NULL
, 0, data
) + 16;
926 c
->out_job
= allocate_job(stats_len
); /* fake job to hold stats data */
927 if (!c
->out_job
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
929 /* now actually format the stats data */
930 r
= fmt(c
->out_job
->body
, stats_len
, data
);
931 /* and set the actual body size */
932 c
->out_job
->body_size
= r
;
933 if (r
> stats_len
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
936 return reply_line(c
, STATE_SENDJOB
, "OK %d\r\n", r
- 2);
940 do_list_tubes(conn c
, ms l
)
946 /* first, measure how big a buffer we will need */
947 resp_z
= 6; /* initial "---\n" and final "\r\n" */
948 for (i
= 0; i
< l
->used
; i
++) {
950 resp_z
+= 3 + strlen(t
->name
); /* including "- " and "\n" */
953 c
->out_job
= allocate_job(resp_z
); /* fake job to hold response data */
954 if (!c
->out_job
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
956 /* now actually format the response */
957 buf
= c
->out_job
->body
;
958 buf
+= snprintf(buf
, 5, "---\n");
959 for (i
= 0; i
< l
->used
; i
++) {
961 buf
+= snprintf(buf
, 4 + strlen(t
->name
), "- %s\n", t
->name
);
967 return reply_line(c
, STATE_SENDJOB
, "OK %d\r\n", resp_z
- 2);
971 fmt_job_stats(char *buf
, size_t size
, job j
)
976 return snprintf(buf
, size
, JOB_STATS_FMT
,
981 (unsigned int) (t
- j
->creation
),
984 (unsigned int) (j
->deadline
- t
),
993 fmt_stats_tube(char *buf
, size_t size
, tube t
)
995 return snprintf(buf
, size
, STATS_TUBE_FMT
,
1002 t
->stat
.total_jobs_ct
,
1005 t
->stat
.waiting_ct
);
1009 maybe_enqueue_incoming_job(conn c
)
1013 /* do we have a complete job? */
1014 if (c
->in_job_read
== j
->body_size
) return enqueue_incoming_job(c
);
1016 /* otherwise we have incomplete data, so just keep waiting */
1017 c
->state
= STATE_WANTDATA
;
1022 remove_this_reserved_job(conn c
, job j
)
1026 global_stat
.reserved_ct
--;
1027 j
->tube
->stat
.reserved_ct
--;
1030 c
->soonest_job
= NULL
;
1031 if (!job_list_any_p(&c
->reserved_jobs
)) conn_remove(c
);
1036 remove_reserved_job(conn c
, job j
)
1038 return remove_this_reserved_job(c
, find_reserved_job_in_conn(c
, j
));
1042 name_is_ok(const char *name
, size_t max
)
1044 size_t len
= strlen(name
);
1045 return len
> 0 && len
<= max
&&
1046 strspn(name
, NAME_CHARS
) == len
&& name
[0] != '-';
1050 prot_remove_tube(tube t
)
1052 ms_remove(&tubes
, t
);
1056 dispatch_cmd(conn c
)
1058 int r
, i
, timeout
= -1;
1063 char *size_buf
, *delay_buf
, *ttr_buf
, *pri_buf
, *end_buf
, *name
;
1064 unsigned int pri
, delay
, ttr
, body_size
;
1065 unsigned long long int id
;
1068 /* NUL-terminate this string so we can use strtol and friends */
1069 c
->cmd
[c
->cmd_len
- 2] = '\0';
1071 /* check for possible maliciousness */
1072 if (strlen(c
->cmd
) != c
->cmd_len
- 2) {
1073 return reply_msg(c
, MSG_BAD_FORMAT
);
1076 type
= which_cmd(c
);
1077 dprintf("got %s command: \"%s\"\n", op_names
[(int) type
], c
->cmd
);
1081 r
= read_pri(&pri
, c
->cmd
+ 4, &delay_buf
);
1082 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1084 r
= read_delay(&delay
, delay_buf
, &ttr_buf
);
1085 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1087 r
= read_ttr(&ttr
, ttr_buf
, &size_buf
);
1088 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1091 body_size
= strtoul(size_buf
, &end_buf
, 10);
1092 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1094 if (body_size
> job_data_size_limit
) {
1095 return reply_msg(c
, MSG_JOB_TOO_BIG
);
1098 /* don't allow trailing garbage */
1099 if (end_buf
[0] != '\0') return reply_msg(c
, MSG_BAD_FORMAT
);
1101 conn_set_producer(c
);
1103 c
->in_job
= make_job(pri
, delay
, ttr
? : 1, body_size
+ 2, c
->use
);
1107 /* throw away the job body and respond with OUT_OF_MEMORY */
1109 /* Invert the meaning of in_job_read while throwing away data -- it
1110 * counts the bytes that remain to be thrown away. */
1111 c
->in_job_read
= body_size
+ 2;
1114 if (c
->in_job_read
== 0) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1116 c
->state
= STATE_BITBUCKET
;
1122 /* it's possible we already have a complete job */
1123 maybe_enqueue_incoming_job(c
);
1127 /* don't allow trailing garbage */
1128 if (c
->cmd_len
!= CMD_PEEK_READY_LEN
+ 2) {
1129 return reply_msg(c
, MSG_BAD_FORMAT
);
1133 j
= job_copy(pq_peek(&c
->use
->ready
));
1135 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1137 reply_job(c
, j
, MSG_FOUND
);
1139 case OP_PEEK_DELAYED
:
1140 /* don't allow trailing garbage */
1141 if (c
->cmd_len
!= CMD_PEEK_DELAYED_LEN
+ 2) {
1142 return reply_msg(c
, MSG_BAD_FORMAT
);
1146 j
= job_copy(pq_peek(&c
->use
->delay
));
1148 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1150 reply_job(c
, j
, MSG_FOUND
);
1152 case OP_PEEK_BURIED
:
1153 /* don't allow trailing garbage */
1154 if (c
->cmd_len
!= CMD_PEEK_BURIED_LEN
+ 2) {
1155 return reply_msg(c
, MSG_BAD_FORMAT
);
1159 j
= job_copy(buried_job_p(c
->use
)? j
= c
->use
->buried
.next
: NULL
);
1161 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1163 reply_job(c
, j
, MSG_FOUND
);
1167 id
= strtoull(c
->cmd
+ CMD_PEEKJOB_LEN
, &end_buf
, 10);
1168 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1171 /* So, peek is annoying, because some other connection might free the
1172 * job while we are still trying to write it out. So we copy it and
1173 * then free the copy when it's done sending. */
1174 j
= job_copy(peek_job(id
));
1176 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1178 reply_job(c
, j
, MSG_FOUND
);
1180 case OP_RESERVE_TIMEOUT
:
1182 timeout
= strtol(c
->cmd
+ CMD_RESERVE_TIMEOUT_LEN
, &end_buf
, 10);
1183 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1184 case OP_RESERVE
: /* FALLTHROUGH */
1185 /* don't allow trailing garbage */
1186 if (type
== OP_RESERVE
&& c
->cmd_len
!= CMD_RESERVE_LEN
+ 2) {
1187 return reply_msg(c
, MSG_BAD_FORMAT
);
1193 if (conn_has_close_deadline(c
) && !conn_ready(c
)) {
1194 return reply_msg(c
, MSG_DEADLINE_SOON
);
1197 /* try to get a new job for this guy */
1198 wait_for_job(c
, timeout
);
1203 id
= strtoull(c
->cmd
+ CMD_DELETE_LEN
, &end_buf
, 10);
1204 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1208 j
= remove_reserved_job(c
, j
) ? :
1209 remove_ready_job(j
) ? :
1210 remove_buried_job(j
);
1212 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1214 j
->state
= JOB_STATE_INVALID
;
1215 r
= binlog_write_job(j
);
1218 if (!r
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
1220 reply(c
, MSG_DELETED
, MSG_DELETED_LEN
, STATE_SENDWORD
);
1224 id
= strtoull(c
->cmd
+ CMD_RELEASE_LEN
, &pri_buf
, 10);
1225 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1227 r
= read_pri(&pri
, pri_buf
, &delay_buf
);
1228 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1230 r
= read_delay(&delay
, delay_buf
, NULL
);
1231 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1234 j
= remove_reserved_job(c
, job_find(id
));
1236 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1238 /* We want to update the delay deadline on disk, so reserve space for
1241 z
= binlog_reserve_space_update(j
);
1242 if (!z
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1243 j
->reserved_binlog_space
+= z
;
1250 r
= enqueue_job(j
, delay
, !!delay
);
1251 if (r
< 0) return reply_serr(c
, MSG_INTERNAL_ERROR
);
1253 return reply(c
, MSG_RELEASED
, MSG_RELEASED_LEN
, STATE_SENDWORD
);
1256 /* out of memory trying to grow the queue, so it gets buried */
1258 reply(c
, MSG_BURIED
, MSG_BURIED_LEN
, STATE_SENDWORD
);
1262 id
= strtoull(c
->cmd
+ CMD_BURY_LEN
, &pri_buf
, 10);
1263 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1265 r
= read_pri(&pri
, pri_buf
, NULL
);
1266 if (r
) return reply_msg(c
, MSG_BAD_FORMAT
);
1269 j
= remove_reserved_job(c
, job_find(id
));
1271 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1275 if (!r
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
1276 reply(c
, MSG_BURIED
, MSG_BURIED_LEN
, STATE_SENDWORD
);
1280 count
= strtoul(c
->cmd
+ CMD_KICK_LEN
, &end_buf
, 10);
1281 if (end_buf
== c
->cmd
+ CMD_KICK_LEN
) {
1282 return reply_msg(c
, MSG_BAD_FORMAT
);
1284 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1288 i
= kick_jobs(c
->use
, count
);
1290 return reply_line(c
, STATE_SENDWORD
, "KICKED %u\r\n", i
);
1293 id
= strtoull(c
->cmd
+ CMD_TOUCH_LEN
, &end_buf
, 10);
1294 if (errno
) return twarn("strtoull"), reply_msg(c
, MSG_BAD_FORMAT
);
1298 j
= touch_job(c
, job_find(id
));
1301 reply(c
, MSG_TOUCHED
, MSG_TOUCHED_LEN
, STATE_SENDWORD
);
1303 return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1307 /* don't allow trailing garbage */
1308 if (c
->cmd_len
!= CMD_STATS_LEN
+ 2) {
1309 return reply_msg(c
, MSG_BAD_FORMAT
);
1314 do_stats(c
, fmt_stats
, NULL
);
1318 id
= strtoull(c
->cmd
+ CMD_JOBSTATS_LEN
, &end_buf
, 10);
1319 if (errno
) return reply_msg(c
, MSG_BAD_FORMAT
);
1324 if (!j
) return reply(c
, MSG_NOTFOUND
, MSG_NOTFOUND_LEN
, STATE_SENDWORD
);
1326 if (!j
->tube
) return reply_serr(c
, MSG_INTERNAL_ERROR
);
1327 do_stats(c
, (fmt_fn
) fmt_job_stats
, j
);
1330 name
= c
->cmd
+ CMD_STATS_TUBE_LEN
;
1331 if (!name_is_ok(name
, 200)) return reply_msg(c
, MSG_BAD_FORMAT
);
1335 t
= tube_find(name
);
1336 if (!t
) return reply_msg(c
, MSG_NOTFOUND
);
1338 do_stats(c
, (fmt_fn
) fmt_stats_tube
, t
);
1342 /* don't allow trailing garbage */
1343 if (c
->cmd_len
!= CMD_LIST_TUBES_LEN
+ 2) {
1344 return reply_msg(c
, MSG_BAD_FORMAT
);
1348 do_list_tubes(c
, &tubes
);
1350 case OP_LIST_TUBE_USED
:
1351 /* don't allow trailing garbage */
1352 if (c
->cmd_len
!= CMD_LIST_TUBE_USED_LEN
+ 2) {
1353 return reply_msg(c
, MSG_BAD_FORMAT
);
1357 reply_line(c
, STATE_SENDWORD
, "USING %s\r\n", c
->use
->name
);
1359 case OP_LIST_TUBES_WATCHED
:
1360 /* don't allow trailing garbage */
1361 if (c
->cmd_len
!= CMD_LIST_TUBES_WATCHED_LEN
+ 2) {
1362 return reply_msg(c
, MSG_BAD_FORMAT
);
1366 do_list_tubes(c
, &c
->watch
);
1369 name
= c
->cmd
+ CMD_USE_LEN
;
1370 if (!name_is_ok(name
, 200)) return reply_msg(c
, MSG_BAD_FORMAT
);
1373 TUBE_ASSIGN(t
, tube_find_or_make(name
));
1374 if (!t
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1377 TUBE_ASSIGN(c
->use
, t
);
1378 TUBE_ASSIGN(t
, NULL
);
1381 reply_line(c
, STATE_SENDWORD
, "USING %s\r\n", c
->use
->name
);
1384 name
= c
->cmd
+ CMD_WATCH_LEN
;
1385 if (!name_is_ok(name
, 200)) return reply_msg(c
, MSG_BAD_FORMAT
);
1388 TUBE_ASSIGN(t
, tube_find_or_make(name
));
1389 if (!t
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1392 if (!ms_contains(&c
->watch
, t
)) r
= ms_append(&c
->watch
, t
);
1393 TUBE_ASSIGN(t
, NULL
);
1394 if (!r
) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1396 reply_line(c
, STATE_SENDWORD
, "WATCHING %d\r\n", c
->watch
.used
);
1399 name
= c
->cmd
+ CMD_IGNORE_LEN
;
1400 if (!name_is_ok(name
, 200)) return reply_msg(c
, MSG_BAD_FORMAT
);
1404 for (i
= 0; i
< c
->watch
.used
; i
++) {
1405 t
= c
->watch
.items
[i
];
1406 if (strncmp(t
->name
, name
, MAX_TUBE_NAME_LEN
) == 0) break;
1410 if (t
&& c
->watch
.used
< 2) return reply_msg(c
, MSG_NOT_IGNORED
);
1412 if (t
) ms_remove(&c
->watch
, t
); /* may free t if refcount => 0 */
1415 reply_line(c
, STATE_SENDWORD
, "WATCHING %d\r\n", c
->watch
.used
);
1418 return reply_msg(c
, MSG_UNKNOWN_COMMAND
);
1422 /* There are three reasons this function may be called. We need to check for
1425 * 1. A reserved job has run out of time.
1426 * 2. A waiting client's reserved job has entered the safety margin.
1427 * 3. A waiting client's requested timeout has occurred.
1429 * If any of these happen, we must do the appropriate thing. */
1431 h_conn_timeout(conn c
)
1433 int r
, should_timeout
= 0;
1436 /* Check if the client was trying to reserve a job. */
1437 if (conn_waiting(c
) && conn_has_close_deadline(c
)) should_timeout
= 1;
1439 /* Check if any reserved jobs have run out of time. We should do this
1440 * whether or not the client is waiting for a new reservation. */
1441 while ((j
= soonest_job(c
))) {
1442 if (j
->deadline
>= time(NULL
)) break;
1444 /* This job is in the middle of being written out. If we return it to
1445 * the ready queue, someone might free it before we finish writing it
1446 * out to the socket. So we'll copy it here and free the copy when it's
1448 if (j
== c
->out_job
) {
1449 c
->out_job
= job_copy(c
->out_job
);
1452 timeout_ct
++; /* stats */
1454 r
= enqueue_job(remove_this_reserved_job(c
, j
), 0, 0);
1455 if (r
< 1) bury_job(j
, 0); /* out of memory, so bury it */
1456 r
= conn_update_evq(c
, c
->evq
.ev_events
);
1457 if (r
== -1) return twarnx("conn_update_evq() failed"), conn_close(c
);
1460 if (should_timeout
) {
1461 dprintf("conn_waiting(%p) = %d\n", c
, conn_waiting(c
));
1462 return reply_msg(remove_waiting_conn(c
), MSG_DEADLINE_SOON
);
1463 } else if (conn_waiting(c
) && c
->pending_timeout
>= 0) {
1464 dprintf("conn_waiting(%p) = %d\n", c
, conn_waiting(c
));
1465 c
->pending_timeout
= -1;
1466 return reply_msg(remove_waiting_conn(c
), MSG_TIMED_OUT
);
1471 enter_drain_mode(int sig
)
1488 r
= conn_update_evq(c
, EV_READ
| EV_PERSIST
);
1489 if (r
== -1) return twarnx("update events failed"), conn_close(c
);
1491 /* was this a peek or stats command? */
1492 if (c
->out_job
&& c
->out_job
->state
== JOB_STATE_COPY
) job_free(c
->out_job
);
1495 c
->reply_sent
= 0; /* now that we're done, reset this */
1496 c
->state
= STATE_WANTCOMMAND
;
1504 struct iovec iov
[2];
1507 case STATE_WANTCOMMAND
:
1508 r
= read(c
->fd
, c
->cmd
+ c
->cmd_read
, LINE_BUF_SIZE
- c
->cmd_read
);
1509 if (r
== -1) return check_err(c
, "read()");
1510 if (r
== 0) return conn_close(c
); /* the client hung up */
1512 c
->cmd_read
+= r
; /* we got some bytes */
1514 c
->cmd_len
= cmd_len(c
); /* find the EOL */
1516 /* yay, complete command line */
1517 if (c
->cmd_len
) return do_cmd(c
);
1519 /* c->cmd_read > LINE_BUF_SIZE can't happen */
1521 /* command line too long? */
1522 if (c
->cmd_read
== LINE_BUF_SIZE
) {
1523 c
->cmd_read
= 0; /* discard the input so far */
1524 return reply_msg(c
, MSG_BAD_FORMAT
);
1527 /* otherwise we have an incomplete line, so just keep waiting */
1529 case STATE_BITBUCKET
:
1530 /* Invert the meaning of in_job_read while throwing away data -- it
1531 * counts the bytes that remain to be thrown away. */
1532 to_read
= min(c
->in_job_read
, BUCKET_BUF_SIZE
);
1533 r
= read(c
->fd
, bucket
, to_read
);
1534 if (r
== -1) return check_err(c
, "read()");
1535 if (r
== 0) return conn_close(c
); /* the client hung up */
1537 c
->in_job_read
-= r
; /* we got some bytes */
1539 /* (c->in_job_read < 0) can't happen */
1541 if (c
->in_job_read
== 0) return reply_serr(c
, MSG_OUT_OF_MEMORY
);
1543 case STATE_WANTDATA
:
1546 r
= read(c
->fd
, j
->body
+ c
->in_job_read
, j
->body_size
-c
->in_job_read
);
1547 if (r
== -1) return check_err(c
, "read()");
1548 if (r
== 0) return conn_close(c
); /* the client hung up */
1550 c
->in_job_read
+= r
; /* we got some bytes */
1552 /* (j->in_job_read > j->body_size) can't happen */
1554 maybe_enqueue_incoming_job(c
);
1556 case STATE_SENDWORD
:
1557 r
= write(c
->fd
, c
->reply
+ c
->reply_sent
, c
->reply_len
- c
->reply_sent
);
1558 if (r
== -1) return check_err(c
, "write()");
1559 if (r
== 0) return conn_close(c
); /* the client hung up */
1561 c
->reply_sent
+= r
; /* we got some bytes */
1563 /* (c->reply_sent > c->reply_len) can't happen */
1565 if (c
->reply_sent
== c
->reply_len
) return reset_conn(c
);
1567 /* otherwise we sent an incomplete reply, so just keep waiting */
1572 iov
[0].iov_base
= (void *)(c
->reply
+ c
->reply_sent
);
1573 iov
[0].iov_len
= c
->reply_len
- c
->reply_sent
; /* maybe 0 */
1574 iov
[1].iov_base
= j
->body
+ c
->out_job_sent
;
1575 iov
[1].iov_len
= j
->body_size
- c
->out_job_sent
;
1577 r
= writev(c
->fd
, iov
, 2);
1578 if (r
== -1) return check_err(c
, "writev()");
1579 if (r
== 0) return conn_close(c
); /* the client hung up */
1581 /* update the sent values */
1583 if (c
->reply_sent
>= c
->reply_len
) {
1584 c
->out_job_sent
+= c
->reply_sent
- c
->reply_len
;
1585 c
->reply_sent
= c
->reply_len
;
1588 /* (c->out_job_sent > j->body_size) can't happen */
1591 if (c
->out_job_sent
== j
->body_size
) return reset_conn(c
);
1593 /* otherwise we sent incomplete data, so just keep waiting */
1595 case STATE_WAIT
: /* keep an eye out in case they hang up */
1596 /* but don't hang up just because our buffer is full */
1597 if (LINE_BUF_SIZE
- c
->cmd_read
< 1) break;
1599 r
= read(c
->fd
, c
->cmd
+ c
->cmd_read
, LINE_BUF_SIZE
- c
->cmd_read
);
1600 if (r
== -1) return check_err(c
, "read()");
1601 if (r
== 0) return conn_close(c
); /* the client hung up */
1602 c
->cmd_read
+= r
; /* we got some bytes */
1606 #define want_command(c) ((c)->fd && ((c)->state == STATE_WANTCOMMAND))
1607 #define cmd_data_ready(c) (want_command(c) && (c)->cmd_read)
1610 h_conn(const int fd
, const short which
, conn c
)
1613 twarnx("Argh! event fd doesn't match conn fd.");
1615 return conn_close(c
);
1621 event_add(&c
->evq
, NULL
); /* seems to be necessary */
1624 /* fall through... */
1626 /* fall through... */
1631 while (cmd_data_ready(c
) && (c
->cmd_len
= cmd_len(c
))) do_cmd(c
);
1642 while ((j
= delay_q_peek())) {
1643 if (j
->deadline
> t
) break;
1645 r
= enqueue_job(j
, 0, 0);
1646 if (r
< 1) bury_job(j
, 0); /* out of memory, so bury it */
1649 set_main_delay_timeout();
1653 h_accept(const int fd
, const short which
, struct event
*ev
)
1658 struct sockaddr addr
;
1660 if (which
== EV_TIMEOUT
) return h_delay();
1662 addrlen
= sizeof addr
;
1663 cfd
= accept(fd
, &addr
, &addrlen
);
1665 if (errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
) twarn("accept()");
1666 if (errno
== EMFILE
) brake();
1670 flags
= fcntl(cfd
, F_GETFL
, 0);
1671 if (flags
< 0) return twarn("getting flags"), close(cfd
), v();
1673 r
= fcntl(cfd
, F_SETFL
, flags
| O_NONBLOCK
);
1674 if (r
< 0) return twarn("setting O_NONBLOCK"), close(cfd
), v();
1676 c
= make_conn(cfd
, STATE_WANTCOMMAND
, default_tube
, default_tube
);
1677 if (!c
) return twarnx("make_conn() failed"), close(cfd
), brake();
1679 dprintf("accepted conn, fd=%d\n", cfd
);
1680 r
= conn_set_evq(c
, EV_READ
| EV_PERSIST
, (evh
) h_conn
);
1681 if (r
== -1) return twarnx("conn_set_evq() failed"), close(cfd
), brake();
1687 start_time
= time(NULL
);
1688 memset(op_ct
, 0, sizeof(op_ct
));
1690 ms_init(&tubes
, NULL
, NULL
);
1692 TUBE_ASSIGN(default_tube
, tube_find_or_make("default"));
1693 if (!default_tube
) twarnx("Out of memory during startup!");
1697 prot_replay_binlog(job binlog_jobs
)
1703 for (j
= binlog_jobs
->next
; j
!= binlog_jobs
; j
= nj
) {
1706 binlog_reserve_space_update(j
); /* reserve space for a delete */
1709 case JOB_STATE_BURIED
:
1712 case JOB_STATE_DELAYED
:
1713 if (start_time
< j
->deadline
) delay
= j
->deadline
- start_time
;
1716 r
= enqueue_job(j
, delay
, 0);
1717 if (r
< 1) twarnx("error processing binlog job %llu", j
->id
);