[core] Reduce shared disk cache lock contention (#1186) · apache/paimon-rust@8d08e48 · GitHub
Skip to content

Commit 8d08e48

Browse files
[core] Reduce shared disk cache lock contention (#1186)
1 parent e602ec5 commit 8d08e48

5 files changed

Lines changed: 554 additions & 59 deletions

File tree

‎crates/paimon/src/io/cache/disk.rs‎

Lines changed: 37 additions & 7 deletions

‎crates/paimon/src/io/cache/mod.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub(crate) struct LocalCache {
4848
block_size: u64,
4949
whitelist: HashSet<FileType>,
5050
file_size_capacity: usize,
51+
// Bound queued and active background fills.
52+
fill_slots: Arc<tokio::sync::Semaphore>,
5153
}
5254

5355
#[derive(Debug)]
@@ -76,6 +78,7 @@ impl LocalCache {
7678
block_size,
7779
whitelist: FileType::parse_whitelist(whitelist),
7880
file_size_capacity: DEFAULT_FILE_SIZE_CAPACITY,
81+
fill_slots: Arc::new(tokio::sync::Semaphore::new(8)),
7982
})
8083
}
8184

@@ -124,13 +127,30 @@ impl LocalCache {
124127
block_size: config.block_size,
125128
whitelist: config.whitelist,
126129
file_size_capacity,
130+
fill_slots: Arc::new(tokio::sync::Semaphore::new(8)),
127131
})
128132
}
129133

130134
fn block_size(&self) -> u64 {
131135
self.block_size
132136
}
133137

138+
fn background_fill_slot(&self) -> Option<tokio::sync::OwnedSemaphorePermit> {
139+
// Current-thread runtimes stop driving spawned fills when block_on returns.
140+
// Keep those fills synchronous so their block locks cannot outlive the read.
141+
if !matches!(
142+
tokio::runtime::Handle::try_current().map(|handle| handle.runtime_flavor()),
143+
Ok(tokio::runtime::RuntimeFlavor::MultiThread)
144+
) {
145+
return None;
146+
}
147+
// Limit background payloads to 8 MiB per cache; otherwise fill synchronously.
148+
if !matches!(self.backend, CacheBackend::SharedDisk(_)) || self.block_size > 1024 * 1024 {
149+
return None;
150+
}
151+
self.fill_slots.clone().try_acquire_owned().ok()
152+
}
153+
134154
fn block_key(&self, path: &str, block_index: u64) -> BlockKey {
135155
BlockKey::with_namespace(&self.namespace, path, self.block_size, block_index)
136156
}

‎crates/paimon/src/io/cache/reader.rs‎

Lines changed: 115 additions & 2 deletions

0 commit comments

Comments
 (0)