1- use std:: {
2- cmp:: max,
3- path:: PathBuf ,
4- sync:: Arc ,
5- thread:: { JoinHandle , available_parallelism, spawn} ,
6- } ;
1+ use std:: { cmp:: max, path:: PathBuf , sync:: Arc , thread:: available_parallelism, time:: Instant } ;
72
83use anyhow:: { Ok , Result } ;
94use parking_lot:: Mutex ;
105use turbo_persistence:: {
116 ArcSlice , CompactConfig , KeyBase , StoreKey , TurboPersistence , ValueBuffer ,
127} ;
8+ use turbo_tasks:: { JoinHandle , message_queue:: TimingEvent , spawn, turbo_tasks} ;
139
1410use crate :: database:: {
1511 key_value_database:: { KeySpace , KeyValueDatabase } ,
@@ -84,7 +80,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
8480 ) -> Result < WriteBatch < ' _ , Self :: SerialWriteBatch < ' _ > , Self :: ConcurrentWriteBatch < ' _ > > > {
8581 // Wait for the compaction to finish
8682 if let Some ( join_handle) = self . compact_join_handle . lock ( ) . take ( ) {
87- join_handle. join ( ) . unwrap ( ) ?;
83+ join_handle. join ( ) ?;
8884 }
8985 // Start a new write batch
9086 Ok ( WriteBatch :: concurrent ( TurboWriteBatch {
@@ -100,23 +96,44 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
10096 fn shutdown ( & self ) -> Result < ( ) > {
10197 // Wait for the compaction to finish
10298 if let Some ( join_handle) = self . compact_join_handle . lock ( ) . take ( ) {
103- join_handle. join ( ) . unwrap ( ) ?;
99+ join_handle. join ( ) ?;
104100 }
105101 // Compact the database on shutdown
106- self . db . compact ( & CompactConfig {
107- max_merge_segment_count : if self . is_ci {
108- // Fully compact in CI to reduce cache size
109- usize:: MAX
110- } else {
111- available_parallelism ( ) . map_or ( 4 , |c| max ( 4 , c. get ( ) ) )
112- } ,
113- ..COMPACT_CONFIG
114- } ) ?;
102+ if self . is_ci {
103+ // Fully compact in CI to reduce cache size
104+ do_compact ( & self . db , "Finished database compaction" , usize:: MAX ) ?;
105+ } else {
106+ // Compact with a reasonable limit in non-CI environments
107+ do_compact (
108+ & self . db ,
109+ "Finished database compaction" ,
110+ available_parallelism ( ) . map_or ( 4 , |c| max ( 4 , c. get ( ) ) ) ,
111+ ) ?;
112+ }
115113 // Shutdown the database
116114 self . db . shutdown ( )
117115 }
118116}
119117
118+ fn do_compact (
119+ db : & TurboPersistence ,
120+ message : & ' static str ,
121+ max_merge_segment_count : usize ,
122+ ) -> Result < ( ) > {
123+ let start = Instant :: now ( ) ;
124+ // Compact the database with the given max merge segment count
125+ let ran = db. compact ( & CompactConfig {
126+ max_merge_segment_count,
127+ ..COMPACT_CONFIG
128+ } ) ?;
129+ if ran {
130+ let elapsed = start. elapsed ( ) ;
131+ turbo_tasks ( )
132+ . send_compilation_event ( Arc :: new ( TimingEvent :: new ( message. to_string ( ) , elapsed) ) ) ;
133+ }
134+ Ok ( ( ) )
135+ }
136+
120137pub struct TurboWriteBatch < ' a > {
121138 batch : turbo_persistence:: WriteBatch < WriteBuffer < ' static > , 5 > ,
122139 db : & ' a Arc < TurboPersistence > ,
@@ -144,12 +161,12 @@ impl<'a> BaseWriteBatch<'a> for TurboWriteBatch<'a> {
144161 if let Some ( compact_join_handle) = self . compact_join_handle {
145162 // Start a new compaction in the background
146163 let db = self . db . clone ( ) ;
147- let handle = spawn ( move || {
148- db . compact ( & CompactConfig {
149- max_merge_segment_count : available_parallelism ( )
150- . map_or ( 4 , |c| max ( 4 , c . get ( ) / 2 ) ) ,
151- .. COMPACT_CONFIG
152- } )
164+ let handle = spawn ( async move {
165+ do_compact (
166+ & db ,
167+ "Finished database compaction" ,
168+ available_parallelism ( ) . map_or ( 4 , |c| max ( 4 , c . get ( ) / 2 ) ) ,
169+ )
153170 } ) ;
154171 compact_join_handle. lock ( ) . replace ( handle) ;
155172 }
0 commit comments