\Drupal\strata\Flush Lease

Keeps two flushes, or two compactions, from running at once.

Cron can overlap with itself, with Drush, and with a request that triggered a flush directly. Two flushes reading the same journal window would each seal it, producing two segments describing the same operations and two commits racing for the same ref.

The lease is a row whose name is the primary key, so acquiring it is an INSERT that either succeeds or collides. A holder that dies without releasing leaves a row behind, which is why every lease carries a deadline rather than only an owner.

Summary

Methods
Properties
Constants
__construct
acquire
release
holds
collect
No public properties found
TABLE
FLUSH
COMPACTION
DEFAULT_TTL
No protected methods found
No protected properties found
No protected constants found
No private methods found
held
clock
database
No private constants found

Constant

TABLE

TABLE = 'strata_lock'

The table leases live in.

FLUSH

FLUSH = 'flush'

Lease the flush pipeline takes.

COMPACTION

COMPACTION = 'compaction'

Lease compaction takes.

DEFAULT_TTL

DEFAULT_TTL = 300

Seconds a lease is held before it is considered abandoned.

Properties

$held

$held : array

The token this instance holds, keyed by lease name.

Type

array<string, string> —

$clock

$clock : \Closure

Returns the current unix timestamp.

Type

Closure

$database

$database : \Drupal\Core\Database\Connection

Type

Connection

Methods

__construct()

__construct(\Drupal\Core\Database\Connection  $database, callable  $clock = null) : mixed

Constructs a lease store.

Parameters

\Drupal\Core\Database\Connection $database

Where leases are recorded.

callable $clock

Returns the current unix timestamp. Injected so a test can drive expiry without sleeping.

Returns

mixed —

acquire()

acquire(string  $name, int  $ttl = \self::DEFAULT_TTL) : bool

Takes a lease, or reports that someone else holds it.

An expired lease is taken over rather than waited for. The previous holder is gone by definition, and a flush that never runs because a crashed process left a row is worse than one that overlaps a process which no longer exists.

Parameters

string $name

Lease name.

int $ttl

Seconds before this lease expires.

Returns

bool —

TRUE when the lease is now held by this instance.

release()

release(string  $name) : bool

Releases a lease this instance holds.

The token is part of the condition, so a process whose lease already expired and was taken over cannot release the new holder's lease on its way out.

Parameters

string $name

Lease name.

Returns

bool —

TRUE when a lease held by this instance was released.

holds()

holds(string  $name) : bool

Whether this instance holds a lease.

Parameters

string $name

Lease name.

Returns

bool —

TRUE when this instance took it and has not released it.

collect()

collect() : int

Removes expired leases.

Returns

int —

How many were removed.