src/lease.bend source
src/lease.bend on the hub · documented module
import Baseimport ./json.bend as Jimport ./core.bend as C# Lease# =====## Leader election over a coordination.k8s.io/v1 Lease, deciding as# client-go's LeaderElector.tryAcquireOrRenew does:## - No Lease: create one naming us.# - A Lease held by someone else whose record we saw change less than# leaseDuration ago (by our own clock: remote timestamps are never# compared, so clock skew cannot steal a lease; our clock stepping back# counts as no time passing, so a step cannot either): wait.# - Otherwise (ours, unheld, or expired): write it naming us, through the# resourceVersion the read returned, so of two contenders only one# write lands (optimistic concurrency).## Taking a lease from another holder counts a transition and resets# acquireTime; renewing our own keeps both.# What we last saw of the record, and when (our clock, in ms).type Obs is Data: Obs{holder: String, renew: String, at: U32}type LeaseAct is Data: LCreate{lease: J.Json} LUpdate{lease: J.Json} LWait{}type Decision is Data: Decision{act: LeaseAct, obs: Obs}def Obs.none() -> Obs: Obs{"", "", 0}def Lease.spec(+l: J.Json, field: String) -> J.Json: J.Json.get(J.Json.get(l, "spec"), field)def Lease.holder(+l: J.Json) -> String: J.Json.str_or(Lease.spec(l, "holderIdentity"), "")def Lease.renew_time(+l: J.Json) -> String: J.Json.str_or(Lease.spec(l, "renewTime"), "")def Lease.transitions(+l: J.Json) -> U32: Maybe.default(&2, U32, J.Json.as_u32(Lease.spec(l, "leaseTransitions")), 0)def Lease.new_spec(id: String, +dur: U32, acquire: String, renew: String, +trans: U32) -> J.Json: J.Json.obj([J.KV{"holderIdentity", J.JStr{id}}, J.KV{"leaseDurationSeconds", J.Json.u32(dur)}, J.KV{"acquireTime", J.JStr{acquire}}, J.KV{"renewTime", J.JStr{renew}}, J.KV{"leaseTransitions", J.Json.u32(trans)}])# A fresh Lease naming us.def Lease.fresh(ns: String, name: String, id: String, +dur: U32, +now: String) -> J.Json: J.Json.obj([J.KV{"apiVersion", J.JStr{"coordination.k8s.io/v1"}}, J.KV{"kind", J.JStr{"Lease"}}, J.KV{"metadata", J.Json.obj([J.KV{"name", J.JStr{name}}, J.KV{"namespace", J.JStr{ns}}])}, J.KV{"spec", Lease.new_spec(id, dur, now, now, 0)}])def Lease.elapsed.go(+d: U32, back: Bool) -> U32: match back: case True{}: 0 case False{}: d# Time since an observation, by our clock: a clock that stepped back (now# before at, by the wrapped difference) counts as no time at all, so a# clock step can never make a live lease look expired.def Lease.elapsed(+now: U32, +at: U32) -> U32: Lease.elapsed.go((now - at : U32), U32.is_ge((now - at : U32), 2147483648))def Lease.observe.same(o: Obs, +now: U32, back: Bool) -> Obs: match o back: case Obs{h, r, at} True{}: # the clock stepped back: count from now Obs{h, r, now} case x False{}: xdef Obs.at(o: Obs) -> U32: match o: case Obs{h, r, at}: atdef Lease.observe.go(h: String, r: String, +o: Obs, +now: U32, same: Bool) -> Obs: match same: case True{}: Lease.observe.same(o, now, U32.is_ge((now - Obs.at(o) : U32), 2147483648)) case False{}: Obs{h, r, now}# The record seen now: the old observation if nothing changed, else a new# one stamped now.def Lease.observe(+l: J.Json, o: Obs, +now: U32) -> Obs: match o: case Obs{+h, +r, at}: Lease.observe.go(Lease.holder(l), Lease.renew_time(l), Obs{h, r, at}, now, Bool.and(String.eq(h, Lease.holder(l)), String.eq(r, Lease.renew_time(l))))def Lease.duration.pos(+d: U32, +dur: U32, zero: Bool) -> U32: match zero: case True{}: dur case False{}: ddef Lease.duration.go(m: Maybe<&2, U32>, +dur: U32) -> U32: match m: case Some{+d}: Lease.duration.pos(d, dur, U32.is_eq(d, 0)) case None{}: dur# The holder's own lease duration (spec.leaseDurationSeconds), so a# contender configured shorter cannot expire a holder early; ours when the# Lease does not say.def Lease.duration(+l: J.Json, +dur: U32) -> U32: Lease.duration.go(J.Json.as_u32(Lease.spec(l, "leaseDurationSeconds")), dur)# Whether another holder's lease is still live by our clock.def Lease.held(+l: J.Json, +me: String, o: Obs, +now: U32, +dur: U32) -> Bool: match o: case Obs{h, r, +at}: Bool.and(Bool.not(String.is_empty(Lease.holder(l))), Bool.and(Bool.not(String.eq(Lease.holder(l), me)), U32.is_lt(Lease.elapsed(now, at), (Lease.duration(l, dur) * 1000 : U32))))def Lease.take.go(+l: J.Json, me: String, +dur: U32, +now: String, ours: Bool) -> J.Json: match ours: case True{}: J.Json.set(l, "spec", Lease.new_spec(me, dur, J.Json.str_or(Lease.spec(l, "acquireTime"), now), now, Lease.transitions(l))) case False{}: J.Json.set(l, "spec", Lease.new_spec(me, dur, now, now, (Lease.transitions(l) + 1 : U32)))# The Lease rewritten to name us, keeping its metadata (resourceVersion).def Lease.take(+l: J.Json, +me: String, +dur: U32, +now: String) -> J.Json: Lease.take.go(l, me, dur, now, String.eq(Lease.holder(l), me))def Lease.decide.fin(+l: J.Json, +me: String, o: Obs, +dur: U32, +now: String, held: Bool) -> Decision: match held: case True{}: Decision{LWait{}, o} case False{}: Decision{LUpdate{Lease.take(l, me, dur, now)}, o}def Lease.decide.held(+l: J.Json, +me: String, +o: Obs, +now_ms: U32, +dur: U32, +now: String) -> Decision: Lease.decide.fin(l, me, o, dur, now, Lease.held(l, me, o, now_ms, dur))def Lease.decide.got(+l: J.Json, +me: String, o: Obs, +now_ms: U32, +dur: U32, +now: String) -> Decision: Lease.decide.held(l, me, Lease.observe(l, o, now_ms), now_ms, dur, now)# One round: the Lease read (None when absent), us, what we saw before,# our clock in ms, the lease duration in seconds, and the time to stamp.def Lease.decide(cur: Maybe<&2, J.Json>, ns: String, name: String, +me: String, o: Obs, +now_ms: U32, +dur: U32, +now: String) -> Decision: match cur: case None{}: Decision{LCreate{Lease.fresh(ns, name, me, dur, now)}, Obs{me, now, now_ms}} case Some{+l}: Lease.decide.got(l, me, o, now_ms, dur, now)# The Lease handed back: no holder, a one-second duration, so the next# candidate takes it at once (client-go's ReleaseOnCancel).def Lease.release(+l: J.Json, +now: String) -> J.Json: J.Json.set(l, "spec", Lease.new_spec("", 1, now, now, Lease.transitions(l)))