|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! See `main.rs` for how to run it. |
| 19 | +//! |
| 20 | +//! [`object_store_spill`] shows how to use the [`TempFileFactory]` API to configure |
| 21 | +//! DataFusion to spill intermediate results to remote storage when it exceeds |
| 22 | +//! its memory limits. |
| 23 | +use std::future::Future; |
| 24 | +use std::io::Write; |
| 25 | +use std::path::Path as StdPath; |
| 26 | +use std::pin::Pin; |
| 27 | +use std::sync::Arc; |
| 28 | +use std::sync::atomic::{AtomicU64, Ordering}; |
| 29 | + |
| 30 | +use bytes::Bytes; |
| 31 | +use datafusion::common::Result; |
| 32 | +use datafusion::error::DataFusionError; |
| 33 | +use datafusion::execution::disk_manager::DiskManagerBuilder; |
| 34 | +use datafusion::execution::runtime_env::RuntimeEnvBuilder; |
| 35 | +use datafusion::execution::{SpillFile, SpillWriter, TempFileFactory}; |
| 36 | +use datafusion::prelude::{SessionConfig, SessionContext}; |
| 37 | +use futures::{Stream, StreamExt, stream}; |
| 38 | +use object_store::local::LocalFileSystem; |
| 39 | +use object_store::path::Path; |
| 40 | +use object_store::{ObjectStore, ObjectStoreExt, PutPayload}; |
| 41 | + |
| 42 | +/// Demonstrates configuring DataFusion with spill files backed by an ObjectStore. |
| 43 | +pub async fn object_store_spill() -> Result<()> { |
| 44 | + let tmp_dir = tempfile::tempdir().map_err(DataFusionError::IoError)?; |
| 45 | + let store: Arc<dyn ObjectStore> = |
| 46 | + Arc::new(LocalFileSystem::new_with_prefix(tmp_dir.path())?); |
| 47 | + |
| 48 | + // A spill factory defines how data will be spilled. |
| 49 | + let spill_factory = Arc::new(ObjectStoreTempFileFactory::new(store)); |
| 50 | + let runtime = RuntimeEnvBuilder::new() |
| 51 | + .with_disk_manager_builder( |
| 52 | + DiskManagerBuilder::default().with_spill_factory(spill_factory), |
| 53 | + ) |
| 54 | + .build_arc()?; |
| 55 | + |
| 56 | + // Queries run with this context use the custom factory whenever an operator spills. |
| 57 | + let _ctx = |
| 58 | + SessionContext::new_with_config_rt(SessionConfig::new(), Arc::clone(&runtime)); |
| 59 | + |
| 60 | + // Exercise the same spill file path directly so the example does not depend on |
| 61 | + // a particular query plan spilling for this small input. |
| 62 | + let spill_file = runtime |
| 63 | + .disk_manager |
| 64 | + .create_tmp_file("object store spill example")?; |
| 65 | + let mut writer = spill_file.open_writer()?; |
| 66 | + writer.write_all(b"spill data stored through ObjectStore")?; |
| 67 | + writer.finish()?; |
| 68 | + |
| 69 | + let chunks: Vec<Bytes> = spill_file |
| 70 | + .read_stream()? |
| 71 | + .collect::<Vec<_>>() |
| 72 | + .await |
| 73 | + .into_iter() |
| 74 | + .collect::<Result<_>>()?; |
| 75 | + assert_eq!(chunks.concat(), b"spill data stored through ObjectStore"); |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +struct ObjectStoreTempFileFactory { |
| 81 | + store: Arc<dyn ObjectStore>, |
| 82 | + counter: AtomicU64, |
| 83 | +} |
| 84 | + |
| 85 | +impl ObjectStoreTempFileFactory { |
| 86 | + fn new(store: Arc<dyn ObjectStore>) -> Self { |
| 87 | + Self { |
| 88 | + store, |
| 89 | + counter: AtomicU64::new(0), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl std::panic::RefUnwindSafe for ObjectStoreTempFileFactory {} |
| 95 | +impl std::panic::UnwindSafe for ObjectStoreTempFileFactory {} |
| 96 | + |
| 97 | +impl TempFileFactory for ObjectStoreTempFileFactory { |
| 98 | + fn create_temp_file(&self, description: &str) -> Result<Arc<dyn SpillFile>> { |
| 99 | + let id = self.counter.fetch_add(1, Ordering::Relaxed); |
| 100 | + let description = sanitize_path_part(description); |
| 101 | + let location = Path::from(format!("spill/{description}-{id}.bin")); |
| 102 | + |
| 103 | + Ok(Arc::new(ObjectStoreSpillFile { |
| 104 | + store: Arc::clone(&self.store), |
| 105 | + location, |
| 106 | + size: Arc::new(AtomicU64::new(0)), |
| 107 | + })) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +struct ObjectStoreSpillFile { |
| 112 | + store: Arc<dyn ObjectStore>, |
| 113 | + location: Path, |
| 114 | + size: Arc<AtomicU64>, |
| 115 | +} |
| 116 | + |
| 117 | +impl SpillFile for ObjectStoreSpillFile { |
| 118 | + fn path(&self) -> Option<&StdPath> { |
| 119 | + None |
| 120 | + } |
| 121 | + |
| 122 | + fn size(&self) -> Option<u64> { |
| 123 | + Some(self.size.load(Ordering::Relaxed)) |
| 124 | + } |
| 125 | + |
| 126 | + fn read_stream(&self) -> Result<Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>> { |
| 127 | + let store = Arc::clone(&self.store); |
| 128 | + let location = self.location.clone(); |
| 129 | + |
| 130 | + Ok(Box::pin(stream::once(async move { |
| 131 | + Ok(store.get(&location).await?.bytes().await?) |
| 132 | + }))) |
| 133 | + } |
| 134 | + |
| 135 | + fn open_writer(&self) -> Result<Box<dyn SpillWriter>> { |
| 136 | + Ok(Box::new(ObjectStoreSpillWriter { |
| 137 | + store: Arc::clone(&self.store), |
| 138 | + location: self.location.clone(), |
| 139 | + size: Arc::clone(&self.size), |
| 140 | + buffer: Vec::new(), |
| 141 | + })) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +struct ObjectStoreSpillWriter { |
| 146 | + store: Arc<dyn ObjectStore>, |
| 147 | + location: Path, |
| 148 | + size: Arc<AtomicU64>, |
| 149 | + buffer: Vec<u8>, |
| 150 | +} |
| 151 | + |
| 152 | +impl Write for ObjectStoreSpillWriter { |
| 153 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 154 | + self.buffer.extend_from_slice(buf); |
| 155 | + Ok(buf.len()) |
| 156 | + } |
| 157 | + |
| 158 | + fn flush(&mut self) -> std::io::Result<()> { |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl SpillWriter for ObjectStoreSpillWriter { |
| 164 | + fn finish(&mut self) -> Result<()> { |
| 165 | + let store = Arc::clone(&self.store); |
| 166 | + let location = self.location.clone(); |
| 167 | + let data = std::mem::take(&mut self.buffer); |
| 168 | + let size = data.len() as u64; |
| 169 | + |
| 170 | + // This simple example buffers the spill and uploads it on finish. |
| 171 | + // Production remote stores should consider multipart or streaming uploads. |
| 172 | + block_on_object_store(async move { |
| 173 | + store |
| 174 | + .put(&location, PutPayload::from_bytes(data.into())) |
| 175 | + .await?; |
| 176 | + Ok(()) |
| 177 | + })?; |
| 178 | + |
| 179 | + self.size.store(size, Ordering::Relaxed); |
| 180 | + Ok(()) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +fn block_on_object_store<T>(future: impl Future<Output = Result<T>>) -> Result<T> { |
| 185 | + if let Ok(handle) = tokio::runtime::Handle::try_current() { |
| 186 | + tokio::task::block_in_place(|| handle.block_on(future)) |
| 187 | + } else { |
| 188 | + tokio::runtime::Runtime::new() |
| 189 | + .map_err(DataFusionError::IoError)? |
| 190 | + .block_on(future) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +fn sanitize_path_part(value: &str) -> String { |
| 195 | + value |
| 196 | + .chars() |
| 197 | + .map(|c| if c.is_ascii_alphanumeric() { c } else { '_' }) |
| 198 | + .collect() |
| 199 | +} |
0 commit comments