Skip to content

Commit 276ef5b

Browse files
committed
use swap strategy in SharedWal
1 parent f03ab8b commit 276ef5b

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

libsql-wal/src/registry.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::num::NonZeroU64;
33
use std::path::{Path, PathBuf};
44
use std::sync::atomic::{AtomicBool, Ordering};
55
use std::sync::Arc;
6-
use std::time::Instant;
6+
use std::time::{Duration, Instant};
77

88
use dashmap::DashMap;
99
use libsql_sys::ffi::Sqlite3DbHeader;
@@ -27,6 +27,9 @@ use crate::segment::Segment;
2727
use crate::segment::{current::CurrentSegment, sealed::SealedSegment};
2828
use crate::shared_wal::{SharedWal, SwapLog};
2929
use crate::storage::{OnStoreCallback, Storage};
30+
use crate::swap_strategy::duration::DurationSwapStrategy;
31+
use crate::swap_strategy::frame_count::FrameCountSwapStrategy;
32+
use crate::swap_strategy::SwapStrategy;
3033
use crate::transaction::TxGuard;
3134
use crate::{LibsqlFooter, LIBSQL_PAGE_SIZE};
3235
use libsql_sys::name::NamespaceName;
@@ -332,6 +335,17 @@ where
332335

333336
let (new_frame_notifier, _) = tokio::sync::watch::channel(next_frame_no.get() - 1);
334337

338+
// FIXME: make swap strategy configurable
339+
// This strategy will perform a swap if either the wal is bigger than 20k frames, or older
340+
// than 10 minutes, or if the frame count is greater than a 1000 and the wal was last
341+
// swapped more than 30 secs ago
342+
let swap_strategy = Box::new(
343+
DurationSwapStrategy::new(Duration::from_secs(5 * 60))
344+
.or(FrameCountSwapStrategy::new(20_000))
345+
.or(FrameCountSwapStrategy::new(1000)
346+
.and(DurationSwapStrategy::new(Duration::from_secs(30)))),
347+
);
348+
335349
let shared = Arc::new(SharedWal {
336350
current,
337351
wal_lock: Default::default(),
@@ -347,8 +361,8 @@ where
347361
)),
348362
shutdown: false.into(),
349363
checkpoint_notifier: self.checkpoint_notifier.clone(),
350-
max_segment_size: 1000.into(),
351364
io: self.io.clone(),
365+
swap_strategy,
352366
});
353367

354368
self.opened

libsql-wal/src/shared_wal.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::collections::BTreeMap;
2-
use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering};
2+
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
33
use std::sync::Arc;
44
use std::time::Instant;
55

@@ -16,6 +16,7 @@ use crate::io::file::FileExt;
1616
use crate::io::Io;
1717
use crate::replication::storage::ReplicateFromStorage;
1818
use crate::segment::current::CurrentSegment;
19+
use crate::swap_strategy::SwapStrategy;
1920
use crate::transaction::{ReadTransaction, Savepoint, Transaction, TxGuard, WriteTransaction};
2021
use libsql_sys::name::NamespaceName;
2122

@@ -46,15 +47,14 @@ pub struct SharedWal<IO: Io> {
4647
pub(crate) registry: Arc<dyn SwapLog<IO>>,
4748
#[allow(dead_code)] // used by replication
4849
pub(crate) checkpointed_frame_no: AtomicU64,
49-
/// max frame_no acknoledged by the durable storage
50+
/// max frame_no acknowledged by the durable storage
5051
pub(crate) durable_frame_no: Arc<Mutex<u64>>,
5152
pub(crate) new_frame_notifier: tokio::sync::watch::Sender<u64>,
5253
pub(crate) stored_segments: Box<dyn ReplicateFromStorage>,
5354
pub(crate) shutdown: AtomicBool,
5455
pub(crate) checkpoint_notifier: mpsc::Sender<CheckpointMessage>,
55-
/// maximum size the segment is allowed to grow
56-
pub(crate) max_segment_size: AtomicUsize,
5756
pub(crate) io: Arc<IO>,
57+
pub(crate) swap_strategy: Box<dyn SwapStrategy>,
5858
}
5959

6060
impl<IO: Io> SharedWal<IO> {
@@ -275,9 +275,10 @@ impl<IO: Io> SharedWal<IO> {
275275
}
276276

277277
if tx.is_commited()
278-
&& current.count_committed() > self.max_segment_size.load(Ordering::Relaxed)
278+
&& self.swap_strategy.should_swap(current.count_committed())
279279
{
280280
self.swap_current(&tx)?;
281+
self.swap_strategy.swapped();
281282
}
282283

283284
Ok(())

0 commit comments

Comments
 (0)