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
|
Read-only |
|
Protect |
|
Admit |
|
A snapshot of the store: hot and cold sizes, budget, pinned keys and the last sweep. |
|
Release hot copies, lowest priority first, down to the low watermark. |
|
Clear the pin on |
Classes
|
Greedy-Dual-Size-Frequency with aging, plus pins, minimum residency and second-hit admission. |
|
Least-recently-used release: the simple alternative to |
|
Decides what enters and leaves the hot tier. |
|
What one sweep did: the keys released and the hot bytes before and after. |
|
Bounded hot store over a cold source of truth. |
Exceptions
The hot write failed after the cold store accepted the record. |
|
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:
_ResidencyPolicyGreedy-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, withFits accesses since admission to hot,cost(size) = cost_per_request + cost_per_byte * sizeandLthe H of the last released key at that time (per process; resets at restart). Releasing the lowest H raisesL, 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 thanL’s reset) would otherwise keep it favoured after every restart. A key not accessed during this process (a cronsweep) is ranked on its recency instead:Fis0.5 ** (age / seed_half_life_s),agebeing the time since itslast_access, so among such keys of equal size the least recently used goes first (a key seeded this way and then accessed continues fromF + 1).
- exception xdol.tiering.HotWriteError[source]#
Bases:
TieringErrorThe 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:
_ResidencyPolicyLeast-recently-used release: the simple alternative to
GdsfPolicy.Once hot bytes pass the high watermark, unprotected hot keys are released oldest-
last_accessfirst (the consumer stops at the low watermark).
- class xdol.tiering.PlacementPolicy(*args, **kwargs)[source]#
Bases:
ProtocolDecides what enters and leaves the hot tier.
- admit(key, stats, *, now)[source]#
Whether
key(with itsstats, orNoneif unseen) may enter hot at timenow.- Return type:
- on_hot_access(key, stats, *, admitted)[source]#
keywas accessed in hot;admitted: it was not there before.- Return type:
- class xdol.tiering.SweepReport(keys_released, bytes_before, bytes_after)[source]#
Bases:
objectWhat 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:
MutableMappingBounded hot store over a cold source of truth. See the module docstring.
- Parameters:
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 onlysweep()enforces it:prefetch()andlocal_paths()still fillhot until the next sweep, which releases every hot copy, protected or not);
> 0: bounded.policy (
PlacementPolicy|None) – aPlacementPolicy; defaultGdsfPolicy.tracker (
AccessLog|None) – anAccessLog(itsclockis the store’s clock); defaultxdol.access.default_access_log_for()(hot).max_item_fraction (
float) – in(0, 1]; with a bounded budget, an item larger thanmax_item_fraction * budget_bytesis 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 default1.0keeps only items larger than the whole budget out.prefetch()andlocal_paths()are explicit requests and are exempt.
Keys already in
hotat construction are not recorded as accesses. Their size comes from the tracker, else from the file size whenhotis file-backed, else counts as 0 until the key’s next hot hit.
- xdol.tiering.local_paths(store, *, path_of=None)[source]#
Read-only
{key: real file path}; materialises each key intohot.Materialisation is an explicit request, like
prefetch(): exempt from the policy and frommax_item_fraction.path_of(key)gives the file path ofkeyinhot; the default isdol.path_of()on the hot store, which follows wrapped layers (TypeErroron access ifhotis not file-backed, before anything is fetched;KeyErrorfor a key leaving its root).FileNotFoundErroris raised if the file is not there after materialisation.key in pathschecks the store without fetching.- Return type:
- xdol.tiering.pin(store, key)[source]#
Protect
key’s hot copy from release (persisted in the tracker).- Return type:
- xdol.tiering.prefetch(store, keys)[source]#
Admit
keysto hot regardless of policy, as reads served from cold.An explicit request: exempt from
max_item_fractiontoo.The release this may trigger never releases
keys.- Return type:
- xdol.tiering.status(store)[source]#
A snapshot of the store: hot and cold sizes, budget, pinned keys and the last sweep.
- Return type:
- xdol.tiering.sweep(store, *, exclude=())[source]#
Release hot copies, lowest priority first, down to the low watermark.
Never touches
cold, nor the keys inexclude. Warns if every remaining key is protected, once per stuck episode (until a sweep reaches it again). Withbudget_bytes=0(no hot tier) every hot copy is released, protected or not.- Return type: