Fencing Tokens
Fencing Tokens
Fencing tokens prevent stale leaders from performing writes after losing a lock. Each lock grant produces a monotonically increasing token per resource. An operation is valid only if it carries the latest token for that resource.
You are given a sequence of events:
grant: a lock grant with a tokenop: a write operation carrying a token
Rules:
- Track the latest token per resource from
grantevents. - An
opis accepted if its token equals the latest token for that resource. - Otherwise it is rejected.
Return a slice of booleans for each op event, in order.
Types
type Event struct {
Kind string // "grant" or "op"
Resource string
Client int
Token int
}
Function signature
func FencingDecisions(events []Event) []bool
Example
[grant r1 t1, op r1 t1, op r1 t0, grant r1 t2, op r1 t2]
=> [true, false, true]
Constraints
0 <= len(events) <= 100000Token >= 0
Notes
- If an
oparrives before any grant for its resource, it is rejected.
Run tests to see results
No issues detected