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/>.
26 #include <sys/resource.h>
27 #include <sys/param.h>
39 typedef struct binlog
*binlog
;
50 /* max size we will create a log file */
51 size_t binlog_size_limit
= 10 << 20;
53 char *binlog_dir
= NULL
;
54 static int binlog_index
= 0;
55 static int binlog_version
= 2;
58 static binlog oldest_binlog
= 0,
73 dirp
= opendir(binlog_dir
);
76 while ((dp
= readdir(dirp
)) != NULL
) {
77 name_len
= strlen(dp
->d_name
);
78 if (name_len
> 7 && !strncmp("binlog.", dp
->d_name
, 7)) {
79 val
= strtol(dp
->d_name
+ 7, &endptr
, 10);
80 if (endptr
&& *endptr
== 0) {
81 if (max
== 0 || val
> max
) max
= val
;
82 if (min
== 0 || val
< min
) min
= val
;
88 binlog_index
= (int) max
;
93 binlog_remove_oldest()
95 binlog b
= oldest_binlog
;
99 oldest_binlog
= b
->next
;
106 binlog_iref(binlog b
)
113 binlog_dref(binlog b
)
116 if (b
->refs
< 1) return twarnx("refs is zero for binlog: %s", b
->path
);
120 while (oldest_binlog
&& oldest_binlog
->refs
== 0) {
121 binlog_remove_oldest();
128 binlog_warn(int fd, const char* path, const char *msg)
130 warnx("WARNING, %s at %s:%u.\n%s", msg, path, lseek(fd, 0, SEEK_CUR),
131 " Continuing. You may be missing data.");
135 #define binlog_warn(b, fmt, args...) \
136 warnx("WARNING, " fmt " at %s:%u. %s: ", \
137 ##args, b->path, lseek(b->fd, 0, SEEK_CUR), \
138 "Continuing. You may be missing data.")
141 binlog_read_log_file(binlog b
, job binlog_jobs
)
146 char tubename
[MAX_TUBE_NAME_LEN
];
151 r
= read(b
->fd
, &version
, sizeof(version
));
152 if (r
== -1) return twarn("read()");
153 if (r
< sizeof(version
)) {
154 return binlog_warn(b
, "EOF while reading version record");
157 if (version
!= binlog_version
) {
158 return warnx("%s: binlog version mismatch %d %d", b
->path
, version
,
162 while (read(b
->fd
, &namelen
, sizeof(size_t)) == sizeof(size_t)) {
164 r
= read(b
->fd
, tubename
, namelen
);
165 if (r
== -1) return twarn("read()");
167 lseek(b
->fd
, SEEK_CUR
, 0);
168 return binlog_warn(b
, "EOF while reading tube name");
172 tubename
[namelen
] = '\0';
173 r
= read(b
->fd
, &js
, sizeof(struct job
));
174 if (r
== -1) return twarn("read()");
175 if (r
< sizeof(struct job
)) {
176 return binlog_warn(b
, "EOF while reading job record");
183 case JOB_STATE_INVALID
:
186 binlog_dref(j
->binlog
);
191 case JOB_STATE_READY
:
192 case JOB_STATE_DELAYED
:
194 t
= tube_find_or_make(tubename
);
195 j
= make_job_with_id(js
.pri
, js
.delay
, js
.ttr
, js
.body_size
,
197 j
->next
= j
->prev
= j
;
198 j
->creation
= js
.creation
;
199 job_insert(binlog_jobs
, j
);
202 if (js
.body_size
> j
->body_size
) {
203 warnx("job size increased from %zu to %zu", j
->body_size
,
206 binlog_dref(j
->binlog
);
208 return binlog_warn(b
, "EOF while reading job body");
210 r
= read(b
->fd
, j
->body
, js
.body_size
);
211 if (r
== -1) return twarn("read()");
212 if (r
< js
.body_size
) {
213 warnx("dropping incomplete job %llu", j
->id
);
215 binlog_dref(j
->binlog
);
217 return binlog_warn(b
, "EOF while reading job body");
224 j
->deadline
= js
.deadline
;
228 j
->timeout_ct
= js
.timeout_ct
;
229 j
->release_ct
= js
.release_ct
;
230 j
->bury_ct
= js
.bury_ct
;
231 j
->kick_ct
= js
.kick_ct
;
233 /* this is a complete record, so we can move the binlog ref */
234 if (namelen
&& js
.body_size
) {
235 binlog_dref(j
->binlog
);
236 j
->binlog
= binlog_iref(b
);
243 binlog_close(binlog b
)
246 if (b
->fd
< 0) return;
253 make_binlog(char *path
)
257 b
= (binlog
) malloc(sizeof(struct binlog
) + strlen(path
) + 1);
258 if (!b
) return twarnx("OOM"), (binlog
) 0;
259 strcpy(b
->path
, path
);
274 if (!binlog_dir
) return NULL
;
276 r
= snprintf(path
, PATH_MAX
, "%s/binlog.%d", binlog_dir
, ++binlog_index
);
277 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
), (binlog
)0;
279 return make_binlog(path
);
285 if (newest_binlog
) newest_binlog
->next
= b
;
287 if (!oldest_binlog
) oldest_binlog
= b
;
293 binlog_open(binlog log
, size_t *written
)
296 size_t bytes_written
;
298 if (!binlog_iref(log
)) return;
300 fd
= open(log
->path
, O_WRONLY
| O_CREAT
, 0400);
302 if (fd
< 0) return twarn("Cannot open binlog %s", log
->path
);
304 #ifdef HAVE_POSIX_FALLOCATE
307 r
= posix_fallocate(fd
, 0, binlog_size_limit
);
312 return twarn("Cannot allocate space for binlog %s", log
->path
);
316 /* Allocate space in a slow but portable way. */
321 #define ZERO_BUF_SIZE 512
322 char buf
[ZERO_BUF_SIZE
] = {}; /* initialize to zero */
324 for (i
= 0; i
< binlog_size_limit
; i
+= w
) {
325 w
= write(fd
, &buf
, ZERO_BUF_SIZE
);
327 twarn("Cannot allocate space for binlog %s", log
->path
);
334 p
= lseek(fd
, 0, SEEK_SET
);
344 bytes_written
= write(fd
, &binlog_version
, sizeof(int));
346 if (bytes_written
< sizeof(int)) {
347 twarn("Cannot write to binlog");
353 if (written
) *written
= bytes_written
;
357 /* returns 1 on success, 0 on error. */
363 if (!current_binlog
) return 0;
365 next
= current_binlog
->next
;
369 /* assert(current_binlog->reserved == 0); */
371 binlog_close(current_binlog
);
372 current_binlog
= next
;
381 binlog_close(current_binlog
);
384 /* Returns the number of jobs successfully written (either 0 or 1). */
385 /* If we are not using the binlog at all (!current_binlog), then we pretend to
386 have made a successful write and return 1. */
388 binlog_write_job(job j
)
391 size_t tube_namelen
, to_write
= 0;
392 struct iovec vec
[4], *vptr
;
395 if (!current_binlog
) return 1;
398 vec
[0].iov_base
= (char *) &tube_namelen
;
399 to_write
+= vec
[0].iov_len
= sizeof(size_t);
401 vec
[1].iov_base
= j
->tube
->name
;
404 /* we could save some bytes in the binlog file by only saving some parts of
406 vec
[2].iov_base
= (char *) j
;
407 to_write
+= vec
[2].iov_len
= sizeof(struct job
);
409 printf("writing job %lld state %d\n", j
->id
, j
->state
);
411 if (j
->state
== JOB_STATE_READY
|| j
->state
== JOB_STATE_DELAYED
) {
413 tube_namelen
= strlen(j
->tube
->name
);
414 to_write
+= vec
[1].iov_len
= tube_namelen
;
416 vec
[3].iov_base
= j
->body
;
417 to_write
+= vec
[3].iov_len
= j
->body_size
;
419 } else if (j
->state
== JOB_STATE_INVALID
) {
420 if (j
->binlog
) binlog_dref(j
->binlog
);
423 return twarnx("unserializable job state: %d", j
->state
), 0;
426 if (to_write
> current_binlog
->reserved
) {
427 r
= binlog_use_next();
428 if (!r
) return twarnx("failed to use next binlog"), 0;
431 if (j
->state
&& !j
->binlog
) j
->binlog
= binlog_iref(current_binlog
);
433 while (to_write
> 0) {
434 written
= writev(current_binlog
->fd
, vec
, vcnt
);
437 if (errno
== EAGAIN
) continue;
438 if (errno
== EINTR
) continue;
441 binlog_close(current_binlog
);
447 if (to_write
> 0 && written
> 0) {
448 for (vptr
= vec
; written
>= vptr
->iov_len
; vptr
++) {
449 written
-= vptr
->iov_len
;
452 vptr
->iov_base
= (char *) vptr
->iov_base
+ written
;
453 vptr
->iov_len
-= written
;
466 /* open a new binlog with more space to reserve */
467 b
= make_next_binlog();
468 if (!b
) return twarnx("error making next binlog"), (binlog
) 0;
469 binlog_open(b
, &header
);
471 /* open failed, so we can't reserve any space */
477 b
->free
= binlog_size_limit
- header
;
483 can_move_reserved(size_t n
, binlog from
, binlog to
)
485 return from
->reserved
>= n
&& to
->free
>= n
;
489 move_reserved(size_t n
, binlog from
, binlog to
)
498 ensure_free_space(size_t n
)
502 if (newest_binlog
->free
>= n
) return n
;
504 /* open a new binlog */
505 fb
= make_future_binlog();
506 if (!fb
) return twarnx("make_future_binlog"), 0;
512 /* Preserve some invariants immediately after any space reservation.
513 * Invariant 1: current_binlog->reserved >= n.
514 * Invariant 2: current_binlog->reserved is congruent to n (mod z), where z
515 * is the size of a delete record in the binlog. */
517 maintain_invariant(size_t n
)
519 size_t reserved_later
, remainder
, complement
, z
, r
;
521 /* In this function, reserved bytes are conserved (they are neither created
522 * nor destroyed). We just move them around to preserve the invariant. We
523 * might have to create new free space (i.e. allocate a new binlog file),
527 /* This is a loop, but it's guaranteed to run at most once. The proof is
528 * left as an exercise for the reader. */
529 while (current_binlog
->reserved
< n
) {
530 size_t to_move
= current_binlog
->reserved
;
532 r
= ensure_free_space(to_move
);
534 /* TODO unreserve n bytes */
535 return twarnx("ensure_free_space"), 0;
538 move_reserved(to_move
, current_binlog
, newest_binlog
);
545 z
= sizeof(size_t) + sizeof(struct job
);
546 reserved_later
= current_binlog
->reserved
- n
;
547 remainder
= reserved_later
% z
;
548 if (remainder
== 0) return n
;
549 complement
= z
- remainder
;
550 if (can_move_reserved(complement
, newest_binlog
, current_binlog
)) {
551 move_reserved(complement
, newest_binlog
, current_binlog
);
555 r
= ensure_free_space(remainder
);
556 if (r
!= remainder
) {
557 /* TODO unreserve n bytes */
558 return twarnx("ensure_free_space"), 0;
560 move_reserved(remainder
, current_binlog
, newest_binlog
);
565 /* Returns the number of bytes successfully reserved: either 0 or n. */
567 binlog_reserve_space(size_t n
)
569 /* This return value must be nonzero but is otherwise ignored. */
570 if (!current_binlog
) return 1;
572 if (current_binlog
->free
>= n
) {
573 current_binlog
->free
-= n
;
574 current_binlog
->reserved
+= n
;
575 return maintain_invariant(n
);
578 ensure_free_space(n
);
580 newest_binlog
->free
-= n
;
581 newest_binlog
->reserved
+= n
;
582 return maintain_invariant(n
);
585 /* Returns the number of bytes reserved. */
587 binlog_reserve_space_put(job j
)
591 /* reserve space for the initial job record */
593 z
+= strlen(j
->tube
->name
);
594 z
+= sizeof(struct job
);
597 /* plus space for a delete to come later */
599 z
+= sizeof(struct job
);
601 return binlog_reserve_space(z
);
605 binlog_reserve_space_update(job j
)
610 z
+= sizeof(struct job
);
611 return binlog_reserve_space(z
);
621 r
= snprintf(path
, PATH_MAX
, "%s/lock", binlog_dir
);
622 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
), 0;
624 lock_fd
= open(path
, O_WRONLY
|O_CREAT
, 0600);
625 if (lock_fd
== -1) return twarn("open"), 0;
627 lock
.l_type
= F_WRLCK
;
628 lock
.l_whence
= SEEK_SET
;
631 r
= fcntl(lock_fd
, F_SETLK
, &lock
);
632 if (r
) return twarn("fcntl"), 0;
638 binlog_init(job binlog_jobs
)
640 int binlog_index_min
;
646 if (!binlog_dir
) return;
648 /* Recover any jobs in old binlogs */
650 if (stat(binlog_dir
, &sbuf
) < 0) {
651 if (mkdir(binlog_dir
, 0700) < 0) return twarn("%s", binlog_dir
);
652 } else if (!(sbuf
.st_mode
& S_IFDIR
)) {
653 twarnx("%s", binlog_dir
);
657 binlog_index_min
= binlog_scan_dir();
659 if (binlog_index_min
) {
660 for (idx
= binlog_index_min
; idx
<= binlog_index
; idx
++) {
661 r
= snprintf(path
, PATH_MAX
, "%s/binlog.%d", binlog_dir
, idx
);
662 if (r
> PATH_MAX
) return twarnx("path too long: %s", binlog_dir
);
664 fd
= open(path
, O_RDONLY
);
669 b
= binlog_iref(add_binlog(make_binlog(path
)));
671 binlog_read_log_file(b
, binlog_jobs
);
681 /* Set up for writing out new jobs */
683 b
= make_next_binlog();
684 if (!b
) return twarnx("error making first binlog");
686 if (b
->fd
>= 0) add_binlog(b
);
691 binlog_oldest_index()
693 if (!oldest_binlog
) return "0";
695 return strrchr(oldest_binlog
->path
, '.') + 1;
699 binlog_current_index()
701 if (!newest_binlog
) return "0";
703 return strrchr(newest_binlog
->path
, '.') + 1;