locktable

package
v3.0.0-rc48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

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

View Source
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

View Source
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

func Lease

func Lease(d time.Duration) time.Duration

Lease returns the lease a request is granted: the default when it names none, and the longest granted when it names more.

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.

func (ErrHeld) Error

func (t ErrHeld) Error() string

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.

func NewHeld

func NewHeld() *Held

NewHeld returns an empty record of held locks.

func (*Held) Add

func (t *Held) Add(lock Lock)

Add records a lock a client of this node was granted.

func (*Held) List

func (t *Held) List() []Lock

List returns the locks the clients of this node hold, by name.

func (*Held) Remove

func (t *Held) Remove(name, id string)

Remove forgets the lock name held under id.

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 NewTable

func NewTable() *Table

NewTable returns an empty table, not rebuilt.

func (*Table) Acquire

func (t *Table) Acquire(ctx context.Context, req Request) (Lock, error)

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

func (t *Table) Forget(name, id string)

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

func (t *Table) Generation() uint64

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

func (t *Table) Granted() []Lock

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) IsRebuilt

func (t *Table) IsRebuilt() bool

IsRebuilt says the table holds every lock the cluster holds.

func (*Table) List

func (t *Table) List() []Lock

List returns the locks held, by name.

func (*Table) Rebuild

func (t *Table) Rebuild(generation uint64, locks []Lock) ([]Lock, bool)

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.

func (*Table) Release

func (t *Table) Release(name, id string) (Lock, bool)

Release frees the lock name granted under id, and returns it, with whether it was held under it. A lock that lapsed, or was granted again since, is not this id's to release.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL