alamb commented on code in PR #6612:
URL: https://github.com/apache/arrow-rs/pull/6612#discussion_r1821275908
##########
parquet/src/arrow/async_reader/store.rs:
##########
@@ -99,27 +99,62 @@ impl ParquetObjectReader {
..self
}
}
+
+ /// Perform IO on the provided tokio runtime
+ ///
+ /// Tokio is a cooperative scheduler, and relies on tasks yielding in a
timely manner
+ /// to service IO. Therefore, running IO and CPU-bound tasks, such as
parquet decoding,
+ /// on the same tokio runtime can lead to degraded throughput, dropped
connections and
+ /// other issues. For more information see [here].
+ ///
+ /// [here]:
https://www.influxdata.com/blog/using-rustlangs-async-tokio-runtime-for-cpu-bound-tasks/
+ pub fn with_runtime(self, handle: Handle) -> Self {
+ Self {
+ runtime: Some(handle),
+ ..self
+ }
+ }
+
+ fn spawn<F, O, E>(&self, f: F) -> BoxFuture<'_, Result<O>>
+ where
+ F: for<'a> FnOnce(&'a Arc<dyn ObjectStore>, &'a Path) -> BoxFuture<'a,
Result<O, E>>
+ + Send
+ + 'static,
+ O: Send + 'static,
+ E: Into<ParquetError> + Send + 'static,
+ {
+ match &self.runtime {
+ Some(handle) => {
+ let path = self.meta.location.clone();
+ let store = Arc::clone(&self.store);
+ handle
+ .spawn(async move { f(&store, &path).await })
+ .map_ok_or_else(
+ |e| match e.try_into_panic() {
+ Err(e) => Err(ParquetError::External(Box::new(e))),
+ Ok(p) => std::panic::resume_unwind(p),
+ },
+ |res| res.map_err(|e| e.into()),
+ )
+ .boxed()
+ }
+ None => f(&self.store, &self.meta.location)
+ .map_err(|e| e.into())
+ .boxed(),
+ }
+ }
}
impl AsyncFileReader for ParquetObjectReader {
fn get_bytes(&mut self, range: Range<usize>) -> BoxFuture<'_,
Result<Bytes>> {
- self.store
- .get_range(&self.meta.location, range)
- .map_err(|e| e.into())
- .boxed()
+ self.spawn(|store, path| store.get_range(path, range))
}
fn get_byte_ranges(&mut self, ranges: Vec<Range<usize>>) -> BoxFuture<'_,
Result<Vec<Bytes>>>
where
Self: Send,
{
- async move {
- self.store
- .get_ranges(&self.meta.location, &ranges)
- .await
- .map_err(|e| e.into())
- }
- .boxed()
+ self.spawn(|store, path| async move { store.get_ranges(path,
&ranges).await }.boxed())
Review Comment:
I see -- I think the thing I was missing is that `load_and_finish` takes
`self` which is used to actually do the IO. Thank you for the clarification
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]