mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-18 10:43:15 +00:00
fix get_disk_id
This commit is contained in:
@@ -34,6 +34,9 @@ pub enum DiskError {
|
|||||||
|
|
||||||
#[error("volume not empty")]
|
#[error("volume not empty")]
|
||||||
VolumeNotEmpty,
|
VolumeNotEmpty,
|
||||||
|
|
||||||
|
#[error("corrupted backend")]
|
||||||
|
CorruptedBackend
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DiskError {
|
impl DiskError {
|
||||||
|
|||||||
+74
-11
@@ -26,17 +26,23 @@ use uuid::Uuid;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FormatInfo {
|
pub struct FormatInfo {
|
||||||
pub id: Option<Uuid>,
|
pub id: Option<Uuid>,
|
||||||
pub _data: Vec<u8>,
|
pub data: Vec<u8>,
|
||||||
pub _file_info: Option<Metadata>,
|
pub file_info: Option<Metadata>,
|
||||||
pub _last_check: Option<OffsetDateTime>,
|
pub last_check: Option<OffsetDateTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
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)]
|
#[derive(Debug)]
|
||||||
pub struct LocalDisk {
|
pub struct LocalDisk {
|
||||||
pub root: PathBuf,
|
pub root: PathBuf,
|
||||||
pub _format_path: PathBuf,
|
pub format_path: PathBuf,
|
||||||
pub format_info: Mutex<FormatInfo>,
|
pub format_info: Mutex<FormatInfo>,
|
||||||
// pub id: Mutex<Option<Uuid>>,
|
// pub id: Mutex<Option<Uuid>>,
|
||||||
// pub format_data: Mutex<Vec<u8>>,
|
// pub format_data: Mutex<Vec<u8>>,
|
||||||
@@ -79,14 +85,14 @@ impl LocalDisk {
|
|||||||
|
|
||||||
let format_info = FormatInfo {
|
let format_info = FormatInfo {
|
||||||
id,
|
id,
|
||||||
_data: format_data,
|
data: format_data,
|
||||||
_file_info: format_meta,
|
file_info: format_meta,
|
||||||
_last_check: format_last_check,
|
last_check: format_last_check,
|
||||||
};
|
};
|
||||||
|
|
||||||
let disk = Self {
|
let disk = Self {
|
||||||
root,
|
root,
|
||||||
_format_path: format_path,
|
format_path: format_path,
|
||||||
format_info: Mutex::new(format_info),
|
format_info: Mutex::new(format_info),
|
||||||
// // format_legacy,
|
// // format_legacy,
|
||||||
// format_file_info: Mutex::new(format_meta),
|
// format_file_info: Mutex::new(format_meta),
|
||||||
@@ -99,6 +105,17 @@ impl LocalDisk {
|
|||||||
Ok(disk)
|
Ok(disk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn check_format_json(&self) ->Result<Metadata>{
|
||||||
|
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<()> {
|
async fn make_meta_volumes(&self) -> Result<()> {
|
||||||
let buckets = format!("{}/{}", super::RUSTFS_META_BUCKET, super::BUCKET_META_PREFIX);
|
let buckets = format!("{}/{}", super::RUSTFS_META_BUCKET, super::BUCKET_META_PREFIX);
|
||||||
let multipart = format!("{}/{}", super::RUSTFS_META_BUCKET, "multipart");
|
let multipart = format!("{}/{}", super::RUSTFS_META_BUCKET, "multipart");
|
||||||
@@ -441,9 +458,55 @@ impl DiskAPI for LocalDisk {
|
|||||||
async fn get_disk_id(&self) -> Option<Uuid> {
|
async fn get_disk_id(&self) -> Option<Uuid> {
|
||||||
warn!("local get_disk_id");
|
warn!("local get_disk_id");
|
||||||
// TODO: check format file
|
// 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,是否有效
|
// TODO: 判断源文件id,是否有效
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user