From b388829a96ce05d32eda8e44e75026b24bb50766 Mon Sep 17 00:00:00 2001 From: Yingwen Date: Fri, 2 Aug 2024 14:16:37 +0800 Subject: [PATCH] fix: avoid total size overflow (#4487) feat: avoid total size overflow --- src/mito2/src/cache/file_cache.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mito2/src/cache/file_cache.rs b/src/mito2/src/cache/file_cache.rs index 008a717593..b6d667ea04 100644 --- a/src/mito2/src/cache/file_cache.rs +++ b/src/mito2/src/cache/file_cache.rs @@ -199,7 +199,9 @@ impl FileCache { .metakey(Metakey::ContentLength) .await .context(OpenDalSnafu)?; - let (mut total_size, mut total_keys) = (0, 0); + // Use i64 for total_size to reduce the risk of overflow. + // It is possible that the total size of the cache is larger than i32::MAX. + let (mut total_size, mut total_keys) = (0i64, 0); while let Some(entry) = lister.try_next().await.context(OpenDalSnafu)? { let meta = entry.metadata(); if !meta.is_file() { @@ -212,13 +214,11 @@ impl FileCache { self.memory_index .insert(key, IndexValue { file_size }) .await; - total_size += file_size; + total_size += i64::from(file_size); total_keys += 1; } // The metrics is a signed int gauge so we can updates it finally. - CACHE_BYTES - .with_label_values(&[FILE_TYPE]) - .add(total_size.into()); + CACHE_BYTES.with_label_values(&[FILE_TYPE]).add(total_size); info!( "Recovered file cache, num_keys: {}, num_bytes: {}, cost: {:?}",