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>
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
;
47 static binlog first_binlog
= NULL
, last_binlog
= NULL
;
60 dirp
= opendir(binlog_dir
);
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
;
75 binlog_index
= (int) max
;
82 binlog b
= first_binlog
;
86 first_binlog
= b
->next
;
87 if (!first_binlog
) last_binlog
= NULL
;
101 binlog_dref(binlog b
)
104 if (b
->refs
< 1) return twarnx("refs is zero for binlog: %s", b
->path
);
108 while (first_binlog
&& first_binlog
->refs
== 0) binlog_remove_first();
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.");
120 binlog_replay(int fd
, job binlog_jobs
, const char *path
)
125 char tubename
[MAX_TUBE_NAME_LEN
];
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
,
141 while (read(fd
, &namelen
, sizeof(size_t)) == sizeof(size_t)) {
143 r
= read(fd
, tubename
, namelen
);
144 if (r
== -1) return twarn("read()");
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
);
160 case JOB_STATE_INVALID
:
163 binlog_dref(j
->binlog
);
168 case JOB_STATE_READY
:
169 case JOB_STATE_DELAYED
:
171 t
= tube_find_or_make(tubename
);
172 j
= make_job_with_id(js
.pri
, js
.delay
, js
.ttr
, js
.body_size
,
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
);
182 binlog_dref(j
->binlog
);
184 return binlog_warn("job body is too short", fd
, path
);
191 j
->deadline
= js
.deadline
;
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
);
212 if (binlog_fd
< 0) return;
214 binlog_dref(last_binlog
);
219 add_binlog(char *path
)
223 b
= (binlog
)malloc(sizeof(struct binlog
) + strlen(path
) + 1);
224 if (!b
) return twarnx("OOM"), NULL
;
225 strcpy(b
->path
, path
);
228 if (last_binlog
) last_binlog
->next
= b
;
230 if (!first_binlog
) first_binlog
= b
;
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);
250 twarn("Cannot open binlog %s", path
);
255 bytes_written
= write(fd
, &binlog_version
, sizeof(int));
257 if (bytes_written
< sizeof(int)) {
258 twarn("Cannot write to binlog");
260 binlog_dref(last_binlog
);
270 if (binlog_fd
< 0) return;
272 binlog_dref(last_binlog
);
273 binlog_fd
= binlog_open();
277 binlog_write_job(job j
)
279 size_t tube_namelen
, to_write
;
280 struct iovec vec
[4], *vptr
;
283 if (binlog_fd
< 0) return;
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
;
293 /* we could save some bytes in the binlog file by only saving some parts of
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
) {
301 tube_namelen
= strlen(j
->tube
->name
);
302 vec
[1].iov_len
= tube_namelen
;
303 to_write
+= tube_namelen
;
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
);
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
);
325 twarn("Cannot write to binlog");
330 bytes_written
+= written
;
332 if (to_write
> 0 && written
> 0) {
333 for (vptr
= vec
; written
>= vptr
->iov_len
; vptr
++) {
334 written
-= vptr
->iov_len
;
337 vptr
->iov_base
= (char *) vptr
->iov_base
+ written
;
338 vptr
->iov_len
-= written
;
345 binlog_read(job binlog_jobs
)
347 int binlog_index_min
;
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
);
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
);
374 b
= binlog_iref(add_binlog(path
));
375 binlog_replay(fd
, binlog_jobs
, path
);
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
;
401 r
= fcntl(lock_fd
, F_SETLK
, &lock
);
402 if (r
) return twarn("fcntl"), 0;
410 binlog_fd
= binlog_open();
414 binlog_oldest_index()
416 if (!first_binlog
) return "0";
418 return strrchr(first_binlog
->path
, '.') + 1;
422 binlog_current_index()
424 if (!last_binlog
) return "0";
426 return strrchr(last_binlog
->path
, '.') + 1;