diff --git a/tiering/offload/ops.py b/tiering/offload/ops.py index d5305c6..1ad8c4e 100644 --- a/tiering/offload/ops.py +++ b/tiering/offload/ops.py @@ -74,6 +74,17 @@ def __init__(self, offset, wanted, got): SHORT_LIMIT = 3 # consecutive attempts that end short at the same offset +# Consecutive promotes that end on a reader error. Each one has already spent the whole +# per-chunk retry budget (TIERING_PROMOTE_RETRY_S, 10 min by default), so three in a row is +# half an hour of the same copy answering nothing, over the runner's growing back-off: the +# same count as SHORT_LIMIT, and too few to call a short outage permanent. +READ_ERROR_LIMIT = 3 +# Fixed `Result.reason` strings for "this remote copy cannot be had": the runner compares +# on these, never on prose (the long text goes to restore_note and the events). +REASON_CHUNK_MISMATCH = "chunk mismatch" +REASON_SHORT_REMOTE = "remote permanently short" +REASON_UNREADABLE = "remote unreadable" +UNOBTAINABLE_REASONS = (REASON_CHUNK_MISMATCH, REASON_SHORT_REMOTE, REASON_UNREADABLE) WANT_SPAN = 64 * MIB # a want inside the next 64 MiB does not move the fill cursor DEFAULT_RETRY_S = 600.0 DEFAULT_STREAM_FETCHES = 6 # chunks fetched at once while someone is watching the file @@ -1111,7 +1122,29 @@ def _short_remote(db, id, offset, step): "promote cannot finish, use the restore path") _fail(db, id, reason, offset=offset) st.log_event(db, id, "promote_short_remote", {"offset": offset, "attempts": run}) - return Result(False, "failed", step, reason) + return Result(False, "failed", step, REASON_SHORT_REMOTE) + + +def _read_error(db, id, e, step): + """A reader fault that is not a bad chunk or a short read (the release is gone, the + server answers EIO). Transient the first times; READ_ERROR_LIMIT promotes in a row + that end on one means the remote cannot be read: stop (state=failed).""" + detail = f"{type(e).__name__}: {e}" + st.log_event(db, id, "promote_read_error", {"reason": detail}) + run = 0 + for ev in reversed(st.events(db, id)): + if ev["kind"] == "promote_read_error": + run += 1 + else: + break + if run < READ_ERROR_LIMIT: + log.warning("%s: %s", id, detail) + return Result(False, "promoting", step, detail) + reason = (f"the remote cannot be read ({run} promotes in a row ended on a read error, " + f"last {detail}); promote cannot finish, use the restore path") + _fail(db, id, reason, attempts=run) + st.log_event(db, id, "promote_unreadable_remote", {"attempts": run}) + return Result(False, "failed", step, REASON_UNREADABLE) def _swapped(lib, stub, state, step) -> bool: @@ -1267,7 +1300,7 @@ def ours_symlink(): except ChunkMismatch as e: _fail(db, id, f"chunk mismatch from the remote at offset {e.offset}; nothing swapped", offset=e.offset) - return Result(False, "failed", step, "chunk mismatch") + return Result(False, "failed", step, REASON_CHUNK_MISMATCH) except OpsError as e: # a refusal about the stub itself: retrying cannot help _mark_failed(fpath, head_len) _fail(db, id, f"fill refused: {e}") @@ -1284,9 +1317,12 @@ def ours_symlink(): # off again after FAILED_HOLD_S, or a later read would fail at once and never # ask for the file (tierfs sends no `want` while a failed line stands). _mark_failed(fpath, head_len) + res = _read_error(db, id, e, step) + if res.state == "failed": # permanent: the line stays, as for a short remote + return res _sleep(FAILED_HOLD_S) _drop_failed_lines(fpath) - return _stall(db, id, "promote_read_error", f"{type(e).__name__}: {e}", "promoting", step) + return res _record(db, id, 1, "promoting", after_step) step = 1 diff --git a/tiering/offload/runner.py b/tiering/offload/runner.py index 526c083..b27e405 100644 --- a/tiering/offload/runner.py +++ b/tiering/offload/runner.py @@ -54,6 +54,11 @@ PROTECTED_COLUMNS = ("id", "state", "created_ts", "updated_ts", "lib_path") REGISTER_BAD_FRAC = 0.05 # missing_local + ambiguous over this share of listed fails register (on_pool is not counted) RESTORED_NOTE = "restored from the restore path" # ops.RESTORED_NOTE +# ops.UNOBTAINABLE_REASONS: a promote that ends failed with one of these cannot get the exact remote copy +UNOBTAINABLE = {"chunk mismatch": "chunk mismatch on every copy", + "remote permanently short": "remote permanently short on every copy", + "remote unreadable": "remote unreadable on every copy"} +HASH_RETRY_MAX = 3 # a substitute's stopped hash build is tried again this many times per process (each reads the whole file) ORPHAN_NOTES = ("L replaced", RESTORED_NOTE) # failed rows whose stub the orphan cleanup may remove # ops events, alerted once per id ALERT_EVENTS = ("promote_short_remote", "takeover_orphan", "substitute_hash_failed") @@ -621,6 +626,8 @@ def __init__(self, cfg: Config, deps: Deps, clock=time.time, sleep=time.sleep): self.ops_alerted: set = set() # (kind, id), once per id, ever self.dry_switched: set = set() # ids whose dry-run auto switch was logged (once per process) self.hash_resumed: set = set() # ids whose pending hash build was resumed (once per process) + self.hash_fails: dict = {} # id -> stopped hash builds this process + self.hash_retry_at: dict = {} # id -> not before (clock), after a stopped hash build self.hash_waiting: set = set() # ids whose promote waits for the hash build (logged once) self.switching: set = set() # ids with an automatic live switch running self.restore_tried: set = set() # ids the auto restore has tried (once per id and process) @@ -1488,6 +1495,8 @@ def _resume_hashes(self): id = n[:-5] if not n.endswith(".json") or id in self.hash_resumed: continue + if self.hash_retry_at.get(id, 0.0) > self.clock(): + continue # a build that stopped waits out its back-off try: with open(os.path.join(self.cfg.manifests, n)) as f: pending = json.load(f).get("pending") is True @@ -1505,14 +1514,33 @@ def _resume_hashes(self): def run(id=id): try: - if self.deps.hash_resume(self._open(), id) is None: + out = self.deps.hash_resume(self._open(), id) + if out is None: # another process is building it (a by-hand switch): look again later self.hash_resumed.discard(id) + elif out is True: + self.hash_fails.pop(id, None) + self.hash_retry_at.pop(id, None) + else: + self._hash_stopped(id) except Exception as e: log.error("hash resume %s raised %s", id, type(e).__name__) + self._hash_stopped(id) log.info("resuming the hash build of %s", id) self._spawn(run) + def _hash_stopped(self, id): + """A hash build stopped (a read fault, a zero run): the manifest is still pending, so ask + again after the same capped doubling delay promotes use, at most HASH_RETRY_MAX times + per process. Every try reads the whole file from the remote, so the cap, not the + delay, is what bounds the reads; after it only a restart (or a by-hand switch) tries.""" + with self.mu: + n = self.hash_fails[id] = self.hash_fails.get(id, 0) + 1 + if n >= HASH_RETRY_MAX: + return + self.hash_retry_at[id] = self.clock() + min(RETRY_DELAY * 2 ** (n - 1), BACKOFF_MAX) + self.hash_resumed.discard(id) + def _hash_pending(self, id) -> bool: """A substitute whose hash build has not ended: its manifest says `pending`, so there is no chunk list and no sha256 for a promote to check against yet.""" @@ -2251,17 +2279,26 @@ def _after(self, db, id, what, res): if res.state == "failed": self._note_failure(self.clock(), f"{what} {id[:8]} failed: {res.reason}") self._alert(f"{what}_failed", id, reason=res.reason) - if what == "promote" and res.reason == "chunk mismatch": - # every copy (upload and twin) gave a bad chunk: if someone is watching and a - # restore-path release exists, move them onto it before the viewer reaches the - # bad spot; any other outcome falls through to the lost-file flow - if self._auto_switch(id, "promote chunk mismatch", from_promote=True) == "switched": + if what == "promote" and res.reason in UNOBTAINABLE: + # the exact remote copy cannot be had (a bad chunk on every copy, a remote that + # stays short, a remote that cannot be read): if someone is watching, move them + # onto the next best copy (the planned substitute, else a live search) before + # the viewer reaches the bad spot; any other outcome falls through to the + # lost-file flow + why = UNOBTAINABLE[res.reason] + if self._auto_switch(id, "promote " + res.reason, from_promote=True) == "switched": + self.restore_tried.add(id) # the row is retired: nothing left to restore return rd = self.redownloader() - if rd is not None: - rd.mark_lost(db, id, "a", "chunk mismatch on every copy") + if (res.reason == "remote permanently short" and self.cfg.auto_restore + and self.deps.restore is not None and id not in self.restore_tried): + # its promote_short_remote event starts the auto restore this pass: a + # nightly re-download of the same row as well would fetch it twice + log.info("%s: left to the auto restore, not marked lost", id) + elif rd is not None: + rd.mark_lost(db, id, "a", why) else: - self._once("lost_file", id, reason="chunk mismatch on every copy") + self._once("lost_file", id, reason=why) if id in self.flagged: self.flagged.discard(id) self._kv_set(db, "flagged", sorted(self.flagged)) diff --git a/tiering/offload/substitute.py b/tiering/offload/substitute.py index 2a4f048..71245c3 100644 --- a/tiering/offload/substitute.py +++ b/tiering/offload/substitute.py @@ -446,13 +446,13 @@ def hash_build(db, nid, d: Deps, *, stop: threading.Event | None = None) -> bool list and the final manifest are written and it returns True: the row can be promoted like any other. On a zero run or a read fault the manifest stays pending (tierfs keeps checking for zero runs), the row keeps no `sha256` and an event `substitute_hash_failed` says why. - One build per id at a time: False when this process is already building it, None when - another process is (a by-hand switch beside the runner) or the row is not `offloaded` now - (retired by a later substitute, or failed by hand): nothing is read, and the caller may ask - again later.""" + One build per id at a time: None when this process or another one (a by-hand switch beside + the runner) is already building it, or when the row is not `offloaded` now (retired by a + later substitute, or failed by hand): nothing is read, and the caller may ask again later. + False means the build ran and stopped (a zero run, a read fault, a stop request).""" with _hashing_mu: if nid in _hashing: - return False # already being built in this process + return None # already being built in this process: not a stopped build _hashing.add(nid) try: with ops._id_lock(db, f"{nid}.hash") as mine: