"""Exact arithmetic for the PLR reservation-shrink rule, not a storage simulator. Source: Chan et al., FAST 2014, section 4.2.2, printed pages 167-168: https://files.usenix.org/s3fs-public/cdn/fast14-paper_chan.pdf All sizes are integer bytes. 'used' is one sampled occupancy, bounded by reserved; it does not count cumulative traffic across intervening merges. u = alpha * used/reserved + (1-alpha) * previous released = floor((1-u) * reserved/chunk) * chunk This evaluates the proposed size only, not occupied-extent reclamation, merge scheduling, I/O, durability or CodFS source behavior. Zero reserve is handled as the documented full-overwrite branch, not as a division by zero. Run: python3 -B model.py Tests: python3 -B -m unittest discover -s . -p test_model.py Original Hesela teaching code; no upstream implementation copied. """ from fractions import Fraction import json def shrink(chunk, reserved, used, previous, alpha): for name, value in (("chunk", chunk), ("reserved", reserved), ("used", used)): if type(value) is not int: raise TypeError(name + " must be an integer byte count") if chunk <= 0 or reserved < 0 or reserved % chunk or not 0 <= used <= reserved: raise ValueError("invalid sizes or occupancy") for name, value in (("previous", previous), ("alpha", alpha)): if not isinstance(value, Fraction): raise TypeError(name + " must be an exact Fraction") if not 0 <= value <= 1: raise ValueError(name + " outside [0, 1]") if reserved == 0: return {"utility": None, "released": 0, "reserved": 0, "mode": "full-overwrite"} utility = alpha * Fraction(used, reserved) + (1 - alpha) * previous released = ((1 - utility) * reserved // chunk) * chunk remaining = reserved - released return {"utility": utility, "released": released, "reserved": remaining, "mode": "full-overwrite" if remaining == 0 else "parity-logging"} def examples(): mib = 1024 ** 2 first = shrink(2 * mib, 8 * mib, 0, Fraction(1, 2), Fraction(1, 2)) second = shrink(2 * mib, first["reserved"], 0, first["utility"], Fraction(1, 2)) zero = shrink(2 * mib, 8 * mib, 0, Fraction(0), Fraction(1)) return {"scope": "synthetic arithmetic, not measured performance", "sizeUnit": "byte", "idle_first": first, "idle_second": second, "zero_reserve": zero} if __name__ == "__main__": print(json.dumps(examples(), default=str, indent=2))