refactor: Use ReplicationFactor type in more places

- Remove the replication_factor.replication_factor() in favor of
  usize::from(replication_factor) to make the conversion more explicit.

- Implement Display on ReplicationFactor so that it can be formatted
  without converting to usize

- Use ReplicationFactor in the constructor of LayoutVersion and add a
  method to get a ReplicationFactor from a LayoutVersion, despite
  LayoutVersion still storing it as usize internally.
This commit is contained in:
Yureka
2025-04-20 20:33:34 +02:00
parent e79b485aa8
commit c8e9c45889
4 changed files with 22 additions and 15 deletions
+6 -6
View File
@@ -46,11 +46,11 @@ impl LayoutManager {
let cluster_layout = match persist_cluster_layout.load() {
Ok(x) => {
if x.current().replication_factor != replication_factor.replication_factor() {
if x.current().replication_factor() != replication_factor {
return Err(Error::Message(format!(
"Previous cluster layout has replication factor {}, which is different than the one specified in the config file ({}). The previous cluster layout can be purged, if you know what you are doing, simply by deleting the `cluster_layout` file in your metadata directory.",
x.current().replication_factor,
replication_factor.replication_factor()
x.current().replication_factor(),
replication_factor,
)));
}
x
@@ -301,11 +301,11 @@ impl LayoutManager {
adv.update_trackers
);
if adv.current().replication_factor != self.replication_factor.replication_factor() {
if adv.current().replication_factor() != self.replication_factor {
let msg = format!(
"Received a cluster layout from another node with replication factor {}, which is different from what we have in our configuration ({}). Discarding the cluster layout we received.",
adv.current().replication_factor,
self.replication_factor.replication_factor()
adv.current().replication_factor(),
self.replication_factor,
);
error!("{}", msg);
return Err(Error::Message(msg));
+7 -2
View File
@@ -11,12 +11,13 @@ use garage_util::error::*;
use super::graph_algo::*;
use super::*;
use crate::replication_mode::*;
// The Message type will be used to collect information on the algorithm.
pub type Message = Vec<String>;
impl LayoutVersion {
pub fn new(replication_factor: usize) -> Self {
pub fn new(replication_factor: ReplicationFactor) -> Self {
// We set the default zone redundancy to be Maximum, meaning that the maximum
// possible value will be used depending on the cluster topology
let parameters = LayoutParameters {
@@ -25,7 +26,7 @@ impl LayoutVersion {
LayoutVersion {
version: 0,
replication_factor,
replication_factor: usize::from(replication_factor),
partition_size: 0,
roles: LwwMap::new(),
node_id_vec: Vec::new(),
@@ -132,6 +133,10 @@ impl LayoutVersion {
.map(move |i| self.node_id_vec[*i as usize])
}
pub fn replication_factor(&self) -> ReplicationFactor {
ReplicationFactor::new(self.replication_factor).unwrap()
}
// ===================== internal information extractors ======================
pub(crate) fn expect_get_node_capacity(&self, uuid: &Uuid) -> u64 {
+8 -6
View File
@@ -38,14 +38,10 @@ impl ReplicationFactor {
}
}
pub fn replication_factor(&self) -> usize {
self.0
}
pub fn read_quorum(&self, consistency_mode: ConsistencyMode) -> usize {
match consistency_mode {
ConsistencyMode::Dangerous | ConsistencyMode::Degraded => 1,
ConsistencyMode::Consistent => self.replication_factor().div_ceil(2),
ConsistencyMode::Consistent => usize::from(*self).div_ceil(2),
}
}
@@ -53,7 +49,7 @@ impl ReplicationFactor {
match consistency_mode {
ConsistencyMode::Dangerous => 1,
ConsistencyMode::Degraded | ConsistencyMode::Consistent => {
(self.replication_factor() + 1) - self.read_quorum(ConsistencyMode::Consistent)
(usize::from(*self) + 1) - self.read_quorum(ConsistencyMode::Consistent)
}
}
}
@@ -65,6 +61,12 @@ impl std::convert::From<ReplicationFactor> for usize {
}
}
impl std::fmt::Display for ReplicationFactor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.0.fmt(f)
}
}
pub fn parse_replication_mode(
config: &Config,
) -> Result<(ReplicationFactor, ConsistencyMode), Error> {
+1 -1
View File
@@ -68,7 +68,7 @@ impl SystemMetrics {
let replication_factor = system.replication_factor;
meter
.u64_value_observer("garage_replication_factor", move |observer| {
observer.observe(replication_factor.replication_factor() as u64, &[])
observer.observe(usize::from(replication_factor) as u64, &[])
})
.with_description("Garage replication factor setting")
.init()