Skip to content

Commit

Permalink
feat(config)!: Remove ambiguity from CLI config (#1010)
Browse files Browse the repository at this point in the history
* feat(config)!: get rid of ambiguity in MongoDB args

* remove retries

* remove feature flag

* Fix integration tests

Co-authored-by: /alex/ <alexander.schmidt@iota.org>
  • Loading branch information
grtlr and Alex6323 authored Jan 10, 2023
1 parent ab38091 commit 399457d
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 100 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/_test_int.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
#mongodb-replica-set: test-rs

- name: Test DB
env:
MONGODB_CONN_STR: mongodb://root:root@localhost:27017
uses: actions-rs/cargo@v1
with:
command: ci-test-int
11 changes: 6 additions & 5 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ services:
depends_on:
influx:
condition: service_started
mongo:
condition: service_started
hornet:
condition: service_healthy
build:
Expand All @@ -30,10 +28,13 @@ services:
- "8042:8042/tcp" # REST API
- "9100:9100/tcp" # Metrics
tty: true
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
command:
- "--mongodb-conn-str=mongodb://mongo:27017"
- "--mongodb-username=${MONGODB_USERNAME}"
- "--mongodb-password=${MONGODB_PASSWORD}"
- "--mongodb-conn-str=${MONGODB_CONN_STR}"
- "--influxdb-url=http://influx:8086"
- "--influxdb-username=${INFLUXDB_USERNAME}"
- "--influxdb-password=${INFLUXDB_PASSWORD}"
Expand Down
22 changes: 1 addition & 21 deletions src/bin/inx-chronicle/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,16 @@ pub struct MongoDbArgs {
default_value = mongodb::DEFAULT_CONN_STR,
)]
pub mongodb_conn_str: String,
/// The MongoDb username.
#[arg(long, value_name = "USERNAME", env = "MONGODB_USERNAME", default_value = mongodb::DEFAULT_USERNAME)]
pub mongodb_username: String,
/// The MongoDb password.
#[arg(long, value_name = "PASSWORD", env = "MONGODB_PASSWORD", default_value = mongodb::DEFAULT_PASSWORD)]
pub mongodb_password: String,
/// The MongoDb database name.
#[arg(long, value_name = "NAME", default_value = mongodb::DEFAULT_DATABASE_NAME)]
pub mongodb_database_name: String,
/// The MongoDb minimum pool size.
#[arg(long, value_name = "SIZE", default_value_t = mongodb::DEFAULT_MIN_POOL_SIZE)]
pub mongodb_min_pool_size: u32,
}

