victor created YARN-11994:
-----------------------------

             Summary: EntityGroupFSTimelineStore: LRU cache eviction holds 
global lock causing all HTTP threads blocked and service hang
                 Key: YARN-11994
                 URL: https://issues.apache.org/jira/browse/YARN-11994
             Project: Hadoop YARN
          Issue Type: Bug
          Components: timelineservice
    Affects Versions: 3.4.1
            Reporter: victor
         Attachments: YARN-11994.patch

## EntityGroupFSTimelineStore: LRU cache eviction holds global lock causing all 
HTTP threads blocked and service hang

**Component:** yarn-timeline-service
**Affects Version/s:** 3.3.x, 3.4.x (confirmed on 3.4.1-amzn-1)
**Fix Version/s:** None (unfixed as of 2026-09-24)
**Priority:** Critical
**Type:** Bug

---

### Description

`EntityGroupFSTimelineStore` (Timeline Service v1.5) uses a 
`Collections.synchronizedMap(LinkedHashMap)` as its LRU entity group cache. 
When the cache reaches capacity and a `put()` triggers `removeEldestEntry()`, 
the eviction callback calls `EntityCacheItem.forceRelease()` — a `synchronized` 
method — **while still holding the global `SynchronizedMap` monitor**. If 
another thread is executing `EntityCacheItem.refreshCache()` (also 
`synchronized`, performing HDFS reads + JSON parsing + LevelDB writes that can 
run for minutes), the eviction thread blocks on the `EntityCacheItem` lock, 
holding the global map lock indefinitely. This starves all subsequent HTTP 
request threads that need to access the cache.

**Observed impact in production:**
- 202 threads BLOCKED for 30+ minutes (stable across 7 jstack samples)
- ~20,000 CLOSE_WAIT TCP connections accumulated
- File descriptor exhaustion (32,768 limit) → complete service failure

### Environment

| Item | Value |
|---|---|
| Hadoop version | 3.4.1-amzn-1 (Amazon EMR) |
| Timeline Service version | v1.5 (`EntityGroupFSTimelineStore` + 
`LevelDBCacheTimelineStore`) |
| Deployment | Dozens of YARN clusters sharing a single Timeline Server |
| `app-cache-size` | 100 (default: 10) |
| FD limit (`ulimit -n`) | 32,768 |
| JVM | -Xms2g -Xmx20g, ParallelGC, MaxMetaspaceSize unset |

---

### Root Cause Analysis

Three design-level issues combine to produce the hang.

#### Issue 1: Global lock held during potentially-blocking eviction call

**File:** `EntityGroupFSTimelineStore.java` — `removeEldestEntry()`

```java
cachedLogs = Collections.synchronizedMap(
  new LinkedHashMap<TimelineEntityGroupId, EntityCacheItem>(
      appCacheMaxSize + 1, 0.75f, true) {
    @Override
    protected boolean removeEldestEntry(
        Map.Entry<TimelineEntityGroupId, EntityCacheItem> eldest) {
      if (super.size() > appCacheMaxSize) {
        EntityCacheItem cacheItem = eldest.getValue();
        cacheItem.forceRelease();  // synchronized — may block!
        // ↑ Called inside SynchronizedMap.put(), which holds the global map 
monitor.
        return true;
      }
      return false;
    }
  });
```

#### Issue 2: `refreshCache()` holds its monitor lock during long I/O

**File:** `EntityCacheItem.java:86-126`

`refreshCache()` is `synchronized` and performs HDFS reads + JSON parsing + 
LevelDB writes inside the lock. For large files (~276MB observed), holds the 
lock for 30+ minutes.

#### Issue 3: Hardcoded 10-second refresh interval

**File:** `EntityCacheItem.java:150-152`

```java
private boolean needRefresh() {
  return (Time.monotonicNow() - lastRefresh > 10000);  // hardcoded, not 
configurable
}
```

### Thread Blocking Chain

```
Thread qtp-240 (RUNNABLE):
  → refreshCache() holds EntityCacheItem monitor
  → loadDetailLog() → doParse() → while(iter.hasNext()) HDFS+LevelDB I/O
  → Running for 30+ minutes

Thread qtp-71 (BLOCKED):
  → getCachedStore() → synchronized(cachedLogs) holds global map monitor
  → put() triggers removeEldestEntry()
  → forceRelease() waits for EntityCacheItem monitor (held by qtp-240)

182 HTTP request threads (BLOCKED):
  → All waiting for global map monitor (held by qtp-71)
```

