Skip to content

Commit

Permalink
Use less disk space (and update tests).
Browse files Browse the repository at this point in the history
  • Loading branch information
kr committed Oct 1, 2009
1 parent 2accf5d commit 819d0eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
21 changes: 11 additions & 10 deletions binlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/stat.h>
#include <stdarg.h>
#include <limits.h>
#include <stddef.h>

#include "tube.h"
#include "job.h"
Expand All @@ -52,13 +53,15 @@ size_t binlog_size_limit = BINLOG_SIZE_LIMIT_DEFAULT;

char *binlog_dir = NULL;
static int binlog_index = 0;
static int binlog_version = 2;
static int binlog_version = 3;
static int lock_fd;

static binlog oldest_binlog = 0,
current_binlog = 0,
newest_binlog = 0;

static const size_t job_record_size = offsetof(struct job, pad);

static int
binlog_scan_dir()
{
Expand Down Expand Up @@ -170,9 +173,9 @@ binlog_read_log_file(binlog b, job binlog_jobs)
}

tubename[namelen] = '\0';
r = read(b->fd, &js, sizeof(struct job));
r = read(b->fd, &js, job_record_size);
if (r == -1) return twarn("read()");
if (r < sizeof(struct job)) {
if (r < job_record_size) {
return binlog_warn(b, "EOF while reading job record");
}

Expand Down Expand Up @@ -408,10 +411,8 @@ binlog_write_job(job j)
vec[1].iov_base = j->tube->name;
vec[1].iov_len = 0;

/* we could save some bytes in the binlog file by only saving some parts of
* the job struct */
vec[2].iov_base = (char *) j;
to_write += vec[2].iov_len = sizeof(struct job);
to_write += vec[2].iov_len = job_record_size;

if (j->state == JOB_STATE_READY || j->state == JOB_STATE_DELAYED) {
if (!j->binlog) {
Expand Down Expand Up @@ -554,7 +555,7 @@ maintain_invariant(size_t n)

/* Invariant 2. */

z = sizeof(size_t) + sizeof(struct job);
z = sizeof(size_t) + job_record_size;
reserved_later = current_binlog->reserved - n;
remainder = reserved_later % z;
if (remainder == 0) return n;
Expand Down Expand Up @@ -611,12 +612,12 @@ binlog_reserve_space_put(job j)
/* reserve space for the initial job record */
z += sizeof(size_t);
z += strlen(j->tube->name);
z += sizeof(struct job);
z += job_record_size;
z += j->body_size;

/* plus space for a delete to come later */
z += sizeof(size_t);
z += sizeof(struct job);
z += job_record_size;

return binlog_reserve_space(z);
}
Expand All @@ -627,7 +628,7 @@ binlog_reserve_space_update(job j)
size_t z = 0;

z += sizeof(size_t);
z += sizeof(struct job);
z += job_record_size;
return binlog_reserve_space(z);
}

Expand Down
36 changes: 27 additions & 9 deletions sh-tests/binlog-diskfull-delete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,26 @@ put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

diff - "$out1" <<EOF
INSERTED 1
INSERTED 2
INSERTED 3
INSERTED 4
INSERTED 5
INSERTED 6
EOF
res=$?
test "$res" -eq 0 || exit $res

# Check that the second binlog file is present
test "$(stat --printf=%s "$logdir"/binlog.2)" -eq $size || exit 1

# Make beanstalkd think the disk is full now.
fiu-ctrl -e posix/io/oc/open -i $ENOSPC $bpid

Expand All @@ -89,11 +98,14 @@ put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

diff - "$out1" <<EOF
INSERTED 5
INSERTED 6
INSERTED 7
INSERTED 8
INSERTED 9
OUT_OF_MEMORY
OUT_OF_MEMORY
EOF
Expand All @@ -108,13 +120,17 @@ delete 1
delete 2
delete 3
delete 4
delete 5
delete 6
EOF

diff - "$out1" <<EOF
DELETED
DELETED
DELETED
DELETED
DELETED
DELETED
EOF
res=$?
test "$res" -eq 0 || exit $res
Expand All @@ -138,10 +154,10 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

diff - "$out1" <<EOF
INSERTED 9
INSERTED 10
INSERTED 11
INSERTED 12
INSERTED 13
INSERTED 14
INSERTED 15
EOF
res=$?
test "$res" -eq 0 || exit $res
Expand All @@ -159,12 +175,13 @@ if ! ps -p $bpid >/dev/null; then
fi

$nc $server $port <<EOF > "$out2"
delete 5
delete 6
delete 7
delete 8
delete 9
delete 10
delete 11
delete 12
delete 13
delete 14
delete 15
EOF

diff - "$out2" <<EOF
Expand All @@ -174,5 +191,6 @@ DELETED
DELETED
DELETED
DELETED
DELETED
EOF

18 changes: 14 additions & 4 deletions sh-tests/binlog-diskfull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,18 @@ put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
put 0 0 100 50
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

diff - "$out1" <<EOF
INSERTED 1
INSERTED 2
INSERTED 3
INSERTED 4
INSERTED 5
OUT_OF_MEMORY
OUT_OF_MEMORY
EOF
Expand All @@ -98,10 +104,10 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

diff - "$out1" <<EOF
INSERTED 6
INSERTED 7
INSERTED 8
INSERTED 9
INSERTED 10
INSERTED 11
EOF
res=$?
test "$res" -eq 0 || exit $res
Expand All @@ -122,10 +128,12 @@ $nc $server $port <<EOF > "$out2"
delete 1
delete 2
delete 3
delete 6
delete 7
delete 4
delete 5
delete 8
delete 9
delete 10
delete 11
EOF

diff - "$out2" <<EOF
Expand All @@ -136,5 +144,7 @@ DELETED
DELETED
DELETED
DELETED
DELETED
DELETED
EOF

0 comments on commit 819d0eb

Please sign in to comment.