Skip to content

Commit 502a6d4

Browse files
committed
fix(session): clear caches before shutdown
1 parent 657986f commit 502a6d4

3 files changed

Lines changed: 40 additions & 13 deletions

File tree

rust/ffi/session.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::ptr;
33
use std::sync::atomic::{AtomicU64, Ordering};
44
use std::sync::Arc;
55

6+
use lance::dataset::{DEFAULT_INDEX_CACHE_SIZE, DEFAULT_METADATA_CACHE_SIZE};
67
use lance::session::Session;
8+
use lance_core::cache::{CacheBackend, MokaCacheBackend};
79

810
use crate::error::{clear_last_error, set_last_error, ErrorCode};
911

@@ -62,24 +64,42 @@ fn create_session_inner(
6264
index_cache_size_bytes: u64,
6365
metadata_cache_size_bytes: u64,
6466
) -> FfiResult<SessionHandle> {
65-
let session = if index_cache_size_bytes == 0 && metadata_cache_size_bytes == 0 {
66-
Arc::new(Session::default())
67-
} else {
68-
Arc::new(Session::new(
69-
u64_to_usize(index_cache_size_bytes, "index_cache_size_bytes")?,
70-
u64_to_usize(metadata_cache_size_bytes, "metadata_cache_size_bytes")?,
71-
Default::default(),
72-
))
73-
};
74-
Ok(SessionHandle { session })
67+
let (index_cache_size_bytes, metadata_cache_size_bytes) =
68+
if index_cache_size_bytes == 0 && metadata_cache_size_bytes == 0 {
69+
(DEFAULT_INDEX_CACHE_SIZE, DEFAULT_METADATA_CACHE_SIZE)
70+
} else {
71+
(
72+
u64_to_usize(index_cache_size_bytes, "index_cache_size_bytes")?,
73+
u64_to_usize(metadata_cache_size_bytes, "metadata_cache_size_bytes")?,
74+
)
75+
};
76+
let index_cache: Arc<dyn CacheBackend> =
77+
Arc::new(MokaCacheBackend::with_capacity(index_cache_size_bytes));
78+
let session = Arc::new(Session::with_index_cache_backend(
79+
index_cache.clone(),
80+
metadata_cache_size_bytes,
81+
Default::default(),
82+
));
83+
Ok(SessionHandle {
84+
session,
85+
index_cache,
86+
})
87+
}
88+
89+
fn clear_session_caches(handle: &SessionHandle) {
90+
if let Some(runtime) = crate::runtime::initialized_runtime() {
91+
runtime.block_on(async {
92+
handle.index_cache.clear().await;
93+
handle.session.file_metadata_cache().clear().await;
94+
});
95+
}
7596
}
7697

7798
#[no_mangle]
7899
pub unsafe extern "C" fn lance_close_session(session: *mut c_void) {
79100
if !session.is_null() {
80-
unsafe {
81-
let _ = Box::from_raw(session as *mut SessionHandle);
82-
}
101+
let handle = unsafe { Box::from_raw(session as *mut SessionHandle) };
102+
clear_session_caches(&handle);
83103
}
84104
}
85105

rust/ffi/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use arrow::array::RecordBatch;
44
use arrow::datatypes::Schema;
55
use lance::session::Session;
66
use lance::Dataset;
7+
use lance_core::cache::CacheBackend;
78

89
use crate::datafusion_stream::DataFusionStream;
910
use crate::scanner::{LanceStream, LanceTakeStream};
@@ -14,6 +15,8 @@ pub(crate) type SchemaHandle = Arc<Schema>;
1415

1516
pub(crate) struct SessionHandle {
1617
pub(crate) session: Arc<Session>,
18+
// Lance does not expose clearing its index cache through Session.
19+
pub(crate) index_cache: Arc<dyn CacheBackend>,
1720
}
1821

1922
pub(crate) struct DatasetHandle {

rust/runtime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub fn runtime() -> Result<&'static Runtime, io::Error> {
1313
}
1414
}
1515

16+
pub fn initialized_runtime() -> Option<&'static Runtime> {
17+
RUNTIME.get()?.as_ref().ok()
18+
}
19+
1620
pub fn handle() -> Result<Handle, io::Error> {
1721
Ok(runtime()?.handle().clone())
1822
}

0 commit comments

Comments
 (0)