savagearts-tech opened a new issue, #71461:
URL: https://github.com/apache/doris/issues/71461

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   4.1.5 (also verified 4.1.4-rc01 is NOT affected; introduced into branch-4.1 
by PR #66717, merged 2026-08-25)
   
   ### What's Wrong?
   
   
   FE occasionally hangs completely: all queries and refresh operations stall, 
and jstack shows a **Java-level deadlock** between two threads in the external 
meta cache path. We captured **6 identical deadlocks** in production (3 FE 
nodes, Doris 4.1.5, JDK 17.0.2, Kubernetes).
   
   The two threads:
   
   1. **Cache invalidation thread** (`thrift-server-pool-*`, executing `REFRESH 
CATALOG` forwarded to master, or journal replay of a refresh): holds the 
`AbstractExternalMetaCache` object **monitor** (added by #66717), then calls 
`entry.close()` → `MetaCacheEntry.invalidateAll()` (holds the per-entry lock) → 
Caffeine `clear()` → waits for a **ConcurrentHashMap bin lock**.
   2. **Query miss-load thread** (`mysql-nio-pool-*`, first query touching a 
not-yet-loaded cache entry): inside Caffeine `computeIfAbsent` it **holds the 
same CHM bin lock** while running the loader, which reaches 
`AbstractExternalMetaCache.initCatalog()` and **waits for the monitor** held by 
thread 1.
   
   Result: classic AB-BA deadlock, both threads blocked forever, FE stops 
serving.
   
   Observed lock cycle (from production jstack):
   
   ```
   Thread A (invalidateCatalog):  holds AEMC monitor ──► waits CHM bin lock
   Thread B (miss load):          holds CHM bin lock ──► waits AEMC monitor
   ```
   
   ### What You Expected?
   
   
   Cache invalidation and miss loads should not be able to deadlock; `REFRESH 
CATALOG` and concurrent queries should always make progress.
   
   ### How to Reproduce?
   
   
   Trigger condition (production-observed): an **Iceberg external catalog** 
with meta cache enabled, while `REFRESH CATALOG` (or its journal replay) 
executes concurrently with queries that miss-load cache entries.
   
   Important amplifier: we run with 
`enable_external_meta_cache_manual_miss_load = false` (upstream default is 
`true`). With `false`, the miss load executes synchronously on the query thread 
inside Caffeine's `computeIfAbsent` bin lock, which widens the deadlock window. 
With the default `true` the window is narrower but the lock cycle still exists.
   
   A deterministic reproducer (JUnit, no cluster needed): pre-insert two keys 
with equal `hashCode()` (e.g. `"Aa"` / `"BB"`, both 2112) into a 
`MetaCacheEntry` so Caffeine's loading bin contains a visible 
non-ReservationNode, start a loader thread that enters the bin, then start an 
`invalidateCatalog` thread. On unpatched 4.1.5 both threads end up BLOCKED 
(join timeout 60s). Happy to attach the test.
   
   ### Anything Else?
   
   ## Root Cause Analysis
   
   In 4.1.5, PR #66717 added `synchronized` to methods of 
`AbstractExternalMetaCache` (`invalidateCatalog` / `initCatalog`):
   
   - `invalidateCatalog()` (AEMC.java:236, holds monitor) → `removed.close()` → 
`MetaCacheEntry.invalidateAll()` (MetaCacheEntry.java:540, holds per-entry 
lock) → Caffeine `clear()` → CHM bin lock
   - `MetaCacheEntry.get()` (MetaCacheEntry.java:304, holds CHM bin lock inside 
`computeIfAbsent`) → loader → `ExternalMetaCacheMgr.prepareCatalogByEngine()` → 
`AEMC.initCatalog()` (AEMC.java:166, waits monitor)
   
   The monitor→bin-lock (A) and bin-lock→monitor (B) order is inverted → 
deadlock. 4.1.4-rc01 has no `synchronized` on these methods, so the cycle 
cannot form.
   
   ## Anything Else (jstack excerpt, production, 4.1.5)
   
   <details><summary>jstack: Found one Java-level deadlock (full two-thread 
stacks)</summary>
   
   ```
   Found one Java-level deadlock:
   =============================
   "thrift-server-pool-798":
     waiting to lock monitor 0x00007fa8f806e670 (object 0x00000005341088c8, a 
java.util.concurrent.ConcurrentHashMap$Node),
     which is held by "mysql-nio-pool-5660"
   
   "mysql-nio-pool-5660":
     waiting to lock monitor 0x00007fa42002ed90 (object 0x0000000504990290, a 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache),
     which is held by "thrift-server-pool-798"
   
   "thrift-server-pool-798":
        at 
java.util.concurrent.ConcurrentHashMap.computeIfPresent([email protected]/ConcurrentHashMap.java:1819)
        - waiting to lock <0x00000005341088c8> (a 
java.util.concurrent.ConcurrentHashMap$Node)
        at 
com.github.benmanes.caffeine.cache.BoundedLocalCache.removeNode(BoundedLocalCache.java:1843)
        at 
com.github.benmanes.caffeine.cache.BoundedLocalCache.clear(BoundedLocalCache.java:1819)
        at 
com.github.benmanes.caffeine.cache.LocalManualCache.invalidateAll(LocalManualCache.java:150)
        at 
org.apache.doris.datasource.metacache.MetaCacheEntry.invalidateAll(MetaCacheEntry.java:540)
        - locked <0x0000000530b692b0> (a java.lang.Object)
        at 
org.apache.doris.datasource.metacache.MetaCacheEntry.close(MetaCacheEntry.java:553)
        at 
org.apache.doris.datasource.metacache.CatalogEntryGroup.lambda$close$1(CatalogEntryGroup.java:57)
        at 
org.apache.doris.datasource.metacache.CatalogEntryGroup$$Lambda$5480/0x00000008028efae0.accept(Unknown
 Source)
        at 
java.util.concurrent.ConcurrentHashMap.forEach([email protected]/ConcurrentHashMap.java:1603)
        at 
org.apache.doris.datasource.metacache.CatalogEntryGroup.close(CatalogEntryGroup.java:55)
        at 
org.apache.doris.datasource.metacache.AbstractExternalMetaCache.invalidateCatalog(AbstractExternalMetaCache.java:236)
        - locked <0x0000000504990290> (a 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache.invalidateCatalog(IcebergExternalMetaCache.java:593)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache.invalidateCatalogEntries(IcebergExternalMetaCache.java:602)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.lambda$invalidateCatalog$3(ExternalMetaCacheMgr.java:390)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.safeInvalidate(ExternalMetaCacheMgr.java:766)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.routeCatalogEngines(ExternalMetaCacheMgr.java:744)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.invalidateCatalog(ExternalMetaCacheMgr.java:388)
        at 
org.apache.doris.datasource.ExternalCatalog.onRefreshCache(ExternalCatalog.java:725)
        at 
org.apache.doris.catalog.RefreshManager.refreshCatalogInternal(RefreshManager.java:84)
        at 
org.apache.doris.catalog.RefreshManager.handleRefreshCatalog(RefreshManager.java:65)
        at 
org.apache.doris.nereids.trees.plans.commands.refresh.RefreshCatalogCommand.run(RefreshCatalogCommand.java:83)
        at 
org.apache.doris.qe.StmtExecutor.executeByNereids(StmtExecutor.java:778)
        at org.apache.doris.qe.StmtExecutor.execute(StmtExecutor.java:617)
        at 
org.apache.doris.service.FrontendServiceImpl.forward(FrontendServiceImpl.java:1250)
        at 
org.apache.doris.thrift.FrontendService$Processor$forward.getResult(FrontendService.java:4905)
        at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:40)
        at 
org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:264)
   
   "mysql-nio-pool-5660":
        at 
org.apache.doris.datasource.metacache.AbstractExternalMetaCache.initCatalog(AbstractExternalMetaCache.java:166)
        - waiting to lock <0x0000000504990290> (a 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.prepareCatalogByEngineLocked(ExternalMetaCacheMgr.java:263)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.prepareCatalogByEngine(ExternalMetaCacheMgr.java:240)
        at 
org.apache.doris.datasource.ExternalMetaCacheMgr.iceberg(ExternalMetaCacheMgr.java:188)
        at 
org.apache.doris.datasource.iceberg.IcebergUtils.loadPartitionInfo(IcebergUtils.java:2142)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache.loadSnapshotProjection(IcebergExternalMetaCache.java:1150)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache.executeForGeneration(IcebergExternalMetaCache.java:1199)
        at 
org.apache.doris.datasource.metacache.MetaCacheEntry.loadAndTrack(MetaCacheEntry.java:1485)
        at 
org.apache.doris.datasource.metacache.MetaCacheEntry.lambda$get$1(MetaCacheEntry.java:304)
        at 
com.github.benmanes.caffeine.cache.BoundedLocalCache.doComputeIfAbsent(BoundedLocalCache.java:2404)
        at 
java.util.concurrent.ConcurrentHashMap.compute([email protected]/ConcurrentHashMap.java:1955)
        - locked <0x00000005341088c8> (a 
java.util.concurrent.ConcurrentHashMap$Node)
        at 
com.github.benmanes.caffeine.cache.BoundedLocalCache.computeIfAbsent(BoundedLocalCache.java:2387)
        at 
org.apache.doris.datasource.metacache.MetaCacheEntry.get(MetaCacheEntry.java:304)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalMetaCache.getSnapshotCache(IcebergExternalMetaCache.java:424)
        at 
org.apache.doris.datasource.iceberg.IcebergUtils.getLatestSnapshotCacheValue(IcebergUtils.java:2402)
        at 
org.apache.doris.datasource.iceberg.IcebergExternalTable.loadSnapshot(IcebergExternalTable.java:264)
        at 
org.apache.doris.nereids.StatementContext.loadSnapshots(StatementContext.java:1130)
        at 
org.apache.doris.nereids.rules.analysis.BindRelation.getLogicalPlan(BindRelation.java:481)
        at 
org.apache.doris.nereids.rules.analysis.BindRelation.doBindRelation(BindRelation.java:158)
        at 
org.apache.doris.nereids.jobs.rewrite.BottomUpVisitorRewriteJob.rewrite(BottomUpVisitorRewriteJob.java:107)
   ```
   
   </details>
   
   ### Proposed fix direction (we have a working patch, happy to submit a PR)
   
   Keep the budget-release semantics but decouple it from the blocking path:
   
   1. In `invalidateCatalog` (inside the monitor): remove the entry and 
**release budgets with scalar arithmetic** (`releaseBudgets()`) — never 
touching Caffeine structures while holding the monitor.
   2. Keep the `drain()` of the detached entry **outside** the monitor.
   3. Split `releaseBudget()`/`drain()` so the CAS-based release is idempotent; 
keep lock order strictly one-way: `monitor → manager lock`, never the reverse.
   
   With this patch the deterministic reproducer passes in 0.5s (vs 60s timeout 
BLOCKED before), and the full metacache test package (93 tests) passes.
   
   ### Are you willing to submit PR?
   
   - [x] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to