Skip to content

Lease architecture

The PathLease controller decides where each PathLease stands among the leases of its repository. This document describes how, and why leases work the way they do.

Several agents changing one repository is the problem databases call concurrency control, and a lease borrows its answer from there.

Database idea In kslip
A transaction’s write set and read set A lease’s writes and reads.
Declaring the sets before running, so that order can be fixed up front A lease is created before the work starts, from a prediction of what the work touches.
A conflict: two writes of one item, or a write of what another transaction read Two leases conflict when their writes meet, or one’s writes meet the other’s reads.
The older transaction wins The earlier lease merges first, or runs first.
Optimistic control: run freely, validate at commit Ordered mode: both pieces of work proceed, and the later one rebases before it merges.
Pessimistic control: lock, and make others wait Exclusive mode: the later lease waits.
Locking a range, not only the rows in it A pattern describes paths that do not exist, so a lease on a directory covers files created in it.
Lock timeouts A lease expires unless its holder renews it.

Tracking reads is what closes the gap between “no two leases change the same file” and “the result still builds”. Work that changes a function and work that adds a caller of it change different files and conflict all the same.

The controller records each lease’s queue position with its first decision. This keeps a newly observed lease from moving ahead of a holder that has already started work.

Leases are decided in groups: the leases of one namespace that name the same repository. A change to any lease of a group, including its creation and deletion, makes the controller decide the whole group again.

The controller reads group membership and saved positions from the API server. It appends undecided leases after the existing queue, breaking ties by creation time and then name. Existing Pending and Held leases without a saved position keep precedence over undecided leases and receive positions in creation-time and name order.

On each pass the controller:

  1. Sets aside the leases that have expired, and reports them Expired. A lease reported Expired stays that way on later passes.
  2. Walks the remaining leases from earliest to latest. For each, it compares the lease with every earlier one:
    • no conflict: nothing to record;
    • a conflict where either lease is Exclusive: the earlier lease goes into the later one’s waitingFor;
    • a conflict between two Ordered leases: the earlier lease goes into the later one’s mergeAfter.
  3. Reports each lease Pending when its waitingFor holds anything and Held otherwise, and sets MergeReady when it is held and its mergeAfter is empty. It saves the decisions in queue order, recording each new position in the same write as its first decision.
  4. Schedules the next pass for the moment the next lease of the group expires.

A lease that is waiting still counts for the leases after it. Work that conflicts with a waiting lease merges after it, so a lease keeps its place in line however long it waits.

Whether two leases conflict comes down to whether two patterns can describe a common path. The answer has to hold for paths that do not exist, so it cannot be found by listing files.

internal/pathset answers it by walking the two patterns together, segment by segment. At each step it asks whether one more segment of a common path can be described by both: a ** describes any segment and may stay in place for the next one, and two ordinary segments describe a common name when their wildcards can be reconciled, which the same walk decides character by character. Positions that lead nowhere are remembered, so the walk visits each pair of positions once.

The package is tested against its definition. For every pair of patterns over a small vocabulary, the tests list all paths short enough to matter and check that the walk reports an intersection exactly when some path matches both.

A lease expires leaseDurationSeconds after its renewTime, or after its creation when it has never been renewed. The controller compares that moment with its own clock, so the holder’s clock and the controller’s need to agree to within a small part of the lease duration.

Expiry is final because the group moves on. Once a lease is out of the way, leases behind it are held and may start work on the same paths. Reviving the expired lease would put two pieces of work on those paths under an order neither of them was given.

A PathLease claims paths. Git access remains available outside the lease. The holder waits for Held before starting, renews while it runs, waits for MergeReady before merging, and deletes the lease afterwards. The Change controller checks these conditions before merging its own tasks.

A lease is also a prediction. Work can turn out to touch paths its lease does not name. Such work takes another lease for those paths, which takes its place after the leases that exist.