Skip to content

Commit 769938b

Browse files
authored
fix(jni): cache each open file's segments and merge reads across small gaps only (#66)
## Summary Closes #65. Spark reads of list-layout files read several times the bytes they need, because the JNI data source opens files without a segment cache and a list column's element segments are requested once per row range of a scan. On a 47 MB list-layout file of nested event data, a 29-column read now reads 13.7 MB instead of 39.5 MB, and 15.7 MB instead of 68.8 MB when Spark splits the file in four. Before this change, the same read of the file written without the list layout read 30.0 MB, so the list layout now pays off instead of costing more. ## Changes - Each file the JNI data source opens gets its own segment cache, capped at 64 MiB and dropped with the open file. Both `NativeDataSource.open` and `openFiles` use it. - `JavaReadable` merges reads across gaps of at most 64 KiB instead of 1 MiB. With a cache, a merged read that spans a segment the cache already holds reads it again; at 1 MiB that left list-layout reads at 1.3x to 1.8x the bytes they need, at 64 KiB 1.0x to 1.2x. ## Testing - `writer::tests::list_layout_scan_reads_each_segment_once`: writes a list-of-struct column with the list layout, scans it as four row ranges through the data source, and checks that no segment is read twice with the JNI open options. It also checks that without them some segments are, so the test keeps reproducing the problem. - `cargo test -p vortex-jni`, `cargo clippy -p vortex-jni --all-targets`. - Spark, local mode, the connector on develop with this native library, same file, bytes the JVM read for the 29-column read: list layout 21.5 to 25.0 MB before, 14.5 MB after; split in four, 77 to 92 MB before, 48.4 MB after. Without the list layout: 30.6 MB before, 29.5 MB after. Row counts match. ## Not fixed here Spark reads each split of a file in its own task, so splits can't share a cache, and an elements segment that spans the whole file is read once per split. Reading a list-layout file in one split avoids that (`vortex.splitFiles=false`); writing list elements in chunks that line up with the outer row chunks would fix it for split reads too. Signed-off-by: gitRasheed <[email protected]>
1 parent abb4830 commit 769938b

3 files changed

Lines changed: 155 additions & 5 deletions

File tree

‎vortex-jni/src/data_source.rs‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ use url::Url;
2424
use vortex::error::VortexResult;
2525
use vortex::error::vortex_err;
2626
use vortex::expr::stats::Precision;
27+
use vortex::file::VortexOpenOptions;
2728
use vortex::file::multi::MultiFileDataSource;
2829
use vortex::file::multi::parse_uri_or_path;
2930
use vortex::io::filesystem::FileSystemRef;
3031
use vortex::io::runtime::BlockingRuntime;
3132
use vortex::io::session::RuntimeSessionExt;
33+
use vortex::layout::segments::MokaSegmentCache;
34+
use vortex::layout::segments::SegmentEviction;
3235
use vortex::scan::DataSourceRef;
3336
use vortex_arrow::ArrowSessionExt;
3437

@@ -39,6 +42,25 @@ use crate::io::JavaFileSystem;
3942
use crate::object_store::object_store_fs;
4043
use crate::session::session_ref;
4144

