mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-27 15:37:02 +00:00
fix get_disk_id
This commit is contained in:
+56
-50
@@ -32,11 +32,13 @@ pub struct FormatInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FormatInfo {
|
impl FormatInfo {
|
||||||
pub fn last_check_valid(&self)->bool{
|
pub fn last_check_valid(&self) -> bool {
|
||||||
let now = OffsetDateTime::now_utc();
|
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)
|
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)]
|
||||||
@@ -44,6 +46,7 @@ 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 endpoint: Endpoint,
|
||||||
// pub id: Mutex<Option<Uuid>>,
|
// pub id: Mutex<Option<Uuid>>,
|
||||||
// pub format_data: Mutex<Vec<u8>>,
|
// pub format_data: Mutex<Vec<u8>>,
|
||||||
// pub format_file_info: Mutex<Option<Metadata>>,
|
// pub format_file_info: Mutex<Option<Metadata>>,
|
||||||
@@ -92,6 +95,7 @@ impl LocalDisk {
|
|||||||
|
|
||||||
let disk = Self {
|
let disk = Self {
|
||||||
root,
|
root,
|
||||||
|
endpoint: ep.clone(),
|
||||||
format_path: format_path,
|
format_path: format_path,
|
||||||
format_info: Mutex::new(format_info),
|
format_info: Mutex::new(format_info),
|
||||||
// // format_legacy,
|
// // format_legacy,
|
||||||
@@ -105,16 +109,16 @@ impl LocalDisk {
|
|||||||
Ok(disk)
|
Ok(disk)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_format_json(&self) ->Result<Metadata>{
|
async fn check_format_json(&self) -> Result<Metadata> {
|
||||||
let p = self.format_path;
|
let md = fs::metadata(&self.format_path).await.map_err(|e| match e.kind() {
|
||||||
let md = fs::metadata(&p).await.map_err(|e|match e.kind(){
|
ErrorKind::NotFound => DiskError::DiskNotFound,
|
||||||
ErrorKind::NotFound => DiskError::DiskNotFound,
|
ErrorKind::PermissionDenied => DiskError::FileAccessDenied,
|
||||||
ErrorKind::PermissionDenied => DiskError::FileAccessDenied,
|
_ => {
|
||||||
_ => {
|
warn!("check_format_json err {:?}", e);
|
||||||
warn!("check_format_json err {:?}",e);
|
DiskError::CorruptedBackend
|
||||||
DiskError::CorruptedBackend},
|
}
|
||||||
})?;
|
})?;
|
||||||
Ok(md)
|
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);
|
||||||
@@ -455,58 +459,60 @@ impl DiskAPI for LocalDisk {
|
|||||||
self.root.clone()
|
self.root.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_disk_id(&self) -> Option<Uuid> {
|
async fn get_disk_id(&self) -> Result<Option<Uuid>> {
|
||||||
warn!("local get_disk_id");
|
warn!("local get_disk_id");
|
||||||
// TODO: check format file
|
// TODO: check format file
|
||||||
let mut format_info = self.format_info.lock().await;
|
let mut format_info = self.format_info.lock().await;
|
||||||
|
|
||||||
let id = format_info.id.clone();
|
let id = format_info.id.clone();
|
||||||
|
|
||||||
if format_info.last_check_valid(){
|
if format_info.last_check_valid() {
|
||||||
return id
|
return Ok(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 file_meta = self.check_format_json().await?;
|
||||||
|
|
||||||
let b = fs::read(&format_info.file_path).await.map_err(|e|match e.kind(){
|
if let Some(file_info) = &format_info.file_info {
|
||||||
ErrorKind::NotFound => DiskError::DiskNotFound,
|
if utils::fs::same_file(&file_meta, file_info) {
|
||||||
ErrorKind::PermissionDenied => DiskError::FileAccessDenied,
|
format_info.last_check = Some(OffsetDateTime::now_utc());
|
||||||
_ => {
|
|
||||||
warn!("check_format_json err {:?}",e);
|
|
||||||
DiskError::CorruptedBackend},
|
|
||||||
})?;
|
|
||||||
|
|
||||||
|
return Ok(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let fm = FormatV3::try_from(b.as_slice()).map_err(|e|{
|
let b = fs::read(&self.format_path).await.map_err(|e| match e.kind() {
|
||||||
warn!("decode format.json err {:?}",e);
|
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
|
DiskError::CorruptedBackend
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let (m,n) = fm.find_disk_index_by_disk_id(fm.erasure.this)?;
|
|
||||||
|
|
||||||
let disk_id = fm.erasure.this;
|
let (m, n) = fm.find_disk_index_by_disk_id(fm.erasure.this)?;
|
||||||
|
|
||||||
if m != self.endpoint.set_idx || n != self.endpoint.disk_idx {
|
let disk_id = fm.erasure.this;
|
||||||
return DiskError::InconsistentDisk;
|
|
||||||
}
|
|
||||||
|
|
||||||
format_info.id = Some(disk_id);
|
match (self.endpoint.set_idx, self.endpoint.disk_idx) {
|
||||||
format_info.file_info= Some(file_meta);
|
(Some(set_idx), Some(disk_idx)) => {
|
||||||
format_info.data = b;
|
if m != set_idx || n != disk_idx {
|
||||||
format_info.last_check = Some(OffsetDateTime::now_utc());
|
return Err(Error::new(DiskError::InconsistentDisk));
|
||||||
;
|
}
|
||||||
|
}
|
||||||
|
_ => return Err(Error::new(DiskError::InconsistentDisk)),
|
||||||
|
}
|
||||||
|
|
||||||
Some(disk_id)
|
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());
|
||||||
|
|
||||||
|
Ok(Some(disk_id))
|
||||||
// TODO: 判断源文件id,是否有效
|
// TODO: 判断源文件id,是否有效
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ pub trait DiskAPI: Debug + Send + Sync + 'static {
|
|||||||
fn is_local(&self) -> bool;
|
fn is_local(&self) -> bool;
|
||||||
fn path(&self) -> PathBuf;
|
fn path(&self) -> PathBuf;
|
||||||
async fn close(&self) -> Result<()>;
|
async fn close(&self) -> Result<()>;
|
||||||
async fn get_disk_id(&self) -> Option<Uuid>;
|
async fn get_disk_id(&self) -> Result<Option<Uuid>>;
|
||||||
async fn set_disk_id(&self, id: Option<Uuid>) -> Result<()>;
|
async fn set_disk_id(&self, id: Option<Uuid>) -> Result<()>;
|
||||||
|
|
||||||
async fn delete(&self, volume: &str, path: &str, opt: DeleteOptions) -> Result<()>;
|
async fn delete(&self, volume: &str, path: &str, opt: DeleteOptions) -> Result<()>;
|
||||||
|
|||||||
@@ -105,8 +105,8 @@ impl DiskAPI for RemoteDisk {
|
|||||||
self.root.clone()
|
self.root.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_disk_id(&self) -> Option<Uuid> {
|
async fn get_disk_id(&self) -> Result<Option<Uuid>> {
|
||||||
self.id.lock().await.clone()
|
Ok(self.id.lock().await.clone())
|
||||||
}
|
}
|
||||||
async fn set_disk_id(&self, id: Option<Uuid>) -> Result<()> {
|
async fn set_disk_id(&self, id: Option<Uuid>) -> Result<()> {
|
||||||
let mut lock = self.id.lock().await;
|
let mut lock = self.id.lock().await;
|
||||||
|
|||||||
+1
-1
@@ -78,7 +78,7 @@ impl Sets {
|
|||||||
disk = local_disk;
|
disk = local_disk;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(_disk_id) = disk.as_ref().unwrap().get_disk_id().await {
|
if let Some(_disk_id) = disk.as_ref().unwrap().get_disk_id().await? {
|
||||||
set_drive.push(disk);
|
set_drive.push(disk);
|
||||||
} else {
|
} else {
|
||||||
warn!("sets new set_drive {}-{} get_disk_id is none", i, j);
|
warn!("sets new set_drive {}-{} get_disk_id is none", i, j);
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
use std::{fs::Metadata, os::unix::fs::MetadataExt};
|
||||||
|
|
||||||
|
pub fn same_file(f1: &Metadata, f2: &Metadata) -> bool {
|
||||||
|
if f1.dev() != f2.dev() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if f1.ino() != f2.ino() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if f1.size() != f2.size() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if f1.permissions() != f2.permissions() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if f1.mtime() != f2.mtime() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod crypto;
|
pub mod crypto;
|
||||||
pub mod ellipses;
|
pub mod ellipses;
|
||||||
|
pub mod fs;
|
||||||
pub mod hash;
|
pub mod hash;
|
||||||
pub mod net;
|
pub mod net;
|
||||||
pub mod path;
|
pub mod path;
|
||||||
|
|||||||
Reference in New Issue
Block a user