Clients time out → FIN sent → server threads still blocked → CLOSE_WAIT 
accumulates → FD exhaustion → complete service failure.

### Related Issues and Fix History

| JIRA | Date | Description | Status |
|---|---|---|---|
| YARN-4265 | 2016-01 | Introduced `EntityGroupFSTimelineStore` with LRU 
`LinkedHashMap` + `removeEldestEntry` | Merged |
| **YARN-4987** | 2016-05-27 | "Read cache concurrency issue between read and 
evict" — introduced `refCount`/`tryRelease()`/`incrRefs()`/`activeStores` | 
Merged then **reverted** |
| **YARN-5432** | 2016-07-28 | "Lock already held by another process while 
LevelDB cache store creation for dag" — **fully reverted YARN-4987** because 
`tryRelease()` caused LevelDB file path conflicts | Merged — **reintroduced the 
bug** |
| YARN-6228 | 2017 | Changed store creation to use 
`DB_COUNTER.getAndIncrement()`, making each LevelDB instance use a unique path 
| Merged — **inadvertently fixed YARN-5432's root cause** |

**Key observation:** YARN-6228's unique DB path naming eliminates the specific 
LevelDB lock conflict that forced YARN-5432 to revert YARN-4987. However, the 
eviction concurrency fix was never re-introduced. The current code (3.3.x / 
3.4.x) remains in the YARN-4987-reverted state with direct `forceRelease()` in 
`removeEldestEntry()`.

---

### Steps to Reproduce

1. Configure a small cache to trigger frequent eviction: set 
`yarn.timeline-service.entity-group-fs-store.app-cache-size` to 5
2. Create large entity log data on HDFS (~200MB, ~200,000 entities)
3. Send concurrent requests exceeding cache size (20 concurrent requests for 10 
entity groups)
4. Observe BLOCKED threads and CLOSE_WAIT connection accumulation

---

### Proposed Fix

Three independent patches. **Recommended minimum: Patch 1 + Patch 3.** All 
three may be applied together for maximum effect.

| # | Patch | Addresses | Risk | Priority |
|---|---|---|---|---|
| 1 | Asynchronous cache eviction | Issue 1 (global lock) + YARN-4987 
read/evict safety | Low | **P0 — core fix** |
| 2 | ReadWriteLock in `EntityCacheItem` | Issue 2 (long lock hold) | Medium | 
P1 — optimization |
| 3 | Configurable refresh interval | Issue 3 (hardcoded 10s) | Low | **P0 — 
simplest fix** |

#### Patch 1: Asynchronous cache eviction (`EntityGroupFSTimelineStore.java`)

Replace the synchronous `forceRelease()` call inside `removeEldestEntry()` with 
an async queue drain. This ensures `put()` returns in O(1) and never holds the 
global map lock waiting for an `EntityCacheItem` monitor.

**YARN-4987 safety:** When the async drain thread calls `forceRelease()`, if a 
reader is still in `refreshCache()`, the drain thread waits — but this wait 
happens outside the global map lock.

**YARN-5432 safety:** YARN-6228's unique DB path naming prevents "Lock already 
held by another process" conflicts even if the same groupId is evicted and 
re-requested.

#### Patch 2: ReadWriteLock to reduce lock hold time (`EntityCacheItem.java`)

Move HDFS/LevelDB I/O outside the `EntityCacheItem` monitor lock using 
`ReentrantReadWriteLock`:
- `refreshCache()` holds the **read lock** during I/O
- `forceRelease()` holds the **write lock** (waits for all I/O to finish)
- `synchronized(this)` is used only for short state reads/writes

**Lock ordering (consistent, no deadlock):** `ioLock` → `synchronized(this)`. 
Never reversed.

#### Patch 3: Configurable cache refresh interval (`YarnConfiguration.java` + 
`EntityCacheItem.java`)

Add configuration key 
`yarn.timeline-service.entity-group-fs-store.cache-refresh-interval-ms` 
(default 10000). Replace the hardcoded 10-second value in `needRefresh()` with 
the configurable field. Recommended value for shared Timeline Server: `60000` 
(1 minute).

---

### Affected Source Files

| File | Description |
|---|---|
| `EntityGroupFSTimelineStore.java` | `cachedLogs` field, 
`removeEldestEntry()`, `getCachedStore()`, `serviceStart()` |
| `EntityCacheItem.java` | `refreshCache()`, `forceRelease()`, `needRefresh()` |
| `YarnConfiguration.java` | New config constant |



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

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

Reply via email to