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>
38 /* max size we will create a log file */
39 size_t binlog_size_limit
= 10 << 20;
41 char *binlog_dir
= NULL
;
42 static int binlog_index
= 0;
43 static int binlog_fd
= -1;
44 static int binlog_version
= 1;
45 static size_t bytes_written
;
47 struct binlog binlog_head
;
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
;
80 binlog_remove(binlog b
)
83 if (b
->next
== b
) return; /* not in a doubly-linked list */
85 b
->next
->prev
= b
->prev
;
86 b
->prev
->next
= b
->next
;
100 binlog_dref(binlog b
)
105 if (b
->refs
< 1) return twarnx("refs is zero for binlog: %s", b
->path
);
109 for (b
= binlog_head
.next
; b
!= binlog_head
.prev
&& b
->refs
== 0; b
= bn
) {
117 binlog_replay(int fd
, job binlog_jobs
)
122 char tubename
[MAX_TUBE_NAME_LEN
];
126 if (read(fd
, &version
, sizeof(version
)) < sizeof(version
)) {
127 return twarnx("oops");
129 if (version
!= binlog_version
) {
130 return twarnx("binlog version mismatch %d %d", version
, binlog_version
);
133 while (read(fd
, &namelen
, sizeof(size_t)) == sizeof(size_t)) {
134 if (namelen
> 0 && read(fd
, tubename
, namelen
) != namelen
) {
135 return twarnx("oops %x %d", namelen
, (int)lseek(fd
, SEEK_CUR
, 0));
138 tubename
[namelen
] = '\0';
139 if (read(fd
, &js
, sizeof(struct job
)) != sizeof(struct job
)) {
140 return twarnx("oops");
145 case JOB_STATE_INVALID
:
148 binlog_dref(j
->binlog
);
153 case JOB_STATE_READY
:
154 case JOB_STATE_DELAYED
:
156 t
= tube_find_or_make(tubename
);
157 j
= make_job_with_id(js
.pri
, js
.delay
, js
.ttr
, js
.body_size
,
159 j
->next
= j
->prev
= j
;
160 j
->binlog
= binlog_iref(binlog_head
.prev
);
161 j
->creation
= js
.creation
;
162 job_insert(binlog_jobs
, j
);
163 if (read(fd
, j
->body
, js
.body_size
) < js
.body_size
) {
172 j
->deadline
= js
.deadline
;
176 j
->timeout_ct
= js
.timeout_ct
;
177 j
->release_ct
= js
.release_ct
;
178 j
->bury_ct
= js
.bury_ct
;
179 j
->kick_ct
= js
.kick_ct
;
187 if (binlog_fd
< 0) return;
193 add_binlog(char *path
)
197 b
= (binlog
)malloc(sizeof(struct binlog
) + strlen(path
) + 1);
198 if (!b
) return twarnx("OOM"), NULL
;
199 strcpy(b
->path
, path
);
201 b
->prev
= binlog_head
.prev
;
202 b
->next
= &binlog_head
;
204 binlog_head
.prev
= b
;
215 sprintf(path
, "%s/binlog.%d", binlog_dir
, ++binlog_index
);
217 if (!add_binlog(path
)) return -1;
218 fd
= open(path
, O_WRONLY
| O_CREAT
, 0400);
221 twarnx("Cannot open binlog %s", path
);
226 bytes_written
= write(fd
, &binlog_version
, sizeof(int));
228 if (bytes_written
< sizeof(int)) {
229 twarnx("Cannot write to binlog");
240 if (binlog_fd
< 0) return;
242 binlog_fd
= binlog_open();
246 binlog_write_job(job j
)
248 size_t tube_namelen
, to_write
;
249 struct iovec vec
[4], *vptr
;
252 if (binlog_fd
< 0) return;
255 vec
[0].iov_base
= (char *) &tube_namelen
;
256 vec
[0].iov_len
= sizeof(size_t);
257 to_write
= sizeof(size_t);
259 vec
[1].iov_base
= j
->tube
->name
;
262 /* we could save some bytes in the binlog file by only saving some parts of
264 vec
[2].iov_base
= (char *) j
;
265 vec
[2].iov_len
= sizeof(struct job
);
266 to_write
+= sizeof(struct job
);
268 if (j
->state
== JOB_STATE_READY
|| j
->state
== JOB_STATE_DELAYED
) {
270 tube_namelen
= strlen(j
->tube
->name
);
271 vec
[1].iov_len
= tube_namelen
;
272 to_write
+= tube_namelen
;
273 j
->binlog
= binlog_iref(binlog_head
.prev
);
275 vec
[3].iov_base
= j
->body
;
276 vec
[3].iov_len
= j
->body_size
;
277 to_write
+= j
->body_size
;
279 } else if (j
->state
== JOB_STATE_INVALID
) {
280 if (j
->binlog
) binlog_dref(j
->binlog
);
284 if ((bytes_written
+ to_write
) > binlog_size_limit
) binlog_open_next();
285 if (binlog_fd
< 0) return;
287 while (to_write
> 0) {
288 size_t written
= writev(binlog_fd
, vec
, vcnt
);
291 twarnx("Cannot write to binlog");
296 bytes_written
+= written
;
298 if (to_write
> 0 && written
> 0) {
299 for (vptr
= vec
; written
>= vptr
->iov_len
; vptr
++) {
300 written
-= vptr
->iov_len
;
303 vptr
->iov_base
= (char *) vptr
->iov_base
+ written
;
304 vptr
->iov_len
-= written
;
311 binlog_read(job binlog_jobs
)
313 int binlog_index_min
;
318 binlog_head
.prev
= binlog_head
.next
= &binlog_head
;
320 if (!binlog_dir
) return;
322 if (stat(binlog_dir
, &sbuf
) < 0) {
323 if (mkdir(binlog_dir
, 0700) < 0) return twarnx("%s", binlog_dir
);
324 } else if (!(sbuf
.st_mode
& S_IFDIR
)) {
325 twarnx("%s", binlog_dir
);
329 binlog_index_min
= binlog_scan_dir();
331 if (binlog_index_min
) {
332 for (idx
= binlog_index_min
; idx
<= binlog_index
; idx
++) {
333 sprintf(path
, "%s/binlog.%d", binlog_dir
, idx
);
334 fd
= open(path
, O_RDONLY
);
340 binlog_replay(fd
, binlog_jobs
);
351 binlog_fd
= binlog_open();