45+
/// The most segment bytes each open file keeps while it is read.
46+
///
47+
/// A scan reads a file as several row ranges, and a segment that spans ranges is requested once per
48+
/// range. A list layout column's elements usually do: its elements are chunked independently of its
49+
/// rows, and its offsets must be read before its elements can be requested, so the requests miss the
50+
/// in-flight deduplication flat columns get. Without a cache each request reads storage again.
51+
const FILE_SEGMENT_CACHE_BYTES: u64 = 64 << 20;
52+
53+
/// The options each file the data source opens gets: its own segment cache, which lives as long as
54+
/// the open file.
55+
pub(crate) fn file_open_options(options: VortexOpenOptions) -> VortexOpenOptions {
56+
let cache = MokaSegmentCache::new(FILE_SEGMENT_CACHE_BYTES, SegmentEviction::Lru);
57+
options.with_segment_cache(Arc::new(cache.for_file("file")))
58+
}
59+
60+
fn with_file_segment_cache(builder: MultiFileDataSource) -> MultiFileDataSource {
61+
builder.with_open_options(file_open_options)
62+
}
63+
4264
/// Wraps an `Arc<dyn DataSource>` behind a single pointer.
4365
pub(crate) struct NativeDataSource {
4466
inner: DataSourceRef,
@@ -93,7 +115,7 @@ pub extern "system" fn Java_dev_vortex_jni_NativeDataSource_open(
93115
.collect::<VortexResult<_>>()?;
94116

95117
// Glob by the path the resolver reports — only it knows how deep each store is mounted.
96-
let mut builder = MultiFileDataSource::new(session.clone());
118+
let mut builder = with_file_segment_cache(MultiFileDataSource::new(session.clone()));
97119
for glob_url in &glob_urls {
98120
let (fs, glob) = object_store_fs(glob_url, &properties, session.handle())?;
99121
builder = builder.with_glob(glob, Some(fs));
@@ -165,7 +187,7 @@ pub extern "system" fn Java_dev_vortex_jni_NativeDataSource_openFiles(
165187
}
166188

167189
let fs: FileSystemRef = Arc::new(fs);
168-
let mut builder = MultiFileDataSource::new(session.clone());
190+
let mut builder = with_file_segment_cache(MultiFileDataSource::new(session.clone()));
169191
for path in ordered_paths {
170192
builder = builder.with_glob(path, Some(Arc::clone(&fs)));
171193
}

‎vortex-jni/src/io/read_at.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ impl JavaReadable {
9191

9292
impl VortexReadAt for JavaReadable {
9393
fn coalesce_config(&self) -> Option<CoalesceConfig> {
94-
// Upcalls have a fixed JNI overhead and the backing storage is usually
95-
// remote, so favor fewer, larger reads.
96-
Some(CoalesceConfig::object_storage())
94+
// Upcalls have a fixed JNI overhead and the backing storage is usually remote, so merge
95+
// nearby reads, but only across small gaps: a merged read that spans a segment the file's
96+
// segment cache already holds reads it again. With a 1 MiB gap, list layout scans read
97+
// 1.3x to 1.8x the bytes they need even with the cache; with 64 KiB, 1.0x to 1.2x.
98+
Some(CoalesceConfig::new(64 << 10, 16 << 20))
9799
}
98100

99101
fn concurrency(&self) -> usize {

‎vortex-jni/src/writer.rs‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,9 +703,23 @@ mod tests {
703703
use vortex::session::SessionVar;
704704
use vortex::session::registry::Id;
705705

706+
use std::sync::Arc;
707+
708+
use futures::StreamExt;
709+
use futures::future::BoxFuture;
710+
use parking_lot::Mutex;
711+
use vortex::array::buffer::BufferHandle;
712+
use vortex::buffer::Alignment;
713+
use vortex::buffer::ByteBuffer;
714+
use vortex::file::VortexOpenOptions;
715+
use vortex::io::VortexReadAt;
716+
use vortex::scan::ScanRequest;
717+
use vortex_arrow::ArrowSessionExt;
718+
706719
use super::LIST_LAYOUT_EDITION;
707720
use super::write_session;
708721
use crate::RUNTIME;
722+
use crate::data_source::file_open_options;
709723
use crate::session::new_session;
710724

711725
#[derive(Clone, Debug)]
@@ -824,4 +838,116 @@ mod tests {
824838
assert!(leaked.is_empty(), "unstable ids in the file: {leaked:?}");
825839
Ok(())
826840
}
841+
842+
/// Records the offset of every read, and doesn't merge reads, so each read is one segment.
843+
#[derive(Clone)]
844+
struct RecordingReads {
845+
bytes: ByteBuffer,
846+
offsets: Arc<Mutex<Vec<u64>>>,
847+
}
848+
849+
impl VortexReadAt for RecordingReads {
850+
fn concurrency(&self) -> usize {
851+
8
852+
}
853+
854+
fn size(&self) -> BoxFuture<'static, VortexResult<u64>> {
855+
let len = self.bytes.len() as u64;
856+
Box::pin(async move { Ok(len) })
857+
}
858+
859+
fn read_at(
860+
&self,
861+
offset: u64,
862+
length: usize,
863+
alignment: Alignment,
864+
) -> BoxFuture<'static, VortexResult<BufferHandle>> {
865+
self.offsets.lock().push(offset);
866+
self.bytes.read_at(offset, length, alignment)
867+
}
868+
}
869+
870+
/// Reads of a segment a scan of `bytes` in `splits` row ranges makes beyond the first.
871+
fn segments_read_twice(
872+
bytes: ByteBuffer,
873+
splits: u64,
874+
options: fn(VortexOpenOptions) -> VortexOpenOptions,
875+
) -> VortexResult<usize> {
876+
let session = new_session();
877+
let offsets = Arc::new(Mutex::new(Vec::new()));
878+
let source = RecordingReads {
879+
bytes,
880+
offsets: Arc::clone(&offsets),
881+
};
882+
RUNTIME.block_on(async {
883+
let file = options(session.open_options())
884+
.open(Arc::new(source))
885+
.await?;
886+
offsets.lock().clear();
887+
let rows = file.row_count();
888+
let data_source = file.data_source()?;
889+
for split in 0..splits {
890+
let request = ScanRequest {
891+
row_range: Some(rows * split / splits..rows * (split + 1) / splits),
892+
..Default::default()
893+
};
894+
let mut partitions = data_source.scan(request).await?.partitions();
895+
while let Some(partition) = partitions.next().await {
896+
let mut chunks = partition?.execute()?;
897+
while let Some(chunk) = chunks.next().await {
898+
let mut ctx = session.create_execution_ctx();
899+
session.arrow().execute_arrow(chunk?, None, &mut ctx)?;
900+
}
901+
}
902+
}
903+
let mut offsets = offsets.lock().clone();
904+
offsets.sort_unstable();
905+
let total = offsets.len();
906+
offsets.dedup();
907+
Ok(total - offsets.len())
908+
})
909+
}
910+
911+
/// A list column's elements are chunked apart from its rows, so a scan's row ranges share
912+
/// element segments, and each range asks for them after it has read its own offsets.
913+
#[test]
914+
fn list_layout_scan_reads_each_segment_once() -> VortexResult<()> {
915+
let session = write_session(&new_session(), true)?;
916+
let rows = 65_536u32;
917+
let elements = StructArray::from_fields(&[
918+
(
919+
"a",
920+
Buffer::from_iter(0i64..i64::from(rows) * 4).into_array(),
921+
),
922+
(
923+
"b",
924+
Buffer::from_iter((0i64..i64::from(rows) * 4).map(|i| i % 7)).into_array(),
925+
),
926+
])?;
927+
let list = ListArray::try_new(
928+
elements.into_array(),
929+
Buffer::from_iter((0..=rows).map(|i| i * 4)).into_array(),
930+
Validity::NonNullable,
931+
&mut session.create_execution_ctx(),
932+
)?;
933+
let root = StructArray::from_fields(&[
934+
("id", Buffer::from_iter(0i64..i64::from(rows)).into_array()),
935+
("arr", list.into_array()),
936+
])?
937+
.into_array();
938+
let mut bytes = ByteBufferMut::empty();
939+
RUNTIME.block_on(
940+
session
941+
.write_options()
942+
.write(&mut bytes, root.to_array_stream()),
943+
)?;
944+
let bytes = bytes.freeze();
945+
946+
assert!(
947+
segments_read_twice(bytes.clone(), 4, |options| options)? > 0,
948+
"the test no longer reproduces re-reads"
949+
);
950+
assert_eq!(segments_read_twice(bytes, 4, file_open_options)?, 0);
951+
Ok(())
952+
}
827953
}

0 commit comments

Comments
 (0)