From 60d02f3afdaefeae206bb633b4f5b099237f32ae Mon Sep 17 00:00:00 2001 From: weisd Date: Wed, 18 Sep 2024 22:57:08 +0800 Subject: [PATCH] fix get_disk_id --- ecstore/src/disk/error.rs | 3 ++ ecstore/src/disk/local.rs | 85 ++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/ecstore/src/disk/error.rs b/ecstore/src/disk/error.rs index c8c0c84b..1b879d7a 100644 --- a/ecstore/src/disk/error.rs +++ b/ecstore/src/disk/error.rs @@ -34,6 +34,9 @@ pub enum DiskError { #[error("volume not empty")] VolumeNotEmpty, + + #[error("corrupted backend")] + CorruptedBackend } impl DiskError { diff --git a/ecstore/src/disk/local.rs b/ecstore/src/disk/local.rs index 5dfcb92d..3c34f336 100644 --- a/ecstore/src/disk/local.rs +++ b/ecstore/src/disk/local.rs @@ -26,17 +26,23 @@ use uuid::Uuid; #[derive(Debug)] pub struct FormatInfo { pub id: Option, - pub _data: Vec, - pub _file_info: Option, - pub _last_check: Option, + pub data: Vec, + pub file_info: Option, + pub last_check: Option, } -impl FormatInfo {} +impl FormatInfo { + pub fn last_check_valid(&self)->bool{ + let now = OffsetDateTime::now_utc(); + self.file_info.is_some() && self.id.is_some() && self.last_check.is_some() && (now.unix_timestamp() - self.last_check.unwrap().unix_timestamp() <=1) + } + +} #[derive(Debug)] pub struct LocalDisk { pub root: PathBuf, - pub _format_path: PathBuf, + pub format_path: PathBuf, pub format_info: Mutex, // pub id: Mutex>, // pub format_data: Mutex>, @@ -79,14 +85,14 @@ impl LocalDisk { let format_info = FormatInfo { id, - _data: format_data, - _file_info: format_meta, - _last_check: format_last_check, + data: format_data, + file_info: format_meta, + last_check: format_last_check, }; let disk = Self { root, - _format_path: format_path, + format_path: format_path, format_info: Mutex::new(format_info), // // format_legacy, // format_file_info: Mutex::new(format_meta), @@ -99,6 +105,17 @@ impl LocalDisk { Ok(disk) } + async fn check_format_json(&self) ->Result{ + let p = self.format_path; + let md = fs::metadata(&p).await.map_err(|e|match e.kind(){ + ErrorKind::NotFound => DiskError::DiskNotFound, + ErrorKind::PermissionDenied => DiskError::FileAccessDenied, + _ => { + warn!("check_format_json err {:?}",e); + DiskError::CorruptedBackend}, + })?; + Ok(md) + } async fn make_meta_volumes(&self) -> Result<()> { let buckets = format!("{}/{}", super::RUSTFS_META_BUCKET, super::BUCKET_META_PREFIX); let multipart = format!("{}/{}", super::RUSTFS_META_BUCKET, "multipart"); @@ -441,9 +458,55 @@ impl DiskAPI for LocalDisk { async fn get_disk_id(&self) -> Option { warn!("local get_disk_id"); // TODO: check format file - let format_info = self.format_info.lock().await; + let mut format_info = self.format_info.lock().await; - format_info.id.clone() + let id = format_info.id.clone(); + + if format_info.last_check_valid(){ + return id + } + + let file_meta = self.check_format_json().await?; + + if let Some(file_info) = format_info.file_info{ + if file_meta == file_info{ + + format_info.last_check = Some(OffsetDateTime::now_utc()); + + return id + } + } + + + let b = fs::read(&format_info.file_path).await.map_err(|e|match e.kind(){ + ErrorKind::NotFound => DiskError::DiskNotFound, + ErrorKind::PermissionDenied => DiskError::FileAccessDenied, + _ => { + warn!("check_format_json err {:?}",e); + DiskError::CorruptedBackend}, + })?; + + + let fm = FormatV3::try_from(b.as_slice()).map_err(|e|{ + warn!("decode format.json err {:?}",e); + DiskError::CorruptedBackend + })?; + + let (m,n) = fm.find_disk_index_by_disk_id(fm.erasure.this)?; + + let disk_id = fm.erasure.this; + + if m != self.endpoint.set_idx || n != self.endpoint.disk_idx { + return DiskError::InconsistentDisk; + } + + format_info.id = Some(disk_id); + format_info.file_info= Some(file_meta); + format_info.data = b; + format_info.last_check = Some(OffsetDateTime::now_utc()); +; + +Some(disk_id) // TODO: 判断源文件id,是否有效 }