Compare commits

..

No commits in common. "e83519694055750cf29c31845d712165dbbebd66" and "30abf7e086359c914b1e083d3eec0f4e5fa37351" have entirely different histories.

2 changed files with 19 additions and 53 deletions

View file

@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
@ -14,12 +13,9 @@ const DRIVE_NPART: usize = 1024;
const HASH_DRIVE_BYTES: (usize, usize) = (2, 3);
const MARKER_FILE_NAME: &str = "garage-marker";
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct DataLayout {
pub(crate) data_dirs: Vec<DataDir>,
markers: HashMap<PathBuf, String>,
/// Primary storage location (index in data_dirs) for each partition
/// = the location where the data is supposed to be, blocks are always
@ -79,17 +75,16 @@ impl DataLayout {
Ok(Self {
data_dirs,
markers: HashMap::new(),
part_prim,
part_sec,
})
}
pub(crate) fn update(self, dirs: &DataDirEnum) -> Result<Self, Error> {
pub(crate) fn update(&mut self, dirs: &DataDirEnum) -> Result<(), Error> {
// Make list of new data directories, exit if nothing changed
let data_dirs = make_data_dirs(dirs)?;
if data_dirs == self.data_dirs {
return Ok(self);
return Ok(());
}
let total_cap = data_dirs.iter().filter_map(|x| x.capacity()).sum::<u64>();
@ -219,43 +214,11 @@ impl DataLayout {
}
// Apply newly generated config
Ok(Self {
*self = Self {
data_dirs,
markers: self.markers,
part_prim,
part_sec,
})
}
pub(crate) fn check_markers(&mut self) -> Result<(), Error> {
let data_dirs = &self.data_dirs;
self.markers
.retain(|k, _| data_dirs.iter().any(|x| x.path == *k));
for dir in self.data_dirs.iter() {
let mut marker_path = dir.path.clone();
marker_path.push(MARKER_FILE_NAME);
let existing_marker = std::fs::read_to_string(&marker_path).ok();
match (existing_marker, self.markers.get(&dir.path)) {
(Some(m1), Some(m2)) => {
if m1 != *m2 {
return Err(Error::Message(format!("Mismatched content for marker file `{}` in data directory `{}`. If you moved data directories or changed their mountpoints, you should remove the `data_layout` file in Garage's metadata directory and restart Garage.", MARKER_FILE_NAME, dir.path.display())));
}
}
(None, Some(_)) => {
return Err(Error::Message(format!("Could not find expected marker file `{}` in data directory `{}`, make sure this data directory is mounted correctly.", MARKER_FILE_NAME, dir.path.display())));
}
(Some(mkr), None) => {
self.markers.insert(dir.path.clone(), mkr);
}
(None, None) => {
let mkr = hex::encode(garage_util::data::gen_uuid().as_slice());
std::fs::write(&marker_path, &mkr)?;
self.markers.insert(dir.path.clone(), mkr);
}
}
}
};
Ok(())
}
@ -292,7 +255,6 @@ impl DataLayout {
pub(crate) fn without_secondary_locations(&self) -> Self {
Self {
data_dirs: self.data_dirs.clone(),
markers: self.markers.clone(),
part_prim: self.part_prim.clone(),
part_sec: self.part_sec.iter().map(|_| vec![]).collect::<Vec<_>>(),
}
@ -360,12 +322,14 @@ fn make_data_dirs(dirs: &DataDirEnum) -> Result<Vec<DataDir>, Error> {
fn dir_not_empty(path: &PathBuf) -> Result<bool, Error> {
for entry in std::fs::read_dir(&path)? {
let dir = entry?;
let ft = dir.file_type()?;
let name = dir.file_name().into_string().ok();
if ft.is_file() && name.as_deref() == Some(MARKER_FILE_NAME) {
return Ok(true);
}
if ft.is_dir() && name.and_then(|hex| hex::decode(&hex).ok()).is_some() {
if dir.file_type()?.is_dir()
&& dir
.file_name()
.into_string()
.ok()
.and_then(|hex| hex::decode(&hex).ok())
.is_some()
{
return Ok(true);
}
}

View file

@ -127,15 +127,17 @@ impl BlockManager {
// Load or compute layout, i.e. assignment of data blocks to the different data directories
let data_layout_persister: Persister<DataLayout> =
Persister::new(&system.metadata_dir, "data_layout");
let mut data_layout = match data_layout_persister.load() {
Ok(layout) => layout
let data_layout = match data_layout_persister.load() {
Ok(mut layout) => {
layout
.update(&config.data_dir)
.ok_or_message("invalid data_dir config")?,
.ok_or_message("invalid data_dir config")?;
layout
}
Err(_) => {
DataLayout::initialize(&config.data_dir).ok_or_message("invalid data_dir config")?
}
};
data_layout.check_markers()?;
data_layout_persister
.save(&data_layout)
.expect("cannot save data_layout");