@@ -4,16 +4,18 @@ use std::sync::Arc;
44use anyhow:: { Context , Result , anyhow, bail, ensure} ;
55use depot:: {
66 cold_tier:: ColdTier ,
7+ conveyer:: branch:: resolve_database_branch,
78 keys,
89 ltx:: { DecodedLtx , decode_ltx_v3} ,
910 types:: {
10- BranchState , ColdShardRef , CommitRow , DatabaseBranchId , decode_cold_shard_ref,
11+ BranchState , BucketId , ColdShardRef , CommitRow , DatabaseBranchId , decode_cold_shard_ref,
1112 decode_commit_row, decode_compaction_root, decode_database_branch_record,
1213 decode_database_pointer, decode_db_head, decode_db_history_pin,
1314 decode_pitr_interval_coverage, decode_retired_cold_object, decode_sqlite_cmp_dirty,
1415 } ,
1516} ;
1617use futures_util:: TryStreamExt ;
18+ use rivet_pools:: __rivet_util:: Id ;
1719use sha2:: { Digest , Sha256 } ;
1820use universaldb:: {
1921 RangeOption ,
@@ -119,15 +121,22 @@ impl<'a> InvariantScan<'a> {
119121 }
120122
121123 async fn check_database_pointer ( & mut self ) -> Result < Option < DatabaseBranchId > > {
122- let mut current = None ;
124+ let resolved = resolve_database_branch (
125+ self . tx ,
126+ BucketId :: from_gas_id ( Id :: nil ( ) ) ,
127+ & self . database_id ,
128+ Serializable ,
129+ )
130+ . await ?;
131+ let mut scanned_current = None ;
123132 for ( key, value) in scan_prefix ( self . tx , keys:: database_pointer_cur_prefix ( ) ) . await ? {
124133 let decoded_key = keys:: decode_database_pointer_cur_key ( & key) ;
125134 let pointer = decode_database_pointer ( & value) ;
126135 match ( decoded_key, pointer) {
127136 ( Ok ( ( _bucket_branch_id, database_id) ) , Ok ( pointer) )
128137 if database_id == self . database_id =>
129138 {
130- if current . replace ( pointer. current_branch ) . is_some ( ) {
139+ if scanned_current . replace ( pointer. current_branch ) . is_some ( ) {
131140 self . violate ( "database pointer appeared more than once" ) ;
132141 }
133142 }
@@ -141,13 +150,16 @@ impl<'a> InvariantScan<'a> {
141150 }
142151 }
143152
144- if current. is_none ( ) {
145- self . violate ( format ! (
146- "database pointer for {} is missing" ,
147- self . database_id
148- ) ) ;
153+ let Some ( current) = resolved else {
154+ self . violate ( format ! ( "database pointer for {} is missing" , self . database_id) ) ;
155+ return Ok ( None ) ;
156+ } ;
157+ if let Some ( scanned_current) = scanned_current
158+ && scanned_current != current
159+ {
160+ self . violate ( "database pointer scan disagreed with branch resolution" ) ;
149161 }
150- Ok ( current)
162+ Ok ( Some ( current) )
151163 }
152164
153165 async fn check_branch_record ( & mut self , branch_id : DatabaseBranchId ) -> Result < ( ) > {
0 commit comments