Documentation
¶
Overview ¶
Package locktable holds the cluster locks: the table the node speaking for the cluster grants them from, and the record every node keeps of the locks its own clients hold.
A lock is granted for a lease, and lapses when the lease ends whatever its holder became: a process that died holding one does not hold it forever.
A lease is counted on the clock of the node keeping the lock, which is the monotonic clock of the process: a step of the wall clock does not end it early. The nodes pass a lease to one another as the time left, which each adds to its own clock, so their clocks need not agree on the time, only tick at the same rate. The holder is to be done before the lease ends, which it counts on its own clock the same way.
The table is in memory, and the node speaking for the cluster changes. A node taking the speaking over rebuilds its table from what every node says its clients hold, and what it granted while it spoke, before it grants anything: its own table is the one it had when it last spoke, or none. The grants are read too because a lock granted to a request a node handed over is held before that node hears of it.
Index ¶
- Constants
- Variables
- func Lease(d time.Duration) time.Duration
- type ErrHeld
- type Held
- type Lock
- type Request
- type Table
- func (t *Table) Acquire(ctx context.Context, req Request) (Lock, error)
- func (t *Table) Drop()
- func (t *Table) Forget(name, id string)
- func (t *Table) Generation() uint64
- func (t *Table) Granted() []Lock
- func (t *Table) IsRebuilt() bool
- func (t *Table) List() []Lock
- func (t *Table) Rebuild(generation uint64, locks []Lock) ([]Lock, bool)
- func (t *Table) Release(name, id string) (Lock, bool)
Constants ¶
const ( // DefaultLease is the lease of a request naming none. DefaultLease = 30 * time.Second // MaxLease is the longest lease granted. A lock is for a short critical // section, and the lease is what frees it when its holder dies. MaxLease = 10 * time.Minute )
Variables ¶
var ( // ErrNotRebuilt is the error of a request to a table not rebuilt yet. ErrNotRebuilt = errors.New("the cluster lock table is not rebuilt from the nodes yet") // SpeakerTable is the table this node grants from while it speaks for the // cluster. SpeakerTable = NewTable() // LocalHeld is the locks the clients of this node hold. LocalHeld = NewHeld() )
Functions ¶
Types ¶
type ErrHeld ¶
type ErrHeld struct {
Lock Lock
}
ErrHeld is the error of a lock still held by another at the end of the wait.
type Held ¶
type Held struct {
// contains filtered or unexported fields
}
Held is the locks the clients of a node hold, which is what a node taking the speaking over rebuilds its table from.
type Lock ¶
type Lock struct {
Name string `json:"name"`
ID string `json:"id"`
// Node is the node whose client holds the lock.
Node string `json:"node"`
// Holder says who holds it, for the reader of a listing.
Holder string `json:"holder,omitempty"`
AcquiredAt time.Time `json:"acquired_at"`
ExpiresAt time.Time `json:"expires_at"`
}
Lock is a cluster lock granted.
type Request ¶
type Request struct {
Name string
Node string
Holder string
// Lease is how long the lock is held once granted, unless released
// before.
Lease time.Duration
// Wait is how long the request waits for a lock held by another.
Wait time.Duration
}
Request is a lock asked for.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is the locks the node speaking for the cluster granted.
func (*Table) Acquire ¶
Acquire grants the lock req names, waiting for as long as req says when another holds it, and answers ErrHeld when the wait ends with the lock still held.
func (*Table) Drop ¶
func (t *Table) Drop()
Drop forgets the table, which is no longer this node's to keep, or is about to be again. The requests waiting are answered ErrNotRebuilt.
func (*Table) Forget ¶
Forget says a lock was released: it is dropped from the locks this node granted before its table was dropped, from its table, where a rebuild may have brought it back from a node that had not heard of the release yet, and its id is kept from the rebuilds to come.
func (*Table) Generation ¶
Generation returns the generation a rebuild is for: the nodes are read after it is, and the table is rebuilt from them only if it was not dropped meanwhile.
func (*Table) Granted ¶
Granted returns the locks this node granted and whose lease has not ended: the ones of its table, and the ones it held when it was last dropped. It is what a node rebuilding its table reads of this one, besides the locks its clients hold.
func (*Table) Rebuild ¶
Rebuild replaces the locks of the table with the ones the nodes say their clients hold, and marks it rebuilt. It says false, and changes nothing, when the table was dropped since generation, which is the nodes being read for a table this node may no longer keep.
Two nodes saying they hold the same lock is a lock two speakers granted apart, which only the end of one of the leases settles: the one held the longest is kept, and the other is still held by its client, which is returned.