# `Ankusa.ClaimCheck.Sweeper`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa-v0.7.0/packages/ankusa/lib/ankusa/claim_check/sweeper.ex#L1)

Retention for `LocalFS`-backed claims, and the upkeep of the claim leases
(rows `?l` of the node's store) that decide what retention may delete.
Runs on a node with the `:dispatch` role (the role that writes leases), and
on a `:storage` node when `claim_check.retention_days` is set;
`Ankusa.ClaimCheck.validate_config!/1` already rejects that setting against
any other blob store at boot, so the retention pass only ever runs against
`BlobStore.LocalFS`.

**Leases.** A claim pack is leased to every hook whose claim points into it,
from check-in until the hook is reclaimed (delivered everywhere, or
dead-lettered and evicted from the DLQ). Reclaiming does not clear the lease
at once: it runs on for `claim_check.redeem_grace_ms`, because "delivered" to
a queue sink only means the broker took a message that carries the ref, and
its consumer redeems it later. Each tick the lease pass reads
`claim_check.sweep_max_objects` lease rows after a rotating cursor and deletes
the ones that are over (grace passed) and the ones whose hook no longer
names that pack (a hook that checked its body in twice, or whose claim row is
gone), taking `claim_leases` off the counters in the same batch.

**Retention.** Claim objects live in day partitions,
`claims/tenant=<t>/dt=<yyyy-mm-dd>/` (or `claims/tenant=<t>/shard=<c>/dt=<yyyy-mm-dd>/`
under the `:sharded` key layout — both are swept, whatever
`claim_check.key_layout` says now), in every store of
`claim_check.blob_stores`. A day strictly before the cutoff
(`retention_days` ago) holds only packs older than the cutoff by creation,
but not necessarily unneeded ones, so the sweeper lists such a partition and
deletes a pack only when no lease names it that is still held or inside its
grace. A leased pack outlives its date cutoff and is deleted by the first
tick after its last lease ends. A partition's directory goes once it is
empty. A name in a partition that is not a pack id (a leftover temporary
file) is deleted with it. Packs written by another node's store are not
leased in this one: a `LocalFS` root shared between nodes is only as safe as
`retention_days` is long.

**One tenant per tick.** A pass over the claims lists the tenant directories
once, in name order, and a tick sweeps the partitions of one of them (listed
by name, never globbed). The tenant a pass last finished is the cursor, kept
in the node's store (`Keys.meta("claim_sweep_cursor")`), so a restart resumes
after it instead of from the first tenant; a pass that has no tenant left
behind the cursor starts over from the first. While tenants are left in a
pass the next step follows in 100 ms (`tenants_left` of the event), so a tick's own work
stays bounded by one tenant's partitions however many tenants the node has.
The tenant list itself is held for the pass: a name per tenant.

**Bounds.** A tick deletes at most `claim_check.sweep_max_objects` packs and at
most as many lease rows. What it left is the `backlog` of the
`[:ankusa, :claim_check, :sweep]` event (objects it did not examine in the
partition it stopped in, `ankusa_claim_check_sweep_backlog`) and
`partitions_left` (expired partitions of that tenant it did not reach); while
either is non-zero the next tick, on the same tenant, follows in a few
seconds instead of after `claim_check.sweep_interval_ms`. Listing one
partition is a single `readdir` of that directory: only deletes, lease reads
and store writes are capped. A store that cannot be read deletes nothing, so a
pack is never swept on a lease lookup that failed.

S3/GCS-backed claims are never covered here; the bucket's lifecycle rule is
their only cutoff and cannot see leases (`claim_check.bucket_expiry_days`
declares it, see `docs/claim-check.md`).

# `report`

```elixir
@type report() :: %{
  deleted: non_neg_integer(),
  scanned: non_neg_integer(),
  packs: non_neg_integer(),
  kept: non_neg_integer(),
  leases_pruned: non_neg_integer(),
  backlog: non_neg_integer(),
  partitions_left: non_neg_integer(),
  tenants_left: non_neg_integer()
}
```

What one tick did, as the `[:ankusa, :claim_check, :sweep]` event reports it.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `report`

```elixir
@spec report(atom(), keyword()) :: report()
```

Run one sweep synchronously and return everything it did.

# `run?`

```elixir
@spec run?(Ankusa.Config.t()) :: boolean()
```

Whether a node with this config runs a sweeper: the `:dispatch` role writes
the leases it prunes, and `claim_check.retention_days` is what it sweeps.

# `start_link`

# `sweep`

```elixir
@spec sweep(atom(), keyword()) :: {non_neg_integer(), non_neg_integer()}
```

Run one sweep synchronously: the lease pass and one tenant of the retention
pass. Returns `{deleted, scanned}` day partitions of that tenant (`deleted` is
the partitions whose directory it removed). `now:` (Unix ms) replaces the
clock, for tests.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
