Files
greptimedb/src/mito2/AGENTS.md
jeremyhi 946e599d81 fix: catch up a writable region that still awaits its Leader lease (#10559)
* feat(store-api): ask an engine whether a region is a confirmed leader

Add RegionEngine::is_confirmed_leader. It answers None for a region the
engine does not find, Some(false) for a follower or, on a node that
requires leases to confirm its leaders, for a leader that a catchup
promoted and that no Leader or StagingLeader lease has confirmed since,
and Some(true) otherwise. The default answers from the role alone, with
the mapping RegionServer::is_region_leader uses.

Document that set_region_role_with_lease also returns false while a
catchup runs on the region or it waits for a reload, and that a catchup
also applies to a leader that is not confirmed.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* fix(mito2): catch a writable region awaiting its Leader lease up again

A migration that rolls back after its catchup can leave the target
region writable but waiting for a Leader lease that never comes, while
the source keeps acknowledging writes. A later upgrade must catch it up
again instead of treating it as ready.

- MitoEngine::is_confirmed_leader reads the role and the lease wait of
  one instance together under the promotion fence lock.
- begin_catchup checks, under that lock, whether the region still awaits
  its lease; if it does, it demotes the region, raises the downgrade
  time so earlier Leader leases are ignored, and marks the catchup. The
  handler then cancels the region's tail replay and reopens it if its
  memtable is not empty or its manifest changed. A catchup in progress
  or a pending reload answers RegionBusy before any demotion.
- Before a remote WAL replay the catchup waits until the entries the
  region acknowledged on this node are durable. If the wait fails, the
  catchup fails and the region stays a follower.
- While a catchup runs, a Leader or StagingLeader lease is still recorded
  and still extends the deadline, but neither promotes the region nor
  ends its wait; the catchup promotes it when it ends with set_writable.
  A catchup that fails or does not set the region writable lets the next
  lease promote it.
- MitoRegion::stop marks the fence stopped, and remove_region stops the
  region before it leaves the region map, so neither a catchup nor a
  Leader lease promotes a closed instance; such a catchup answers
  RegionNotFound.
- MitoEngine::extend_leader_deadline lets an engine that groups regions
  extend the deadline of a region that was not handed a lease.

Every catchup uses the source's replay checkpoint as before.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* fix(metric-engine): confirm a physical region only with both its regions

A physical region is a confirmed leader only if both its metadata and its
data region are, since a catchup may have promoted one without the
other.

A Leader lease that the metadata region ignores, for example while it
catches up, is still not handed to the data region, but it is now
recorded for the data region and extends its deadline, as mito does for
a lease it ignores.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* fix(datanode): catch up an upgraded region that is not a confirmed leader

The upgrade handler asks the engine once whether the region is a
confirmed leader and reports only such a region ready without a
catchup. A writable region that still awaits its Leader lease is sent a
catchup with set_writable, which catches it up again.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* refactor(mito2): share the end of a successful catchup

Move the part of a successful catchup that finalizes the leadership of a
promoted region, ends the catchup and replies into CatchupFinish, so
another catchup path can end the same way. No behaviour change.

Also fold two doc comment lines in region.rs that ran past 100 columns.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* fix(mito2): replace a region caught up again only if its replay succeeds

A catchup of a region that awaits its Leader lease opened its replacement,
registered it at once and dropped the old memtable before the replay. If
the durability wait or the replay then failed, the next Leader lease could
promote the empty or partial replacement, and a retry read the smaller
last entry id of the replacement, so it no longer waited for the entries
the region had acknowledged.

Now the old instance stays registered while an unregistered replacement
opened from its manifest waits for durability and replays the WAL outside
the worker. The worker registers and promotes the replacement only if the
replay succeeded and the old instance is still registered, does not wait
for a reload or a flush, and neither committed nor took a write meanwhile,
with or without the WAL. The catchup demotes the old instance unless it
is in a transient leader state, so a failed edit can make it writable
again. Otherwise the old instance ends its catchup, so a later Leader
lease can promote it with its memtable. A region that a failed catchup
again demoted still awaits its lease, so a retry takes the same path.

Every catchup from a remote WAL now also fails if its replay ends below
the last entry the region acknowledged on this node. A manifest sync of a
follower that awaits its Leader lease is refused, since it would replace
the memtable the region keeps.

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* chore(mito2): log a refused manifest sync and drop two restating comments

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* test: consolidate the catch-up-again tests

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

* test(mito2): cover rows dropped by a catchup again and fix review nits

Signed-off-by: jeremyhi <fengjiachun@gmail.com>

---------

Signed-off-by: jeremyhi <fengjiachun@gmail.com>
2026-11-23 08:51:02 +00:00

19 KiB

mito2 — Agent & Contributor Guide

Navigation aid for src/mito2. Keep it short and point to code; do not duplicate the code here. Paths are relative to the repo root.

Repo-wide rules that apply here: .agents/architecture-invariants.md.

What this crate does

Mito2 is GreptimeDB's primary time-series region storage engine. It owns the write path (memtable + WAL), flushing memtables to Parquet SST files, TWCS/windowed compaction, and the read path (multi-level merge + dedup with snapshot isolation). It implements the RegionEngine trait from store-api.

Module map

Module Path Purpose
engine src/mito2/src/engine.rs MitoEngine (the RegionEngine impl) and request dispatch
worker src/mito2/src/worker.rs, src/mito2/src/worker/ Per-region worker loop; write/alter/flush handlers
region src/mito2/src/region.rs, src/mito2/src/region/version.rs MitoRegion state and copy-on-write VersionControl snapshots
request src/mito2/src/request.rs WriteRequest/RegionRequest types and result channels
wal src/mito2/src/wal.rs Write-ahead log wrapper over log-store
memtable src/mito2/src/memtable/ In-memory write buffers (time-series / bulk / partition)
flush src/mito2/src/flush.rs FlushScheduler, WriteBufferManager, memtable → SST
compaction src/mito2/src/compaction/ Compaction scheduler (scheduler.rs + scheduler/), TWCS picker, strict-window manual picker, compactor, memory control
compaction/multi_region src/mito2/src/compaction/multi_region.rs Enterprise lossless routing by a captured table partition rule; selected SSTs to per-region outputs, without manifest publication
engine/table_compaction src/mito2/src/engine/table_compaction.rs Enterprise conditional publication of table-compaction results: shared-input snapshot, verified-output checks, and the manifest-locked swap of shared inputs for L0 outputs
access_layer src/mito2/src/access_layer.rs SST read/write over the object store
sst src/mito2/src/sst/ Parquet format, file metadata, index layout
series_index src/mito2/src/series_index/ Format interfaces (format.rs), index writer/searcher, sample artifacts and source coverage (artifact.rs), bucket planning, catalogs, reconciliation (maintenance.rs), and deferred purge
read src/mito2/src/read/ ScanRegion, merge, dedup, projection, streaming
manifest src/mito2/src/manifest/ RegionManifestManager, manifest actions/edits
cache src/mito2/src/cache.rs Write/file/page caches
gc src/mito2/src/gc.rs, src/mito2/src/gc/ Dropped-file cleanup worker
schedule src/mito2/src/schedule/ Local/remote background job scheduling
remap_manifest src/mito2/src/remap_manifest.rs Manifest path remapping for region copy/migration
config src/mito2/src/config.rs MitoConfig tuning knobs
tail_replay src/mito2/src/tail_replay.rs, src/mito2/src/worker/handle_tail_replay.rs Background replay of pending WAL tails into SSTs, published with one manifest edit
temp_file_sweeper src/mito2/src/temp_file_sweeper.rs Reclaims temporary files a crashed writer left in a region, triggered when the region becomes a writable leader or a create opens an existing region (under the hdfs-object-store feature)
test_util src/mito2/src/test_util.rs TestEnv and builders (under the test feature)

Write path

MitoEngine::handle_request (engine.rs), or handle_batch_put_requests for the puts of one request to several regions (one message per worker) → worker loop (worker/handle_write.rs) → sequence + WAL assembly (region_write_ctx.rs) → wal.rs → memtable (memtable/) → when buffer pressure trips, flush.rs writes SSTs via access_layer.rs and appends a RegionEdit to the manifest (manifest/manager.rs).

Read path

MitoEngine::handle_query (engine.rs) → read/scan_region.rs takes an immutable Version (region/version.rs) → scans memtables and Parquet SSTs (sst/parquet.rs) → merges (read/) and dedups by sequence → projected, filtered RecordBatch stream.

Internal series merges use the query runtime pool forwarded through QueryScanContext; reader and output-batch accounting uses the separate scan memory tracker. Direct scanner callers default to an unbounded operator pool.

Live compaction queries read shared per-region state directly from MitoRegion, without sending a worker request. The scheduler publishes active and pending task state together; readers clone task Arcs under a short read lock, then filter and build rows outside the lock. Execution handles provide live background phases. There is no second task registry or completed-task history. See compaction/scheduler/state.rs. The datanode provider reuses the lazy inspection scan path and retains residual SQL filters. Task cancellation uses RegionRequest::CancelCompaction through Region.Handle; the owning worker matches the live task ID without entering the DDL queue. Task-specific cancellation preserves other pending manual work when no DDL is pending. DDL cancellation retains its existing scheduling fence and priority.

Public surface

  • Entry: MitoEngine in src/mito2/src/engine.rs, built via MitoEngineBuilder.
  • Trait: impl RegionEngine for MitoEngine (store-api's region engine contract).
  • Consumed by datanode (sends RegionRequests) and the query layer (scans).

When you change X, also touch Y

  • Manifest format (manifest/action.rs): affects crash recovery and follower replay. Keep it backward compatible. Compaction task state does not belong in the manifest; publication uses ordinary file edits after locked input checks. Shared SSTs require Enterprise capability. Compatibility fixtures: tests/compatibility/manifest/table_compaction/ and tests/compatibility/manifest/pending_tails/.
  • SST/Parquet layout (sst/): readers must stay compatible with existing files.
  • Index extensions: shared contracts and provider validation live in sst/index/registry.rs; typed sketch options are enterprise-gated. Lifecycle and failure tests live in engine/index_build_test.rs and compaction/. A background rebuild must not replace a file that carries extension indexes (worker/handle_rebuild_index.rs::needs_background_indexes).
  • Series-index disk limit: tasks estimate usage from entry sizes in open-region snapshots and defer new builds when the shared estimate is full. Cleanup still prunes obsolete range entries and expires series indexes to reclaim published usage. Concurrent builds may overshoot; closed-region files are not counted. Series handles and the SST purger own file deletion.
  • Series format PoC: samples are rebuildable local SST sidecars. Optional writer interfaces (format.rs) integrate with reconciliation, catalog publication, and deferred purge. Defaults retain the original Parquet writer and disable samples. The opt-in columnar backend (series_index/columnar.rs) implements streaming OpenDAL writes and pinned format reads; its wire contract is in docs/formats/mito-series.md. Artifact objects record paths and sizes, including the manifest, so reads need no stat. Codecs currently run inline pending scheduling benchmarks. Sample payload reads coalesce nearby selected ranges in bounded, memory-accounted windows; boundary blocks keep timestamp-first pruning. Aligned native payload blocks share the page-cache budget with Parquet, with generation/namespace isolation and no shared lock held across I/O. Columnar stage timings and read counters are exported through Mito metrics. Columnar SeriesScan raw reads use scanner-wide preparation in read/series_scan/prepared.rs; ordinary record-batch scans retain their SST path. Native reader metadata shares the decoded SST cache reservation; maintenance readers bypass this cache. The paired experimental_series_index_format setting selects legacy or columnar artifacts.
  • Series-index coverage (series_index/catalog.rs): SeriesIndexEntry stores compaction-window width and SST summaries keyed by aligned start in both catalogs and Parquet footers.
  • Local index cache: startup checks a root layout version before workers start; missing or incompatible versions reset the whole rebuildable cache, including catalogs and range indexes. Samples use one generation directory; source SST identity stays in metadata.
  • Columnar samples: series_index/samples.rs implements opt-in sample artifacts, including nullable Float64 fields and preserved sequence/delete rows. Its wire contract is docs/formats/mito-samples.md; time/ID bounds may overlap across leaves and parts. Both columnar backends share cancellation-safe output ownership in series_index/artifact_writer.rs. Format readers do not apply query visibility; the prepared-source adapter applies visibility and the existing merge/dedup pipeline. Eligible sample merges use identity-only keys and omit tag columns; candidates retain memory-accounted series batch/row labels, and the raw adapter attaches explicit labels with region-qualified identities. Scanner preflight declares that representation so lazy streams and planned frames agree. Non-separable tag/sample residuals retain the full flat schema. Manual reconciliation covers all snapshot SSTs regardless of maintenance eligibility, but retains the soft disk limit. Manual builds validate columnar sample and tag schemas before reuse. Columnar reconciliation consolidates snapshot SSTs into UTC-day sample generations; catalogs record all source IDs. Grouped reads require complete daily coverage and all sources still visible, open each artifact once, and fall back for source-sequence-pruned scans. SST replacement invalidates a whole affected generation; catalog publication and reader handles govern retirement.
  • Request types (request.rs): usually tied to proto definitions consumed by datanode.
  • WAL/memtable encoding (wal/, memtable/): breaks replay if changed incompatibly.

Testing

cargo nextest run -p mito2

Tests live next to the code as *_test.rs (e.g. src/mito2/src/engine/flush_test.rs). TestEnv in test_util.rs spins up an engine over an in-process object store.

Gotchas

  • Parquet row groups have uniform row counts except for the final group (sst/parquet/row_selection.rs). Memory-pressure flushing must not create arbitrary partial groups: readers and index row IDs rely on this layout.
  • Sequence numbers are strictly increasing per region; dedup and snapshot reads depend on this. Do not change assignment lightly.
  • Manifest version is monotonic — never reset or skip it. Staging takes no version: its actions stay in memory (manifest/manager.rs) and are committed once, as the next version, when staging ends (MitoRegion::end_staging_locked).
  • In regions whose writes require a lease and whose WAL is the object store WAL, each manifest version has one fixed object name, created conditionally. A late create can succeed again once checkpoint cleanup frees the name, so a commit checks its result (ensure_version_not_checkpointed in manifest/storage.rs).
  • In those regions a commit after the region's Leader deadline sends nothing, and a version conflict fences the region (ManifestContext::fence_for_reload): no lease or catchup promotes it until the worker swaps in an instance reloaded from the new manifest (worker/handle_reload.rs). A tail replay round that fails while the storage holds a newer manifest fences a writable leader the same way. Never await a worker sender while holding a manifest guard.
  • Pending tails (RegionManifest::pending_tails) are WAL ranges a region still has to recover; the flushed entry id never prunes them. Opening a region replays its WAL after RegionManifest::wal_replay_start. The WAL releases at open, after a flush, and on close stay below this node's tails (cap_wal_release); truncate, drop, offline cleanup and catchup release without that cap. Handoff floors are dropped once the flushed entry id reaches them, in RegionManifestBuilder::try_build. While a region has pending tails, scans and memtable/SST stats fail with RegionPendingTails, and so do truncate, entering staging, a repartition flush, a close with flush_on_close, and every ALTER other than adding nullable columns without a default or changing options; compaction pauses. These checks run before any region state changes. Full-listing GC skips a region that has pending tails or whose manifest has_update() reports newer, or fails to check.
  • A leader that is writable, altering or editing replays its pending tails in one background task (tail_replay.rs), started by the worker when a lease, catchup or role change makes the region writable, or when it registers a region created or opened as a writable leader. Each tail goes into its own SSTs, since an SST can give all its rows its largest sequence. The task commits outside the edit queue (ManifestContext::update_manifest_for_tails) and always reports its end, which lets the worker start the next replay or reload a fenced region. The edit that records tails must raise the committed sequence to theirs, or writes accepted during the replay lose to replayed rows. This node's later entry ids must be above every tail's upto, and its unflushed entries at or below one must be in its own tails, or the tail commit's flushed entry id skips them. A node bounds its replays' concurrency and SST writes apart from flushes, and their chunk memory within the global write buffer (TailReplayLimits). Replayed SSTs build indexes as flushed ones (IndexBuildType::Flush), under create_on_flush. RegionRequest::DiscardPendingTails discards one node's tails without replaying them; it commits through update_manifest_for_tails too (worker/handle_tail_replay.rs) and raises the flushed entry id to the largest discarded upto, so the same entry id rule applies.
  • Lock ordering: take the manifest lock before updating version_control; the reverse deadlocks against concurrent flush/compaction.
  • All region I/O runs on tokio workers; never block_on inside a worker.
  • Loaded regions have resident compaction state, including Idle and handed-off DDLs. Publish create/open/catchup results through the worker's register_region; only close/drop unregisters them. See compaction/scheduler/state.rs.
  • Explicit truncate retires shared references too (worker/handle_truncate.rs): All removes all data; ByTimeRanges removes fully contained files. Other regions/readers still protect the shared objects. Stale compaction results must not reinstate truncated inputs.
  • Manifest and runtime file views share immutable FileMetaRef values. The engine-local metadata interner compares complete metadata before sharing; file identity alone is insufficient. Partition expressions are shared by table and complete expression content, so historical SST partition rules remain distinct after repartition. Interned references reclaim entries on final release, including delayed readers, without a maintenance task or new writes. Hash table capacity remains allocated until the interner is dropped. Manifest consumers receive the interner directly. Scrape-time entry counts use weak references and do not add maintenance tasks or retain engine storage. Index replacement preserves old readers and updates the lifetime guard to the newest metadata.
  • Local compaction units use the snapshot-free CompactionContext. Retaining a full CompactionRegion in a unit pins other units' inputs and delays SST purge.
  • Candidate waiting history is owned by the region scheduler (scheduler/history.rs). Background planning works on a private snapshot; only a fenced result may replace resident history, and an accepted plan consumes the fairness turn for each selected key. Execution success or failure must not rewrite waiting ages. TWCS discovery must not mutate scheduler state.
  • Compaction admission is engine-wide (compaction/scheduler/coordinator.rs), but pick/DDL state remains worker-owned. Repick only after every batch unit's Released; FIFO submission is not admission and pipeline children share the parent unit's slot. Keep service accounting across batches until region unload.
  • In flat batches, equal __primary_key dictionary keys imply the same series, but not the reverse: wrap_pk_binary_to_dict gives every row its own key. Compare key bytes when grouping rows by series.
  • Staging regions, ending staging included, never flush: a flush would stage its SST and release the rows' WAL, losing them if staging does not end successfully. Every flush trigger in worker/handle_flush.rs must skip them: background triggers test is_writable() and defers_background_flush(), a flush request is_flushable() and is_staging() (is_writable() and is_flushable() are false while ending staging). handle_close leaves staging first. Tail replays never run for them: entering staging fails while tails are pending.
  • Direct SST imports without a staging binding cannot publish into staging. worker/handle_manifest.rs seals edit admission while EnterStaging drains accepted edits. Scheduler-owned DDLs resume through execution, bypassing initial admission.
  • On a node whose writes need a lease, a region that a catchup made writable waits for a Leader or StagingLeader lease sent after the catchup (awaits_leader_lease). Until then ManifestContext::update_locked refuses its commits unless they are staged or the region is downgrading (a close that must flush downgrades first); only a replay of tails (TailsEditor::Replay) skips the check, a discard does not, and ending staging checks it before commit_staged_locked. No flush or compaction trigger starts work for a waiting region except a downgrade's flush. The lease that ends the wait wakes the worker (handle_leader_lease_wait_ended) to schedule the flushes the wait deferred and, only if the region is then a writable leader, start a compaction, build missing indexes, re-issue the index build retries the wait refused and persist backfilled metadata. A catchup of a waiting region catches it up again into an unregistered replacement while the region stays registered, demoted unless it is in a transient leader state. The replacement replaces it only if the replay succeeds and the region took no write and made no commit meanwhile; otherwise the region keeps its memtable for the next Leader lease (catch_up_again, handle_caught_up_again); the datanode upgrade handler sends that catchup unless RegionEngine::is_confirmed_leader is true, so an engine wrapping mito must implement that method from mito's answers (the metric engine requires both regions of the pair).
  • The latest lease each region received (GrantedLeases in lease.rs) lives outside the region instance, so a reopen during catchup keeps it. Every path that removes a region from a worker's region map must also remove its record and its kept index build retries: RegionWorkerLoop::remove_region (worker/handle_close.rs, also used after a close-time flush), drop_region (worker/handle_drop.rs) and RegionWorkerLoop::clean (worker.rs).

Maintenance contract

Update this file when you add/rename a top-level module, change the write/read path entry points, or alter a persisted format (manifest, SST, WAL).