Skip to content

Commit

Permalink
Set the level in hosts as zone
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Sorokin committed Apr 19, 2024
1 parent 4acebb6 commit a8f515b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/task/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ pub(super) fn read() -> ArgMatches {
fn fd_as_zone_arg() -> Arg {
Arg::new("fd-as-zone")
.long("fd-as-zone")
.action(ArgAction::SetTrue)
.action(ArgAction::Set)
.num_args(0..=1)
.default_missing_value("1")
.value_parser(clap::value_parser!(u8))
.help("Used to insert 'failure_domain' field's value of instances in their 'zone' field.")
}
4 changes: 2 additions & 2 deletions src/task/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,8 @@ impl Cluster {
/// Note that method is intended to be called after cluster is spread
/// - that's it, when there may only be single domain name in instance's `failure_domains`.
pub fn use_failure_domain_as_zone_for_instances(mut self, args: &ArgMatches) -> Self {
if args.get_flag("fd-as-zone") {
self.hosts.use_failure_domain_as_zone(None);
if let Ok(Some(lvl)) = args.try_get_one::<u8>("fd-as-zone") {
self.hosts.use_failure_domain_as_zone(*lvl);
}
self
}
Expand Down
8 changes: 6 additions & 2 deletions src/task/cluster/hst/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,14 +515,18 @@ fn hosts_use_failure_domain_as_zone() {
assert_eq!(failure_domain_instance_zone(&host, "cache-2-1"), None);
assert_eq!(failure_domain_instance_zone(&host, "cache-2-2"), None);

host.use_failure_domain_as_zone(None);
host.use_failure_domain_as_zone(1);
assert_eq!(
failure_domain_instance_zone(&host, "cache-2-1"),
Some("dc-2")
);
assert_eq!(
failure_domain_instance_zone(&host, "cache-2-2"),
Some("server-5")
Some("dc-2")
);
assert_eq!(
failure_domain_instance_zone(&host, "stateboard-1-1"),
Some("dc-1")
);
}

Expand Down
29 changes: 15 additions & 14 deletions src/task/cluster/hst/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,25 +774,26 @@ impl HostV2 {
}

/// For every instance that has finalized failure domain, replace its zone with that domain name.
pub fn use_failure_domain_as_zone(&mut self, dc: Option<String>) {
let dc = if dc.is_some() {
dc
} else if self.name == Name::from("cluster") {
None
} else {
Some(self.name.to_string())
};
pub fn use_failure_domain_as_zone(&mut self, dc_lvl: u8) {
let lvl: u8 = 0;
let zone: Option<String> = None;
self.set_zone(lvl, dc_lvl, zone)
}

pub fn set_zone(&mut self, mut lvl: u8, dc_lvl: u8, mut zone: Option<String>) {
if dc_lvl == lvl {
zone = Some(self.name.to_string());
}

for instance in self.instances.iter_mut() {
if let FailureDomains::Finished(failure_domain) = &instance.failure_domains {
if let Some(dc) = &dc {
instance.config.zone = Some(dc.clone());
} else {
instance.config.zone = Some(failure_domain.clone());
}
instance.config.zone = zone.clone().or(Some(failure_domain.clone()));
}
}

lvl = lvl + 1;
for sub_host in self.hosts.iter_mut() {
sub_host.use_failure_domain_as_zone(dc.clone())
sub_host.set_zone(lvl, dc_lvl, zone.clone())
}
}

Expand Down

0 comments on commit a8f515b

Please sign in to comment.