@@ -6,6 +6,7 @@ use std::sync::Arc;
6
6
use dashmap:: DashMap ;
7
7
use libsql_sys:: ffi:: Sqlite3DbHeader ;
8
8
use parking_lot:: { Condvar , Mutex } ;
9
+ use rand:: Rng ;
9
10
use tokio:: sync:: { mpsc, Notify , Semaphore } ;
10
11
use tokio:: task:: JoinSet ;
11
12
use zerocopy:: { AsBytes , FromZeroes } ;
@@ -32,7 +33,7 @@ enum Slot<IO: Io> {
32
33
33
34
/// Wal Registry maintains a set of shared Wal, and their respective set of files.
34
35
pub struct WalRegistry < IO : Io , S > {
35
- fs : IO ,
36
+ io : IO ,
36
37
path : PathBuf ,
37
38
shutdown : AtomicBool ,
38
39
opened : DashMap < NamespaceName , Slot < IO > > ,
@@ -59,7 +60,7 @@ impl<IO: Io, S> WalRegistry<IO, S> {
59
60
) -> Result < Self > {
60
61
io. create_dir_all ( & path) ?;
61
62
let registry = Self {
62
- fs : io,
63
+ io,
63
64
path,
64
65
opened : Default :: default ( ) ,
65
66
shutdown : Default :: default ( ) ,
@@ -105,14 +106,15 @@ where
105
106
. join ( shared. namespace ( ) . as_str ( ) )
106
107
. join ( format ! ( "{}:{start_frame_no:020}.seg" , shared. namespace( ) ) ) ;
107
108
108
- let segment_file = self . fs . open ( true , true , true , & path) ?;
109
-
109
+ let segment_file = self . io . open ( true , true , true , & path) ?;
110
+ let salt = self . io . with_rng ( |rng| rng . gen ( ) ) ;
110
111
let new = CurrentSegment :: create (
111
112
segment_file,
112
113
path,
113
114
start_frame_no,
114
115
current. db_size ( ) ,
115
116
current. tail ( ) . clone ( ) ,
117
+ salt,
116
118
) ?;
117
119
// sealing must the last fallible operation, because we don't want to end up in a situation
118
120
// where the current log is sealed and it wasn't swapped.
@@ -126,9 +128,8 @@ where
126
128
update_durable ( fno, notifier, durable_frame_no, namespace) . await ;
127
129
} )
128
130
} ) ;
129
- self . storage
130
- . store ( & shared. namespace , sealed. clone ( ) , None , cb) ;
131
- new. tail ( ) . push ( sealed) ;
131
+ new. tail ( ) . push ( sealed. clone ( ) ) ;
132
+ self . storage . store ( & shared. namespace , sealed, None , cb) ;
132
133
}
133
134
134
135
shared. current . swap ( Arc :: new ( new) ) ;
@@ -226,7 +227,7 @@ where
226
227
db_path : & Path ,
227
228
) -> Result < Arc < SharedWal < IO > > > {
228
229
let path = self . path . join ( namespace. as_str ( ) ) ;
229
- self . fs . create_dir_all ( & path) ?;
230
+ self . io . create_dir_all ( & path) ?;
230
231
// TODO: handle that with abstract io
231
232
let dir = walkdir:: WalkDir :: new ( & path) . sort_by_file_name ( ) . into_iter ( ) ;
232
233
@@ -246,7 +247,7 @@ where
246
247
continue ;
247
248
}
248
249
249
- let file = self . fs . open ( false , true , true , entry. path ( ) ) ?;
250
+ let file = self . io . open ( false , true , true , entry. path ( ) ) ?;
250
251
251
252
if let Some ( sealed) =
252
253
SealedSegment :: open ( file. into ( ) , entry. path ( ) . to_path_buf ( ) , Default :: default ( ) ) ?
@@ -265,7 +266,7 @@ where
265
266
}
266
267
}
267
268
268
- let db_file = self . fs . open ( false , true , true , db_path) ?;
269
+ let db_file = self . io . open ( false , true , true , db_path) ?;
269
270
270
271
let mut header: Sqlite3DbHeader = Sqlite3DbHeader :: new_zeroed ( ) ;
271
272
db_file. read_exact_at ( header. as_bytes_mut ( ) , 0 ) ?;
@@ -283,14 +284,16 @@ where
283
284
284
285
let current_path = path. join ( format ! ( "{namespace}:{next_frame_no:020}.seg" ) ) ;
285
286
286
- let segment_file = self . fs . open ( true , true , true , & current_path) ?;
287
+ let segment_file = self . io . open ( true , true , true , & current_path) ?;
288
+ let salt = self . io . with_rng ( |rng| rng. gen ( ) ) ;
287
289
288
290
let current = arc_swap:: ArcSwap :: new ( Arc :: new ( CurrentSegment :: create (
289
291
segment_file,
290
292
current_path,
291
293
next_frame_no,
292
294
db_size,
293
295
tail. into ( ) ,
296
+ salt,
294
297
) ?) ) ;
295
298
296
299
let ( new_frame_notifier, _) = tokio:: sync:: watch:: channel ( next_frame_no. get ( ) - 1 ) ;
@@ -309,6 +312,7 @@ where
309
312
namespace. clone ( ) ,
310
313
) ) ,
311
314
shutdown : false . into ( ) ,
315
+ checkpoint_notifier : self . checkpoint_notifier . clone ( ) ,
312
316
} ) ;
313
317
314
318
self . opened
0 commit comments