1use bytes::Bytes;
5use futures::channel::oneshot;
6use futures::{FutureExt, TryFutureExt};
7use object_store::path::Path;
8use snafu::location;
9use std::collections::BinaryHeap;
10use std::fmt::Debug;
11use std::future::Future;
12use std::ops::Range;
13use std::sync::atomic::{AtomicU64, Ordering};
14use std::sync::{Arc, Mutex};
15use std::time::Instant;
16use tokio::sync::{Notify, Semaphore, SemaphorePermit};
17
18use lance_core::{Error, Result};
19
20use crate::object_store::ObjectStore;
21use crate::traits::Reader;
22
23const BACKPRESSURE_MIN: u64 = 5;
25const BACKPRESSURE_DEBOUNCE: u64 = 60;
27
28static IOPS_COUNTER: AtomicU64 = AtomicU64::new(0);
30static BYTES_READ_COUNTER: AtomicU64 = AtomicU64::new(0);
32
33pub fn iops_counter() -> u64 {
34 IOPS_COUNTER.load(Ordering::Acquire)
35}
36
37pub fn bytes_read_counter() -> u64 {
38 BYTES_READ_COUNTER.load(Ordering::Acquire)
39}
40
41struct IopsQuota {
68 iops_avail: Option<Semaphore>,
70}
71
72struct IopsReservation<'a> {
77 value: Option<SemaphorePermit<'a>>,
78}
79
80impl IopsReservation<'_> {
81 fn forget(&mut self) {
83 if let Some(value) = self.value.take() {
84 value.forget();
85 }
86 }
87}
88
89impl IopsQuota {
90 fn new() -> Self {
95 let initial_capacity = std::env::var("LANCE_PROCESS_IO_THREADS_LIMIT")
96 .map(|s| {
97 let limit = s
98 .parse::<i32>()
99 .expect("LANCE_PROCESS_IO_THREADS_LIMIT must be a positive integer");
100 if limit <= 0 {
101 panic!("LANCE_PROCESS_IO_THREADS_LIMIT must be a positive integer. To disable the limit, unset the environment variable");
102 }
103 limit
104 })
105 .unwrap_or(-1);
107 let iops_avail = if initial_capacity < 0 {
108 None
109 } else {
110 Some(Semaphore::new(initial_capacity as usize))
111 };
112 Self { iops_avail }
113 }
114
115 fn release(&self) {
117 if let Some(iops_avail) = self.iops_avail.as_ref() {
118 iops_avail.add_permits(1);
119 }
120 }
121
122 async fn acquire(&self) -> IopsReservation {
124 if let Some(iops_avail) = self.iops_avail.as_ref() {
125 IopsReservation {
126 value: Some(iops_avail.acquire().await.unwrap()),
127 }
128 } else {
129 IopsReservation { value: None }
130 }
131 }
132}
133
134lazy_static::lazy_static! {
135 static ref IOPS_QUOTA: IopsQuota = IopsQuota::new();
136}
137
138struct PrioritiesInFlight {
147 in_flight: Vec<u128>,
148}
149
150impl PrioritiesInFlight {
151 fn new(capacity: u32) -> Self {
152 Self {
153 in_flight: Vec::with_capacity(capacity as usize * 2),
154 }
155 }
156
157 fn min_in_flight(&self) -> u128 {
158 self.in_flight.first().copied().unwrap_or(u128::MAX)
159 }
160
161 fn push(&mut self, prio: u128) {
162 let pos = match self.in_flight.binary_search(&prio) {
163 Ok(pos) => pos,
164 Err(pos) => pos,
165 };
166 self.in_flight.insert(pos, prio);
167 }
168
169 fn remove(&mut self, prio: u128) {
170 if let Ok(pos) = self.in_flight.binary_search(&prio) {
171 self.in_flight.remove(pos);
172 } else {
173 unreachable!();
174 }
175 }
176}
177
178struct IoQueueState {
179 iops_avail: u32,
181 bytes_avail: i64,
185 pending_requests: BinaryHeap<IoTask>,
187 priorities_in_flight: PrioritiesInFlight,
189 done_scheduling: bool,
192 start: Instant,
194 last_warn: AtomicU64,
196}
197
198impl IoQueueState {
199 fn new(io_capacity: u32, io_buffer_size: u64) -> Self {
200 Self {
201 iops_avail: io_capacity,
202 bytes_avail: io_buffer_size as i64,
203 pending_requests: BinaryHeap::new(),
204 priorities_in_flight: PrioritiesInFlight::new(io_capacity),
205 done_scheduling: false,
206 start: Instant::now(),
207 last_warn: AtomicU64::from(0),
208 }
209 }
210
211 fn finished(&self) -> bool {
212 self.done_scheduling && self.pending_requests.is_empty()
213 }
214
215 fn warn_if_needed(&self) {
216 let seconds_elapsed = self.start.elapsed().as_secs();
217 let last_warn = self.last_warn.load(Ordering::Acquire);
218 let since_last_warn = seconds_elapsed - last_warn;
219 if (last_warn == 0
220 && seconds_elapsed > BACKPRESSURE_MIN
221 && seconds_elapsed < BACKPRESSURE_DEBOUNCE)
222 || since_last_warn > BACKPRESSURE_DEBOUNCE
223 {
224 tracing::event!(tracing::Level::WARN, "Backpressure throttle exceeded");
225 log::debug!("Backpressure throttle is full, I/O will pause until buffer is drained. Max I/O bandwidth will not be achieved because CPU is falling behind");
226 self.last_warn
227 .store(seconds_elapsed.max(1), Ordering::Release);
228 }
229 }
230
231 fn can_deliver(&self, task: &IoTask) -> bool {
232 if self.iops_avail == 0 {
233 false
234 } else if task.priority <= self.priorities_in_flight.min_in_flight() {
235 true
236 } else if task.num_bytes() as i64 > self.bytes_avail {
237 self.warn_if_needed();
238 false
239 } else {
240 true
241 }
242 }
243
244 fn next_task(&mut self) -> Option<IoTask> {
245 let task = self.pending_requests.peek()?;
246 if self.can_deliver(task) {
247 self.priorities_in_flight.push(task.priority);
248 self.iops_avail -= 1;
249 self.bytes_avail -= task.num_bytes() as i64;
250 if self.bytes_avail < 0 {
251 log::debug!(
253 "Backpressure throttle temporarily exceeded by {} bytes due to priority I/O",
254 -self.bytes_avail
255 );
256 }
257 Some(self.pending_requests.pop().unwrap())
258 } else {
259 None
260 }
261 }
262}
263
264struct IoQueue {
269 state: Mutex<IoQueueState>,
271 notify: Notify,
273}
274
275impl IoQueue {
276 fn new(io_capacity: u32, io_buffer_size: u64) -> Self {
277 Self {
278 state: Mutex::new(IoQueueState::new(io_capacity, io_buffer_size)),
279 notify: Notify::new(),
280 }
281 }
282
283 fn push(&self, task: IoTask) {
284 log::trace!(
285 "Inserting I/O request for {} bytes with priority ({},{}) into I/O queue",
286 task.num_bytes(),
287 task.priority >> 64,
288 task.priority & 0xFFFFFFFFFFFFFFFF
289 );
290 let mut state = self.state.lock().unwrap();
291 state.pending_requests.push(task);
292 drop(state);
293
294 self.notify.notify_one();
295 }
296
297 async fn pop(&self) -> Option<IoTask> {
298 loop {
299 {
300 let mut iop_res = IOPS_QUOTA.acquire().await;
305 let mut state = self.state.lock().unwrap();
307 if let Some(task) = state.next_task() {
308 iop_res.forget();
311 return Some(task);
312 }
313
314 if state.finished() {
315 return None;
316 }
317 }
318
319 self.notify.notified().await;
320 }
321 }
322
323 fn on_iop_complete(&self) {
324 let mut state = self.state.lock().unwrap();
325 state.iops_avail += 1;
326 drop(state);
327
328 self.notify.notify_one();
329 }
330
331 fn on_bytes_consumed(&self, bytes: u64, priority: u128, num_reqs: usize) {
332 let mut state = self.state.lock().unwrap();
333 state.bytes_avail += bytes as i64;
334 for _ in 0..num_reqs {
335 state.priorities_in_flight.remove(priority);
336 }
337 drop(state);
338
339 self.notify.notify_one();
340 }
341
342 fn close(&self) {
343 let mut state = self.state.lock().unwrap();
344 state.done_scheduling = true;
345 drop(state);
346
347 self.notify.notify_one();
348 }
349}
350
351struct MutableBatch<F: FnOnce(Response) + Send> {
356 when_done: Option<F>,
357 data_buffers: Vec<Bytes>,
358 num_bytes: u64,
359 priority: u128,
360 num_reqs: usize,
361 err: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
362}
363
364impl<F: FnOnce(Response) + Send> MutableBatch<F> {
365 fn new(when_done: F, num_data_buffers: u32, priority: u128, num_reqs: usize) -> Self {
366 Self {
367 when_done: Some(when_done),
368 data_buffers: vec![Bytes::default(); num_data_buffers as usize],
369 num_bytes: 0,
370 priority,
371 num_reqs,
372 err: None,
373 }
374 }
375}
376
377impl<F: FnOnce(Response) + Send> Drop for MutableBatch<F> {
382 fn drop(&mut self) {
383 let result = if self.err.is_some() {
385 Err(Error::Wrapped {
386 error: self.err.take().unwrap(),
387 location: location!(),
388 })
389 } else {
390 let mut data = Vec::new();
391 std::mem::swap(&mut data, &mut self.data_buffers);
392 Ok(data)
393 };
394 let response = Response {
397 data: result,
398 num_bytes: self.num_bytes,
399 priority: self.priority,
400 num_reqs: self.num_reqs,
401 };
402 (self.when_done.take().unwrap())(response);
403 }
404}
405
406struct DataChunk {
407 task_idx: usize,
408 num_bytes: u64,
409 data: Result<Bytes>,
410}
411
412trait DataSink: Send {
413 fn deliver_data(&mut self, data: DataChunk);
414}
415
416impl<F: FnOnce(Response) + Send> DataSink for MutableBatch<F> {
417 fn deliver_data(&mut self, data: DataChunk) {
419 self.num_bytes += data.num_bytes;
420 match data.data {
421 Ok(data_bytes) => {
422 self.data_buffers[data.task_idx] = data_bytes;
423 }
424 Err(err) => {
425 self.err.get_or_insert(Box::new(err));
427 }
428 }
429 }
430}
431
432struct IoTask {
433 reader: Arc<dyn Reader>,
434 to_read: Range<u64>,
435 when_done: Box<dyn FnOnce(Result<Bytes>) + Send>,
436 priority: u128,
437}
438
439impl Eq for IoTask {}
440
441impl PartialEq for IoTask {
442 fn eq(&self, other: &Self) -> bool {
443 self.priority == other.priority
444 }
445}
446
447impl PartialOrd for IoTask {
448 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
449 Some(self.cmp(other))
450 }
451}
452
453impl Ord for IoTask {
454 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
455 other.priority.cmp(&self.priority)
457 }
458}
459
460impl IoTask {
461 fn num_bytes(&self) -> u64 {
462 self.to_read.end - self.to_read.start
463 }
464
465 async fn run(self) {
466 let bytes = if self.to_read.start == self.to_read.end {
467 Ok(Bytes::new())
468 } else {
469 let bytes_fut = self
470 .reader
471 .get_range(self.to_read.start as usize..self.to_read.end as usize);
472 IOPS_COUNTER.fetch_add(1, Ordering::Release);
473 BYTES_READ_COUNTER.fetch_add(self.num_bytes(), Ordering::Release);
474 bytes_fut.await.map_err(Error::from)
475 };
476 IOPS_QUOTA.release();
477 (self.when_done)(bytes);
478 }
479}
480
481async fn run_io_loop(tasks: Arc<IoQueue>) {
484 loop {
487 let next_task = tasks.pop().await;
488 match next_task {
489 Some(task) => {
490 tokio::spawn(task.run());
491 }
492 None => {
493 return;
495 }
496 }
497 }
498}
499
500pub struct ScanScheduler {
505 object_store: Arc<ObjectStore>,
506 io_queue: Arc<IoQueue>,
507}
508
509impl Debug for ScanScheduler {
510 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
511 f.debug_struct("ScanScheduler")
512 .field("object_store", &self.object_store)
513 .finish()
514 }
515}
516
517struct Response {
518 data: Result<Vec<Bytes>>,
519 priority: u128,
520 num_reqs: usize,
521 num_bytes: u64,
522}
523
524#[derive(Debug, Clone, Copy)]
525pub struct SchedulerConfig {
526 pub io_buffer_size_bytes: u64,
530}
531
532impl SchedulerConfig {
533 pub fn default_for_testing() -> Self {
535 Self {
536 io_buffer_size_bytes: 256 * 1024 * 1024,
537 }
538 }
539
540 pub fn max_bandwidth(store: &ObjectStore) -> Self {
543 Self {
544 io_buffer_size_bytes: 32 * 1024 * 1024 * store.io_parallelism() as u64,
545 }
546 }
547}
548
549impl ScanScheduler {
550 pub fn new(object_store: Arc<ObjectStore>, config: SchedulerConfig) -> Arc<Self> {
557 let io_capacity = object_store.io_parallelism();
558 let io_queue = Arc::new(IoQueue::new(
559 io_capacity as u32,
560 config.io_buffer_size_bytes,
561 ));
562 let scheduler = Self {
563 object_store,
564 io_queue: io_queue.clone(),
565 };
566 tokio::task::spawn(async move { run_io_loop(io_queue).await });
567 Arc::new(scheduler)
568 }
569
570 pub async fn open_file_with_priority(
579 self: &Arc<Self>,
580 path: &Path,
581 base_priority: u64,
582 ) -> Result<FileScheduler> {
583 let reader = self.object_store.open(path).await?;
584 let block_size = self.object_store.block_size() as u64;
585 Ok(FileScheduler {
586 reader: reader.into(),
587 block_size,
588 root: self.clone(),
589 base_priority,
590 })
591 }
592
593 pub async fn open_file(self: &Arc<Self>, path: &Path) -> Result<FileScheduler> {
597 self.open_file_with_priority(path, 0).await
598 }
599
600 fn do_submit_request(
601 &self,
602 reader: Arc<dyn Reader>,
603 request: Vec<Range<u64>>,
604 tx: oneshot::Sender<Response>,
605 priority: u128,
606 ) {
607 let num_iops = request.len() as u32;
608
609 let when_all_io_done = move |bytes_and_permits| {
610 let _ = tx.send(bytes_and_permits);
612 };
613
614 let dest = Arc::new(Mutex::new(Box::new(MutableBatch::new(
615 when_all_io_done,
616 num_iops,
617 priority,
618 request.len(),
619 ))));
620
621 for (task_idx, iop) in request.into_iter().enumerate() {
622 let dest = dest.clone();
623 let io_queue = self.io_queue.clone();
624 let num_bytes = iop.end - iop.start;
625 let task = IoTask {
626 reader: reader.clone(),
627 to_read: iop,
628 priority,
629 when_done: Box::new(move |data| {
630 io_queue.on_iop_complete();
631 let mut dest = dest.lock().unwrap();
632 let chunk = DataChunk {
633 data,
634 task_idx,
635 num_bytes,
636 };
637 dest.deliver_data(chunk);
638 }),
639 };
640 self.io_queue.push(task);
641 }
642 }
643
644 fn submit_request(
645 &self,
646 reader: Arc<dyn Reader>,
647 request: Vec<Range<u64>>,
648 priority: u128,
649 ) -> impl Future<Output = Result<Vec<Bytes>>> + Send {
650 let (tx, rx) = oneshot::channel::<Response>();
651
652 self.do_submit_request(reader, request, tx, priority);
653
654 let io_queue = self.io_queue.clone();
655
656 rx.map(move |wrapped_rsp| {
657 let rsp = wrapped_rsp.unwrap();
660 io_queue.on_bytes_consumed(rsp.num_bytes, rsp.priority, rsp.num_reqs);
661 rsp.data
662 })
663 }
664}
665
666impl Drop for ScanScheduler {
667 fn drop(&mut self) {
668 self.io_queue.close();
669 }
670}
671
672#[derive(Clone, Debug)]
674pub struct FileScheduler {
675 reader: Arc<dyn Reader>,
676 root: Arc<ScanScheduler>,
677 block_size: u64,
678 base_priority: u64,
679}
680
681fn is_close_together(range1: &Range<u64>, range2: &Range<u64>, block_size: u64) -> bool {
682 range2.start <= (range1.end + block_size)
684}
685
686fn is_overlapping(range1: &Range<u64>, range2: &Range<u64>) -> bool {
687 range1.start < range2.end && range2.start < range1.end
688}
689
690impl FileScheduler {
691 pub fn submit_request(
704 &self,
705 request: Vec<Range<u64>>,
706 priority: u64,
707 ) -> impl Future<Output = Result<Vec<Bytes>>> + Send {
708 let priority = ((self.base_priority as u128) << 64) + priority as u128;
710
711 let mut updated_requests = Vec::with_capacity(request.len());
712
713 if !request.is_empty() {
714 let mut curr_interval = request[0].clone();
715
716 for req in request.iter().skip(1) {
717 if is_close_together(&curr_interval, req, self.block_size) {
718 curr_interval.end = curr_interval.end.max(req.end);
719 } else {
720 updated_requests.push(curr_interval);
721 curr_interval = req.clone();
722 }
723 }
724
725 updated_requests.push(curr_interval);
726 }
727
728 let bytes_vec_fut =
729 self.root
730 .submit_request(self.reader.clone(), updated_requests.clone(), priority);
731
732 let mut updated_index = 0;
733 let mut final_bytes = Vec::with_capacity(request.len());
734
735 async move {
736 let bytes_vec = bytes_vec_fut.await?;
737
738 let mut orig_index = 0;
739 while (updated_index < updated_requests.len()) && (orig_index < request.len()) {
740 let updated_range = &updated_requests[updated_index];
741 let orig_range = &request[orig_index];
742 let byte_offset = updated_range.start as usize;
743
744 if is_overlapping(updated_range, orig_range) {
745 let start = orig_range.start as usize - byte_offset;
748 let end = orig_range.end as usize - byte_offset;
749
750 let sliced_range = bytes_vec[updated_index].slice(start..end);
751 final_bytes.push(sliced_range);
752 orig_index += 1;
753 } else {
754 updated_index += 1;
755 }
756 }
757
758 Ok(final_bytes)
759 }
760 }
761
762 pub fn submit_single(
769 &self,
770 range: Range<u64>,
771 priority: u64,
772 ) -> impl Future<Output = Result<Bytes>> + Send {
773 self.submit_request(vec![range], priority)
774 .map_ok(|vec_bytes| vec_bytes.into_iter().next().unwrap())
775 }
776
777 pub fn reader(&self) -> &Arc<dyn Reader> {
783 &self.reader
784 }
785}
786
787#[cfg(test)]
788mod tests {
789 use std::{collections::VecDeque, time::Duration};
790
791 use futures::poll;
792 use rand::RngCore;
793 use tempfile::tempdir;
794
795 use object_store::{memory::InMemory, GetRange, ObjectStore as OSObjectStore};
796 use tokio::{runtime::Handle, time::timeout};
797 use url::Url;
798
799 use crate::{object_store::DEFAULT_DOWNLOAD_RETRY_COUNT, testing::MockObjectStore};
800
801 use super::*;
802
803 #[tokio::test]
804 async fn test_full_seq_read() {
805 let tmpdir = tempdir().unwrap();
806 let tmp_path = tmpdir.path().to_str().unwrap();
807 let tmp_path = Path::parse(tmp_path).unwrap();
808 let tmp_file = tmp_path.child("foo.file");
809
810 let obj_store = Arc::new(ObjectStore::local());
811
812 const DATA_SIZE: u64 = 1024 * 1024;
814 let mut some_data = vec![0; DATA_SIZE as usize];
815 rand::thread_rng().fill_bytes(&mut some_data);
816 obj_store.put(&tmp_file, &some_data).await.unwrap();
817
818 let config = SchedulerConfig::default_for_testing();
819
820 let scheduler = ScanScheduler::new(obj_store, config);
821
822 let file_scheduler = scheduler.open_file(&tmp_file).await.unwrap();
823
824 const READ_SIZE: u64 = 4 * 1024;
826 let mut reqs = VecDeque::new();
827 let mut offset = 0;
828 while offset < DATA_SIZE {
829 reqs.push_back(
830 #[allow(clippy::single_range_in_vec_init)]
831 file_scheduler
832 .submit_request(vec![offset..offset + READ_SIZE], 0)
833 .await
834 .unwrap(),
835 );
836 offset += READ_SIZE;
837 }
838
839 offset = 0;
840 while offset < DATA_SIZE {
842 let data = reqs.pop_front().unwrap();
843 let actual = &data[0];
844 let expected = &some_data[offset as usize..(offset + READ_SIZE) as usize];
845 assert_eq!(expected, actual);
846 offset += READ_SIZE;
847 }
848 }
849
850 #[tokio::test]
851 async fn test_priority() {
852 let some_path = Path::parse("foo").unwrap();
853 let base_store = Arc::new(InMemory::new());
854 base_store
855 .put(&some_path, vec![0; 1000].into())
856 .await
857 .unwrap();
858
859 let semaphore = Arc::new(tokio::sync::Semaphore::new(0));
860 let mut obj_store = MockObjectStore::default();
861 let semaphore_copy = semaphore.clone();
862 obj_store
863 .expect_get_opts()
864 .returning(move |location, options| {
865 let semaphore = semaphore.clone();
866 let base_store = base_store.clone();
867 let location = location.clone();
868 async move {
869 semaphore.acquire().await.unwrap().forget();
870 base_store.get_opts(&location, options).await
871 }
872 .boxed()
873 });
874 let obj_store = Arc::new(ObjectStore::new(
875 Arc::new(obj_store),
876 Url::parse("mem://").unwrap(),
877 None,
878 None,
879 false,
880 false,
881 1,
882 DEFAULT_DOWNLOAD_RETRY_COUNT,
883 ));
884
885 let config = SchedulerConfig {
886 io_buffer_size_bytes: 1024 * 1024,
887 };
888
889 let scan_scheduler = ScanScheduler::new(obj_store, config);
890
891 let file_scheduler = scan_scheduler
892 .open_file(&Path::parse("foo").unwrap())
893 .await
894 .unwrap();
895
896 let first_fut = timeout(
900 Duration::from_secs(10),
901 file_scheduler.submit_single(0..10, 0),
902 )
903 .boxed();
904
905 let mut second_fut = timeout(
907 Duration::from_secs(10),
908 file_scheduler.submit_single(0..20, 100),
909 )
910 .boxed();
911
912 let mut third_fut = timeout(
915 Duration::from_secs(10),
916 file_scheduler.submit_single(0..30, 0),
917 )
918 .boxed();
919
920 semaphore_copy.add_permits(1);
922 assert!(first_fut.await.unwrap().unwrap().len() == 10);
923 assert!(poll!(&mut second_fut).is_pending());
925 assert!(poll!(&mut third_fut).is_pending());
926
927 semaphore_copy.add_permits(1);
929 assert!(third_fut.await.unwrap().unwrap().len() == 30);
930 assert!(poll!(&mut second_fut).is_pending());
931
932 semaphore_copy.add_permits(1);
934 assert!(second_fut.await.unwrap().unwrap().len() == 20);
935 }
936
937 #[tokio::test(flavor = "multi_thread")]
938 async fn test_backpressure() {
939 let some_path = Path::parse("foo").unwrap();
940 let base_store = Arc::new(InMemory::new());
941 base_store
942 .put(&some_path, vec![0; 100000].into())
943 .await
944 .unwrap();
945
946 let bytes_read = Arc::new(AtomicU64::from(0));
947 let mut obj_store = MockObjectStore::default();
948 let bytes_read_copy = bytes_read.clone();
949 obj_store
951 .expect_get_opts()
952 .returning(move |location, options| {
953 let range = options.range.as_ref().unwrap();
954 let num_bytes = match range {
955 GetRange::Bounded(bounded) => bounded.end - bounded.start,
956 _ => panic!(),
957 };
958 bytes_read_copy.fetch_add(num_bytes as u64, Ordering::Release);
959 let location = location.clone();
960 let base_store = base_store.clone();
961 async move { base_store.get_opts(&location, options).await }.boxed()
962 });
963 let obj_store = Arc::new(ObjectStore::new(
964 Arc::new(obj_store),
965 Url::parse("mem://").unwrap(),
966 None,
967 None,
968 false,
969 false,
970 1,
971 DEFAULT_DOWNLOAD_RETRY_COUNT,
972 ));
973
974 let config = SchedulerConfig {
975 io_buffer_size_bytes: 10,
976 };
977
978 let scan_scheduler = ScanScheduler::new(obj_store.clone(), config);
979
980 let file_scheduler = scan_scheduler
981 .open_file(&Path::parse("foo").unwrap())
982 .await
983 .unwrap();
984
985 let wait_for_idle = || async move {
986 let handle = Handle::current();
987 while handle.metrics().num_alive_tasks() != 1 {
988 tokio::time::sleep(Duration::from_millis(10)).await;
989 }
990 };
991 let wait_for_bytes_read_and_idle = |target_bytes: u64| {
992 let bytes_read = &bytes_read;
994 async move {
995 let bytes_read_copy = bytes_read.clone();
996 while bytes_read_copy.load(Ordering::Acquire) < target_bytes {
997 tokio::time::sleep(Duration::from_millis(10)).await;
998 }
999 wait_for_idle().await;
1000 }
1001 };
1002
1003 let first_fut = file_scheduler.submit_single(0..5, 0);
1005 let second_fut = file_scheduler.submit_single(0..5, 0);
1007 let third_fut = file_scheduler.submit_single(0..3, 0);
1009 wait_for_bytes_read_and_idle(10).await;
1011
1012 assert_eq!(first_fut.await.unwrap().len(), 5);
1013 wait_for_bytes_read_and_idle(13).await;
1015
1016 let fourth_fut = file_scheduler.submit_single(0..5, 0);
1018 wait_for_bytes_read_and_idle(13).await;
1019
1020 assert_eq!(third_fut.await.unwrap().len(), 3);
1022 wait_for_bytes_read_and_idle(18).await;
1023
1024 assert_eq!(second_fut.await.unwrap().len(), 5);
1025 let fifth_fut = file_scheduler.submit_request(vec![0..3, 90000..90007], 0);
1032 wait_for_bytes_read_and_idle(21).await;
1033
1034 let fifth_bytes = tokio::time::timeout(Duration::from_secs(10), fifth_fut)
1036 .await
1037 .unwrap();
1038 assert_eq!(
1039 fifth_bytes.unwrap().iter().map(|b| b.len()).sum::<usize>(),
1040 10
1041 );
1042
1043 assert_eq!(fourth_fut.await.unwrap().len(), 5);
1045 wait_for_bytes_read_and_idle(28).await;
1046
1047 let config = SchedulerConfig {
1049 io_buffer_size_bytes: 10,
1050 };
1051
1052 let scan_scheduler = ScanScheduler::new(obj_store, config);
1053 let file_scheduler = scan_scheduler
1054 .open_file(&Path::parse("foo").unwrap())
1055 .await
1056 .unwrap();
1057
1058 let first_fut = file_scheduler.submit_single(0..10, 0);
1059 let second_fut = file_scheduler.submit_single(0..10, 0);
1060
1061 std::thread::sleep(Duration::from_millis(100));
1062 assert_eq!(first_fut.await.unwrap().len(), 10);
1063 assert_eq!(second_fut.await.unwrap().len(), 10);
1064 }
1065
1066 #[test_log::test(tokio::test(flavor = "multi_thread"))]
1067 async fn stress_backpressure() {
1068 let some_path = Path::parse("foo").unwrap();
1072 let obj_store = Arc::new(ObjectStore::memory());
1073 obj_store
1074 .put(&some_path, vec![0; 100000].as_slice())
1075 .await
1076 .unwrap();
1077
1078 let config = SchedulerConfig {
1080 io_buffer_size_bytes: 1,
1081 };
1082 let scan_scheduler = ScanScheduler::new(obj_store.clone(), config);
1083 let file_scheduler = scan_scheduler.open_file(&some_path).await.unwrap();
1084
1085 let mut futs = Vec::with_capacity(10000);
1086 for idx in 0..10000 {
1087 futs.push(file_scheduler.submit_single(idx..idx + 1, idx));
1088 }
1089
1090 for fut in futs {
1091 fut.await.unwrap();
1092 }
1093 }
1094}