xdol.tiering#

A bounded hot store over a cold source of truth (write-through, GDSF release).

TieredStore(hot, cold, budget_bytes=...) is a MutableMapping: writes go to cold first, then hot; reads are served from hot when present, else from cold (and admitted to hot when the policy says so). Placement is decided by a PlacementPolicy (default GdsfPolicy: Greedy-Dual-Size-Frequency with aging, Cherkasova 1998) from the statistics of an xdol.access.AccessLog, whose clock is the store’s clock.

Verbs are free functions, store first: sweep(), pin(), unpin(), status(), prefetch(), local_paths().

Values must support len (their size in bytes): __setitem__ raises TypeError before writing anything otherwise. A value read from cold without len counts as size 0. Single-threaded: there is no locking.

>>> hot, cold = {}, {}
>>> store = TieredStore(hot, cold, budget_bytes=None, tracker=AccessLog())
>>> store['a'] = b'hello'
>>> cold['a'], hot['a']
(b'hello', b'hello')
>>> status(store)['hot_bytes']
5

Functions

local_paths(store, *[, path_of])

Read-only {key: real file path}; materialises each key into hot.

pin(store, key)

Protect key's hot copy from release (persisted in the tracker).

prefetch(store, keys)

Admit keys to hot regardless of policy, as reads served from cold.

status(store)

A snapshot of the store: hot and cold sizes, budget, pinned keys and the last sweep.

sweep(store, *[, exclude])

Release hot copies, lowest priority first, down to the low watermark.

unpin(store, key)

Clear the pin on key so it can be released from hot again.

Classes

GdsfPolicy(*[, high_watermark, ...])

Greedy-Dual-Size-Frequency with aging, plus pins, minimum residency and second-hit admission.

LruPolicy(*[, high_watermark, ...])

Least-recently-used release: the simple alternative to GdsfPolicy.

PlacementPolicy(*args, **kwargs)

Decides what enters and leaves the hot tier.

SweepReport(keys_released, bytes_before, ...)

What one sweep did: the keys released and the hot bytes before and after.

TieredStore(hot, cold, *, budget_bytes[, ...])

Bounded hot store over a cold source of truth.

Exceptions

HotWriteError

The hot write failed after the cold store accepted the record.

TieringError

Base class of tiering errors.

class xdol.tiering.GdsfPolicy(*, high_watermark=0.85, low_watermark=0.75, min_residency_s=604800, admit_after_reads=2, admit_window_s=2592000, cost_per_request=1.0, cost_per_byte=0.0, seed_half_life_s=2592000)[source]#

Bases: _ResidencyPolicy

Greedy-Dual-Size-Frequency with aging, plus pins, minimum residency and second-hit admission.

Each hot key carries H = L + F * cost(size) / size, fixed when it is accessed, with F its accesses since admission to hot, cost(size) = cost_per_request + cost_per_byte * size and L the H of the last released key at that time (per process; resets at restart). Releasing the lowest H raises L, so a key not accessed for a while loses to newer ones however often it was read. A key already hot when this process first sees it starts fresh, F = 1, as if just admitted: its logged history (often long, all of it older than L’s reset) would otherwise keep it favoured after every restart. A key not accessed during this process (a cron sweep) is ranked on its recency instead: F is 0.5 ** (age / seed_half_life_s), age being the time since its last_access, so among such keys of equal size the least recently used goes first (a key seeded this way and then accessed continues from F + 1).

on_hot_access(key, stats, *, admitted)[source]#

Bump the key frequency (a first-seen key starts fresh) and record its size.

Return type:

None

on_hot_drop(key)[source]#

Forget the key.

Return type:

None

priority(frequency, size)[source]#

H of a key accessed now with this frequency and size.

Return type:

float

exception xdol.tiering.HotWriteError[source]#

Bases: TieringError

The hot write failed after the cold store accepted the record.

The record is safe in cold; do not retry the cold write.

class xdol.tiering.LruPolicy(*, high_watermark=0.85, low_watermark=0.75, min_residency_s=604800, admit_after_reads=2, admit_window_s=2592000)[source]#

