lance-io 12.0.0

I/O utilities for Lance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Tests for io_uring reader implementation.

use crate::object_store::ObjectStore;
use lance_core::Result;
use std::io::Write;
use std::time::Duration;
use tempfile::NamedTempFile;

macro_rules! skip_if_no_uring_workers {
    () => {
        if super::thread::URING_THREADS.threads.is_empty() {
            return Ok(());
        }
    };
}

/// Helper to create a temporary file with test data
fn create_test_file(size: usize) -> Result<(NamedTempFile, Vec<u8>)> {
    let mut file = NamedTempFile::new()?;
    let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
    file.write_all(&data)?;
    file.flush()?;
    Ok((file, data))
}

#[tokio::test]
async fn test_read_small_file() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, expected_data) = create_test_file(1024)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Read entire file
    let data = reader.get_all().await.unwrap();
    assert_eq!(data.as_ref(), expected_data.as_slice());

    Ok(())
}

#[tokio::test]
async fn test_read_range() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, expected_data) = create_test_file(4096)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Read a range in the middle
    let range = 1000..2000;
    let data = reader.get_range(range.clone()).await.unwrap();
    assert_eq!(data.as_ref(), &expected_data[range]);

    Ok(())
}

#[tokio::test]
async fn test_read_multiple_ranges() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, expected_data) = create_test_file(8192)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Read multiple ranges
    let ranges = vec![0..100, 500..600, 2000..3000];
    for range in ranges {
        let data = reader.get_range(range.clone()).await.unwrap();
        assert_eq!(data.as_ref(), &expected_data[range]);
    }

    Ok(())
}

#[tokio::test]
async fn test_file_size() -> Result<()> {
    skip_if_no_uring_workers!();
    let size = 5000;
    let (file, _) = create_test_file(size)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    assert_eq!(reader.size().await.unwrap(), size);

    Ok(())
}

#[tokio::test]
async fn test_concurrent_reads() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, expected_data) = create_test_file(16384)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;

    // Perform multiple concurrent reads
    let mut tasks = vec![];
    for i in 0..10 {
        let reader_clone = store.open(&path).await?;
        let expected = expected_data.clone();
        tasks.push(tokio::spawn(async move {
            let range = (i * 1000)..((i + 1) * 1000);
            let data = reader_clone.get_range(range.clone()).await.unwrap();
            assert_eq!(data.as_ref(), &expected[range]);
        }));
    }

    // Wait for all tasks
    for task in tasks {
        task.await.unwrap();
    }

    Ok(())
}

#[tokio::test]
async fn test_large_file_read() -> Result<()> {
    skip_if_no_uring_workers!();
    // Test with a larger file (1MB)
    let size = 1024 * 1024;
    let (file, expected_data) = create_test_file(size)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Read entire file
    let data = reader.get_all().await.unwrap();
    assert_eq!(data.len(), size);
    assert_eq!(data.as_ref(), expected_data.as_slice());

    Ok(())
}

#[tokio::test]
async fn test_read_edge_cases() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, expected_data) = create_test_file(4096)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Read from start
    let data = reader.get_range(0..100).await.unwrap();
    assert_eq!(data.as_ref(), &expected_data[0..100]);

    // Read to end
    let data = reader.get_range(4000..4096).await.unwrap();
    assert_eq!(data.as_ref(), &expected_data[4000..4096]);

    // Read single byte
    let data = reader.get_range(2000..2001).await.unwrap();
    assert_eq!(data.as_ref(), &expected_data[2000..2001]);

    Ok(())
}

#[tokio::test]
async fn test_file_not_found() -> Result<()> {
    skip_if_no_uring_workers!();
    let uri = "file+uring:///nonexistent/file.dat";
    let (store, path) = ObjectStore::from_uri(uri).await.unwrap();

    // Should fail to open non-existent file
    let result = store.open(&path).await;
    assert!(result.is_err());

    Ok(())
}

#[tokio::test]
async fn test_block_size_and_parallelism() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, _) = create_test_file(1024)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Check default values (or configured values)
    assert!(reader.block_size() > 0);
    assert!(reader.io_parallelism() > 0);

    Ok(())
}

#[tokio::test]
async fn test_path() -> Result<()> {
    skip_if_no_uring_workers!();
    let (file, _) = create_test_file(1024)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Verify path is preserved
    assert_eq!(reader.path(), &path);

    Ok(())
}

/// Test that reading past EOF returns an error.
///
/// This exercises the case where `known_size` passed to `open_with_size` is larger
/// than the actual file, causing io_uring to hit EOF before the full read completes.
#[tokio::test]
async fn test_short_read_get_all() -> Result<()> {
    skip_if_no_uring_workers!();
    let actual_size: usize = 8192;
    let (file, _expected_data) = create_test_file(actual_size)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;

    // Open with inflated known_size — the reader will think the file is 2x its real size
    let inflated_size = actual_size * 2;
    let reader = store.open_with_size(&path, inflated_size).await?;

    // get_all() will submit a read for inflated_size bytes from an actual_size file.
    // The kernel reads actual_size bytes then returns 0 (EOF) — this should be an error.
    let result = reader.get_all().await;
    assert!(result.is_err(), "reading past EOF should return an error");

    Ok(())
}

