US06-04: Plan and Execute Safe Restores (#80)
This commit was merged in pull request #80.
This commit is contained in:
@@ -15,6 +15,11 @@ planned → transferring → verified → removing → complete
|
||||
↘ ↘ ↘ failed
|
||||
```
|
||||
|
||||
A restore (US06-04) uses the same rows with ``direction='restore'``: it copies from
|
||||
the medium back into the library and removes nothing, so it goes ``verified →
|
||||
complete`` directly. ``source_path``/``destination_path`` always mean "from"/"to",
|
||||
which is why the evidence table below needs no direction of its own.
|
||||
|
||||
- ``transferring`` — intent recorded; a temporary copy may exist, the destination
|
||||
may or may not have been published. Nothing has been removed.
|
||||
- ``verified`` — the archived bytes exist at their final path, hash exactly as
|
||||
@@ -73,10 +78,22 @@ ALLOWED_TRANSITIONS = {
|
||||
ArchiveState.FAILED: {ArchiveState.PLANNED, ArchiveState.TRANSFERRING},
|
||||
}
|
||||
|
||||
# A restore removes nothing, so it has no ``removing`` step: a verified published
|
||||
# copy is the whole job (US06-04). Keeping this as a separate table means the
|
||||
# archive direction still cannot reach ``complete`` without going through removal.
|
||||
RESTORE_TRANSITIONS = {
|
||||
**ALLOWED_TRANSITIONS,
|
||||
ArchiveState.VERIFIED: {ArchiveState.COMPLETE, ArchiveState.FAILED},
|
||||
}
|
||||
|
||||
TERMINAL_STATES = frozenset({ArchiveState.COMPLETE})
|
||||
# States where this item may already have touched the filesystem.
|
||||
UNSAFE_STATES = frozenset({ArchiveState.TRANSFERRING, ArchiveState.VERIFIED, ArchiveState.REMOVING})
|
||||
|
||||
# Which way the bytes move. Same rows, same evidence table, opposite direction.
|
||||
ARCHIVE = "archive"
|
||||
RESTORE = "restore"
|
||||
|
||||
RESUMABLE = "resumable"
|
||||
FORWARD = "forward"
|
||||
MANUAL = "manual"
|
||||
@@ -94,8 +111,9 @@ class JournalConflict(JournalError):
|
||||
"""Fencing check failed; a newer owner has taken over this operation."""
|
||||
|
||||
|
||||
def can_transition(current: str, target: str) -> bool:
|
||||
return target in ALLOWED_TRANSITIONS.get(current, set())
|
||||
def can_transition(current: str, target: str, direction: str = ARCHIVE) -> bool:
|
||||
table = RESTORE_TRANSITIONS if direction == RESTORE else ALLOWED_TRANSITIONS
|
||||
return target in table.get(current, set())
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
@@ -119,7 +137,7 @@ class ArchiveJournal:
|
||||
if row.journal_state in TERMINAL_STATES:
|
||||
raise InvalidTransition(f"{row.journal_state} is terminal")
|
||||
if row.journal_state != ArchiveState.TRANSFERRING and not can_transition(
|
||||
row.journal_state, ArchiveState.TRANSFERRING
|
||||
row.journal_state, ArchiveState.TRANSFERRING, row.direction
|
||||
):
|
||||
raise InvalidTransition(f"{row.journal_state} -> {ArchiveState.TRANSFERRING}")
|
||||
if row.journal_state != ArchiveState.TRANSFERRING:
|
||||
@@ -160,7 +178,7 @@ class ArchiveJournal:
|
||||
if row.journal_state == target:
|
||||
session.commit()
|
||||
return _operation_dict(row) # idempotent
|
||||
if not can_transition(row.journal_state, target):
|
||||
if not can_transition(row.journal_state, target, row.direction):
|
||||
raise InvalidTransition(f"{row.journal_state} -> {target}")
|
||||
|
||||
row.journal_state = target
|
||||
@@ -192,16 +210,18 @@ class ArchiveJournal:
|
||||
)
|
||||
return [_operation_dict(row) for row in rows]
|
||||
|
||||
def incomplete(self) -> list[dict]:
|
||||
def incomplete(self, *, direction: str | None = None) -> list[dict]:
|
||||
"""Every operation left in a non-terminal, non-planned state — the work a
|
||||
restart has to reason about."""
|
||||
restart has to reason about. Without ``direction`` this spans archives and
|
||||
restores, because either one half-done blocks the other."""
|
||||
with self._session_factory() as session:
|
||||
stmt = select(ArchiveOperation).where(
|
||||
ArchiveOperation.journal_state.not_in([*TERMINAL_STATES, ArchiveState.PLANNED])
|
||||
)
|
||||
if direction is not None:
|
||||
stmt = stmt.where(ArchiveOperation.direction == direction)
|
||||
rows = session.scalars(
|
||||
select(ArchiveOperation)
|
||||
.where(
|
||||
ArchiveOperation.journal_state.not_in([*TERMINAL_STATES, ArchiveState.PLANNED])
|
||||
)
|
||||
.order_by(ArchiveOperation.plan_id, ArchiveOperation.sequence)
|
||||
stmt.order_by(ArchiveOperation.plan_id, ArchiveOperation.sequence)
|
||||
)
|
||||
return [_operation_dict(row) for row in rows]
|
||||
|
||||
@@ -231,6 +251,7 @@ class ArchiveJournal:
|
||||
return {
|
||||
"operation_id": operation_id,
|
||||
"plan_id": row["plan_id"],
|
||||
"direction": row["direction"],
|
||||
"album": row["album"],
|
||||
"asset_id": row["asset_id"],
|
||||
"source_path": row["source_path"],
|
||||
@@ -243,8 +264,8 @@ class ArchiveJournal:
|
||||
"destination_matches": destination_matches,
|
||||
}
|
||||
|
||||
def classify_all(self) -> list[dict]:
|
||||
return [self.classify(row["id"]) for row in self.incomplete()]
|
||||
def classify_all(self, *, direction: str | None = None) -> list[dict]:
|
||||
return [self.classify(row["id"]) for row in self.incomplete(direction=direction)]
|
||||
|
||||
def blocks_mutation(self) -> bool:
|
||||
"""True when any item may have the library half-archived."""
|
||||
@@ -317,6 +338,7 @@ def _operation_dict(row: ArchiveOperation) -> dict:
|
||||
return {
|
||||
"id": row.id,
|
||||
"plan_id": row.plan_id,
|
||||
"direction": row.direction,
|
||||
"sequence": row.sequence,
|
||||
"album": row.album,
|
||||
"asset_id": row.asset_id,
|
||||
|
||||
Reference in New Issue
Block a user