Bases: _ResidencyPolicy

Least-recently-used release: the simple alternative to GdsfPolicy.

Once hot bytes pass the high watermark, unprotected hot keys are released oldest-last_access first (the consumer stops at the low watermark).

class xdol.tiering.PlacementPolicy(*args, **kwargs)[source]#

Bases: Protocol

Decides what enters and leaves the hot tier.

admit(key, stats, *, now)[source]#

Whether key (with its stats, or None if unseen) may enter hot at time now.

Return type:

bool

on_hot_access(key, stats, *, admitted)[source]#

key was accessed in hot; admitted: it was not there before.

Return type:

None

on_hot_drop(key)[source]#

key left hot (released, deleted or overwritten with no hot copy).

Return type:

None

to_release(hot_stats, *, hot_bytes, budget_bytes, now)[source]#

Keys to release from hot, given hot usage versus budget_bytes, at time now.

Return type:

Iterable

class xdol.tiering.SweepReport(keys_released, bytes_before, bytes_after)[source]#

Bases: object

What one sweep did: the keys released and the hot bytes before and after.

class xdol.tiering.TieredStore(hot, cold, *, budget_bytes, policy=None, tracker=None, max_item_fraction=1.0)[source]#

Bases: MutableMapping

Bounded hot store over a cold source of truth. See the module docstring.

Parameters:
  • budget_bytes (int | None) –

    None: hot is a full mirror (never released, every read miss admitted); 0: cold-only, no hot tier (writes skip hot and read misses are not admitted, but only sweep() enforces it: prefetch() and local_paths() still fill

    hot until the next sweep, which releases every hot copy, protected or not); > 0: bounded.

  • policy (PlacementPolicy | None) – a PlacementPolicy; default GdsfPolicy.

  • tracker (AccessLog | None) – an AccessLog (its clock is the store’s clock); default xdol.access.default_access_log_for() (hot).

  • max_item_fraction (float) – in (0, 1]; with a bounded budget, an item larger than max_item_fraction * budget_bytes is never held hot by the store itself: a read miss is served from cold without admission, and a write goes to cold only (dropping any stale hot copy). The default 1.0 keeps only items larger than the whole budget out. prefetch() and local_paths() are explicit requests and are exempt.

Keys already in hot at construction are not recorded as accesses. Their size comes from the tracker, else from the file size when hot is file-backed, else counts as 0 until the key’s next hot hit.

exception xdol.tiering.TieringError[source]#

Bases: Exception

Base class of tiering errors.

xdol.tiering.local_paths(store, *, path_of=None)[source]#

Read-only {key: real file path}; materialises each key into hot.

Materialisation is an explicit request, like prefetch(): exempt from the policy and from max_item_fraction.

path_of(key) gives the file path of key in hot; the default is dol.path_of() on the hot store, which follows wrapped layers (TypeError on access if hot is not file-backed, before anything is fetched; KeyError for a key leaving its root). FileNotFoundError is raised if the file is not there after materialisation. key in paths checks the store without fetching.

Return type:

Mapping

xdol.tiering.pin(store, key)[source]#

Protect key’s hot copy from release (persisted in the tracker).

Return type:

None

xdol.tiering.prefetch(store, keys)[source]#

Admit keys to hot regardless of policy, as reads served from cold.

An explicit request: exempt from max_item_fraction too.

The release this may trigger never releases keys.

Return type:

None

xdol.tiering.status(store)[source]#

A snapshot of the store: hot and cold sizes, budget, pinned keys and the last sweep.

Return type:

dict

xdol.tiering.sweep(store, *, exclude=())[source]#

Release hot copies, lowest priority first, down to the low watermark.

Never touches cold, nor the keys in exclude. Warns if every remaining key is protected, once per stuck episode (until a sweep reaches it again). With budget_bytes=0 (no hot tier) every hot copy is released, protected or not.

Return type:

SweepReport

xdol.tiering.unpin(store, key)[source]#

Clear the pin on key so it can be released from hot again.

Return type:

None