Skip to main content

arrow_avro/reader/async_reader/
spawn.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5use std::future::Future;
6use std::ops::Range;
7
8use bytes::Bytes;
9use futures::future::BoxFuture;
10use futures::{FutureExt, TryFutureExt};
11use tokio::runtime::Handle;
12
13use crate::errors::AvroError;
14use crate::reader::async_reader::AsyncFileReader;
15
16/// An [`AsyncFileReader`] that performs I/O on a separate tokio runtime.
17///
18/// Tokio is a cooperative scheduler, and relies on tasks yielding in a timely
19/// manner to service IO. Therefore, running IO and CPU-bound tasks, such as
20/// avro decoding, on the same tokio runtime can lead to degraded throughput,
21/// dropped connections and other issues. For more information see [here].
22///
23/// This wrapper spawns each operation of the inner reader onto the provided
24/// runtime [`Handle`], so that the runtime driving the avro decoding does not
25/// also drive the I/O.
26///
27/// The inner reader must be [`Clone`] (typically an `Arc`'d handle to some
28/// shared resource) as each spawned task requires a `'static` copy of it.
29///
30/// [here]: https://www.influxdata.com/blog/using-rustlangs-async-tokio-runtime-for-cpu-bound-tasks/
31#[derive(Clone, Debug)]
32pub struct SpawnedReader<R> {
33    inner: R,
34    handle: Handle,
35}
36
37impl<R> SpawnedReader<R> {
38    /// Creates a new [`SpawnedReader`] that performs the I/O of `inner` on `handle`
39    pub fn new(inner: R, handle: Handle) -> Self {
40        Self { inner, handle }
41    }
42
43    /// Returns the inner reader
44    pub fn into_inner(self) -> R {
45        self.inner
46    }
47}
48
49/// Spawns `fut` on `handle`, propagating panics and mapping task cancellation
50/// to [`AvroError::External`]
51fn spawn<T>(
52    handle: &Handle,
53    fut: impl Future<Output = Result<T, AvroError>> + Send + 'static,
54) -> BoxFuture<'static, Result<T, AvroError>>
55where
56    T: Send + 'static,
57{
58    handle
59        .spawn(fut)
60        .map_ok_or_else(
61            |e| match e.try_into_panic() {
62                Err(e) => Err(AvroError::External(Box::new(e))),
63                Ok(p) => std::panic::resume_unwind(p),
64            },
65            |res| res,
66        )
67        .boxed()
68}
69
70impl<R> AsyncFileReader for SpawnedReader<R>
71where
72    R: AsyncFileReader + Clone + Send + 'static,
73{
74    fn get_bytes(&mut self, range: Range<u64>) -> BoxFuture<'_, Result<Bytes, AvroError>> {
75        let mut inner = self.inner.clone();
76        spawn(&self.handle, async move { inner.get_bytes(range).await })
77    }
78
79    fn get_byte_ranges(
80        &mut self,
81        ranges: Vec<Range<u64>>,
82    ) -> BoxFuture<'_, Result<Vec<Bytes>, AvroError>> {
83        let mut inner = self.inner.clone();
84        spawn(
85            &self.handle,
86            async move { inner.get_byte_ranges(ranges).await },
87        )
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use std::sync::{Arc, Mutex};
95    use std::thread::ThreadId;
96
97    /// An in-memory [`AsyncFileReader`] that records the thread each request ran on
98    #[derive(Clone)]
99    struct InMemoryReader {
100        data: Bytes,
101        threads: Arc<Mutex<Vec<ThreadId>>>,
102    }
103
104    impl AsyncFileReader for InMemoryReader {
105        fn get_bytes(&mut self, range: Range<u64>) -> BoxFuture<'_, Result<Bytes, AvroError>> {
106            self.threads
107                .lock()
108                .unwrap()
109                .push(std::thread::current().id());
110            let data = self.data.slice(range.start as usize..range.end as usize);
111            futures::future::ready(Ok(data)).boxed()
112        }
113    }
114
115    #[tokio::test]
116    async fn test_spawned_reader() {
117        let rt = tokio::runtime::Builder::new_multi_thread()
118            .worker_threads(1)
119            .build()
120            .unwrap();
121
122        let inner = InMemoryReader {
123            data: Bytes::from_static(b"hello world"),
124            threads: Default::default(),
125        };
126        let threads = inner.threads.clone();
127        let mut reader = SpawnedReader::new(inner, rt.handle().clone());
128
129        let bytes = reader.get_bytes(0..5).await.unwrap();
130        assert_eq!(bytes.as_ref(), b"hello");
131
132        let ranges = reader.get_byte_ranges(vec![0..5, 6..11]).await.unwrap();
133        assert_eq!(ranges[1].as_ref(), b"world");
134
135        // All I/O must have run on the spawned runtime, not the current one
136        let current_id = std::thread::current().id();
137        let threads = threads.lock().unwrap();
138        assert!(!threads.is_empty());
139        assert!(threads.iter().all(|id| *id != current_id));
140
141        // Runtimes have to be dropped in blocking contexts
142        tokio::runtime::Handle::current().spawn_blocking(move || drop(rt));
143    }
144
145    #[tokio::test]
146    async fn test_spawned_reader_fails_on_shutdown_runtime() {
147        let rt = tokio::runtime::Builder::new_multi_thread()
148            .worker_threads(1)
149            .build()
150            .unwrap();
151
152        let inner = InMemoryReader {
153            data: Bytes::from_static(b"hello world"),
154            threads: Default::default(),
155        };
156        let mut reader = SpawnedReader::new(inner, rt.handle().clone());
157
158        rt.shutdown_background();
159
160        let err = reader.get_bytes(0..1).await.unwrap_err().to_string();
161        assert!(err.contains("was cancelled"), "{err}");
162    }
163}