This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Retry hash file allocation #33565
Merged
Merged
Retry hash file allocation #33565
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ use { | |
atomic::{AtomicU64, AtomicUsize, Ordering}, | ||
Arc, | ||
}, | ||
|
||
}, | ||
tempfile::tempfile_in, | ||
}; | ||
|
@@ -87,22 +88,59 @@ impl AccountHashesFile { | |
if self.writer.is_none() { | ||
// we have hashes to write but no file yet, so create a file that will auto-delete on drop | ||
|
||
let mut data = tempfile_in(&self.dir_for_temp_cache_files).unwrap_or_else(|err| { | ||
panic!( | ||
"Unable to create file within {}: {err}", | ||
self.dir_for_temp_cache_files.display() | ||
) | ||
}); | ||
let get_file = || -> Result<_, std::io::Error> { | ||
let mut data = tempfile_in(&self.dir_for_temp_cache_files).unwrap_or_else(|err| { | ||
panic!( | ||
"Unable to create file within {}: {err}", | ||
self.dir_for_temp_cache_files.display() | ||
) | ||
}); | ||
|
||
// Theoretical performance optimization: write a zero to the end of | ||
// the file so that we won't have to resize it later, which may be | ||
// expensive. | ||
assert!(self.capacity > 0); | ||
data.seek(SeekFrom::Start((self.capacity - 1) as u64))?; | ||
data.write_all(&[0])?; | ||
data.rewind()?; | ||
data.flush()?; | ||
Ok(data) | ||
}; | ||
|
||
// Retry 5 times to allocate the AccountHashesFile. The memory might be fragmented and | ||
// causes memory allocation failure. Therefore, let's retry after failure. Hoping that the | ||
// kernel has the chance to defrag the memory between the retries, and retries succeed. | ||
let mut num_retries = 0; | ||
let data = loop { | ||
num_retries += 1; | ||
|
||
match get_file() { | ||
Ok(data) => { | ||
break data; | ||
} | ||
Err(err) => { | ||
info!( | ||
"Unable to create account hashes file within {}: {}, retry counter {}", | ||
self.dir_for_temp_cache_files.display(), | ||
err, | ||
num_retries | ||
); | ||
|
||
// Theoretical performance optimization: write a zero to the end of | ||
// the file so that we won't have to resize it later, which may be | ||
// expensive. | ||
assert!(self.capacity > 0); | ||
data.seek(SeekFrom::Start((self.capacity - 1) as u64)) | ||
.unwrap(); | ||
data.write_all(&[0]).unwrap(); | ||
data.rewind().unwrap(); | ||
data.flush().unwrap(); | ||
if num_retries > 5 { | ||
panic!( | ||
"Unable to create account hashes file within {}: after {} retries", | ||
self.dir_for_temp_cache_files.display(), | ||
num_retries | ||
); | ||
} | ||
datapoint_info!( | ||
"retry_account_hashes_file_allocation", | ||
("retry", num_retries, i64) | ||
); | ||
thread::sleep(time::Duration::from_millis(num_retries * 100)); | ||
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. 100 milliseconds is a long time. Maybe too long? I'm not sure. How was this constant chosen? 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. yeah. I think this should not happen very often, so choose 100ms to be conservative. |
||
} | ||
} | ||
}; | ||
|
||
//UNSAFE: Required to create a Mmap | ||
let map = unsafe { MmapMut::map_mut(&data) }; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.