diff --git a/src/rpc/layout/manager.rs b/src/rpc/layout/manager.rs index 0c75742b..789603b5 100644 --- a/src/rpc/layout/manager.rs +++ b/src/rpc/layout/manager.rs @@ -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)); diff --git a/src/rpc/layout/version.rs b/src/rpc/layout/version.rs index fdcccc46..6036cfe4 100644 --- a/src/rpc/layout/version.rs +++ b/src/rpc/layout/version.rs @@ -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; 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 { diff --git a/src/rpc/replication_mode.rs b/src/rpc/replication_mode.rs index a3a94085..caf67462 100644 --- a/src/rpc/replication_mode.rs +++ b/src/rpc/replication_mode.rs @@ -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 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> { diff --git a/src/rpc/system_metrics.rs b/src/rpc/system_metrics.rs index a64daec8..31c2e9ff 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -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()