@@ -27,6 +27,7 @@ use crate::linc::bus::Dispatch;
27
27
use crate :: linc:: proto:: { Frames , Message } ;
28
28
use crate :: linc:: { Inbound , NodeId } ;
29
29
use crate :: meta:: DatabaseId ;
30
+ use crate :: replica_commit_store:: ReplicaCommitStore ;
30
31
31
32
use self :: config:: { AllocConfig , DbConfig } ;
32
33
use self :: primary:: compactor:: Compactor ;
@@ -97,8 +98,14 @@ impl Database {
97
98
98
99
fn txn_timeout_duration ( & self ) -> Duration {
99
100
match self {
100
- Database :: Primary { transaction_timeout_duration, .. } => * transaction_timeout_duration,
101
- Database :: Replica { transaction_timeout_duration, .. } => * transaction_timeout_duration,
101
+ Database :: Primary {
102
+ transaction_timeout_duration,
103
+ ..
104
+ } => * transaction_timeout_duration,
105
+ Database :: Replica {
106
+ transaction_timeout_duration,
107
+ ..
108
+ } => * transaction_timeout_duration,
102
109
}
103
110
}
104
111
}
@@ -109,6 +116,7 @@ impl Database {
109
116
path : PathBuf ,
110
117
dispatcher : Arc < dyn Dispatch > ,
111
118
compaction_queue : Arc < CompactionQueue > ,
119
+ replica_commit_store : Arc < ReplicaCommitStore > ,
112
120
) -> Self {
113
121
let database_id = DatabaseId :: from_name ( & config. db_name ) ;
114
122
@@ -118,7 +126,7 @@ impl Database {
118
126
replication_log_compact_interval,
119
127
transaction_timeout_duration,
120
128
} => {
121
- let ( sender, receiver) = tokio:: sync:: watch:: channel ( 0 ) ;
129
+ let ( sender, receiver) = tokio:: sync:: watch:: channel ( None ) ;
122
130
let db = LibsqlDatabase :: new_primary (
123
131
path,
124
132
Compactor :: new (
@@ -129,7 +137,7 @@ impl Database {
129
137
) ,
130
138
false ,
131
139
Box :: new ( move |fno| {
132
- let _ = sender. send ( fno) ;
140
+ let _ = sender. send ( Some ( fno) ) ;
133
141
} ) ,
134
142
)
135
143
. unwrap ( ) ;
@@ -156,8 +164,22 @@ impl Database {
156
164
proxy_request_timeout_duration,
157
165
transaction_timeout_duration,
158
166
} => {
159
- let rdb =
160
- LibsqlDatabase :: new_replica ( path, MAX_INJECTOR_BUFFER_CAPACITY , ( ) ) . unwrap ( ) ;
167
+ let next_frame_no =
168
+ block_in_place ( || replica_commit_store. get_commit_index ( database_id) )
169
+ . map ( |fno| fno + 1 )
170
+ . unwrap_or ( 0 ) ;
171
+
172
+ let commit_callback = Arc :: new ( move |fno| {
173
+ replica_commit_store. commit ( database_id, fno) ;
174
+ } ) ;
175
+
176
+ let rdb = LibsqlDatabase :: new_replica (
177
+ path,
178
+ MAX_INJECTOR_BUFFER_CAPACITY ,
179
+ commit_callback,
180
+ )
181
+ . unwrap ( ) ;
182
+
161
183
let wdb = RemoteDb {
162
184
proxy_request_timeout_duration,
163
185
} ;
@@ -167,7 +189,7 @@ impl Database {
167
189
168
190
let replicator = Replicator :: new (
169
191
dispatcher,
170
- 0 ,
192
+ next_frame_no ,
171
193
database_id,
172
194
primary_node_id,
173
195
injector,
@@ -556,9 +578,10 @@ mod test {
556
578
use tokio:: sync:: Notify ;
557
579
558
580
use crate :: allocation:: replica:: ReplicaConnection ;
559
- use crate :: init_dirs;
560
581
use crate :: linc:: bus:: Bus ;
582
+ use crate :: replica_commit_store:: ReplicaCommitStore ;
561
583
use crate :: snapshot_store:: SnapshotStore ;
584
+ use crate :: { init_dirs, replica_commit_store} ;
562
585
563
586
use super :: * ;
564
587
@@ -567,7 +590,8 @@ mod test {
567
590
let bus = Arc :: new ( Bus :: new ( 0 , |_, _| async { } ) ) ;
568
591
let _queue = bus. connect ( 1 ) ; // pretend connection to node 1
569
592
let tmp = tempfile:: TempDir :: new ( ) . unwrap ( ) ;
570
- let read_db = LibsqlDatabase :: new_replica ( tmp. path ( ) . to_path_buf ( ) , 1 , ( ) ) . unwrap ( ) ;
593
+ let read_db =
594
+ LibsqlDatabase :: new_replica ( tmp. path ( ) . to_path_buf ( ) , 1 , Arc :: new ( |_| ( ) ) ) . unwrap ( ) ;
571
595
let write_db = RemoteDb {
572
596
proxy_request_timeout_duration : Duration :: from_millis ( 100 ) ,
573
597
} ;
@@ -631,16 +655,23 @@ mod test {
631
655
} ,
632
656
} ;
633
657
let ( sender, inbox) = mpsc:: channel ( 10 ) ;
634
- let env = EnvOpenOptions :: new ( ) . max_dbs ( 10 ) . map_size ( 4096 * 100 ) . open ( tmp. path ( ) ) . unwrap ( ) ;
658
+ let env = EnvOpenOptions :: new ( )
659
+ . max_dbs ( 10 )
660
+ . map_size ( 4096 * 100 )
661
+ . open ( tmp. path ( ) )
662
+ . unwrap ( ) ;
635
663
let store = Arc :: new ( SnapshotStore :: new ( tmp. path ( ) . to_path_buf ( ) , env. clone ( ) ) . unwrap ( ) ) ;
636
- let queue = Arc :: new ( CompactionQueue :: new ( env, tmp. path ( ) . to_path_buf ( ) , store) . unwrap ( ) ) ;
664
+ let queue =
665
+ Arc :: new ( CompactionQueue :: new ( env. clone ( ) , tmp. path ( ) . to_path_buf ( ) , store) . unwrap ( ) ) ;
666
+ let replica_commit_store = Arc :: new ( ReplicaCommitStore :: new ( env. clone ( ) ) ) ;
637
667
let mut alloc = Allocation {
638
668
inbox,
639
669
database : Database :: from_config (
640
670
& config,
641
671
tmp. path ( ) . to_path_buf ( ) ,
642
672
bus. clone ( ) ,
643
673
queue,
674
+ replica_commit_store,
644
675
) ,
645
676
connections_futs : JoinSet :: new ( ) ,
646
677
next_conn_id : 0 ,
@@ -656,14 +687,18 @@ mod test {
656
687
657
688
let ( snd, rcv) = oneshot:: channel ( ) ;
658
689
let builder = StepResultsBuilder :: new ( snd) ;
659
- conn. execute ( Program :: seq ( & [ "begin" ] ) , Box :: new ( builder) ) . await . unwrap ( ) ;
690
+ conn. execute ( Program :: seq ( & [ "begin" ] ) , Box :: new ( builder) )
691
+ . await
692
+ . unwrap ( ) ;
660
693
rcv. await . unwrap ( ) . unwrap ( ) ;
661
694
662
695
tokio:: time:: sleep ( Duration :: from_secs ( 1 ) ) . await ;
663
696
664
697
let ( snd, rcv) = oneshot:: channel ( ) ;
665
698
let builder = StepResultsBuilder :: new ( snd) ;
666
- conn. execute ( Program :: seq ( & [ "create table test (x)" ] ) , Box :: new ( builder) ) . await . unwrap ( ) ;
699
+ conn. execute ( Program :: seq ( & [ "create table test (x)" ] ) , Box :: new ( builder) )
700
+ . await
701
+ . unwrap ( ) ;
667
702
assert ! ( rcv. await . unwrap( ) . is_err( ) ) ;
668
703
}
669
704
}
0 commit comments