-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve safekeepers eviction rate limiting (#8456)
This commit tries to fix regular load spikes on staging, caused by too many eviction and partial upload operations running at the same time. Usually it was hapenning after restart, for partial backup the load was delayed. - Add a semaphore for evictions (2 permits by default) - Rename `resident_since` to `evict_not_before` and smooth out the curve by using random duration - Use random duration in partial uploads as well related to #6338 some discussion in https://neondb.slack.com/archives/C033RQ5SPDH/p1720601531744029
- Loading branch information
1 parent
8c828c5
commit f3acfb2
Showing
7 changed files
with
112 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::sync::Arc; | ||
|
||
use rand::Rng; | ||
|
||
use crate::metrics::MISC_OPERATION_SECONDS; | ||
|
||
/// Global rate limiter for background tasks. | ||
#[derive(Clone)] | ||
pub struct RateLimiter { | ||
partial_backup: Arc<tokio::sync::Semaphore>, | ||
eviction: Arc<tokio::sync::Semaphore>, | ||
} | ||
|
||
impl RateLimiter { | ||
/// Create a new rate limiter. | ||
/// - `partial_backup_max`: maximum number of concurrent partial backups. | ||
/// - `eviction_max`: maximum number of concurrent timeline evictions. | ||
pub fn new(partial_backup_max: usize, eviction_max: usize) -> Self { | ||
Self { | ||
partial_backup: Arc::new(tokio::sync::Semaphore::new(partial_backup_max)), | ||
eviction: Arc::new(tokio::sync::Semaphore::new(eviction_max)), | ||
} | ||
} | ||
|
||
/// Get a permit for partial backup. This will block if the maximum number of concurrent | ||
/// partial backups is reached. | ||
pub async fn acquire_partial_backup(&self) -> tokio::sync::OwnedSemaphorePermit { | ||
let _timer = MISC_OPERATION_SECONDS | ||
.with_label_values(&["partial_permit_acquire"]) | ||
.start_timer(); | ||
self.partial_backup | ||
.clone() | ||
.acquire_owned() | ||
.await | ||
.expect("semaphore is closed") | ||
} | ||
|
||
/// Try to get a permit for timeline eviction. This will return None if the maximum number of | ||
/// concurrent timeline evictions is reached. | ||
pub fn try_acquire_eviction(&self) -> Option<tokio::sync::OwnedSemaphorePermit> { | ||
self.eviction.clone().try_acquire_owned().ok() | ||
} | ||
} | ||
|
||
/// Generate a random duration that is a fraction of the given duration. | ||
pub fn rand_duration(duration: &std::time::Duration) -> std::time::Duration { | ||
let randf64 = rand::thread_rng().gen_range(0.0..1.0); | ||
duration.mul_f64(randf64) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
f3acfb2
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.
2192 tests run: 2109 passed, 4 failed, 79 skipped (full report)
Failures on Postgres 14
test_gc_feedback_with_snapshots[github-actions-selfhosted]
: releasetest_gc_feedback[github-actions-selfhosted]
: releasetest_heavy_write_workload[neon_on-github-actions-selfhosted-10-5-5]
: releasetest_storage_controller_many_tenants[github-actions-selfhosted]
: releaseCode coverage* (full report)
functions
:32.8% (7125 of 21694 functions)
lines
:50.4% (57369 of 113806 lines)
* collected from Rust tests only
f3acfb2 at 2024-08-02T18:17:57.871Z :recycle: