EsIndexManager
in package
Manages indexes: versioned physical indexes (`{alias}_vN`) plus a read/write alias pair for a zero-downtime full reindex.
The read alias serves search and the write alias serves indexing. During a reindex the write alias points to the new version and the read alias is switched atomically at the end.
Table of Contents
Properties
Methods
- __construct() : mixed
- beginReindex() : string
- Starts a full reindex of a base: creates a new version and points the write alias to it, so concurrent save hooks already write to the new version. Returns the physical name.
- ensureIndexes() : void
- Creates `_v1` and both aliases for every base of the source that does not exist yet.
- finishReindex() : void
- Finishes a full reindex: atomically swaps the read alias to the new version and cleans up the old ones.
- getStatus() : array<string, mixed>
- Returns cluster health and document counts per index for the monitoring cron endpoint.
- healWriteAlias() : array<int, string>
- Self-heals after a reindex killed midway (out of memory, timeout, kill).
- rollbackReindex() : bool
- Rolls back a failed reindex: points the write alias back to the stable version (the read alias target) and deletes the unfinished `_vN`. Otherwise save hooks would write to a version nobody reads until the next successful reindex, and orphan indexes would pile up.
- getAliasTargets() : array<string|int, string>
- Returns the physical indexes the alias currently points to.
- getPhysicalIndexes() : array<string|int, string>
- Returns the physical indexes of all versions of a base (by name pattern, even without an alias).
Properties
$client read-only
private
Client
$client
$config read-only
private
EsConfig
$config
Methods
__construct()
public
__construct(EsConfig $config, Client $client) : mixed
Parameters
- $config : EsConfig
- $client : Client
beginReindex()
Starts a full reindex of a base: creates a new version and points the write alias to it, so concurrent save hooks already write to the new version. Returns the physical name.
public
beginReindex(DocumentSourceInterface $source, string $base) : string
Parameters
- $source : DocumentSourceInterface
- $base : string
Return values
stringensureIndexes()
Creates `_v1` and both aliases for every base of the source that does not exist yet.
public
ensureIndexes(DocumentSourceInterface $source) : void
Parameters
- $source : DocumentSourceInterface
finishReindex()
Finishes a full reindex: atomically swaps the read alias to the new version and cleans up the old ones.
public
finishReindex(string $base, string $newPhysical) : void
IMPORTANT: the new version is published at the moment of the swap. An exception may leave this method only before it (refresh, alias listing, swap), while the read alias still points to the old version and the caller can safely retry or discard the run. Cleanup of old versions after the swap is best-effort: a failure is only logged (the next reindex removes whatever remains, as it deletes all old versions by name pattern) and must never turn the published version into a rollback candidate.
Parameters
- $base : string
- $newPhysical : string
getStatus()
Returns cluster health and document counts per index for the monitoring cron endpoint.
public
getStatus() : array<string, mixed>
Return values
array<string, mixed>healWriteAlias()
Self-heals after a reindex killed midway (out of memory, timeout, kill).
public
healWriteAlias(DocumentSourceInterface $source[, callable(): bool|null $mayApply = null ]) : array<int, string>
beginReindex() points the write alias to the new _vN at the start, so when the rollback
did not run, the write alias stayed on a version nobody searches and ongoing writes were
lost. Called from the sync that runs outside a running reindex (lock); it points the write
alias back to the read alias target. The unfinished _vN is deleted by the next successful
reindex (finishReindex() removes all old versions by name pattern).
$mayApply is called right before each alias write; the caller verifies in it that the
healing lease is still held and no reindex has started. When it returns false, healing stops
without further changes (the next sync catches up).
Parameters
- $source : DocumentSourceInterface
- $mayApply : callable(): bool|null = null
Return values
array<int, string> —bases whose write alias was repaired
rollbackReindex()
Rolls back a failed reindex: points the write alias back to the stable version (the read alias target) and deletes the unfinished `_vN`. Otherwise save hooks would write to a version nobody reads until the next successful reindex, and orphan indexes would pile up.
public
rollbackReindex(string $base, string $abandonedPhysical) : bool
IMPORTANT: the current alias state decides, not the stored run state. When the read alias already points to the abandoned version (the swap happened and only cleanup or clearing the Redis state failed, or the swap passed with an uncertain response), it is the published version: the write alias is realigned to it and the index is not deleted, because deleting it would take the read alias along and leave search without an index. The guard is per base: with several partially finished bases, published ones stay on the new version and only the others are rolled back.
Parameters
- $base : string
- $abandonedPhysical : string
Return values
bool —true when the unfinished version was discarded, false when it is published and kept
getAliasTargets()
Returns the physical indexes the alias currently points to.
private
getAliasTargets(string $alias) : array<string|int, string>
Parameters
- $alias : string
Return values
array<string|int, string>getPhysicalIndexes()
Returns the physical indexes of all versions of a base (by name pattern, even without an alias).
private
getPhysicalIndexes(string $readAlias) : array<string|int, string>
Parameters
- $readAlias : string