Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.3k
Use NamedTempFile rather than String in DiskManager#1680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -50,8 +50,10 @@ use std::fmt; | ||
| use std::fmt::{Debug, Formatter}; | ||
| use std::fs::File; | ||
| use std::io::BufReader; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::sync::atomic::{AtomicUsize, Ordering}; | ||
| use std::sync::Arc; | ||
| use tempfile::NamedTempFile; | ||
| use tokio::sync::mpsc::{Receiver as TKReceiver, Sender as TKSender}; | ||
| use tokio::task; | ||
| @@ -68,7 +70,7 @@ struct ExternalSorter { | ||
| id: MemoryConsumerId, | ||
| schema: SchemaRef, | ||
| in_mem_batches: Mutex<Vec<RecordBatch>>, | ||
| spills: Mutex<Vec<String>>, | ||
| spills: Mutex<Vec<NamedTempFile>>, | ||
| /// Sort expressions | ||
| expr: Vec<PhysicalSortExpr>, | ||
| runtime: Arc<RuntimeEnv>, | ||
| @@ -221,22 +223,25 @@ impl MemoryConsumer for ExternalSorter { | ||
| let baseline_metrics = self.metrics.new_intermediate_baseline(partition); | ||
| let path = self.runtime.disk_manager.create_tmp_file()?; | ||
| let spillfile = self.runtime.disk_manager.create_tmp_file()?; | ||
| let stream = in_mem_partial_sort( | ||
| &mut *in_mem_batches, | ||
| self.schema.clone(), | ||
| &*self.expr, | ||
| baseline_metrics, | ||
| ); | ||
| let total_size = | ||
| spill_partial_sorted_stream(&mut stream?, path.clone(), self.schema.clone()) | ||
| .await?; | ||
| let total_size = spill_partial_sorted_stream( | ||
| &mut stream?, | ||
| spillfile.path(), | ||
| self.schema.clone(), | ||
| ) | ||
| .await?; | ||
| let mut spills = self.spills.lock().await; | ||
| let used = self.used.swap(0, Ordering::SeqCst); | ||
| self.inner_metrics.record_spill(total_size); | ||
| spills.push(path); | ||
| spills.push(spillfile); | ||
| Ok(used) | ||
| } | ||
| @@ -281,12 +286,12 @@ fn in_mem_partial_sort( | ||
| async fn spill_partial_sorted_stream( | ||
| in_mem_stream: &mut SendableRecordBatchStream, | ||
| path: String, | ||
| path: &Path, | ||
| schema: SchemaRef, | ||
| ) -> Result<usize> { | ||
| let (sender, receiver) = tokio::sync::mpsc::channel(2); | ||
| let path_clone = path.clone(); | ||
| let handle = task::spawn_blocking(move || write_sorted(receiver, path_clone, schema)); | ||
| let path: PathBuf = path.into(); | ||
| let handle = task::spawn_blocking(move || write_sorted(receiver, path, schema)); | ||
| while let Some(item) = in_mem_stream.next().await { | ||
| sender.send(item).await.ok(); | ||
| } | ||
| @@ -301,17 +306,16 @@ async fn spill_partial_sorted_stream( | ||
| } | ||
| fn read_spill_as_stream( | ||
| path: String, | ||
| path: NamedTempFile, | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since ownership of | ||
| schema: SchemaRef, | ||
| ) -> Result<SendableRecordBatchStream> { | ||
| let (sender, receiver): ( | ||
| TKSender<ArrowResult<RecordBatch>>, | ||
| TKReceiver<ArrowResult<RecordBatch>>, | ||
| ) = tokio::sync::mpsc::channel(2); | ||
| let path_clone = path.clone(); | ||
| let join_handle = task::spawn_blocking(move || { | ||
| if let Err(e) = read_spill(sender, path_clone) { | ||
| error!("Failure while reading spill file: {}. Error: {}", path, e); | ||
| if let Err(e) = read_spill(sender, path.path()) { | ||
| error!("Failure while reading spill file: {:?}. Error: {}", path, e); | ||
| } | ||
| }); | ||
| Ok(RecordBatchReceiverStream::create( | ||
| @@ -323,7 +327,7 @@ fn read_spill_as_stream( | ||
| fn write_sorted( | ||
| mut receiver: TKReceiver<ArrowResult<RecordBatch>>, | ||
| path: String, | ||
| path: PathBuf, | ||
| schema: SchemaRef, | ||
| ) -> Result<usize> { | ||
| let mut writer = IPCWriter::new(path.as_ref(), schema.as_ref())?; | ||
| @@ -338,7 +342,7 @@ fn write_sorted( | ||
| Ok(writer.num_bytes as usize) | ||
| } | ||
| fn read_spill(sender: TKSender<ArrowResult<RecordBatch>>, path: String) -> Result<()> { | ||
| fn read_spill(sender: TKSender<ArrowResult<RecordBatch>>, path: &Path) -> Result<()> { | ||
| let file = BufReader::new(File::open(&path)?); | ||
| let reader = FileReader::try_new(file)?; | ||
| for batch in reader { | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tempfiles are now generated using
tempfilerather than string manipulation