-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1727 from tursodatabase/throttle-segment-creation
Swap segment strategy
- Loading branch information
Showing
6 changed files
with
137 additions
and
9 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
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,33 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use parking_lot::Mutex; | ||
|
||
use super::SegmentSwapStrategy; | ||
|
||
/// A wal swap strategy that swaps the current wal if it's older that some duration | ||
pub struct DurationSwapStrategy { | ||
swap_after: Duration, | ||
last_swapped_at: Mutex<Instant>, | ||
} | ||
|
||
impl DurationSwapStrategy { | ||
pub fn new(swap_after: Duration) -> Self { | ||
Self { | ||
swap_after, | ||
last_swapped_at: Mutex::new(Instant::now()), | ||
} | ||
} | ||
} | ||
|
||
impl SegmentSwapStrategy for DurationSwapStrategy { | ||
#[inline(always)] | ||
fn should_swap(&self, _frames_in_wal: usize) -> bool { | ||
let last_swapped_at = self.last_swapped_at.lock(); | ||
last_swapped_at.elapsed() >= self.swap_after | ||
} | ||
|
||
#[inline(always)] | ||
fn swapped(&self) { | ||
*self.last_swapped_at.lock() = Instant::now(); | ||
} | ||
} |
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,22 @@ | ||
use super::SegmentSwapStrategy; | ||
|
||
/// A swap strategy that swaps if the count of frames in the wal exceed some threshold | ||
pub struct FrameCountSwapStrategy { | ||
max_frames_in_wal: usize, | ||
} | ||
|
||
impl FrameCountSwapStrategy { | ||
pub fn new(max_frames_in_wal: usize) -> Self { | ||
Self { max_frames_in_wal } | ||
} | ||
} | ||
|
||
impl SegmentSwapStrategy for FrameCountSwapStrategy { | ||
#[inline(always)] | ||
fn should_swap(&self, frames_in_wal: usize) -> bool { | ||
frames_in_wal >= self.max_frames_in_wal | ||
} | ||
|
||
#[inline(always)] | ||
fn swapped(&self) {} | ||
} |
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,59 @@ | ||
pub(crate) mod duration; | ||
pub(crate) mod frame_count; | ||
|
||
pub(crate) trait SegmentSwapStrategy: Sync + Send + 'static { | ||
fn should_swap(&self, frames_in_wal: usize) -> bool; | ||
fn swapped(&self); | ||
|
||
fn and<O: SegmentSwapStrategy>(self, other: O) -> And<Self, O> | ||
where | ||
Self: Sized, | ||
{ | ||
And(self, other) | ||
} | ||
|
||
fn or<O: SegmentSwapStrategy>(self, other: O) -> Or<Self, O> | ||
where | ||
Self: Sized, | ||
{ | ||
Or(self, other) | ||
} | ||
} | ||
|
||
pub struct And<A, B>(A, B); | ||
|
||
impl<A, B> SegmentSwapStrategy for And<A, B> | ||
where | ||
A: SegmentSwapStrategy, | ||
B: SegmentSwapStrategy, | ||
{ | ||
#[inline(always)] | ||
fn should_swap(&self, frames_in_wal: usize) -> bool { | ||
self.0.should_swap(frames_in_wal) && self.1.should_swap(frames_in_wal) | ||
} | ||
|
||
#[inline(always)] | ||
fn swapped(&self) { | ||
self.0.swapped(); | ||
self.1.swapped(); | ||
} | ||
} | ||
|
||
pub struct Or<A, B>(A, B); | ||
|
||
impl<A, B> SegmentSwapStrategy for Or<A, B> | ||
where | ||
A: SegmentSwapStrategy, | ||
B: SegmentSwapStrategy, | ||
{ | ||
#[inline(always)] | ||
fn should_swap(&self, frames_in_wal: usize) -> bool { | ||
self.0.should_swap(frames_in_wal) || self.1.should_swap(frames_in_wal) | ||
} | ||
|
||
#[inline(always)] | ||
fn swapped(&self) { | ||
self.0.swapped(); | ||
self.1.swapped(); | ||
} | ||
} |
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