doris icon indicating copy to clipboard operation
doris copied to clipboard

[Bug][Meta Cache] Complete global invalidation ordering after PR #65126

Open wenzhenghu opened this issue 4 months ago • 2 comments

Background

PR #65126 now fixes the per-key ordering problem between object cache entries and ID-to-name navigation state for external metadata caches.

The current PR covers the key-scoped paths below for both database and table layers:

  • hot named lookup
  • miss-load / generation-rejected load
  • register / update
  • invalidate / unregister / rename

So this issue no longer tracks those per-key races.

Remaining gap

The remaining correctness gap is on full invalidation / reset style paths that still update object caches and ID maps in separate steps, without a full publication barrier across all affected keys / stripes.

Examples include patterns like:

  • tables.invalidateAll(); tableIdToName.clear();
  • databases.invalidateAll(); dbIdToName.clear();

With the current structure, a concurrent named lookup / load / publication may still race with invalidateAll() / full reset and repopulate object or ID state after cleanup has started.

Why this is a follow-up issue

Fixing this correctly is broader than the per-key ordering work already merged into PR #65126.

A proper solution likely needs:

  • a full invalidation barrier or equivalent global generation protocol
  • fixed-order coordination across stripes / keys to avoid deadlock
  • careful interaction with manual load, refresh, and removal-listener paths
  • dedicated deterministic concurrency tests

That is a larger change than the current PR scope.

Expected scope

This follow-up should focus on the remaining non-per-key cases, including:

  1. full invalidation / reset paths for table caches
  2. full invalidation / reset paths for database caches
  3. interactions between invalidate-all style cleanup and concurrent named lookup / miss-load / publication

Expected guarantees

At a minimum, the final fix should ensure:

  • invalidateAll() / full reset cannot be undone by a concurrent late publication
  • object cache and ID navigation state are cleared under a consistent global ordering rule
  • the per-key guarantees already added in PR #65126 remain unchanged
  • replay stays cache-only
  • no remote I/O is introduced under publication locks

Test expectations

Please add deterministic concurrency tests for at least:

  • invalidateAll() / full reset vs hot named lookup
  • invalidateAll() / full reset vs blocked miss-load
  • both table and database cache paths

Related context

  • PR #65126
  • Review discussion: https://github.com/apache/doris/pull/65126#discussion_r3642519993

wenzhenghu avatar Jul 24 '26 07:07 wenzhenghu

Breakwater initial analysis

Triage: Confirmed FE external-metadata-cache correctness bug. Recommended priority is P2 / medium: the race requires concurrent lookup and drop/rename/invalidation, but it can leave state that explicit cleanup has already removed. The issue is currently open with no labels, assignee, milestone, project, or linked implementation.

Code-level finding

I verified the behavior against PR #65126 at head 19b7bdd5f538d2e8f398cfca79d8a6fbb5c46802.

The root cause is that the object cache has a key-scoped publication protocol, but the corresponding ID-to-name map does not participate in that protocol:

  • ExternalCatalog.getDbNullable(String) calls databases.get(dbName) and then performs dbIdToName.put(...) after MetaCacheEntry.get() has returned.
  • ExternalDatabase.getTableNullable(String) has the identical late tableIdToName.put(...).
  • Table invalidation removes the object and then removes matching ID bindings in invalidateTableCache; database invalidation has the same split sequence in invalidateDatabaseCache. ConcurrentHashMap makes each map operation safe, but it does not make the combined object/ID transition atomic.
  • On a miss, MetaCacheEntry.getWithManualLoad() intentionally returns the loaded object when its captured generation was invalidated, while refusing to cache it. The upper-layer late put cannot distinguish that rejected publication and therefore creates an ID binding for an object that was never committed.
  • The baseline claim is also correct: the legacy MetaCache writes idToName after miss loading or object update, while invalidate() is not synchronized with that combined sequence. PR #65126 adds the hot-hit form of the race but does not introduce the entire bug class.

The immediate consequence is a stale ID-to-name binding after explicit cleanup. A later by-ID lookup can follow that binding and attempt another object load. This does not by itself demonstrate incorrect query results on ordinary named lookup paths, so the issue's limited-impact assessment is reasonable; however, the stale navigation is a definite violated cache invariant rather than a speculative risk.

Recommended fix contract

The durable fix should make object publication and ID binding one key-scoped commit:

  1. Remove the read-side ID-map repair from the two named lookup methods. A hot read should not mutate navigation state.
  2. Publish the ID binding only as part of a successfully committed object load or explicit object publication. A generation-rejected load may still be returned to its initiating caller under the current API contract, but it must not populate either the object cache or the ID map.
  3. Run explicit invalidation/unregister and ID-binding removal under the same key publication lock/generation boundary. A post-invalidation writer from an older operation must be unable to restore the binding.
  4. Route incremental register/update through that same key-scoped mutation API, including the intentional cold-object case where only navigation state is retained. Otherwise register/update can still race with drop/rename even after the named-lookup writes are removed.
  5. Preserve the documented distinction between passive TTL/capacity eviction and explicit cleanup: an ID binding may intentionally outlive passive object eviction so by-ID lookup can reload, but it must not outlive drop, rename of the old key, unregister, reset, or explicit invalidation.

This is best encapsulated in a small coordinator or in outcome-aware MetaCacheEntry mutation APIs (for example, a successful-publication callback and an invalidation callback executed while holding the entry's existing publication lock). A separate map write followed by a cache-presence recheck would remain a TOCTOU race and is not sufficient.

Tests required

Please use latches/test hooks rather than sleeps and cover both database and table coordinators:

  • hot named hit paused at the publication/binding boundary versus drop and versus rename;
  • slow miss load versus drop/rename, with invalidation occurring while the loader is blocked;
  • generation-rejected load: the racing named caller may receive its loaded value, but the object is absent from cache and the ID binding is absent afterward;
  • incremental register/update versus invalidation, including the cold-object/hot-ID behavior;
  • full reset/invalidate-all if it uses a different generation path.

Each test should assert the object entry, ID binding, and by-ID behavior together. For drop, by-ID lookup must return null without triggering a fresh object load. For rename, the old name must not be restored and the ID must resolve only according to the final rename state.

Missing decisions / next steps

No production log, profile, version string, or user reproduction is needed to establish this bug; the race is confirmed statically. Before implementation, maintainers should record:

  • whether the current behavior of returning a generation-rejected loaded object to the initiating named caller remains intentional;
  • the exact passive-eviction versus explicit-invalidation ID-retention contract;
  • the target base and backport branches after PR #65126 is finalized.

Suggested next step: assign the FE/external-catalog cache owner, add the coordinator-level deterministic tests first, then implement the shared publication/invalidation API and apply it symmetrically to database and table paths.

Breakwater-GitHub-Analysis-Slot: slot_c0caa06b02a5

Doris-Breakwater avatar Jul 24 '26 07:07 Doris-Breakwater

Preliminary maintainer assessment

Verdict: confirmed concurrency correctness bug, with high-confidence code-level root cause. The issue's low-frequency/P2-style impact assessment is reasonable, but the invariant violation is real and should be fixed rather than treated as an observability-only problem. The issue is currently open with no labels, assignee, milestone, or linked development item.

Code-path confirmation

I checked PR #65126 at head 19b7bdd5f538d2e8f398cfca79d8a6fbb5c46802 and its merge-base baseline 805b746addd7496e2df520d85b8fc6fe006f8636.

  • In the PR, ExternalDatabase.getTableNullable(String) calls tables.get(finalName) and then independently calls tableIdToName.put(...). ExternalCatalog.getDbNullable(String) has the identical databases.get(...) / dbIdToName.put(...) sequence. A hot read can therefore observe an object, lose the race to explicit invalidation, and restore only the ID mapping afterward.
  • The miss path is also confirmed. MetaCacheEntry.getWithManualLoad() returns the loaded value even when its captured generation changed and cache publication was rejected. The caller cannot distinguish that result from a successfully published object and consequently republishes the ID mapping.
  • Incremental updates have the same split commit: updateTableCache() / updateDatabaseCache() perform object put and then ID-map put, while invalidateTableCache() / invalidateDatabaseCache() invalidate the object and then remove matching ID entries. Full invalidation similarly invalidates objects before clearing the ID map. None of these pairs share one linearization point.
  • This class of problem predates the refactor. Baseline MetaCache.getMetaObj() publishes to metaObjCache before idToName.put, updateCache() also updates the object before the ID map, and invalidate() removes them in separate operations. PR #65126 adds the new hot-hit late-write window but is not the sole origin.

The resulting stale entry is persistent because the ID maps have no TTL. Normal by-ID lookup then resolves the stale name and calls databases.get(name) / tables.get(name). After a drop or rename, the names/existence check will usually reject the reload, so the most likely symptom is repeated load work or failed by-ID resolution rather than a stale object on common name-based query paths. Replay by-ID lookup remains cache-only and normally returns empty once the object entry is gone. This supports the issue's current impact assessment without weakening the correctness finding.

Required fix invariant

The fix should provide one per-object-key publication protocol covering the object entry and its ID navigation entry:

  1. Add the ID mapping only when object publication is still valid under the same publication/generation fence.
  2. Remove the ID mapping during explicit key/full invalidation under that same fence, so an earlier lookup or update cannot write it back afterward.
  3. Do not publish an ID mapping for a generation-rejected load, even though the current in-flight caller may still receive the loaded object.
  4. Route hot named lookup, manual miss load, explicit/incremental put, unregister/drop/rename, and full reset through the same primitive; removing only the two late put calls is insufficient.

A coordinator or a MetaCacheEntry atomic callback/result API would both be viable. The important property is that “object is current” validation, ID-map mutation, and explicit invalidation share the entry's short publication lock; slow remote loading must remain outside that lock.

One compatibility detail must be preserved: the ID map intentionally outlives ordinary TTL/capacity eviction so normal by-ID lookup can reload a cold object. Cleanup should therefore be coupled to explicit invalidation/unregister/reset, not indiscriminately to every Caffeine removal cause.

Recommended deterministic validation

  • Add latch/hook-based tests at both catalog and table levels for:
    • hot named hit paused before ID publication vs unregister/drop and rename;
    • miss loader paused while explicit invalidation advances the generation;
    • generation-rejected publication returning an object to the in-flight caller without leaving either an object entry or ID mapping;
    • incremental object update vs key invalidation, plus full invalidation/reset.
  • After each invalidation wins, assert all of the following: the object cache is empty for the old key, the old ID mapping is absent, replay-by-ID is empty, and normal by-ID does not restore the mapping or repeatedly invoke the loader.
  • For rename, assert the old ID/name pair cannot reappear and only the successfully published new object owns the new mapping.
  • Keep focused coverage in ExternalCatalogTest and ExternalDatabaseTest; extend MetaCacheEntryTest if the chosen atomic API lives there. Run the corresponding FE unit-test suites.

Missing information

No runtime logs, profile, or production reproduction are needed to confirm this code-level race. The only non-blocking triage gaps are the intended target base (stacked on #65126 versus follow-up after merge) and which public release branches containing the baseline implementation should receive a backport. Please also add the repository-standard bug and external-catalog/meta-cache labels and assign an owner.

Breakwater-GitHub-Analysis-Slot: slot_c0caa06b02a5

Doris-Breakwater avatar Jul 24 '26 07:07 Doris-Breakwater