1 /* binlog.c - binary log implementation */
3 /* Copyright (C) 2008 Graham Barr
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/>.
23 #endif /* else we get int types from config.h */
32 #include <sys/resource.h>
33 #include <sys/param.h>
46 typedef struct binlog
*binlog
;
57 /* max size we will create a log file */
58 size_t binlog_size_limit
= BINLOG_SIZE_LIMIT_DEFAULT
;
61 size_t fsync_throttle_ms
= 0;
62 uint64_t last_fsync
= 0;
64 char *binlog_dir
= NULL
;
65 static int binlog_index
= 0;
66 static int binlog_version
= 5;
69 static binlog oldest_binlog
= 0,
73 static const size_t job_record_size
= offsetof(struct job
, pad
);
86 dirp
= opendir(binlog_dir
);
89 while ((dp
= readdir(dirp
)) != NULL
) {
90 name_len
= strlen(dp
->d_name
);
91 if (name_len
> 7 && !strncmp("binlog.", dp
->d_name
, 7)) {
92 val
= strtol(dp
->d_name
+ 7, &endptr
, 10);
93 if (endptr
&& *endptr
== 0) {
94 if (max
== 0 || val
> max
) max
= val
;
95 if (min
== 0 || val
< min
) min
= val
;
101 binlog_index
= (int) max
;
106 binlog_remove_oldest()
108 binlog b
= oldest_binlog
;
112 oldest_binlog
= b
->next
;
119 binlog_iref(binlog b
)
126 binlog_dref(binlog b
)
129 if (b
->refs
< 1) return twarnx("refs is zero for binlog: %s", b
->path
);
133 while (oldest_binlog
&& oldest_binlog
->refs
== 0) {
134 binlog_remove_oldest();
141 binlog_warn(int fd, const char* path, const char *msg)
143 warnx("WARNING, %s at %s:%u.\n%s", msg, path, lseek(fd, 0, SEEK_CUR),
144 " Continuing. You may be missing data.");
148 #define binlog_warn(b, fmt, args...) \
149 warnx("WARNING, " fmt " at %s:%u. %s: ", \
150 ##args, b->path, lseek(b->fd, 0, SEEK_CUR), \
151 "Continuing. You may be missing data.")
154 binlog_read_log_file(binlog b
, job binlog_jobs
)
159 char tubename
[MAX_TUBE_NAME_LEN
];
164 r
= read(b
->fd
, &version
, sizeof(version
));
165 if (r
== -1) return twarn("read()");
166 if (r
< sizeof(version
)) {
167 return binlog_warn(b
, "EOF while reading version record");
170 if (version
!= binlog_version
) {
171 return warnx("%s: binlog version mismatch %d %d", b
->path
, version
,
175 while (read(b
->fd
, &namelen
, sizeof(size_t)) == sizeof(size_t)) {
176 if (namelen
>= MAX_TUBE_NAME_LEN
) {
177 return binlog_warn(b
, "namelen %d exceeds maximum of %d", namelen
, MAX_TUBE_NAME_LEN
- 1);
181 r
= read(b
->fd
, tubename
, namelen
);
182 if (r
== -1) return twarn("read()");
184 lseek(b
->fd
, SEEK_CUR
, 0);
185 return binlog_warn(b
, "EOF while reading tube name");
189 tubename
[namelen
] = '\0';
190 r
= read(b
->fd
, &js
, job_record_size
);
191 if (r
== -1) return twarn("read()");
192 if (r
< job_record_size
) {
193 return binlog_warn(b
, "EOF while reading job record");
200 case JOB_STATE_INVALID
:
203 binlog_dref(j
->binlog
);
208 case JOB_STATE_READY
:
209 case JOB_STATE_DELAYED
:
210 if (!j
&& namelen
> 0) {
211 t
= tube_find_or_make(tubename
);
212 j
= make_job_with_id(js
.pri
, js
.delay
, js
.ttr
, js
.body_size
,
214 j
->next
= j
->prev
= j
;
215 j
->created_at
= js
.created_at
;
216 job_insert(binlog_jobs
, j
);
218 if (js
.body_size
&& namelen
> 0) { /* namelen > 0 only on new jobs */
219 if (js
.body_size
> j
->body_size
) {
220 warnx("job size increased from %zu to %zu", j
->body_size
,
223 binlog_dref(j
->binlog
);
225 return binlog_warn(b
, "EOF while reading job body");
227 r
= read(b
->fd
, j
->body
, js
.body_size
);
228 if (r
== -1) return twarn("read()");
229 if (r
< js
.body_size
) {
230 warnx("dropping incomplete job %llu", j
->id
);
232 binlog_dref(j
->binlog
);
234 return binlog_warn(b
, "EOF while reading job body");
241 j
->deadline_at
= js
.deadline_at
;
245 j
->timeout_ct
= js
.timeout_ct
;
246 j
->release_ct
= js
.release_ct
;
247 j
->bury_ct
= js
.bury_ct
;
248 j
->kick_ct
= js
.kick_ct
;
250 /* this is a complete record, so we can move the binlog ref */
251 if (namelen
&& js
.body_size
) {
252 binlog_dref(j
->binlog
);
253 j
->binlog
= binlog_iref(b
);
260 binlog_close(binlog b
)
263 if (b
->fd
< 0) return;
270 make_binlog(char *path
)
274 b
= (binlog
) malloc(sizeof(struct binlog
) + strlen(path
) + 1);
275 if (!b
) return twarnx("OOM"), (binlog
) 0;
276 strcpy(b
->path
, path
);
291 if (!binlog_dir
) return NULL
;
293 r
= snprintf(path
, PATH_MAX
, "%s/binlog.%d", binlog_dir
, ++binlog_index
);
294 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
), (binlog
)0;
296 return make_binlog(path
);
302 if (newest_binlog
) newest_binlog
->next
= b
;
304 if (!oldest_binlog
) oldest_binlog
= b
;
310 beanstalkd_fallocate(int fd
, off_t offset
, off_t len
)
315 #define ZERO_BUF_SIZE 512
316 char buf
[ZERO_BUF_SIZE
] = {}; /* initialize to zero */
318 /* we only support a 0 offset */
319 if (offset
!= 0) return EINVAL
;
321 if (len
<= 0) return EINVAL
;
323 for (i
= 0; i
< len
; i
+= w
) {
324 w
= write(fd
, &buf
, ZERO_BUF_SIZE
);
325 if (w
== -1) return errno
;
328 p
= lseek(fd
, 0, SEEK_SET
);
329 if (p
== -1) return errno
;
335 binlog_open(binlog log
, size_t *written
)
338 size_t bytes_written
;
340 if (written
) *written
= 0;
342 if (!binlog_iref(log
)) return;
344 fd
= open(log
->path
, O_WRONLY
| O_CREAT
, 0400);
346 if (fd
< 0) return twarn("Cannot open binlog %s", log
->path
);
348 #ifdef HAVE_POSIX_FALLOCATE
351 r
= posix_fallocate(fd
, 0, binlog_size_limit
);
353 r
= beanstalkd_fallocate(fd
, 0, binlog_size_limit
);
359 return twarn("Cannot allocate space for binlog %s", log
->path
);
363 /* Allocate space in a slow but portable way. */
366 r
= beanstalkd_fallocate(fd
, 0, binlog_size_limit
);
371 return twarn("Cannot allocate space for binlog %s", log
->path
);
376 bytes_written
= write(fd
, &binlog_version
, sizeof(int));
377 if (written
) *written
= bytes_written
;
379 if (bytes_written
< sizeof(int)) {
380 twarn("Cannot write to binlog");
389 /* returns 1 on success, 0 on error. */
395 if (!current_binlog
) return 0;
397 next
= current_binlog
->next
;
401 /* assert(current_binlog->reserved == 0); */
403 binlog_close(current_binlog
);
404 current_binlog
= next
;
413 binlog_close(current_binlog
);
416 /* Returns the number of jobs successfully written (either 0 or 1).
418 If this fails, something is seriously wrong. It should never fail because of
419 a full disk. (The binlog_reserve_space_* functions, on the other hand, can
420 fail because of a full disk.)
422 If we are not using the binlog at all (!current_binlog), then we pretend to
423 have made a successful write and return 1. */
425 binlog_write_job(job j
)
428 size_t tube_namelen
, to_write
= 0;
429 struct iovec vec
[4], *vptr
;
433 if (!current_binlog
) return 1;
436 vec
[0].iov_base
= (char *) &tube_namelen
;
437 to_write
+= vec
[0].iov_len
= sizeof(size_t);
439 vec
[1].iov_base
= j
->tube
->name
;
442 vec
[2].iov_base
= (char *) j
;
443 to_write
+= vec
[2].iov_len
= job_record_size
;
445 if (j
->state
== JOB_STATE_READY
|| j
->state
== JOB_STATE_DELAYED
) {
447 tube_namelen
= strlen(j
->tube
->name
);
448 to_write
+= vec
[1].iov_len
= tube_namelen
;
450 vec
[3].iov_base
= j
->body
;
451 to_write
+= vec
[3].iov_len
= j
->body_size
;
453 } else if (j
->state
== JOB_STATE_INVALID
) {
454 if (j
->binlog
) binlog_dref(j
->binlog
);
457 return twarnx("unserializable job state: %d", j
->state
), 0;
460 if (to_write
> current_binlog
->reserved
) {
461 r
= binlog_use_next();
462 if (!r
) return twarnx("failed to use next binlog"), 0;
465 if (j
->state
&& !j
->binlog
) j
->binlog
= binlog_iref(current_binlog
);
467 while (to_write
> 0) {
468 written
= writev(current_binlog
->fd
, vec
, vcnt
);
471 if (errno
== EAGAIN
) continue;
472 if (errno
== EINTR
) continue;
475 binlog_close(current_binlog
);
481 if (to_write
> 0 && written
> 0) {
482 for (vptr
= vec
; written
>= vptr
->iov_len
; vptr
++) {
483 written
-= vptr
->iov_len
;
486 vptr
->iov_base
= (char *) vptr
->iov_base
+ written
;
487 vptr
->iov_len
-= written
;
489 current_binlog
->reserved
-= written
;
490 j
->reserved_binlog_space
-= written
;
493 now
= now_usec() / 1000; /* usec -> msec */
494 if (enable_fsync
&& now
- last_fsync
>= fsync_throttle_ms
) {
495 r
= fdatasync(current_binlog
->fd
);
496 if (r
== -1) return twarn("fdatasync"), 0;
509 /* open a new binlog with more space to reserve */
510 b
= make_next_binlog();
511 if (!b
) return twarnx("error making next binlog"), (binlog
) 0;
512 binlog_open(b
, &header
);
514 /* open failed, so we can't reserve any space */
520 b
->free
= binlog_size_limit
- header
;
526 can_move_reserved(size_t n
, binlog from
, binlog to
)
528 return from
->reserved
>= n
&& to
->free
>= n
;
532 move_reserved(size_t n
, binlog from
, binlog to
)
541 ensure_free_space(size_t n
)
545 if (newest_binlog
&& newest_binlog
->free
>= n
) return n
;
547 /* open a new binlog */
548 fb
= make_future_binlog();
549 if (!fb
) return twarnx("make_future_binlog"), 0;
555 /* Preserve some invariants immediately after any space reservation.
556 * Invariant 1: current_binlog->reserved >= n.
557 * Invariant 2: current_binlog->reserved is congruent to n (mod z), where z
558 * is the size of a delete record in the binlog. */
560 maintain_invariant(size_t n
)
562 size_t reserved_later
, remainder
, complement
, z
, r
;
564 /* In this function, reserved bytes are conserved (they are neither created
565 * nor destroyed). We just move them around to preserve the invariant. We
566 * might have to create new free space (i.e. allocate a new binlog file),
570 /* This is a loop, but it's guaranteed to run at most once. The proof is
571 * left as an exercise for the reader. */
572 while (current_binlog
->reserved
< n
) {
573 size_t to_move
= current_binlog
->reserved
;
575 r
= ensure_free_space(to_move
);
577 twarnx("ensure_free_space");
578 if (newest_binlog
->reserved
>= n
) {
579 newest_binlog
->reserved
-= n
;
581 twarnx("failed to unreserve %zd bytes", n
); /* can't happen */
586 move_reserved(to_move
, current_binlog
, newest_binlog
);
593 z
= sizeof(size_t) + job_record_size
;
594 reserved_later
= current_binlog
->reserved
- n
;
595 remainder
= reserved_later
% z
;
596 if (remainder
== 0) return n
;
597 complement
= z
- remainder
;
598 if (can_move_reserved(complement
, newest_binlog
, current_binlog
)) {
599 move_reserved(complement
, newest_binlog
, current_binlog
);
603 r
= ensure_free_space(remainder
);
604 if (r
!= remainder
) {
605 twarnx("ensure_free_space");
606 if (newest_binlog
->reserved
>= n
) {
607 newest_binlog
->reserved
-= n
;
609 twarnx("failed to unreserve %zd bytes", n
); /* can't happen */
613 move_reserved(remainder
, current_binlog
, newest_binlog
);
618 /* Returns the number of bytes successfully reserved: either 0 or n. */
620 binlog_reserve_space(size_t n
)
624 /* This return value must be nonzero but is otherwise ignored. */
625 if (!current_binlog
) return 1;
627 if (current_binlog
->free
>= n
) {
628 current_binlog
->free
-= n
;
629 current_binlog
->reserved
+= n
;
630 return maintain_invariant(n
);
633 r
= ensure_free_space(n
);
634 if (r
!= n
) return twarnx("ensure_free_space"), 0;
636 newest_binlog
->free
-= n
;
637 newest_binlog
->reserved
+= n
;
638 return maintain_invariant(n
);
641 /* Returns the number of bytes reserved. */
643 binlog_reserve_space_put(job j
)
647 /* reserve space for the initial job record */
649 z
+= strlen(j
->tube
->name
);
650 z
+= job_record_size
;
653 /* plus space for a delete to come later */
655 z
+= job_record_size
;
657 return binlog_reserve_space(z
);
661 binlog_reserve_space_update(job j
)
666 z
+= job_record_size
;
667 return binlog_reserve_space(z
);
677 r
= snprintf(path
, PATH_MAX
, "%s/lock", binlog_dir
);
678 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
), 0;
680 lock_fd
= open(path
, O_WRONLY
|O_CREAT
, 0600);
681 if (lock_fd
== -1) return twarn("open"), 0;
683 lock
.l_type
= F_WRLCK
;
684 lock
.l_whence
= SEEK_SET
;
687 r
= fcntl(lock_fd
, F_SETLK
, &lock
);
688 if (r
) return twarn("fcntl"), 0;
694 binlog_init(job binlog_jobs
)
696 int binlog_index_min
;
703 if (!binlog_dir
) return;
705 /* Recover any jobs in old binlogs */
707 if (stat(binlog_dir
, &sbuf
) < 0) {
708 if (mkdir(binlog_dir
, 0700) < 0) return twarn("%s", binlog_dir
);
709 } else if (!(sbuf
.st_mode
& S_IFDIR
)) {
710 twarnx("%s", binlog_dir
);
714 binlog_index_min
= binlog_scan_dir();
716 if (binlog_index_min
) {
717 for (idx
= binlog_index_min
; idx
<= binlog_index
; idx
++) {
718 r
= snprintf(path
, PATH_MAX
, "%s/binlog.%d", binlog_dir
, idx
);
719 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
);
721 fd
= open(path
, O_RDONLY
);
726 b
= binlog_iref(add_binlog(make_binlog(path
)));
728 binlog_read_log_file(b
, binlog_jobs
);
738 /* Set up for writing out new jobs */
739 n
= ensure_free_space(1);
740 if (!n
) return twarnx("error making first writable binlog");
742 current_binlog
= newest_binlog
;
746 binlog_oldest_index()
748 if (!oldest_binlog
) return "0";
750 return strrchr(oldest_binlog
->path
, '.') + 1;
754 binlog_current_index()
756 if (!newest_binlog
) return "0";
758 return strrchr(newest_binlog
->path
, '.') + 1;