From a51f552b6cd1838c4f0281f6529616790f98fcf7 Mon Sep 17 00:00:00 2001 From: Yunpeng Li Date: Sun, 22 Nov 2026 10:50:35 -0800 Subject: [PATCH] AVA1 1.0.12: read-back progress in Status, no second read-back after a resume in the same process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - C read-back: a job taken up again from its journal in the same process (a Stop then a resume, a failed job resumed, a job parked past its age) no longer reads back what its earlier run already read back. A destroyed job's unfinished large files keep their read-back ranges in memory with the identity of the part file and outboard (device, inode, size, modify and change times); the next run counts them as read back only when still durable by the journal and both files are unchanged. A file written in between, one found wrong before, and every job after a restart are read back whole as before. - Status ext `readback_left` (tag 6) for a JF_READBACK job: bytes of its unfinished large files not read back yet, counting the commit's read-back as it goes. Older senders skip the tag. - A data write that failed over bytes already read back takes them out of what was read back. - Rust sender: Progress::readback_left. - SPEC.md §13.5, changelog, version 1.0.12. Tests: a_file_resumed_in_the_same_process_is_not_read_back_again (fails without the fix: 40 MiB read back instead of 20), a_file_written_between_two_runs_is_read_back_whole, a_job_without_the_read_back_reports_no_readback_left, status_readback_left_is_an_optional_extension. `make check` passes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GtiJiJdHq1hekoZ6vizvVT --- CHANGELOG.md | 25 +++ Cargo.lock | 8 +- Cargo.toml | 2 +- c/ava1_apply.c | 242 ++++++++++++++++++++++++--- c/ava1_apply.h | 5 + c/ava1_data.c | 1 + c/ava1_job.c | 1 + c/ava1_job.h | 4 + c/gen/ava1_gen.c | 11 ++ c/gen/ava1_gen.h | 3 + crates/ava1-ctest/csrc/test_shim.c | 15 ++ crates/ava1-ctest/tests/readback.rs | 151 +++++++++++++++++ crates/ava1-gen/src/core_schema.toml | 1 + crates/ava1/src/gen.rs | 12 +- crates/ava1/src/send.rs | 7 + crates/ava1/tests/schema.rs | 17 ++ schema/ava1.toml | 1 + spec/SPEC.md | 12 +- 18 files changed, 487 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a2b629..87ee0ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,31 @@ All notable changes to the AVA1 crates and C library. The wire format is version 1 throughout; see [README](README.md#versions). +## 1.0.12 + +Read-back progress, and no second read-back of a resumed file in the same process. The wire format gains one +optional field (old peers skip it); nothing changes on disk. + +C library: + +- Read-back: a job taken up again from its journal in the same process (a Stop then a resume, a failed job + resumed, a job parked past its age) no longer reads back what its earlier run already read back. The + process keeps each unfinished large file's read-back ranges when a job is destroyed, with the identity of its + part file and outboard (device, inode, size, modify and change times), and the next run counts those ranges + as read back when they are still durable by the journal and both files are unchanged. A file written in + between, one whose read-back found it wrong, and any job after a restart are read back whole as before + (§13.5). Before, a 156 GB part file resumed after a stop was read back again, mostly after its last byte: + minutes of "finishing" with the upload done. +- `Status` carries ext `readback_left` (tag 6) for a job with `JF_READBACK`: the bytes of its unfinished + large files not read back yet, counting the commit's read-back as it goes, so a sender can show the + read-back after the last byte as progress. +- A write of file data that failed part way over bytes already read back takes them out of what was read + back, as a write that succeeds does. + +Rust sender: + +- `Progress::readback_left` holds the receiver's last `readback_left` (0 when it sends none). + ## 1.0.11 Read-back of a resumed file while it arrives. Nothing changes on the wire format, on disk or in the diff --git a/Cargo.lock b/Cargo.lock index 8115d8d..5ec1ea8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,7 +55,7 @@ checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" [[package]] name = "ava1" -version = "1.0.11" +version = "1.0.12" dependencies = [ "ava1-chaos", "blake2 0.11.0", @@ -74,14 +74,14 @@ dependencies = [ [[package]] name = "ava1-chaos" -version = "1.0.11" +version = "1.0.12" dependencies = [ "tokio", ] [[package]] name = "ava1-ctest" -version = "1.0.11" +version = "1.0.12" dependencies = [ "ava1", "ava1-chaos", @@ -93,7 +93,7 @@ dependencies = [ [[package]] name = "ava1-gen" -version = "1.0.11" +version = "1.0.12" dependencies = [ "serde", "toml", diff --git a/Cargo.toml b/Cargo.toml index 813b733..a6d9944 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ resolver = "3" [workspace.package] edition = "2021" license = "MIT OR Apache-2.0" -version = "1.0.11" +version = "1.0.12" # File::lock (1.89). CI builds the published crates with exactly this version. rust-version = "1.89" repository = "https://github.com/phantomptr/ava1" diff --git a/c/ava1_apply.c b/c/ava1_apply.c index 29e8c41..57e2cf9 100644 --- a/c/ava1_apply.c +++ b/c/ava1_apply.c @@ -423,10 +423,12 @@ static void emit_durable(ava1_job_t *j, const ava1_file_run_t *runs, uint32_t nr free(out); } +static uint64_t rb_bytes_left(const ava1_job_t *j); + void ava1_apply_status(ava1_job_t *j) { ava1_status_t st; char sm[96]; - uint8_t b[192]; + uint8_t b[256]; ava1_w_t w; uint8_t bn; const ava1_data_cfg_t *cfg = ava1_data_cfg(); @@ -449,6 +451,10 @@ void ava1_apply_status(ava1_job_t *j) { st.has_unswept = 1; st.unswept = j->unswept_n; } + if (j->readback) { /* how much the read-back has left: a sender shows it after the last byte (§13.5) */ + st.has_readback_left = 1; + st.readback_left = rb_bytes_left(j); + } if (j->sweep_err) { /* files cannot be made durable: the sender must hear it (SPEC.md §15.7) */ st.has_code = 1; st.code = AVA1_ERR_IO; @@ -1077,26 +1083,24 @@ static int write_chunk(ava1_job_t *j, uint32_t id, uint64_t off, const uint8_t * if (err == 0 && __atomic_load_n(&j->perchunk, __ATOMIC_RELAXED)) err = chunk_sync(j, fd, d, len, off); close(fd); if (ob >= 0) close(ob); + pthread_mutex_lock(&j->mu); + lf->writers--; + /* Over durable bytes (a duplicate): they are not durable again until their batch, so what was read back + * of them no longer counts, and a read-back of the file in flight is void (see "read-back"). A write + * that failed may have changed some of them too: what was read back of them counts for nothing either + * (the job fails, and a later run of it in this process must not take them as read, rb_keep_save). */ + if (!lf->committed && ava1_rset_overlaps(&lf->durable, off, off + len)) { + if (ava1_rset_remove(&lf->rb_todo, off, off + len) < 0) ava1_rset_clear(&lf->rb_todo); + if (ava1_rset_remove(&lf->rb_done, off, off + len) < 0) ava1_rset_clear(&lf->rb_done); + if (!err && lf->rb_found && lf->rb_found_g * AVA1_GROUP_LEN >= off && lf->rb_found_g * AVA1_GROUP_LEN < off + len) + lf->rb_found = 0; /* the wrong group was written again: it is read back again */ + lf->rb_gen++; + } if (err) { - pthread_mutex_lock(&j->mu); - lf->writers--; pthread_mutex_unlock(&j->mu); return err; } - pthread_mutex_lock(&j->mu); - lf->writers--; - if (!lf->committed) { /* else: a late duplicate */ - (void)ava1_rset_add(&lf->written, off, off + len); - /* Over durable bytes (a duplicate): they are not durable again until their batch, so what was read - * back of them no longer counts, and a read-back of the file in flight is void (see "read-back"). */ - if (ava1_rset_overlaps(&lf->durable, off, off + len)) { - if (ava1_rset_remove(&lf->rb_todo, off, off + len) < 0) ava1_rset_clear(&lf->rb_todo); - if (ava1_rset_remove(&lf->rb_done, off, off + len) < 0) ava1_rset_clear(&lf->rb_done); - if (lf->rb_found && lf->rb_found_g * AVA1_GROUP_LEN >= off && lf->rb_found_g * AVA1_GROUP_LEN < off + len) - lf->rb_found = 0; /* the wrong group was written again: it is read back again */ - lf->rb_gen++; - } - } + if (!lf->committed) (void)ava1_rset_add(&lf->written, off, off + len); /* else: a late duplicate */ j->unsynced_bytes += len; j->bytes_received += len; j->applied_since_tune++; @@ -2910,6 +2914,7 @@ void ava1_apply_reset(ava1_job_t *j, uint32_t id, uint16_t reason) { ava1_rset_clear(&lf->durable); ava1_rset_clear(&lf->rb_todo); ava1_rset_clear(&lf->rb_done); + __atomic_store_n(&lf->rb_cread, 0, __ATOMIC_RELAXED); lf->rb_found = 0; lf->rb_found_g = 0; lf->rb_gen++; /* a live read-back of the old bytes in flight counts for nothing */ @@ -3058,7 +3063,7 @@ static void rb_say_how(ava1_job_t *j, int drop, const char *how, int direct) { * first group that differs; a file shorter than its size counts as wrong); -errno = they could not be * read; -EINTR = the job is ending, or `*quit` was set (another stripe already has the answer). */ static int rb_check(ava1_job_t *j, uint32_t id, int rfd, int pfd, int ob_fd, uint64_t from, uint64_t to, uint8_t *buf, - const int *quit, int live, uint64_t *bad) { + const int *quit, int live, uint64_t *bad, uint64_t *count) { uint8_t cvs[READBACK_GROUPS][32]; uint64_t off = from; while (off < to) { @@ -3106,6 +3111,7 @@ static int rb_check(ava1_job_t *j, uint32_t id, int rfd, int pfd, int ob_fd, uin } __atomic_add_fetch(&j->rb_hash_us, mono_us() - t_hash, __ATOMIC_RELAXED); if (rc) return rc; + if (count) __atomic_add_fetch(count, len, __ATOMIC_RELAXED); /* the commit's progress, for Status */ off += len; } return 0; @@ -3185,7 +3191,7 @@ static int rb_live_one(ava1_job_t *j, int crowded) { pthread_mutex_unlock(&j->mu); if ((rfd = rb_open_direct(j, id)) < 0) rfd = fd; if (!(buf = rb_buf_alloc())) rc = -ENOMEM; - else rc = rb_check(j, id, rfd, fd, ob, from, to, buf, NULL, 1, &bad); + else rc = rb_check(j, id, rfd, fd, ob, from, to, buf, NULL, 1, &bad, NULL); free(buf); if (rfd != fd) close(rfd); close(fd); @@ -3277,6 +3283,187 @@ static void rb_live_kick(ava1_job_t *j) { j->id[1], j->id[2], j->id[3], n); } +/* ---- what was read back, kept for a later run of the same job in this process ---- + * A job taken up again from its journal (a Stop then a resume, a failure the sender resumes, a job parked past + * its age) used to start from nothing read back: all its files held durable was read back again, and on a + * drive that cannot read back as fast as the data arrives, most of it after the last byte. The journal records + * what is durable, not what was read back, so the process keeps that itself: when a job is destroyed, each + * unfinished large file's rb_done moves here with the identity of its part file and outboard (device, inode, + * size, modify and change times), and when the job is taken up again in this process, what of it is still + * durable by the journal, of files that are still the same, counts as read back. Everything else is read + * again: a file reset or written since (its ranges or its times changed), one whose read-back found it wrong + * before, a job taken up after a restart (nothing is kept across one: ava1_data_stop forgets it all). As within + * one run (SPEC.md §13.5), a byte that changes on the drive after its group was read back, without its file + * being written, is not found. */ +#define RB_KEEP_MAX 16u + +typedef struct { + uint64_t dev, ino, size; + int64_t mt_s, mt_ns, ct_s, ct_ns; +} rb_ident_t; + +typedef struct { + uint8_t job[16]; + uint32_t file_id; + uint64_t size; /* the manifest entry's */ + rb_ident_t part, ob; + ava1_rset_t done; + uint64_t seq; /* the oldest goes first when the table is full */ +} rb_keep_t; + +static pthread_mutex_t rb_keep_mu = PTHREAD_MUTEX_INITIALIZER; +static rb_keep_t rb_keep[RB_KEEP_MAX]; +static uint32_t rb_keep_n; +static uint64_t rb_keep_seq; + +static void rb_ident_of(const struct stat *st, rb_ident_t *o) { + memset(o, 0, sizeof *o); + o->dev = (uint64_t)st->st_dev; + o->ino = (uint64_t)st->st_ino; + o->size = (uint64_t)st->st_size; +#if defined(__APPLE__) + o->mt_s = (int64_t)st->st_mtimespec.tv_sec; + o->mt_ns = (int64_t)st->st_mtimespec.tv_nsec; + o->ct_s = (int64_t)st->st_ctimespec.tv_sec; + o->ct_ns = (int64_t)st->st_ctimespec.tv_nsec; +#else + o->mt_s = (int64_t)st->st_mtim.tv_sec; + o->mt_ns = (int64_t)st->st_mtim.tv_nsec; + o->ct_s = (int64_t)st->st_ctim.tv_sec; + o->ct_ns = (int64_t)st->st_ctim.tv_nsec; +#endif +} + +/* The identity of file `id`'s part file and outboard, as they are on the drive now. 0, or -1. */ +static int rb_idents(const ava1_job_t *j, uint32_t id, rb_ident_t *part, rb_ident_t *ob) { + char p[PATH_CAP], o[600]; + struct stat st; + ava1_apply_path(j, id, 1, p, sizeof p); + snprintf(o, sizeof o, "%s/%u.ob", j->dir, id); + if (!p[0] || stat(p, &st) != 0) return -1; + rb_ident_of(&st, part); + if (stat(o, &st) != 0) return -1; + rb_ident_of(&st, ob); + return 0; +} + +/* Caller holds rb_keep_mu. Takes entry k out of the table; *out gets it (or it is freed when NULL). */ +static void rb_keep_remove_locked(uint32_t k, rb_keep_t *out) { + if (out) *out = rb_keep[k]; + else ava1_rset_clear(&rb_keep[k].done); + rb_keep[k] = rb_keep[--rb_keep_n]; + memset(&rb_keep[rb_keep_n], 0, sizeof rb_keep[rb_keep_n]); +} + +/* Caller holds rb_keep_mu. The entry of (job, file `id`), or UINT32_MAX. */ +static uint32_t rb_keep_find_locked(const uint8_t job[16], uint32_t id) { + uint32_t k; + for (k = 0; k < rb_keep_n; k++) + if (rb_keep[k].file_id == id && memcmp(rb_keep[k].job, job, 16) == 0) return k; + return UINT32_MAX; +} + +/* job_destroy, every thread of the job joined: what each unfinished large file has read back moves to the + * table (see above). */ +void ava1_apply_rb_keep_save(ava1_job_t *j) { + uint32_t id; + if (!j->lf || j->discard_parts) return; + for (id = 0; id < j->m.n; id++) { + ava1_lfile_t *lf = j->lf[id]; + rb_keep_t e; + uint32_t k; + if (!lf || !lf->rb_done.n || lf->committed || lf->rb_bad || lf->rb_found || ava1_bits_get(&j->done, id) || + groups_of(j->m.e[id].size) < 2) + continue; + memset(&e, 0, sizeof e); + if (rb_idents(j, id, &e.part, &e.ob) != 0) continue; + memcpy(e.job, j->id, 16); + e.file_id = id; + e.size = j->m.e[id].size; + e.done = lf->rb_done; /* moved: the job is going */ + memset(&lf->rb_done, 0, sizeof lf->rb_done); + pthread_mutex_lock(&rb_keep_mu); + if ((k = rb_keep_find_locked(j->id, id)) != UINT32_MAX) rb_keep_remove_locked(k, NULL); /* this run is newer */ + if (rb_keep_n == RB_KEEP_MAX) { + uint32_t old = 0; + for (k = 1; k < rb_keep_n; k++) + if (rb_keep[k].seq < rb_keep[old].seq) old = k; + rb_keep_remove_locked(old, NULL); + } + e.seq = ++rb_keep_seq; + rb_keep[rb_keep_n++] = e; + pthread_mutex_unlock(&rb_keep_mu); + } +} + +void ava1_apply_rb_keep_clear(void) { + pthread_mutex_lock(&rb_keep_mu); + while (rb_keep_n) rb_keep_remove_locked(rb_keep_n - 1, NULL); + pthread_mutex_unlock(&rb_keep_mu); +} + +/* Caller holds j->mu; job thread, when the job's map is answered (only the first time does anything): what + * this process kept of the job's files (see above) and is still theirs counts as read back. Returns those + * bytes. */ +static uint64_t rb_keep_restore(ava1_job_t *j) { + uint64_t bytes = 0; + uint32_t k, n; + if (j->rb_keep_looked || !j->lf) return 0; + j->rb_keep_looked = 1; + n = j->lfl_all ? j->m.n : j->lfl_n; + for (k = 0; k < n; k++) { + uint32_t id = j->lfl_all ? k : j->lfl[k], at; + ava1_lfile_t *lf = j->lf[id]; + uint64_t size = j->m.e[id].size; + rb_ident_t part, ob; + rb_keep_t e; + size_t a = 0, b = 0; + pthread_mutex_lock(&rb_keep_mu); + at = rb_keep_find_locked(j->id, id); + if (at != UINT32_MAX) rb_keep_remove_locked(at, &e); + pthread_mutex_unlock(&rb_keep_mu); + if (at == UINT32_MAX) continue; + if (!lf || e.size != size || lf->rb_done.n || lf->committed || lf->committing || lf->rb_busy || + ava1_bits_get(&j->done, id) || rb_idents(j, id, &part, &ob) != 0 || memcmp(&part, &e.part, sizeof part) != 0 || + memcmp(&ob, &e.ob, sizeof ob) != 0) { + ava1_rset_clear(&e.done); + continue; + } + /* rb_done = kept ∩ durable, in whole groups (the last one may end at the file's end) */ + while (a < e.done.n && b < lf->durable.n) { + uint64_t ea = e.done.v[2 * a], eb = e.done.v[2 * a + 1], da = lf->durable.v[2 * b], db = lf->durable.v[2 * b + 1]; + uint64_t lo = ea > da ? ea : da, hi = eb < db ? eb : db; + lo = (lo + AVA1_GROUP_LEN - 1) / AVA1_GROUP_LEN * AVA1_GROUP_LEN; + if (hi > size) hi = size; + if (hi < size) hi = hi / AVA1_GROUP_LEN * AVA1_GROUP_LEN; + if (lo < hi && ava1_rset_add(&lf->rb_done, lo, hi) == 0) bytes += hi - lo; /* out of memory: read again */ + if (eb < db) a++; + else b++; + } + ava1_rset_clear(&e.done); + } + return bytes; +} + +/* Caller holds j->mu. JF_READBACK: bytes of the job's unfinished large files not yet read back (live, kept + * from an earlier run, or by the commit's read-back so far): Status `readback_left` (SPEC.md §13.5). A file of + * one group is not read back; a file no chunk has reached yet is not counted. */ +static uint64_t rb_bytes_left(const ava1_job_t *j) { + uint64_t left = 0; + uint32_t k, n; + if (!j->lf || !j->prepared) return 0; + n = j->lfl_all ? j->m.n : j->lfl_n; + for (k = 0; k < n; k++) { + uint32_t id = j->lfl_all ? k : j->lfl[k]; + const ava1_lfile_t *lf = j->lf[id]; + uint64_t size = j->m.e[id].size, have; + if (!lf || lf->committed || ava1_bits_get(&j->done, id) || groups_of(size) < 2) continue; + have = ava1_rset_covered(&lf->rb_done) + __atomic_load_n(&lf->rb_cread, __ATOMIC_RELAXED); + if (have < size) left += size - have; + } + return left; +} + /* Caller holds j->mu. Hands what file `id` holds durable and nobody has read back (nor is to: rb_todo) to the * live read-back; returns those bytes. */ static uint64_t rb_hand_over(ava1_job_t *j, uint32_t id) { @@ -3325,11 +3512,12 @@ static uint64_t rb_hand_over(ava1_job_t *j, uint32_t id) { * number of times: a range read back, waiting to be, or being read is not handed over again. A job taken up * a second time starts from nothing read back (the journal records what is durable, not what was read). */ void ava1_apply_readback_resumed(ava1_job_t *j) { - uint64_t bytes = 0; + uint64_t bytes = 0, kept = 0; uint32_t k, files = 0; pthread_mutex_lock(&j->mu); if (j->readback && j->lf && j->prepared && !j->finished && !j->final_status && !j->stopping) { uint32_t n = j->lfl_all ? j->m.n : j->lfl_n; + kept = rb_keep_restore(j); for (k = 0; k < n; k++) { uint64_t got = rb_hand_over(j, j->lfl_all ? k : j->lfl[k]); bytes += got; @@ -3337,6 +3525,9 @@ void ava1_apply_readback_resumed(ava1_job_t *j) { } } pthread_mutex_unlock(&j->mu); + if (kept) + fprintf(stderr, "[ava1] job %02x%02x%02x%02x: read-back: %llu MiB read back by an earlier run are not read again\n", + j->id[0], j->id[1], j->id[2], j->id[3], (unsigned long long)(kept >> 20)); if (bytes) fprintf(stderr, "[ava1] job %02x%02x%02x%02x: read-back: %llu MiB of %u files, durable before, are read back while the rest " @@ -3359,6 +3550,7 @@ typedef struct { uint64_t size; int fd, rfd, ob_fd; /* rfd: what reads (O_DIRECT, or fd); see rb_check */ int *quit; /* shared: set when a stripe has the answer (a mismatch or an error) */ + uint64_t *count; /* shared: bytes read and found right (the file's rb_cread) */ uint64_t bad; int rc; } rb_stripe_t; @@ -3374,7 +3566,8 @@ static int readback_stripe(rb_stripe_t *s) { uint64_t n = s->sp[k].g1 - s->sp[k].g0, lo = s->from > at ? s->from - at : 0, hi = s->to - at < n ? s->to - at : n; if (lo < hi) { uint64_t b0 = (s->sp[k].g0 + lo) * AVA1_GROUP_LEN, b1 = (s->sp[k].g0 + hi) * AVA1_GROUP_LEN; - rc = rb_check(s->j, s->id, s->rfd, s->fd, s->ob_fd, b0, b1 < s->size ? b1 : s->size, buf, s->quit, 0, &s->bad); + rc = rb_check(s->j, s->id, s->rfd, s->fd, s->ob_fd, b0, b1 < s->size ? b1 : s->size, buf, s->quit, 0, &s->bad, + s->count); } at += n; } @@ -3393,7 +3586,7 @@ static void *readback_stripe_main(void *arg) { * what was sent; 1 = it does not (*bad = the first group found wrong); -errno = it could not be read; * -EINTR = the job is ending. */ static int readback_large(ava1_job_t *j, uint32_t id, int fd, int rfd, int ob_fd, const rb_span_t *sp, uint32_t nsp, uint64_t total, - uint64_t *bad) { + uint64_t *count, uint64_t *bad) { uint64_t size = j->m.e[id].size, per; rb_stripe_t s[READBACK_STRIPES]; pthread_t th[READBACK_STRIPES]; @@ -3429,6 +3622,7 @@ static int readback_large(ava1_job_t *j, uint32_t id, int fd, int rfd, int ob_fd s[k].rfd = rfd; s[k].ob_fd = ob_fd; s[k].quit = &quit; + s[k].count = count; s[k].bad = 0; s[k].rc = 0; } @@ -3558,6 +3752,7 @@ static void commit_large(ava1_job_t *j, uint32_t id) { pthread_mutex_lock(&j->mu); /* Its live read-backs in flight end first (no new one starts: `committing` is set). */ while (lf->rb_busy && !j->stopping) pthread_cond_wait(&j->cv, &j->mu); + __atomic_store_n(&lf->rb_cread, 0, __ATOMIC_RELAXED); found = lf->rb_found; bad = lf->rb_found_g; if (j->stopping) rb = -EINTR; @@ -3566,7 +3761,7 @@ static void commit_large(ava1_job_t *j, uint32_t id) { if (found) rb = 1; else if (!rb && total) { int rfd = rb_open_direct(j, id); /* one for every stripe: pread shares it */ - rb = readback_large(j, id, lf->fd, rfd >= 0 ? rfd : lf->fd, lf->ob_fd, sp, nsp, total, &bad); + rb = readback_large(j, id, lf->fd, rfd >= 0 ? rfd : lf->fd, lf->ob_fd, sp, nsp, total, &lf->rb_cread, &bad); if (rfd >= 0) close(rfd); } free(sp); @@ -3575,6 +3770,7 @@ static void commit_large(ava1_job_t *j, uint32_t id) { if (rb == -EINTR) { /* the job is ending: a resume commits (and reads back) the file again */ pthread_mutex_lock(&j->mu); lf->committing = 0; + __atomic_store_n(&lf->rb_cread, 0, __ATOMIC_RELAXED); pthread_mutex_unlock(&j->mu); return; } diff --git a/c/ava1_apply.h b/c/ava1_apply.h index 6e94176..2332c94 100644 --- a/c/ava1_apply.h +++ b/c/ava1_apply.h @@ -72,6 +72,11 @@ void ava1_apply_commit_ready(ava1_job_t *j); /* JF_READBACK: hands the durable ranges nobody has read back (those a job found in its journal) to the * live read-back. Job thread, once the job is prepared; does nothing without the read-back. */ void ava1_apply_readback_resumed(ava1_job_t *j); +/* JF_READBACK: what a destroyed job's unfinished large files had read back is kept in this process, so a later + * run of the job taken up from its journal does not read it again (ava1_apply.c, "what was read back, kept"). + * Save: job_destroy, its threads joined. Clear: when the data layer stops (nothing is kept across a restart). */ +void ava1_apply_rb_keep_save(ava1_job_t *j); +void ava1_apply_rb_keep_clear(void); /* One line: where the job's time went, as a share of its wall time (scan, data fsync, directory * fsyncs, journal on the job thread; commit and preallocate summed over workers, so they can pass * 100% together). Printed once when the job ends, always. Returns the length written. */ diff --git a/c/ava1_data.c b/c/ava1_data.c index 9e257ef..3ec1191 100644 --- a/c/ava1_data.c +++ b/c/ava1_data.c @@ -432,6 +432,7 @@ void ava1_data_stop(void) { pthread_join(D.house, NULL); pthread_join(D.recover, NULL); ava1_job_free_all(); + ava1_apply_rb_keep_clear(); /* a restart reads everything back again */ } /* ---- messages ---------------------------------------------------------------------- */ diff --git a/c/ava1_job.c b/c/ava1_job.c index d89b14b..5edd602 100644 --- a/c/ava1_job.c +++ b/c/ava1_job.c @@ -291,6 +291,7 @@ static void job_destroy(ava1_job_t *j) { (void)unlink(ob); } } + ava1_apply_rb_keep_save(j); /* every thread is joined: what was read back stays for a later run */ if (j->lf) { for (i = 0; i < j->m.n; i++) if (j->lf[i]) { diff --git a/c/ava1_job.h b/c/ava1_job.h index da4a060..b8d9ebb 100644 --- a/c/ava1_job.h +++ b/c/ava1_job.h @@ -66,6 +66,9 @@ typedef struct { /* one large file being assembled */ uint32_t rb_gen; uint8_t rb_busy, rb_found; uint64_t rb_found_g; + /* JF_READBACK: bytes the commit's read-back of this file has read and found right so far (atomic; 0 + * outside a commit), for the job's Status `readback_left`. */ + uint64_t rb_cread; } ava1_lfile_t; /* Durable-by-log (SPEC.md §15.7). A small file's bytes go to `pack.` in the job directory and @@ -236,6 +239,7 @@ struct ava1_job { uint64_t rb_drops, rb_drops_ok, rb_direct_reads; int rb_how_logged, rb_direct_err; /* ... and why O_DIRECT was refused, the last time it was */ uint32_t rb_bufs; /* read-back buffers in use, live and at commit (under mu; at most READBACK_STRIPES) */ + int rb_keep_looked; /* the read-back this process kept of the job's files was looked up (job thread) */ int resumed; /* decided at open (journal replay) or at an attach that finds durable work; never per re-arm */ uint64_t prog_gen; /* the attach generation `resumed` was last decided under */ uint32_t tune_ticks, tune_busy; /* queue occupancy since the last tuning step */ diff --git a/c/gen/ava1_gen.c b/c/gen/ava1_gen.c index 9a63925..7f3bd16 100644 --- a/c/gen/ava1_gen.c +++ b/c/gen/ava1_gen.c @@ -2889,6 +2889,7 @@ int ava1_status_encode(const ava1_status_t *m, ava1_w_t *w) { if (m->has_result) ext_n++; if (m->has_code) ext_n++; if (m->has_unswept) ext_n++; + if (m->has_readback_left) ext_n++; ava1_w_u16(w, ext_n); if (m->has_current) { size_t at = ava1_w_ext_begin(w, 1); @@ -2915,6 +2916,11 @@ int ava1_status_encode(const ava1_status_t *m, ava1_w_t *w) { ava1_w_u32(w, m->unswept); ava1_w_ext_end(w, at); } + if (m->has_readback_left) { + size_t at = ava1_w_ext_begin(w, 6); + ava1_w_u64(w, m->readback_left); + ava1_w_ext_end(w, at); + } return w->err; } @@ -2968,6 +2974,11 @@ int ava1_status_decode(const uint8_t *buf, size_t len, ava1_status_t *m) { m->has_unswept = 1; m->unswept = ava1_r_u32(&vr); break; + case 6: + if (m->has_readback_left) return AVA1_E_DUP_EXT; + m->has_readback_left = 1; + m->readback_left = ava1_r_u64(&vr); + break; default: continue; } diff --git a/c/gen/ava1_gen.h b/c/gen/ava1_gen.h index 40e5e19..66aee30 100644 --- a/c/gen/ava1_gen.h +++ b/c/gen/ava1_gen.h @@ -1194,6 +1194,9 @@ typedef struct { /* Durable-by-log: done files not yet durable in place; absent means 0 (SPEC.md §15.7). */ int has_unswept; uint32_t unswept; + /* JF_READBACK: bytes of the job's unfinished large files not yet read back from the drive; absent when the receiver does not say (SPEC.md §13.5). */ + int has_readback_left; + uint64_t readback_left; } ava1_status_t; int ava1_status_encode(const ava1_status_t *m, ava1_w_t *w); diff --git a/crates/ava1-ctest/csrc/test_shim.c b/crates/ava1-ctest/csrc/test_shim.c index 588f75d..69a311e 100644 --- a/crates/ava1-ctest/csrc/test_shim.c +++ b/crates/ava1-ctest/csrc/test_shim.c @@ -1062,6 +1062,7 @@ static size_t g_ev_len; static int g_ev_done; /* the recorder has seen JOB_DONE */ static int g_same_device = 1; static ava1_job_t *g_job; +static uint64_t g_status_rb_left = UINT64_MAX; /* the last Status's readback_left (UINT64_MAX: absent) */ static uint8_t TEST_JOB[16] = { 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 }; static const uint8_t TEST_OWNER[32] = { 1 }; static int g_trace; /* record hook points as events */ @@ -1548,6 +1549,7 @@ static void rec_emit(ava1_job_t *j, uint8_t type, uint8_t flags, const uint8_t * ava1_status_t st; static uint16_t last_code; if (ava1_status_decode(body, len, &st) != 0) return; + __atomic_store_n(&g_status_rb_left, st.has_readback_left ? st.readback_left : UINT64_MAX, __ATOMIC_SEQ_CST); if (st.has_code && st.code && st.code != last_code) { /* a receiver-side failure the sender must see */ snprintf(line, sizeof line, "status code=%u unswept=%u\n", st.code, st.has_unswept ? st.unswept : 0); ev_add(line, 0); @@ -1974,6 +1976,19 @@ void ava1_test_recv_args(uint8_t kind) { g_kind = kind; } /* Another JobOpen in the same process. drop_old releases the current session's reference * first; when the open is refused the current job (if kept) stays current. */ +/* A Stop: the job is cancelled (JobCancel) and unlisted, its journal kept; the caller's reference stays + * until a reopen drops it. */ +void ava1_test_recv_cancel(void) { + if (g_job) ava1_recv_cancel(g_job); +} + +/* A Status of the current job now: its readback_left, or UINT64_MAX when it carries none. */ +uint64_t ava1_test_status_readback_left(void) { + __atomic_store_n(&g_status_rb_left, UINT64_MAX, __ATOMIC_SEQ_CST); + if (g_job) ava1_apply_status(g_job); + return __atomic_load_n(&g_status_rb_left, __ATOMIC_SEQ_CST); +} + int ava1_test_recv_reopen(int drop_old) { ava1_job_t *old = g_job; int rc; diff --git a/crates/ava1-ctest/tests/readback.rs b/crates/ava1-ctest/tests/readback.rs index f6719ba..e99047d 100644 --- a/crates/ava1-ctest/tests/readback.rs +++ b/crates/ava1-ctest/tests/readback.rs @@ -30,6 +30,8 @@ extern "C" { fn ava1_test_batch_synced_held() -> u32; fn ava1_test_apply_bytes_received() -> u64; fn ava1_test_apply_hold(on: i32); + fn ava1_test_recv_cancel(); + fn ava1_test_status_readback_left() -> u64; } const G: usize = GROUP as usize; @@ -780,6 +782,155 @@ fn ending_a_job_during_a_live_read_back_is_prompt() { assert!(!root.join("big.bin").exists()); } +/// The part file of `dest`'s one large file (a merge: `.ava-part` beside the final name). +fn part_file(dest: &std::path::Path) -> std::path::PathBuf { + std::fs::read_dir(dest) + .unwrap() + .map(|e| e.unwrap().path()) + .find(|p| p.to_string_lossy().ends_with(".ava-part")) + .expect("a part file") +} + +/// A file stopped half way and resumed in the same process (the job cancelled, then taken up again +/// from its journal): what the first run read back is not read again, live or after the last byte. +/// Before 1.0.12 the second run read back all the first run's bytes again (#873: on a console, up to a +/// quarter of an hour after the last byte for a large game). The Status reports what is left to read. +#[test] +fn a_file_resumed_in_the_same_process_is_not_read_back_again() { + let _k = Knobs; + let t = tmp("kept"); + std::fs::create_dir_all(t.join("dest")).unwrap(); + let d = data(40 * G + 123, 21); + let m = Manifest { + entries: vec![file("big", d.len() as u64)], + }; + let r = CRecv::open( + &t.join("jobs"), + &t.join("dest"), + gen::JF_READBACK, + gen::POLICY_REPLACE, + 0, + ); + r.manifest(&m); + r.wait_event("map status=0", 5000); + send_groups(&r, 0, &d, &(0..20).collect::>()); + wait_until("the first half read back", 10_000, || { + rb_stats()[0] == (20 * G) as u64 + }); + assert_eq!( + unsafe { ava1_test_status_readback_left() }, + (d.len() - 20 * G) as u64, + "the Status says what is left to read back" + ); + unsafe { ava1_test_recv_cancel() }; + assert_eq!(r.reopen(true), 0); + r.manifest(&m); + wait_count(&r, "map status=0", 2); + assert!(r.events().contains("partial=1"), "{}", r.events()); + assert_eq!( + unsafe { ava1_test_status_readback_left() }, + (d.len() - 20 * G) as u64, + "the first run's read-back still counts" + ); + send_groups(&r, 0, &d, &(20..30).collect::>()); + wait_until("the new groups read back live", 10_000, || { + rb_stats()[0] == (10 * G) as u64 + }); + assert_eq!( + unsafe { ava1_test_status_readback_left() }, + (d.len() - 30 * G) as u64 + ); + send_groups(&r, 0, &d, &(30..41).collect::>()); + r.root(0, *blake3::hash(&d).as_bytes()); + assert_eq!(r.wait(10_000), 0, "{}", r.events()); + assert_eq!(std::fs::read(t.join("dest/big")).unwrap(), d); + let st = rb_stats(); + assert_eq!( + st[0] + st[1], + (d.len() - 20 * G) as u64, + "only what the first run did not read back: {st:?}" + ); + assert_eq!(st[3], 0, "{st:?}"); +} + +/// The same, but the part file was written between the two runs (by anything but the job): nothing +/// the first run read back counts, the whole file is read back again, and the changed byte is found +/// and the file sent again. +#[test] +fn a_file_written_between_two_runs_is_read_back_whole() { + let _k = Knobs; + let t = tmp("kept-touched"); + std::fs::create_dir_all(t.join("dest")).unwrap(); + let d = data(12 * G + 41, 22); + let m = Manifest { + entries: vec![file("big", d.len() as u64)], + }; + let r = CRecv::open( + &t.join("jobs"), + &t.join("dest"), + gen::JF_READBACK, + gen::POLICY_REPLACE, + 0, + ); + r.manifest(&m); + r.wait_event("map status=0", 5000); + // two batches: the resume re-hashes the last one (§13.4), so the byte changed below is in the first + send_groups(&r, 0, &d, &(0..3).collect::>()); + wait_until("the first groups read back", 10_000, || { + rb_stats()[0] == (3 * G) as u64 + }); + send_groups(&r, 0, &d, &(3..6).collect::>()); + wait_until("the first half read back", 10_000, || { + rb_stats()[0] == (6 * G) as u64 + }); + unsafe { ava1_test_recv_cancel() }; + assert_eq!(r.reopen(true), 0); + { + use std::os::unix::fs::FileExt; + std::thread::sleep(std::time::Duration::from_millis(20)); // a later modify time + let f = std::fs::OpenOptions::new() + .write(true) + .open(part_file(&t.join("dest"))) + .unwrap(); + let at = G + 9; + f.write_all_at(&[!d[at]], at as u64).unwrap(); + } + r.manifest(&m); + wait_count(&r, "map status=0", 2); + assert_eq!( + unsafe { ava1_test_status_readback_left() }, + d.len() as u64, + "nothing of the first run counts" + ); + send_groups(&r, 0, &d, &(6..13).collect::>()); + r.root(0, *blake3::hash(&d).as_bytes()); + r.wait_event("retry 0 1", 10_000); + assert_eq!(rb_stats()[3], 1, "the changed byte was found"); + assert!(!t.join("dest/big").exists(), "nothing was committed"); + send_large(&r, 0, &d); + assert_eq!(r.wait(10_000), 0, "{}", r.events()); + assert_eq!(std::fs::read(t.join("dest/big")).unwrap(), d); +} + +/// A job without the read-back says nothing about it in its Status (an older sender sees the frame it +/// always did). +#[test] +fn a_job_without_the_read_back_reports_no_readback_left() { + let _k = Knobs; + let t = tmp("no-rb-status"); + std::fs::create_dir_all(t.join("dest")).unwrap(); + let d = data(4 * G + 5, 23); + let m = Manifest { + entries: vec![file("big", d.len() as u64)], + }; + let r = CRecv::open(&t.join("jobs"), &t.join("dest"), 0, gen::POLICY_REPLACE, 0); + r.manifest(&m); + r.wait_event("map status=0", 5000); + assert_eq!(unsafe { ava1_test_status_readback_left() }, u64::MAX); + send_large(&r, 0, &d); + assert_eq!(r.wait(10_000), 0, "{}", r.events()); +} + mod common; /// End to end, Rust sender to C receiver, with the read-back on: a file past 4 GiB whose bytes at diff --git a/crates/ava1-gen/src/core_schema.toml b/crates/ava1-gen/src/core_schema.toml index 86528d3..6b7106e 100644 --- a/crates/ava1-gen/src/core_schema.toml +++ b/crates/ava1-gen/src/core_schema.toml @@ -731,6 +731,7 @@ ext = [ { tag = 3, name = "result", ty = "bytes", doc = "`job.run`: the finished operation's reply body, at most 128 KiB (SPEC.md §7.1.1)." }, { tag = 4, name = "code", ty = "u16", doc = "A failed job's `ERR_*` code." }, { tag = 5, name = "unswept", ty = "u32", doc = "Durable-by-log: done files not yet durable in place; absent means 0 (SPEC.md §15.7)." }, + { tag = 6, name = "readback_left", ty = "u64", doc = "JF_READBACK: bytes of the job's unfinished large files not yet read back from the drive; absent when the receiver does not say (SPEC.md §13.5)." }, ] [[message]] diff --git a/crates/ava1/src/gen.rs b/crates/ava1/src/gen.rs index a484b1e..a0709cf 100644 --- a/crates/ava1/src/gen.rs +++ b/crates/ava1/src/gen.rs @@ -1455,6 +1455,8 @@ pub struct Status { pub code: Option, /// Durable-by-log: done files not yet durable in place; absent means 0 (SPEC.md §15.7). pub unswept: Option, + /// JF_READBACK: bytes of the job's unfinished large files not yet read back from the drive; absent when the receiver does not say (SPEC.md §13.5). + pub readback_left: Option, } impl Message for Status { @@ -1477,12 +1479,14 @@ impl Message for Status { if self.result.is_some() { ext_n += 1; } if self.code.is_some() { ext_n += 1; } if self.unswept.is_some() { ext_n += 1; } + if self.readback_left.is_some() { ext_n += 1; } w.u16(ext_n); if let Some(v) = &self.current { w.ext(1, |w| { w.str(v) })?; } if let Some(v) = &self.state { w.ext(2, |w| { w.u8(*v); Ok(()) })?; } if let Some(v) = &self.result { w.ext(3, |w| { w.bytes(v) })?; } if let Some(v) = &self.code { w.ext(4, |w| { w.u16(*v); Ok(()) })?; } if let Some(v) = &self.unswept { w.ext(5, |w| { w.u32(*v); Ok(()) })?; } + if let Some(v) = &self.readback_left { w.ext(6, |w| { w.u64(*v); Ok(()) })?; } Ok(()) } @@ -1535,6 +1539,12 @@ impl Message for Status { m.unswept = Some(vr.u32()?); vr.finish()?; } + 6 => { + if m.readback_left.is_some() { return Err(DecodeError::DupExt(6)); } + let mut vr = Reader::new(v); + m.readback_left = Some(vr.u64()?); + vr.finish()?; + } _ => {} } } @@ -3109,7 +3119,7 @@ pub fn sample(name: &str, rng: &mut SplitMix) -> Option> { "Durable" => Durable { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, files: vec![FileRun::default(); rng.below(3) as usize], ranges: vec![FileRange::default(); rng.below(3) as usize], }.to_bytes().ok(), "FileRoot" => FileRoot { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, file_id: rng.next_u64() as u32, root: { let mut a = [0u8; 32]; rng.fill(&mut a); a }, }.to_bytes().ok(), "FileRetry" => FileRetry { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, file_id: rng.next_u64() as u32, reason: rng.next_u64() as u16, }.to_bytes().ok(), - "Status" => Status { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, files_done: rng.next_u64() as u32, files_total: rng.next_u64() as u32, bytes_received: rng.next_u64(), bytes_durable: rng.next_u64(), bytes_total: rng.next_u64(), bottleneck: rng.next_u64() as u8, workers: rng.next_u64() as u8, lanes: rng.next_u64() as u8, sequential: rng.next_u64() as u8, current: if rng.below(2) == 1 { Some(rng.ascii(20)) } else { None }, state: if rng.below(2) == 1 { Some(rng.next_u64() as u8) } else { None }, result: if rng.below(2) == 1 { Some({ let n = rng.below(41) as usize; let mut v = vec![0u8; n]; rng.fill(&mut v); v }) } else { None }, code: if rng.below(2) == 1 { Some(rng.next_u64() as u16) } else { None }, unswept: if rng.below(2) == 1 { Some(rng.next_u64() as u32) } else { None }, }.to_bytes().ok(), + "Status" => Status { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, files_done: rng.next_u64() as u32, files_total: rng.next_u64() as u32, bytes_received: rng.next_u64(), bytes_durable: rng.next_u64(), bytes_total: rng.next_u64(), bottleneck: rng.next_u64() as u8, workers: rng.next_u64() as u8, lanes: rng.next_u64() as u8, sequential: rng.next_u64() as u8, current: if rng.below(2) == 1 { Some(rng.ascii(20)) } else { None }, state: if rng.below(2) == 1 { Some(rng.next_u64() as u8) } else { None }, result: if rng.below(2) == 1 { Some({ let n = rng.below(41) as usize; let mut v = vec![0u8; n]; rng.fill(&mut v); v }) } else { None }, code: if rng.below(2) == 1 { Some(rng.next_u64() as u16) } else { None }, unswept: if rng.below(2) == 1 { Some(rng.next_u64() as u32) } else { None }, readback_left: if rng.below(2) == 1 { Some(rng.next_u64()) } else { None }, }.to_bytes().ok(), "JobDone" => JobDone { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, status: rng.next_u64() as u16, files: rng.next_u64() as u32, bytes: rng.next_u64(), message: if rng.below(2) == 1 { Some(rng.ascii(20)) } else { None }, settling: if rng.below(2) == 1 { Some(rng.next_u64() as u8) } else { None }, }.to_bytes().ok(), "JobCancel" => JobCancel { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, reason: rng.next_u64() as u16, }.to_bytes().ok(), "SkipProgress" => SkipProgress { job_id: { let mut a = [0u8; 16]; rng.fill(&mut a); a }, bytes_skipped: rng.next_u64(), }.to_bytes().ok(), diff --git a/crates/ava1/src/send.rs b/crates/ava1/src/send.rs index 46b3ab6..e5837dc 100644 --- a/crates/ava1/src/send.rs +++ b/crates/ava1/src/send.rs @@ -215,6 +215,10 @@ pub struct Progress { pub settling: AtomicBool, /// The receiver's last reported `unswept` count while the sender waits for it to settle. pub unswept: AtomicU32, + /// With `JF_READBACK`: the bytes of the job's unfinished large files the receiver has not + /// read back yet, from its last `Status` (`readback_left`, SPEC.md §13.5). 0 until a + /// receiver reports it; a receiver that does not (an older one) leaves it 0. + pub readback_left: AtomicU64, /// Times the sender ran for this job (more than one: it reconnected and resumed). pub attempts: AtomicU32, /// The most files the receiver reported unswept while the sender waited for it to settle. @@ -2129,6 +2133,9 @@ pub async fn run_upload( Ok(st) => { receiver_bn = st.bottleneck; sh.sched.lock().unwrap().floor = st.workers.max(1) as usize; + if let Some(left) = st.readback_left { + pg.readback_left.store(left, Ordering::Relaxed); + } } // M6: like every other control frame, a malformed Status is a // protocol error, not something to shrug off. diff --git a/crates/ava1/tests/schema.rs b/crates/ava1/tests/schema.rs index 649b6b5..a1f5802 100644 --- a/crates/ava1/tests/schema.rs +++ b/crates/ava1/tests/schema.rs @@ -171,6 +171,7 @@ fn status_with_result() -> Status { result: Some(b"cause".to_vec()), code: Some(ERR_IO), unswept: None, + readback_left: None, } } @@ -204,6 +205,22 @@ fn management_structs_round_trip() { assert_eq!(Status::decode(&sr.to_bytes().unwrap()).unwrap(), sr); } +/// `Status.readback_left` (1.0.12) is an extension: it round-trips, a Status without it (an older +/// receiver's) decodes with `None`. A decoder that predates it skips the unknown tag. +#[test] +fn status_readback_left_is_an_optional_extension() { + use ava1::wire::Message; + let with = Status { + readback_left: Some(156_412_936_192), + ..status_with_result() + }; + let bytes = with.to_bytes().unwrap(); + assert_eq!(Status::decode(&bytes).unwrap(), with); + let without = status_with_result(); + let old = without.to_bytes().unwrap(); + assert_eq!(Status::decode(&old).unwrap().readback_left, None); +} + #[test] fn every_core_method_constant_is_unique_and_pinned() { let src = include_str!("../../../schema/ava1.toml"); diff --git a/schema/ava1.toml b/schema/ava1.toml index 86528d3..6b7106e 100644 --- a/schema/ava1.toml +++ b/schema/ava1.toml @@ -731,6 +731,7 @@ ext = [ { tag = 3, name = "result", ty = "bytes", doc = "`job.run`: the finished operation's reply body, at most 128 KiB (SPEC.md §7.1.1)." }, { tag = 4, name = "code", ty = "u16", doc = "A failed job's `ERR_*` code." }, { tag = 5, name = "unswept", ty = "u32", doc = "Durable-by-log: done files not yet durable in place; absent means 0 (SPEC.md §15.7)." }, + { tag = 6, name = "readback_left", ty = "u64", doc = "JF_READBACK: bytes of the job's unfinished large files not yet read back from the drive; absent when the receiver does not say (SPEC.md §13.5)." }, ] [[message]] diff --git a/spec/SPEC.md b/spec/SPEC.md index 8dd5be2..aaa339c 100644 --- a/spec/SPEC.md +++ b/spec/SPEC.md @@ -875,7 +875,11 @@ back from the drive and compares its CV with the outboard's before the file is c group is already hashed from the drive at commit). A group is read back only once it is durable: the sync batch that wrote it has fsynced the part file and the outboard and journaled the range. It may be read then, while the rest of the file still arrives, in any order; the groups a resumed job holds durable -from before (its journal's ranges) may be read the same way once the job's map is answered. The commit, +from before (its journal's ranges) may be read the same way once the job's map is answered. A receiver may +also count as read back what an earlier run of the job in the same process read back, of ranges still durable by +the journal, when the part file and the outboard have not changed since (the C node keeps it in memory with each +file's device, inode, size and modify and change times, and forgets it all when its data layer stops): a Stop +and a resume then do not read the first run's bytes again. Nothing is carried across a restart. The commit, once every byte is durable and the outboard's root equals the sender's root, reads back the groups that were not (the last batch's and those the early reads had no time for). A group written again after it was read back (a duplicate chunk) is read back again once that write is durable, @@ -896,7 +900,11 @@ receiver should read past the cache. The C node drops each range's cached pages `posix_fadvise(POSIX_FADV_DONTNEED)`, and on FreeBSD, where that only deactivates the pages, also `msync(MS_INVALIDATE)` over a mapping of the range) and reads through an `O_DIRECT` descriptor where the filesystem allows one; it logs which, once per job. Where neither is possible the read goes through the -cache, and what it proves is what any later reader of the file will get. Small files are out of its scope (their roots are checked from the +cache, and what it proves is what any later reader of the file will get. While the job runs, the C node's +`Status` carries ext `readback_left`: the bytes of its unfinished large files (those it has opened) not read +back yet, so a sender can show the read-back after the last byte, which on a large file takes minutes, as +progress. It is absent from a job without the flag and from a receiver that does not send it; a sender must not +rely on it. Small files are out of its scope (their roots are checked from the log record or the file when they are recovered, §15.7). ## 14. Journal and resume