Return a temporary file from a randomized choice in the configured locations If the file can not be created for some reason, returns an error message referencing the request description
(
self: &Arc<Self>,
request_description: &str,
)
| 302 | /// If the file can not be created for some reason, returns an |
| 303 | /// error message referencing the request description |
| 304 | pub fn create_tmp_file( |
| 305 | self: &Arc<Self>, |
| 306 | request_description: &str, |
| 307 | ) -> Result<RefCountedTempFile> { |
| 308 | let mut guard = self.local_dirs.lock(); |
| 309 | let local_dirs = guard.as_mut().ok_or_else(|| { |
| 310 | resources_datafusion_err!( |
| 311 | "Memory Exhausted while {request_description} (DiskManager is disabled)" |
| 312 | ) |
| 313 | })?; |
| 314 | |
| 315 | // Create a temporary directory if needed |
| 316 | if local_dirs.is_empty() { |
| 317 | let tempdir = tempfile::tempdir().map_err(DataFusionError::IoError)?; |
| 318 | |
| 319 | debug!( |
| 320 | "Created directory '{:?}' as DataFusion tempfile directory for {}", |
| 321 | tempdir.path().to_string_lossy(), |
| 322 | request_description, |
| 323 | ); |
| 324 | |
| 325 | local_dirs.push(Arc::new(tempdir)); |
| 326 | } |
| 327 | |
| 328 | let dir_index = rng().random_range(0..local_dirs.len()); |
| 329 | self.active_files_count.fetch_add(1, Ordering::Relaxed); |
| 330 | Ok(RefCountedTempFile { |
| 331 | parent_temp_dir: Arc::clone(&local_dirs[dir_index]), |
| 332 | tempfile: Arc::new( |
| 333 | Builder::new() |
| 334 | .tempfile_in(local_dirs[dir_index].as_ref()) |
| 335 | .map_err(DataFusionError::IoError)?, |
| 336 | ), |
| 337 | current_file_disk_usage: Arc::new(AtomicU64::new(0)), |
| 338 | disk_manager: Arc::clone(self), |
| 339 | }) |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /// A wrapper around a [`NamedTempFile`] that also contains |