@@ -9,6 +9,7 @@ use pyo3::prelude::{
99} ;
1010use pyo3:: types:: { PyDict , PyDictMethods , PyTuple , PyTupleMethods } ;
1111use pyo3:: { IntoPyObjectExt , pyclass, pyfunction, wrap_pyfunction} ;
12+ use sqlbuild_cache:: artifacts:: constants:: SQL_TEST_CACHE_FILE ;
1213use sqlbuild_cache:: artifacts:: main:: new_sql_test_record:: new_sql_test_record;
1314use sqlbuild_cache:: artifacts:: main:: reusable_sql_test_record:: reusable_sql_test_record;
1415use sqlbuild_cache:: artifacts:: models:: { SqlTestArtifactRecord , SqlTestRecordMiss } ;
@@ -66,6 +67,10 @@ const SQL_FILE_SUFFIX: &str = ".sql";
6667const SQL_TEST_CACHE_LOGGER : & str = "sqlbuild.compile.reuse" ;
6768/// The miss reason of a SQL test the cache file holds no record for.
6869const NOT_RECORDED : & str = "not_recorded" ;
70+ /// Missed records a cache-miss debug line names before summarising the rest.
71+ const LOGGED_MISSES : usize = 16 ;
72+ /// Leading characters of a record key that name it in a cache-miss debug line.
73+ const LOGGED_KEY_CHARS : usize = 12 ;
6974/// `functions/<language>` path components that already lead a function's path.
7075const LANGUAGE_DIR_DEPTH : usize = 2 ;
7176const PUBLISHED_STAGING_CHANGED : & str = "changed" ;
@@ -453,20 +458,43 @@ fn test_identities<'py>(
453458 Ok ( identities)
454459}
455460
456- /// Log why a warm SQL test artifact cache could not keep some artifacts, by reason.
457- fn log_cache_misses ( py : Python < ' _ > , misses : & BTreeMap < & ' static str , usize > ) -> PyResult < ( ) > {
461+ /// Log a warm SQL test cache's misses: counts by reason, then each record's key and reason.
462+ fn log_cache_misses (
463+ py : Python < ' _ > ,
464+ misses : & BTreeMap < & ' static str , usize > ,
465+ missed : & [ String ] ,
466+ unreadable : bool ,
467+ ) -> PyResult < ( ) > {
458468 let reasons: String = misses
459469 . iter ( )
460470 . map ( |( reason, count) | format ! ( "{reason}={count}" ) )
461471 . collect :: < Vec < _ > > ( )
462472 . join ( "," ) ;
473+ let detail: String = if unreadable {
474+ "the cache file held no usable records" . to_owned ( )
475+ } else {
476+ let mut shown: Vec < String > = missed. iter ( ) . take ( LOGGED_MISSES ) . cloned ( ) . collect ( ) ;
477+ if missed. len ( ) > LOGGED_MISSES {
478+ shown. push ( format ! ( "{} more" , missed. len( ) - LOGGED_MISSES ) ) ;
479+ }
480+ format ! ( "records {}" , shown. join( "," ) )
481+ } ;
463482 host:: log_debug_message (
464483 py,
465484 SQL_TEST_CACHE_LOGGER ,
466- & format ! ( "SQL test artifact cache missed: {reasons}" ) ,
485+ & format ! ( "SQL test artifact cache missed: {reasons} ({detail}) " ) ,
467486 )
468487}
469488
489+ /// Whether `cache_dir` holds a SQL test cache file, so an empty read means it was unreadable.
490+ fn cache_file_present ( cache_dir : & Bound < ' _ , PyAny > ) -> PyResult < bool > {
491+ if cache_dir. is_none ( ) {
492+ return Ok ( false ) ;
493+ }
494+ let directory: PathBuf = cache_dir. extract ( ) ?;
495+ Ok ( directory. join ( SQL_TEST_CACHE_FILE ) . is_file ( ) )
496+ }
497+
470498/// Plan every SQL test whose cached artifact under `target_dir` cannot be reused.
471499pub ( crate ) fn plan_tests < ' py > (
472500 target_dir : & Bound < ' py , PyAny > ,
@@ -503,6 +531,7 @@ pub(crate) fn plan_tests<'py>(
503531 let mut cached_paths: Vec < PathBuf > = Vec :: new ( ) ;
504532 let mut cached_records: Vec < ( String , SqlTestArtifactRecord ) > = Vec :: new ( ) ;
505533 let mut misses: BTreeMap < & ' static str , usize > = BTreeMap :: new ( ) ;
534+ let mut missed: Vec < String > = Vec :: new ( ) ;
506535 for ( index, ( test, identity) ) in tests. into_iter ( ) . zip ( identities) . enumerate ( ) {
507536 let Some ( ( record_key, identity) ) = identity else {
508537 pending. push ( ( test. unbind ( ) , None , None ) ) ;
@@ -517,6 +546,8 @@ pub(crate) fn plan_tests<'py>(
517546 } ) ;
518547 if let Err ( reason) = reusable {
519548 * misses. entry ( reason) . or_default ( ) += 1 ;
549+ let short: & str = record_key. get ( ..LOGGED_KEY_CHARS ) . unwrap_or ( & record_key) ;
550+ missed. push ( format ! ( "{short}:{reason}" ) ) ;
520551 }
521552 if let Ok ( record) = reusable {
522553 let path = tests_root_path. join ( & record. relative_path ) ;
@@ -528,8 +559,8 @@ pub(crate) fn plan_tests<'py>(
528559 }
529560 yield_interpreter ( py, index) ;
530561 }
531- if !cached . is_empty ( ) && !misses . is_empty ( ) {
532- log_cache_misses ( py, & misses) ?;
562+ if !misses . is_empty ( ) && ( !cached . is_empty ( ) || cache_file_present ( & cache_dir ) ? ) {
563+ log_cache_misses ( py, & misses, & missed , cached . is_empty ( ) ) ?;
533564 }
534565 let pending_tests: Vec < Bound < ' py , PyAny > > = pending
535566 . iter ( )
0 commit comments