Skip to content
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

Improve error messages when memory is exhausted while sorting #4348

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions datafusion/core/src/execution/disk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub struct DiskManager {
/// TempDirs to put temporary files in.
///
/// If `Some(vec![])` a new OS specified temporary directory will be created
/// If `None` an error will be returned
/// If `None` an error will be returned (configured not to spill)
local_dirs: Mutex<Option<Vec<TempDir>>>,
}

Expand Down Expand Up @@ -103,21 +103,26 @@ impl DiskManager {
}

/// Return a temporary file from a randomized choice in the configured locations
pub fn create_tmp_file(&self) -> Result<NamedTempFile> {
///
/// If the file can not be created for some reason, returns an
/// error message referencing the request description
pub fn create_tmp_file(&self, request_description: &str) -> Result<NamedTempFile> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully once we rework the error handling story, we can move away from this nonsense

let mut guard = self.local_dirs.lock();
let local_dirs = guard.as_mut().ok_or_else(|| {
DataFusionError::ResourcesExhausted(
"Cannot spill to temporary file as DiskManager is disabled".to_string(),
)
DataFusionError::ResourcesExhausted(format!(
"Memory Exhausted while {} (DiskManager is disabled)",
request_description
))
})?;

// Create a temporary directory if needed
if local_dirs.is_empty() {
let tempdir = tempfile::tempdir().map_err(DataFusionError::IoError)?;

debug!(
"Created directory '{:?}' as DataFusion tempfile directory",
tempdir.path().to_string_lossy()
"Created directory '{:?}' as DataFusion tempfile directory for {}",
tempdir.path().to_string_lossy(),
request_description,
);

local_dirs.push(tempdir);
Expand Down Expand Up @@ -160,7 +165,7 @@ mod tests {
assert_eq!(0, local_dir_snapshot(&dm).len());

// can still create a tempfile however:
let actual = dm.create_tmp_file()?;
let actual = dm.create_tmp_file("Testing")?;

// Now the tempdir has been created on demand
assert_eq!(1, local_dir_snapshot(&dm).len());
Expand Down Expand Up @@ -192,7 +197,7 @@ mod tests {
);

let dm = DiskManager::try_new(config)?;
let actual = dm.create_tmp_file()?;
let actual = dm.create_tmp_file("Testing")?;

// the file should be in one of the specified local directories
assert_path_in_dirs(actual.path(), local_dirs.into_iter());
Expand All @@ -204,10 +209,9 @@ mod tests {
fn test_disabled_disk_manager() {
let config = DiskManagerConfig::Disabled;
let manager = DiskManager::try_new(config).unwrap();
let e = manager.create_tmp_file().unwrap_err().to_string();
assert_eq!(
e,
"Resources exhausted: Cannot spill to temporary file as DiskManager is disabled"
manager.create_tmp_file("Testing").unwrap_err().to_string(),
"Resources exhausted: Memory Exhausted while Testing (DiskManager is disabled)",
)
}

Expand Down
2 changes: 1 addition & 1 deletion datafusion/core/src/physical_plan/sorts/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl MemoryConsumer for ExternalSorter {
.metrics_set
.new_intermediate_tracking(partition, self.runtime.clone());

let spillfile = self.runtime.disk_manager.create_tmp_file()?;
let spillfile = self.runtime.disk_manager.create_tmp_file("Sorting")?;
let stream = in_mem_partial_sort(
&mut in_mem_batches,
self.schema.clone(),
Expand Down