Initialize the addr struct to zero.
[beanstalkd.git] / binlog.c
blobec937c509e41232f80887ad897f66bc25e6c6ee1
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/>.
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <sys/resource.h>
27 #include <sys/param.h>
28 #include <sys/uio.h>
29 #include <sys/stat.h>
30 #include <stdarg.h>
32 #include "tube.h"
33 #include "job.h"
34 #include "binlog.h"
35 #include "util.h"
37 /* max size we will create a log file */
38 size_t binlog_size_limit = 10 << 20;
40 char *binlog_dir = NULL;
41 static int binlog_index = 0;
42 static int binlog_fd = -1;
43 static int binlog_version = 1;
44 static size_t bytes_written;
45 static int lock_fd;
47 static binlog first_binlog = NULL, last_binlog = NULL;
49 static int
50 binlog_scan_dir()
52 DIR *dirp;
53 struct dirent *dp;
54 long min = 0;
55 long max = 0;
56 long val;
57 char *endptr;
58 size_t name_len;
60 dirp = opendir(binlog_dir);
61 if (!dirp) return 0;
63 while ((dp = readdir(dirp)) != NULL) {
64 name_len = strlen(dp->d_name);
65 if (name_len > 7 && !strncmp("binlog.", dp->d_name, 7)) {
66 val = strtol(dp->d_name + 7, &endptr, 10);
67 if (endptr && *endptr == 0) {
68 if (max == 0 || val > max) max = val;
69 if (min == 0 || val < min) min = val;
74 closedir(dirp);
75 binlog_index = (int) max;
76 return (int) min;
79 static void
80 binlog_remove_first()
82 binlog b = first_binlog;
84 if (!b) return;
86 first_binlog = b->next;
87 if (!first_binlog) last_binlog = NULL;
89 unlink(b->path);
90 free(b);
93 static binlog
94 binlog_iref(binlog b)
96 if (b) b->refs++;
97 return b;
100 static void
101 binlog_dref(binlog b)
103 if (!b) return;
104 if (b->refs < 1) return twarnx("refs is zero for binlog: %s", b->path);
106 --b->refs;
107 if (b->refs < 1) {
108 while (first_binlog && first_binlog->refs == 0) binlog_remove_first();
112 static void
113 binlog_warn(const char *msg, int fd, const char* path)
115 warnx("WARNING, %s at %s:%u.\n%s", msg, path, lseek(fd, 0, SEEK_CUR),
116 " Continuing with next file. You may be missing data.");
119 static void
120 binlog_replay(int fd, job binlog_jobs, const char *path)
122 struct job js;
123 tube t;
124 job j;
125 char tubename[MAX_TUBE_NAME_LEN];
126 size_t namelen;
127 ssize_t r;
128 int version;
130 r = read(fd, &version, sizeof(version));
131 if (r == -1) return twarn("read()");
132 if (r < sizeof(version)) {
133 return binlog_warn("version record is too short", fd, path);
136 if (version != binlog_version) {
137 return warnx("%s: binlog version mismatch %d %d", path, version,
138 binlog_version);
141 while (read(fd, &namelen, sizeof(size_t)) == sizeof(size_t)) {
142 if (namelen > 0) {
143 r = read(fd, tubename, namelen);
144 if (r == -1) return twarn("read()");
145 if (r < namelen) {
146 lseek(fd, SEEK_CUR, 0);
147 return binlog_warn("tube name is too short", fd, path);
151 tubename[namelen] = '\0';
152 r = read(fd, &js, sizeof(struct job));
153 if (r == -1) return twarn("read()");
154 if (r < sizeof(struct job)) {
155 return binlog_warn("job record is too short", fd, path);
158 j = job_find(js.id);
159 switch (js.state) {
160 case JOB_STATE_INVALID:
161 if (j) {
162 job_remove(j);
163 binlog_dref(j->binlog);
164 job_free(j);
165 j = NULL;
167 break;
168 case JOB_STATE_READY:
169 case JOB_STATE_DELAYED:
170 if (!j) {
171 t = tube_find_or_make(tubename);
172 j = make_job_with_id(js.pri, js.delay, js.ttr, js.body_size,
173 t, js.id);
174 j->next = j->prev = j;
175 j->creation = js.creation;
176 job_insert(binlog_jobs, j);
177 r = read(fd, j->body, js.body_size);
178 if (r == -1) return twarn("read()");
179 if (r < js.body_size) {
180 warnx("dropping incomplete job %llu", j->id);
181 job_remove(j);
182 binlog_dref(j->binlog);
183 job_free(j);
184 return binlog_warn("job body is too short", fd, path);
187 break;
189 if (j) {
190 j->state = js.state;
191 j->deadline = js.deadline;
192 j->pri = js.pri;
193 j->delay = js.delay;
194 j->ttr = js.ttr;
195 j->timeout_ct = js.timeout_ct;
196 j->release_ct = js.release_ct;
197 j->bury_ct = js.bury_ct;
198 j->kick_ct = js.kick_ct;
200 /* this is a complete record, so we can move the binlog ref */
201 if (namelen && js.body_size) {
202 binlog_dref(j->binlog);
203 j->binlog = binlog_iref(last_binlog);
209 void
210 binlog_close()
212 if (binlog_fd < 0) return;
213 close(binlog_fd);
214 binlog_dref(last_binlog);
215 binlog_fd = -1;
218 static binlog
219 add_binlog(char *path)
221 binlog b;
223 b = (binlog)malloc(sizeof(struct binlog) + strlen(path) + 1);
224 if (!b) return twarnx("OOM"), NULL;
225 strcpy(b->path, path);
226 b->refs = 0;
227 b->next = NULL;
228 if (last_binlog) last_binlog->next = b;
229 last_binlog = b;
230 if (!first_binlog) first_binlog = b;
232 return b;
235 static int
236 binlog_open()
238 char path[PATH_MAX];
239 binlog b;
240 int fd, r;
242 if (!binlog_dir) return -1;
243 r = snprintf(path, PATH_MAX, "%s/binlog.%d", binlog_dir, ++binlog_index);
244 if (r > PATH_MAX) return twarnx("path too long: %s", binlog_dir), -1;
246 if (!binlog_iref(add_binlog(path))) return -1;
247 fd = open(path, O_WRONLY | O_CREAT, 0400);
249 if (fd < 0) {
250 twarn("Cannot open binlog %s", path);
251 return -1;
255 bytes_written = write(fd, &binlog_version, sizeof(int));
257 if (bytes_written < sizeof(int)) {
258 twarn("Cannot write to binlog");
259 close(fd);
260 binlog_dref(last_binlog);
261 return -1;
264 return fd;
267 static void
268 binlog_open_next()
270 if (binlog_fd < 0) return;
271 close(binlog_fd);
272 binlog_dref(last_binlog);
273 binlog_fd = binlog_open();
276 void
277 binlog_write_job(job j)
279 size_t tube_namelen, to_write;
280 struct iovec vec[4], *vptr;
281 int vcnt = 3;
283 if (binlog_fd < 0) return;
284 tube_namelen = 0;
286 vec[0].iov_base = (char *) &tube_namelen;
287 vec[0].iov_len = sizeof(size_t);
288 to_write = sizeof(size_t);
290 vec[1].iov_base = j->tube->name;
291 vec[1].iov_len = 0;
293 /* we could save some bytes in the binlog file by only saving some parts of
294 * the job struct */
295 vec[2].iov_base = (char *) j;
296 vec[2].iov_len = sizeof(struct job);
297 to_write += sizeof(struct job);
299 if (j->state == JOB_STATE_READY || j->state == JOB_STATE_DELAYED) {
300 if (!j->binlog) {
301 tube_namelen = strlen(j->tube->name);
302 vec[1].iov_len = tube_namelen;
303 to_write += tube_namelen;
304 vcnt = 4;
305 vec[3].iov_base = j->body;
306 vec[3].iov_len = j->body_size;
307 to_write += j->body_size;
309 } else if (j->state == JOB_STATE_INVALID) {
310 if (j->binlog) binlog_dref(j->binlog);
311 j->binlog = NULL;
312 } else {
313 return twarnx("unserializable job state: %d", j->state);
316 if ((bytes_written + to_write) > binlog_size_limit) binlog_open_next();
317 if (binlog_fd < 0) return;
319 if (j->state && !j->binlog) j->binlog = binlog_iref(last_binlog);
321 while (to_write > 0) {
322 size_t written = writev(binlog_fd, vec, vcnt);
324 if (written < 0) {
325 twarn("Cannot write to binlog");
326 binlog_close();
327 return;
330 bytes_written += written;
331 to_write -= written;
332 if (to_write > 0 && written > 0) {
333 for (vptr = vec; written >= vptr->iov_len; vptr++) {
334 written -= vptr->iov_len;
335 vptr->iov_len = 0;
337 vptr->iov_base = (char *) vptr->iov_base + written;
338 vptr->iov_len -= written;
344 void
345 binlog_read(job binlog_jobs)
347 int binlog_index_min;
348 struct stat sbuf;
349 int fd, idx, r;
350 char path[PATH_MAX];
351 binlog b;
353 if (!binlog_dir) return;
355 if (stat(binlog_dir, &sbuf) < 0) {
356 if (mkdir(binlog_dir, 0700) < 0) return twarn("%s", binlog_dir);
357 } else if (!(sbuf.st_mode & S_IFDIR)) {
358 twarnx("%s", binlog_dir);
359 return;
362 binlog_index_min = binlog_scan_dir();
364 if (binlog_index_min) {
365 for (idx = binlog_index_min; idx <= binlog_index; idx++) {
366 r = snprintf(path, PATH_MAX, "%s/binlog.%d", binlog_dir, idx);
367 if (r > PATH_MAX) return twarnx("path too long: %s", binlog_dir);
369 fd = open(path, O_RDONLY);
371 if (fd < 0) {
372 twarn("%s", path);
373 } else {
374 b = binlog_iref(add_binlog(path));
375 binlog_replay(fd, binlog_jobs, path);
376 close(fd);
377 binlog_dref(b);
385 binlog_lock()
387 int r;
388 struct flock lock;
389 char path[PATH_MAX];
391 r = snprintf(path, PATH_MAX, "%s/lock", binlog_dir);
392 if (r > PATH_MAX) return twarnx("path too long: %s", binlog_dir), 0;
394 lock_fd = open(path, O_WRONLY|O_CREAT, 0600);
395 if (lock_fd == -1) return twarn("open"), 0;
397 lock.l_type = F_WRLCK;
398 lock.l_whence = SEEK_SET;
399 lock.l_start = 0;
400 lock.l_len = 0;
401 r = fcntl(lock_fd, F_SETLK, &lock);
402 if (r) return twarn("fcntl"), 0;
404 return 1;
407 void
408 binlog_init()
410 binlog_fd = binlog_open();
413 const char *
414 binlog_oldest_index()
416 if (!first_binlog) return "0";
418 return strrchr(first_binlog->path, '.') + 1;
421 const char *
422 binlog_current_index()
424 if (!last_binlog) return "0";
426 return strrchr(last_binlog->path, '.') + 1;