@@ -857,6 +857,8 @@ impl RegionLoadCacheTask {
857857 }
858858 }
859859 }
860+ // Releases the Version after the scope to avoid holding the memtables and file handles
861+ // for a long time.
860862 }
861863 let total_files = files_to_download. len ( ) as i64 ;
862864
@@ -873,6 +875,14 @@ impl RegionLoadCacheTask {
873875 for ( puffin_key, file_size) in files_to_download {
874876 let current_size = file_cache. puffin_cache_size ( ) ;
875877 let capacity = file_cache. puffin_cache_capacity ( ) ;
878+ let region_state = self . region . state ( ) ;
879+ if !can_load_cache ( region_state) {
880+ info ! (
881+ "Stopping index cache by state: {:?}, region: {}, current_size: {}, capacity: {}" ,
882+ region_state, region_id, current_size, capacity
883+ ) ;
884+ break ;
885+ }
876886
877887 // Checks if adding this file would exceed capacity
878888 if current_size + file_size > capacity {
@@ -942,3 +952,16 @@ fn maybe_load_cache(
942952 let task = RegionLoadCacheTask :: new ( region. clone ( ) ) ;
943953 write_cache. load_region_cache ( task) ;
944954}
955+
956+ fn can_load_cache ( state : RegionRoleState ) -> bool {
957+ match state {
958+ RegionRoleState :: Leader ( RegionLeaderState :: Writable )
959+ | RegionRoleState :: Leader ( RegionLeaderState :: Staging )
960+ | RegionRoleState :: Leader ( RegionLeaderState :: Altering )
961+ | RegionRoleState :: Leader ( RegionLeaderState :: Downgrading )
962+ | RegionRoleState :: Leader ( RegionLeaderState :: Editing ) => true ,
963+ RegionRoleState :: Leader ( RegionLeaderState :: Dropping )
964+ | RegionRoleState :: Leader ( RegionLeaderState :: Truncating ) => true ,
965+ RegionRoleState :: Follower => true ,
966+ }
967+ }
0 commit comments