From 446ed3320aa1c009025193cb375cec99d0e4b774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20Dimitroff=20H=C3=B3di?= Date: Thu, 7 Mar 2024 12:40:01 -0300 Subject: [PATCH] Replaced tokio-retry with simple retry loop --- node/Cargo.lock | 12 ------------ node/Cargo.toml | 1 - node/tools/Cargo.toml | 1 - node/tools/src/k8s.rs | 45 ++++++++++++++++++++++++++++--------------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/node/Cargo.lock b/node/Cargo.lock index 8e33dab2..8f242b32 100644 --- a/node/Cargo.lock +++ b/node/Cargo.lock @@ -3060,17 +3060,6 @@ dependencies = [ "syn 2.0.51", ] -[[package]] -name = "tokio-retry" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f57eb36ecbe0fc510036adff84824dd3c24bb781e21bfa67b69d556aa85214f" -dependencies = [ - "pin-project", - "rand 0.8.5", - "tokio", -] - [[package]] name = "tokio-rustls" version = "0.24.1" @@ -3870,7 +3859,6 @@ dependencies = [ "serde_json", "tempfile", "tokio", - "tokio-retry", "tower", "tracing", "tracing-subscriber", diff --git a/node/Cargo.toml b/node/Cargo.toml index d08616dc..0d2e3bcd 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -81,7 +81,6 @@ test-casing = "0.1.0" thiserror = "1.0.40" time = "0.3.23" tokio = { version = "1.34.0", features = ["full"] } -tokio-retry = "0.3.0" tracing = { version = "0.1.37", features = ["attributes"] } tracing-subscriber = { version = "0.3.16", features = ["env-filter", "fmt"] } kube = { version = "0.88.1", features = ["runtime", "derive"] } diff --git a/node/tools/Cargo.toml b/node/tools/Cargo.toml index 93e80fa4..3546e97c 100644 --- a/node/tools/Cargo.toml +++ b/node/tools/Cargo.toml @@ -26,7 +26,6 @@ rocksdb.workspace = true serde.workspace = true serde_json.workspace = true tokio.workspace = true -tokio-retry.workspace = true tracing.workspace = true tracing-subscriber.workspace = true vise-exporter.workspace = true diff --git a/node/tools/src/k8s.rs b/node/tools/src/k8s.rs index 01dcbf37..a10561b9 100644 --- a/node/tools/src/k8s.rs +++ b/node/tools/src/k8s.rs @@ -17,8 +17,6 @@ use kube::{ }; use std::collections::BTreeMap; use std::net::SocketAddr; -use tokio_retry::strategy::FixedInterval; -use tokio_retry::Retry; use tracing::log::info; use zksync_consensus_roles::node; use zksync_protobuf::serde::Serde; @@ -52,12 +50,11 @@ impl ConsensusNode { namespace: &str, ) -> anyhow::Result { let pods: Api = Api::namespaced(client.clone(), namespace); - // Wait until the pod is running, otherwise we get 500 error. - let pod = Retry::spawn(FixedInterval::from_millis(1000).take(15), || { - get_running_pod(&pods, &self.id) + // Wait until the pod is running, otherwise we get an error. + retry(15, 1000, || async { + get_running_pod(&pods, &self.id).await }) - .await?; - Ok(pod) + .await } /// Fetchs the pod's IP address and assignts to self.node_addr @@ -322,15 +319,13 @@ async fn get_running_pod(pods: &Api, label: &str) -> anyhow::Result { let pod = pods .list(&lp) .await? - .iter() - .next() - .with_context(|| format!("Pod not found: {label}")) - .cloned()?; - if is_pod_running(&pod) { - Ok(pod) - } else { - Err(anyhow::format_err!("Pod not ready")) + .items + .pop() + .with_context(|| format!("Pod not found: {label}"))?; + if !is_pod_running(&pod) { + anyhow::bail!("Pod is not running"); } + Ok(pod) } fn is_pod_running(pod: &Pod) -> bool { @@ -359,3 +354,23 @@ fn get_cli_args(peers: &[NodeAddr]) -> Vec { .to_vec() } } + +async fn retry(retries: usize, delay: usize, mut f: F) -> anyhow::Result +where + F: FnMut() -> Fut, + Fut: std::future::Future>, +{ + let delay = std::time::Duration::from_millis(delay.try_into()?); + let mut count = 0; + loop { + let result = f().await; + if result.is_ok() { + return result; + } + count += 1; + if count > retries { + return result; + } + std::thread::sleep(delay); + } +}