15 static void warnpos(File
*, int, char*, ...);
16 static int readrec(File
*, job
, int*);
17 static int readrec5(File
*, job
, int*);
18 static int readfull(File
*, void*, int, int*, char*);
20 FAlloc
*falloc
= &rawfalloc
;
27 typedef struct Jobrec5 Jobrec5
;
35 uint64 created_at
; // usec
36 uint64 deadline_at
; // usec
49 Jobrec5size
= offsetof(Jobrec5
, pad
)
72 fileaddjob(File
*f
, job j
)
77 if (!h
->fprev
) h
->fprev
= h
;
88 filermjob(File
*f
, job j
)
91 if (f
!= j
->file
) return;
92 j
->fnext
->fprev
= j
->fprev
;
93 j
->fprev
->fnext
= j
->fnext
;
97 f
->w
->alive
-= j
->walused
;
103 // Fileread reads jobs from f->path into list.
104 // It returns 0 on success, or 1 if any errors occurred.
106 fileread(File
*f
, job list
)
110 if (!readfull(f
, &v
, sizeof(v
), &err
, "version")) {
116 while (readrec(f
, list
, &err
));
121 while (readrec5(f
, list
, &err
));
126 warnx("%s: unknown version: %d", f
->path
, v
);
131 // Readrec reads a record from f->fd into linked list l.
132 // If an error occurs, it sets *err to 1.
133 // Readrec returns the number of records read, either 1 or 0.
135 readrec(File
*f
, job l
, int *err
)
142 char tubename
[MAX_TUBE_NAME_LEN
];
144 r
= read(f
->fd
, &namelen
, sizeof(int));
147 warnpos(f
, 0, "error");
151 if (r
!= sizeof(int)) {
155 if (namelen
>= MAX_TUBE_NAME_LEN
) {
156 warnpos(f
, -r
, "namelen %d exceeds maximum of %d", namelen
, MAX_TUBE_NAME_LEN
- 1);
162 warnpos(f
, -r
, "namelen %d is negative", namelen
);
168 r
= readfull(f
, tubename
, namelen
, err
, "tube name");
174 tubename
[namelen
] = '\0';
176 r
= readfull(f
, &jr
, sizeof(Jobrec
), err
, "job struct");
182 // are we reading trailing zeroes?
183 if (!jr
.id
) return 0;
186 if (!(j
|| namelen
)) {
187 // We read a short record without having seen a
188 // full record for this job, so the full record
189 // was in an eariler file that has been deleted.
190 // Therefore the job itself has either been
191 // deleted or migrated; either way, this record
192 // should be ignored.
203 if (jr
.body_size
> job_data_size_limit
) {
204 warnpos(f
, -r
, "job %"PRIu64
" is too big (%zd > %zd)", jr
.id
, job_data_size_limit
);
207 t
= tube_find_or_make(tubename
);
208 j
= make_job_with_id(jr
.pri
, jr
.delay
, jr
.ttr
, jr
.body_size
,
210 j
->next
= j
->prev
= j
;
211 j
->r
.created_at
= jr
.created_at
;
216 // full record; read the job body
218 if (jr
.body_size
!= j
->r
.body_size
) {
219 warnpos(f
, -r
, "job %"PRIu64
" size changed", j
->r
.id
);
220 warnpos(f
, -r
, "was %zu, now %zu", j
->r
.body_size
, jr
.body_size
);
223 r
= readfull(f
, j
->body
, j
->r
.body_size
, err
, "job body");
229 // since this is a full record, we can move
230 // the file pointer and decref the old
232 filermjob(j
->file
, j
);
242 filermjob(j
->file
, j
);
252 filermjob(j
->file
, j
);
259 // Readrec5 is like readrec, but it reads a record in "version 5"
260 // of the log format.
262 readrec5(File
*f
, job l
, int *err
)
269 char tubename
[MAX_TUBE_NAME_LEN
];
271 r
= read(f
->fd
, &namelen
, sizeof(namelen
));
274 warnpos(f
, 0, "error");
278 if (r
!= sizeof(namelen
)) {
282 if (namelen
>= MAX_TUBE_NAME_LEN
) {
283 warnpos(f
, -r
, "namelen %d exceeds maximum of %d", namelen
, MAX_TUBE_NAME_LEN
- 1);
289 r
= readfull(f
, tubename
, namelen
, err
, "v5 tube name");
295 tubename
[namelen
] = '\0';
297 r
= readfull(f
, &jr
, Jobrec5size
, err
, "v5 job struct");
303 // are we reading trailing zeroes?
304 if (!jr
.id
) return 0;
307 if (!(j
|| namelen
)) {
308 // We read a short record without having seen a
309 // full record for this job, so the full record
310 // was in an eariler file that has been deleted.
311 // Therefore the job itself has either been
312 // deleted or migrated; either way, this record
313 // should be ignored.
324 if (jr
.body_size
> job_data_size_limit
) {
325 warnpos(f
, -r
, "job %"PRIu64
" is too big (%zd > %zd)", jr
.id
, job_data_size_limit
);
328 t
= tube_find_or_make(tubename
);
329 j
= make_job_with_id(jr
.pri
, jr
.delay
, jr
.ttr
, jr
.body_size
,
331 j
->next
= j
->prev
= j
;
332 j
->r
.created_at
= jr
.created_at
;
336 j
->r
.delay
= jr
.delay
* 1000; // us => ns
337 j
->r
.ttr
= jr
.ttr
* 1000; // us => ns
338 j
->r
.body_size
= jr
.body_size
;
339 j
->r
.created_at
= jr
.created_at
* 1000; // us => ns
340 j
->r
.deadline_at
= jr
.deadline_at
* 1000; // us => ns
341 j
->r
.reserve_ct
= jr
.reserve_ct
;
342 j
->r
.timeout_ct
= jr
.timeout_ct
;
343 j
->r
.release_ct
= jr
.release_ct
;
344 j
->r
.bury_ct
= jr
.bury_ct
;
345 j
->r
.kick_ct
= jr
.kick_ct
;
346 j
->r
.state
= jr
.state
;
349 // full record; read the job body
351 if (jr
.body_size
!= j
->r
.body_size
) {
352 warnpos(f
, -r
, "job %"PRIu64
" size changed", j
->r
.id
);
353 warnpos(f
, -r
, "was %zu, now %zu", j
->r
.body_size
, jr
.body_size
);
356 r
= readfull(f
, j
->body
, j
->r
.body_size
, err
, "v5 job body");
362 // since this is a full record, we can move
363 // the file pointer and decref the old
365 filermjob(j
->file
, j
);
375 filermjob(j
->file
, j
);
385 filermjob(j
->file
, j
);
393 readfull(File
*f
, void *c
, int n
, int *err
, char *desc
)
397 r
= read(f
->fd
, c
, n
);
400 warnpos(f
, 0, "error reading %s", desc
);
405 warnpos(f
, -r
, "unexpected EOF reading %d bytes (got %d): %s", n
, r
, desc
);
414 warnpos(File
*f
, int adj
, char *fmt
, ...)
419 off
= lseek(f
->fd
, 0, SEEK_CUR
);
420 fprintf(stderr
, "%s:%u: ", f
->path
, off
+adj
);
422 vfprintf(stderr
, fmt
, ap
);
428 // Opens f for writing, writes a header, and initializes
429 // f->free and f->resv.
430 // Sets f->iswopen if successful.
438 fd
= open(f
->path
, O_WRONLY
|O_CREAT
, 0400);
440 twarn("open %s", f
->path
);
444 r
= falloc(fd
, f
->w
->filesize
);
448 twarn("falloc %s", f
->path
);
451 twarn("unlink %s", f
->path
);
456 n
= write(fd
, &ver
, sizeof(int));
457 if (n
< sizeof(int)) {
458 twarn("write %s", f
->path
);
466 f
->free
= f
->w
->filesize
- n
;
472 filewrite(File
*f
, job j
, void *buf
, int len
)
476 r
= write(f
->fd
, buf
, len
);
492 filewrjobshort(File
*f
, job j
)
496 nl
= 0; // name len 0 indicates short record
497 r
= filewrite(f
, j
, &nl
, sizeof nl
) &&
498 filewrite(f
, j
, &j
->r
, sizeof j
->r
);
501 if (j
->r
.state
== Invalid
) {
502 filermjob(j
->file
, j
);
510 filewrjobfull(File
*f
, job j
)
515 nl
= strlen(j
->tube
->name
);
517 filewrite(f
, j
, &nl
, sizeof nl
) &&
518 filewrite(f
, j
, j
->tube
->name
, nl
) &&
519 filewrite(f
, j
, &j
->r
, sizeof j
->r
) &&
520 filewrite(f
, j
, j
->body
, j
->r
.body_size
);
528 if (!f
->iswopen
) return;
530 (void)ftruncate(f
->fd
, f
->w
->filesize
- f
->free
);
539 fileinit(File
*f
, Wal
*w
, int n
)
543 f
->path
= fmtalloc("%s/binlog.%d", w
->dir
, n
);
548 // Adds f to the linked list in w,
549 // updating w->tail and w->head as necessary.
551 fileadd(File
*f
, Wal
*w
)