impl From<&MongoDbArgs> for chronicle::db::MongoDbConfig {
fn from(value: &MongoDbArgs) -> Self {
Self {
conn_str: value.mongodb_conn_str.clone(),
username: value.mongodb_username.clone(),
password: value.mongodb_password.clone(),
database_name: value.mongodb_database_name.clone(),
min_pool_size: value.mongodb_min_pool_size,
}
}
}
Expand Down Expand Up @@ -128,12 +116,6 @@ pub struct InxArgs {
/// The address of the node INX interface Chronicle tries to connect to - if enabled.
#[arg(long, value_name = "URL", default_value = inx::DEFAULT_URL)]
pub inx_url: String,
/// Time to wait until a new connection attempt is made.
#[arg(long, value_name = "DURATION", value_parser = parse_duration, default_value = inx::DEFAULT_RETRY_INTERVAL)]
pub inx_retry_interval: std::time::Duration,
/// Maximum number of tries to establish an INX connection.
#[arg(long, value_name = "COUNT", default_value_t = inx::DEFAULT_RETRY_COUNT)]
pub inx_retry_count: usize,
/// Milestone at which synchronization should begin. If set to `1` Chronicle will try to sync back until the
/// genesis block. If set to `0` Chronicle will start syncing from the most recent milestone it received.
#[arg(long, value_name = "START", default_value_t = inx::DEFAULT_SYNC_START)]
Expand All @@ -149,8 +131,6 @@ impl From<&InxArgs> for inx::InxConfig {
Self {
enabled: !value.disable_inx,
url: value.inx_url.clone(),
conn_retry_interval: value.inx_retry_interval,
conn_retry_count: value.inx_retry_count,
sync_start_milestone: value.inx_sync_start.into(),
}
}
Expand Down Expand Up @@ -216,7 +196,7 @@ pub struct JwtArgs {
pub jwt_expiration: std::time::Duration,
}

#[cfg(any(feature = "inx", feature = "api"))]
#[cfg(feature = "api")]
fn parse_duration(arg: &str) -> Result<std::time::Duration, humantime::DurationError> {
arg.parse::<humantime::Duration>().map(Into::into)
}
Expand Down
4 changes: 1 addition & 3 deletions src/bin/inx-chronicle/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

use chronicle::db::MongoDbConfig;
use serde::{Deserialize, Serialize};

/// Configuration of Chronicle.
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
#[derive(Clone, Default, Debug)]
pub struct ChronicleConfig {
pub mongodb: MongoDbConfig,
#[cfg(any(feature = "analytics", feature = "metrics"))]
Expand Down
15 changes: 1 addition & 14 deletions src/bin/inx-chronicle/stardust_inx/config.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use std::time::Duration;

use chronicle::types::tangle::MilestoneIndex;
use serde::{Deserialize, Serialize};

pub const DEFAULT_ENABLED: bool = true;
pub const DEFAULT_URL: &str = "http://localhost:9029";
pub const DEFAULT_RETRY_INTERVAL: &str = "5s";
pub const DEFAULT_RETRY_COUNT: usize = 30;
pub const DEFAULT_SYNC_START: u32 = 0;

/// Configuration for an INX connection.
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[derive(Clone, Debug)]
pub struct InxConfig {
pub enabled: bool,
/// The bind address of node's INX interface.
pub url: String,
/// The time that has to pass until a new connection attempt is made.
#[serde(with = "humantime_serde")]
pub conn_retry_interval: Duration,
/// The number of retries when connecting fails.
pub conn_retry_count: usize,
/// The milestone at which synchronization should begin.
pub sync_start_milestone: MilestoneIndex,
}
Expand All @@ -33,8 +22,6 @@ impl Default for InxConfig {
Self {
enabled: DEFAULT_ENABLED,
url: DEFAULT_URL.to_string(),
conn_retry_interval: DEFAULT_RETRY_INTERVAL.parse::<humantime::Duration>().unwrap().into(),
conn_retry_count: DEFAULT_RETRY_COUNT,
sync_start_milestone: DEFAULT_SYNC_START.into(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/inx-chronicle/stardust_inx/error.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use chronicle::types::tangle::MilestoneIndex;
use chronicle::{inx::InxError, types::tangle::MilestoneIndex};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum InxWorkerError {
#[cfg(feature = "analytics")]
#[error("Analytics error: {0}")]
Analytics(#[from] chronicle::db::collections::analytics::Error),
#[error("failed to establish connection")]
ConnectionError,
#[error("failed to establish connection: {0}")]
ConnectionError(#[from] InxError),
#[cfg(any(feature = "analytics", feature = "metrics"))]
#[error("InfluxDb error: {0}")]
InfluxDb(#[from] influxdb::Error),
Expand Down
19 changes: 3 additions & 16 deletions src/bin/inx-chronicle/stardust_inx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use chronicle::{
tangle::MilestoneIndex,
},
};
use eyre::{bail, eyre, Result};
use eyre::{bail, Result};
use futures::{StreamExt, TryStreamExt};
use tokio::{task::JoinSet, try_join};
use tracing::{debug, info, instrument, trace, trace_span, warn, Instrument};
use tracing::{debug, info, instrument, trace, trace_span, Instrument};

pub use self::{config::InxConfig, error::InxWorkerError};

Expand Down Expand Up @@ -102,20 +102,7 @@ impl InxWorker {
bail!(InxWorkerError::InvalidAddress(self.config.url.clone()));
}

for i in 0..self.config.conn_retry_count {
match Inx::connect(self.config.url.clone()).await {
Ok(inx_client) => return Ok(inx_client),
Err(_) => {
warn!(
"INX connection failed. Retrying in {}s. {} retries remaining.",
self.config.conn_retry_interval.as_secs(),
self.config.conn_retry_count - i
);
tokio::time::sleep(self.config.conn_retry_interval).await;
}
}
}
Err(eyre!(InxWorkerError::ConnectionError))
Ok(Inx::connect(self.config.url.clone()).await?)
}

pub async fn run(&mut self) -> Result<()> {
Expand Down
5 changes: 1 addition & 4 deletions src/db/influxdb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

//! Holds the `InfluxDb` config and its defaults.

use serde::{Deserialize, Serialize};

/// The default InfluxDb URL to connect to.
pub const DEFAULT_URL: &str = "http://localhost:8086";
/// The default InfluxDb username.
Expand All @@ -26,8 +24,7 @@ pub const DEFAULT_METRICS_DATABASE_NAME: &str = "chronicle_metrics";

/// The influxdb [`influxdb::Client`] config.
#[must_use]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct InfluxDbConfig {
/// The address of the InfluxDb instance.
pub url: String,
Expand Down
19 changes: 1 addition & 18 deletions src/db/mongodb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,20 @@ use mongodb::{
error::Error,
options::{ConnectionString, HostInfo},
};
use serde::{Deserialize, Serialize};

/// The default connection string of the database.
pub const DEFAULT_CONN_STR: &str = "mongodb://localhost:27017";
/// The default MongoDB username.
pub const DEFAULT_USERNAME: &str = "root";
/// The default MongoDB password.
pub const DEFAULT_PASSWORD: &str = "root";
/// The default name of the database to connect to.
pub const DEFAULT_DATABASE_NAME: &str = "chronicle";
/// The default minimum amount of connections in the pool.
pub const DEFAULT_MIN_POOL_SIZE: u32 = 2;

/// The [`super::MongoDb`] config.
#[must_use]
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct MongoDbConfig {
/// The connection string of the database.
pub conn_str: String,
/// The MongoDB username.
pub username: String,
/// The MongoDB password.
pub password: String,
/// The name of the database to connect to.
pub database_name: String,
/// The minimum amount of connections in the pool.
pub min_pool_size: u32,
}

impl MongoDbConfig {
Expand All @@ -53,10 +39,7 @@ impl Default for MongoDbConfig {
fn default() -> Self {
Self {
conn_str: DEFAULT_CONN_STR.to_string(),
username: DEFAULT_USERNAME.to_string(),
password: DEFAULT_PASSWORD.to_string(),
database_name: DEFAULT_DATABASE_NAME.to_string(),
min_pool_size: DEFAULT_MIN_POOL_SIZE,
}
}
}
11 changes: 1 addition & 10 deletions src/db/mongodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use config::MongoDbConfig;
use mongodb::{
bson::{doc, Document},
error::Error,
options::{ClientOptions, Credential},
options::ClientOptions,
Client,
};

Expand All @@ -33,15 +33,6 @@ impl MongoDb {
let mut client_options = ClientOptions::parse(&config.conn_str).await?;

client_options.app_name = Some("Chronicle".to_string());
client_options.min_pool_size = Some(config.min_pool_size);

if client_options.credential.is_none() {
let credential = Credential::builder()
.username(config.username.clone())
.password(config.password.clone())
.build();
client_options.credential = Some(credential);
}

let client = Client::with_options(client_options)?;

Expand Down
6 changes: 0 additions & 6 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ pub async fn setup_database(database_name: impl ToString) -> eyre::Result<MongoD
if let Ok(conn_str) = std::env::var("MONGODB_CONN_STR") {
test_config.conn_str = conn_str;
};
if let Ok(username) = std::env::var("MONGODB_USERNAME") {
test_config.username = username;
};
if let Ok(password) = std::env::var("MONGODB_PASSWORD") {
test_config.password = password;
};

let db = MongoDb::connect(&test_config).await?;
db.clear().await?;
Expand Down

0 comments on commit 399457d

Please sign in to comment.