@@ -14,6 +14,10 @@ use sqlbuild_orchestration::api::errors::ClientError;
1414use sqlbuild_orchestration:: api:: main:: serve_api:: serve_api;
1515
1616use crate :: adhoc:: main:: adhoc_planner:: adhoc_planner;
17+ use sqlbuild_config:: project:: main:: load_control_config:: load_control_config;
18+ use sqlbuild_config:: project:: main:: project_config_paths:: project_config_paths;
19+ use sqlbuild_config:: project:: main:: sync_interval_seconds:: sync_interval_seconds;
20+ use sqlbuild_config:: project:: models:: ControlConfig ;
1721use sqlbuild_orchestration:: api:: models:: {
1822 ApiAuthentication , DevelopmentScope , HumanLogin , ServerFacts ,
1923} ;
@@ -73,17 +77,17 @@ use crate::native_commands::_helpers::terminal::{
7377use crate :: native_commands:: constants:: {
7478 ADMIN_COMMAND , COLUMNS_VARIABLE , FALLBACK_COLUMNS , METADATA_OVERLAP_SECONDS ,
7579 METADATA_POLL_SECONDS , METADATA_READER_CLOSE_SECONDS , METADATA_RECONCILE_SECONDS ,
76- METADATA_REPLY_BYTES , METADATA_SYNC_CHECK_SECONDS , SCHEDULER_COMMAND ,
77- UNCAUGHT_EXCEPTION_STATUS , UNKNOWN_COMMAND ,
80+ METADATA_REPLY_BYTES , METADATA_SAFETY_SYNC_SECONDS , METADATA_SYNC_CHECK_SECONDS ,
81+ SAFETY_SYNC_ENV , SCHEDULER_COMMAND , UNCAUGHT_EXCEPTION_STATUS , UNKNOWN_COMMAND ,
7882} ;
7983use crate :: native_commands:: errors:: PythonNeeded ;
8084use crate :: native_commands:: main:: _run_admin:: run_admin;
8185use crate :: native_commands:: main:: _run_control:: run_control;
8286use crate :: native_commands:: main:: _run_cost:: run_cost;
8387use crate :: native_commands:: models:: {
8488 CommandOutput , InstalledPackage , LocationCompileRequest , LocationCompiler ,
85- LocationMetadataPage , LocationMetadataSync , LocationServe , LostStdout , NativeCommand ,
86- NativeInvocation , ProjectLocation , ScanToken ,
89+ LocationMetadataPage , LocationMetadataSync , LocationServe , LostStdout , MetadataReply ,
90+ NativeCommand , NativeInvocation , ProjectLocation , ScanToken ,
8791} ;
8892use crate :: native_commands:: types:: LocationPublication ;
8993use crate :: workers:: main:: docker_image_sources:: docker_image_sources;
@@ -335,7 +339,7 @@ pub(crate) fn location_result<T: serde::de::DeserializeOwned>(
335339
336340pub ( crate ) fn metadata_sync (
337341 ( locations, generation) : ( Vec < WarehouseStateLocation > , i64 ) ,
338- reconcile_seconds : u32 ,
342+ ( reconcile_seconds, safety_seconds ) : ( u32 , u64 ) ,
339343) -> LocationMetadataSync {
340344 LocationMetadataSync {
341345 locations,
@@ -355,6 +359,9 @@ pub(crate) fn metadata_sync(
355359 next_at : Instant :: now ( ) ,
356360 idle : true ,
357361 cycle_started_at : None ,
362+ safety_seconds,
363+ safety_at : Instant :: now ( ) + Duration :: from_secs ( safety_seconds) ,
364+ busy : false ,
358365 change_tokens : HashMap :: new ( ) ,
359366 scan_token : None ,
360367 scan_delivered : false ,
@@ -483,15 +490,9 @@ pub(crate) async fn begin_after_run(
483490 Ok ( due) => due,
484491 Err ( error) => return ( state, Err ( error) ) ,
485492 } ;
486- let ran = match ( state. cycle_started_at , due. runs_ended_at ) {
487- ( None , _) => true ,
488- ( Some ( started_at) , Some ( ended_at) ) => ended_at >= started_at,
489- ( Some ( _) , None ) => false ,
490- } ;
493+ let ran = state. sync_due ( due. runs_ended_at ) ;
491494 if ran {
492- state. idle = false ;
493- state. cycle_started_at = Some ( due. now ) ;
494- state. next_at = Instant :: now ( ) ;
495+ state. begin_sync ( due. now ) ;
495496 }
496497 ( state, Ok ( ran) )
497498}
@@ -589,6 +590,7 @@ pub(crate) fn next_scan(mut state: LocationMetadataSync) -> LocationMetadataSync
589590 }
590591 state. idle = true ;
591592 state. next_at = Instant :: now ( ) ;
593+ state. safety_at = Instant :: now ( ) + Duration :: from_secs ( state. safety_seconds ) ;
592594 drop ( write_output ( & [ (
593595 Stream :: Stderr ,
594596 "Warehouse history synchronization completed.\n " ,
@@ -699,16 +701,56 @@ pub(crate) fn location_options(namespace: &Namespace) -> Result<LocationServe, C
699701 "SQLBUILD_METADATA_RECONCILE_SECONDS must be a positive integer" . into ( ) ,
700702 )
701703 } ) ?;
704+ let environment = match std:: env:: var ( SAFETY_SYNC_ENV ) {
705+ Ok ( value) => Some ( value) ,
706+ Err ( std:: env:: VarError :: NotPresent ) => None ,
707+ Err ( std:: env:: VarError :: NotUnicode ( _) ) => {
708+ return Err ( ControlError :: Invalid ( format ! (
709+ "{SAFETY_SYNC_ENV} must be text"
710+ ) ) ) ;
711+ }
712+ } ;
713+ let safety_sync_seconds = safety_sync_seconds (
714+ text ( namespace, "location_sync_at_least_every" ) . as_deref ( ) ,
715+ environment. as_deref ( ) ,
716+ || {
717+ load_control_config ( & project_config_paths ( & project_dir) ) . map_err ( |failure| {
718+ failure. into_config_failure ( ) . map_or_else (
719+ || "sqlbuild_project.toml could not be read" . to_owned ( ) ,
720+ |failure| failure. render ( |_| "sqlbuild_project.toml" . to_owned ( ) ) ,
721+ )
722+ } )
723+ } ,
724+ )
725+ . map_err ( ControlError :: Invalid ) ?;
702726 Ok ( LocationServe {
703727 project_dir,
704728 server_url : text ( namespace, "location_server_url" ) . unwrap_or_default ( ) ,
705729 location_id,
706730 target,
707731 image_id : text ( namespace, "location_image_id" ) ,
708732 metadata_reconcile_seconds,
733+ safety_sync_seconds,
709734 } )
710735}
711736
737+ /// The safety sync interval from the flag, then the environment, then the project, then thirty minutes.
738+ pub ( crate ) fn safety_sync_seconds (
739+ flag : Option < & str > ,
740+ environment : Option < & str > ,
741+ project : impl FnOnce ( ) -> Result < ControlConfig , String > ,
742+ ) -> Result < u64 , String > {
743+ if let Some ( text) = flag {
744+ return sync_interval_seconds ( text) . map_err ( |help| format ! ( "--sync-at-least-every {help}" ) ) ;
745+ }
746+ if let Some ( text) = environment. filter ( |text| !text. trim ( ) . is_empty ( ) ) {
747+ return sync_interval_seconds ( text) . map_err ( |help| format ! ( "{SAFETY_SYNC_ENV} {help}" ) ) ;
748+ }
749+ Ok ( project ( ) ?
750+ . sync_at_least_every_seconds
751+ . unwrap_or ( METADATA_SAFETY_SYNC_SECONDS ) )
752+ }
753+
712754/// Reconcile from the server after restarts or lost acknowledgements; transient outages retry.
713755pub ( crate ) async fn location_generation (
714756 options : & LocationServe ,
@@ -1152,6 +1194,7 @@ pub(crate) async fn read_metadata_page(
11521194 mut state : LocationMetadataSync ,
11531195 stopped : impl Future < Output = Result < ( ) , ControlError > > ,
11541196) -> Result < Option < ( LocationMetadataSync , Option < WarehouseHistoryPage > ) > , ControlError > {
1197+ state. busy = false ;
11551198 if let Some ( statistics) = & state. statistics
11561199 && statistics. names . is_empty ( )
11571200 {
@@ -1181,10 +1224,15 @@ pub(crate) async fn read_metadata_page(
11811224 Duration :: from_secs( METADATA_POLL_SECONDS ) ,
11821225 exchange_metadata_read( reader, request) ,
11831226 ) => match result {
1184- Ok ( ( reader, Ok ( page) ) ) => {
1227+ Ok ( ( reader, Ok ( MetadataReply :: Page ( page) ) ) ) => {
11851228 state. reader = Some ( reader) ;
11861229 Ok ( Some ( ( state, page) ) )
11871230 }
1231+ Ok ( ( reader, Ok ( MetadataReply :: Busy ) ) ) => {
1232+ state. busy = true ;
1233+ state. reader = Some ( reader) ;
1234+ Ok ( Some ( ( state, None ) ) )
1235+ }
11881236 Ok ( ( reader, Err ( _) ) ) => {
11891237 stop_location_runtime( Some ( reader) ) . await ?;
11901238 Ok ( Some ( ( state, None ) ) )
@@ -1230,10 +1278,7 @@ pub(crate) async fn close_metadata_reader(
12301278async fn exchange_metadata_read (
12311279 mut reader : LocationCompiler ,
12321280 request : Vec < u8 > ,
1233- ) -> (
1234- LocationCompiler ,
1235- Result < Option < WarehouseHistoryPage > , ControlError > ,
1236- ) {
1281+ ) -> ( LocationCompiler , Result < MetadataReply , ControlError > ) {
12371282 let result = async {
12381283 reader. stdin . write_all ( & request) . await ?;
12391284 reader. stdin . write_all ( b"\n " ) . await ?;
@@ -1253,7 +1298,13 @@ async fn exchange_metadata_read(
12531298 "location reply exceeds its byte limit" . into ( ) ,
12541299 ) ) ;
12551300 }
1256- serde_json:: from_str ( & line)
1301+ let reply: serde_json:: Value = serde_json:: from_str ( & line)
1302+ . map_err ( |_| ControlError :: Invalid ( "invalid location reply" . into ( ) ) ) ?;
1303+ if reply. get ( "busy" ) == Some ( & serde_json:: Value :: Bool ( true ) ) {
1304+ return Ok ( MetadataReply :: Busy ) ;
1305+ }
1306+ serde_json:: from_value ( reply)
1307+ . map ( MetadataReply :: Page )
12571308 . map_err ( |_| ControlError :: Invalid ( "invalid location reply" . into ( ) ) )
12581309 }
12591310 . await ;
0 commit comments