/// Test that a range read extending past EOF returns an error.
#[tokio::test]
async fn test_short_read_get_range_past_eof() -> Result<()> {
    skip_if_no_uring_workers!();
    let actual_size: usize = 8192;
    let (file, _expected_data) = create_test_file(actual_size)?;
    let file_path = file.path().to_str().unwrap();
    let uri = format!("file+uring://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Request a range that starts inside the file but extends past EOF.
    // File is 8192 bytes; reading 4096..16384 hits EOF — this should be an error.
    let range_start = 4096;
    let range_end = actual_size * 2; // 16384, well past EOF
    let result = reader.get_range(range_start..range_end).await;
    assert!(
        result.is_err(),
        "range extending past EOF should return an error"
    );

    Ok(())
}

/// Test that when push_to_sq fails (SQ full), the request's future returns
/// an error instead of hanging forever.
///
/// This directly tests the thread-path scenario: create an IoUring with
/// queue_depth=2, fill the SQ, then try to push a 3rd request. The 3rd
/// request's future should return an error within the timeout.
///
/// BUG: currently the failed push silently drops the request, so the
/// future hangs and the timeout fires.
#[tokio::test]
async fn test_retry_sq_full_thread() -> Result<()> {
    skip_if_no_uring_workers!();
    use super::future::UringReadFuture;
    use super::requests::{IoRequest, RequestState};
    use super::thread::push_to_sq;
    use bytes::BytesMut;
    use io_uring::IoUring;
    use std::collections::HashMap;
    use std::os::unix::io::AsRawFd;
    use std::sync::{Arc, Mutex};

    let (file, _) = create_test_file(4096)?;
    let fd = file.as_file().as_raw_fd();

    // Create a tiny ring with queue_depth=2
    let mut ring = IoUring::new(2).unwrap();
    let mut pending: HashMap<u64, Arc<IoRequest>> = HashMap::new();

    // Helper to create a request
    let make_request = || {
        Arc::new(IoRequest {
            fd,
            offset: 0,
            length: 4096,
            thread_id: std::thread::current().id(),
            state: Mutex::new(RequestState {
                completed: false,
                waker: None,
                err: None,
                buffer: BytesMut::zeroed(4096),
                bytes_read: 0,
            }),
        })
    };

    // Fill the SQ (capacity=2)
    let _r1 = make_request();
    let _r2 = make_request();
    push_to_sq(&mut ring, &mut pending, _r1).unwrap();
    push_to_sq(&mut ring, &mut pending, _r2).unwrap();

    // 3rd push should fail — SQ is full
    let r3 = make_request();
    let push_result = push_to_sq(&mut ring, &mut pending, r3.clone());
    assert!(push_result.is_err(), "3rd push should fail (SQ full)");

    // r3's future should return an error, not hang forever.
    // BUG: currently nobody sets completed=true or err on r3, so the future hangs.
    let future = UringReadFuture { request: r3 };
    let result = tokio::time::timeout(Duration::from_secs(2), future).await;
    assert!(
        result.is_ok(),
        "future timed out — request was dropped without error on SQ-full push failure"
    );

    Ok(())
}

/// Test that when push_to_sq fails (SQ full) on the current-thread path,
/// the request's future returns an error instead of hanging forever.
///
/// Uses UringCurrentThreadFuture (which will be a no-op poller since the
/// thread-local URING has no knowledge of this request) after push_to_sq
/// has already completed the request with an error.
#[tokio::test(flavor = "current_thread")]
async fn test_retry_sq_full_current_thread() -> Result<()> {
    skip_if_no_uring_workers!();
    use super::current_thread_future::UringCurrentThreadFuture;
    use super::requests::{IoRequest, RequestState};
    use super::thread::push_to_sq;
    use bytes::BytesMut;
    use io_uring::IoUring;
    use std::collections::HashMap;
    use std::os::unix::io::AsRawFd;
    use std::sync::{Arc, Mutex};

    let (file, _) = create_test_file(4096)?;
    let fd = file.as_file().as_raw_fd();

    // Create a tiny ring with queue_depth=2
    let mut ring = IoUring::new(2).unwrap();
    let mut pending: HashMap<u64, Arc<IoRequest>> = HashMap::new();

    let make_request = || {
        Arc::new(IoRequest {
            fd,
            offset: 0,
            length: 4096,
            thread_id: std::thread::current().id(),
            state: Mutex::new(RequestState {
                completed: false,
                waker: None,
                err: None,
                buffer: BytesMut::zeroed(4096),
                bytes_read: 0,
            }),
        })
    };

    // Fill the SQ (capacity=2)
    push_to_sq(&mut ring, &mut pending, make_request()).unwrap();
    push_to_sq(&mut ring, &mut pending, make_request()).unwrap();

    // 3rd push should fail — SQ is full
    let r3 = make_request();
    let push_result = push_to_sq(&mut ring, &mut pending, r3.clone());
    assert!(push_result.is_err(), "3rd push should fail (SQ full)");

    // r3's future should return an error, not hang forever.
    let future = UringCurrentThreadFuture::new(r3);
    let result = tokio::time::timeout(Duration::from_secs(2), future).await;
    assert!(
        result.is_ok(),
        "future timed out — request was dropped without error on SQ-full push failure"
    );

    Ok(())
}

#[tokio::test]
async fn test_uring_not_enabled_with_file_scheme() -> Result<()> {
    // Verify that files opened with file:// don't use uring
    let (file, expected_data) = create_test_file(1024)?;
    let file_path = file.path().to_str().unwrap();
    // Use regular file:// scheme, should NOT use uring
    let uri = format!("file://{}", file_path);

    let (store, path) = ObjectStore::from_uri(&uri).await?;
    let reader = store.open(&path).await?;

    // Should still be able to read, just won't use uring
    let data = reader.get_all().await.unwrap();
    assert_eq!(data.as_ref(), expected_data.as_slice());

    Ok(())
}