Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add general config and secrets opts to snapshot creator #2471

Merged
merged 10 commits into from
Jul 24, 2024
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions core/bin/snapshots_creator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ zksync_env_config.workspace = true
zksync_types.workspace = true
zksync_object_store.workspace = true
zksync_vlog.workspace = true
zksync_core_leftovers.workspace = true

anyhow.workspace = true
structopt.workspace = true
tokio = { workspace = true, features = ["full"] }
tracing.workspace = true
futures.workspace = true
Expand Down
51 changes: 35 additions & 16 deletions core/bin/snapshots_creator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
//! at a time).

use anyhow::Context as _;
use structopt::StructOpt;
use tokio::{sync::watch, task::JoinHandle};
use zksync_config::{
configs::{DatabaseSecrets, ObservabilityConfig, PrometheusConfig},
SnapshotsCreatorConfig,
};
use zksync_config::configs::PrometheusConfig;
use zksync_core_leftovers::temp_config_store::{load_database_secrets, load_general_config};
use zksync_dal::{ConnectionPool, Core};
use zksync_env_config::{object_store::SnapshotsObjectStoreConfig, FromEnv};
use zksync_object_store::ObjectStoreFactory;
use zksync_vlog::prometheus::PrometheusExporterConfig;

Expand All @@ -28,9 +26,9 @@ mod metrics;
mod tests;

async fn maybe_enable_prometheus_metrics(
prometheus_config: Option<PrometheusConfig>,
stop_receiver: watch::Receiver<bool>,
) -> anyhow::Result<Option<JoinHandle<anyhow::Result<()>>>> {
let prometheus_config = PrometheusConfig::from_env().ok();
match prometheus_config.map(|c| (c.gateway_endpoint(), c.push_interval())) {
Some((Some(gateway_endpoint), push_interval)) => {
tracing::info!("Starting prometheus exporter with gateway {gateway_endpoint:?} and push_interval {push_interval:?}");
Expand All @@ -49,18 +47,36 @@ async fn maybe_enable_prometheus_metrics(
/// Minimum number of storage log chunks to produce.
const MIN_CHUNK_COUNT: u64 = 10;

#[derive(StructOpt)]
#[structopt(name = "ZKsync snapshot creator", author = "Matter Labs")]
struct Opt {
/// Path to the configuration file.
#[structopt(long)]
config_path: Option<std::path::PathBuf>,

/// Path to the secrets file.
#[structopt(long)]
secrets_path: Option<std::path::PathBuf>,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let (stop_sender, stop_receiver) = watch::channel(false);

let observability_config =
ObservabilityConfig::from_env().context("ObservabilityConfig::from_env()")?;
let opt = Opt::from_args();
let general_config = load_general_config(opt.config_path).context("general config")?;
let database_secrets = load_database_secrets(opt.secrets_path).context("database secrets")?;

let observability_config = general_config
.observability
.context("observability config")?;
let log_format: zksync_vlog::LogFormat = observability_config
.log_format
.parse()
.context("Invalid log format")?;

let prometheus_exporter_task = maybe_enable_prometheus_metrics(stop_receiver).await?;
let prometheus_exporter_task =
maybe_enable_prometheus_metrics(general_config.prometheus_config, stop_receiver).await?;
let mut builder = zksync_vlog::ObservabilityBuilder::new().with_log_format(log_format);
if let Some(sentry_url) = observability_config.sentry_url {
builder = builder
Expand All @@ -71,16 +87,19 @@ async fn main() -> anyhow::Result<()> {
let _guard = builder.build();
tracing::info!("Starting snapshots creator");

let object_store_config =
SnapshotsObjectStoreConfig::from_env().context("SnapshotsObjectStoreConfig::from_env()")?;
let blob_store = ObjectStoreFactory::new(object_store_config.0)
let creator_config = general_config
.snapshot_creator
.context("snapshot creator config")?;

let object_store_config = creator_config
.clone()
.object_store
.context("snapshot creator object storage config")?;

let blob_store = ObjectStoreFactory::new(object_store_config)
.create_store()
.await?;

let database_secrets = DatabaseSecrets::from_env().context("DatabaseSecrets")?;
let creator_config =
SnapshotsCreatorConfig::from_env().context("SnapshotsCreatorConfig::from_env")?;

let replica_pool = ConnectionPool::<Core>::builder(
database_secrets.replica_url()?,
creator_config.concurrent_queries_count,
Expand Down
1 change: 1 addition & 0 deletions core/bin/snapshots_creator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use rand::{thread_rng, Rng};
use zksync_config::SnapshotsCreatorConfig;
use zksync_dal::{Connection, CoreDal};
use zksync_object_store::{MockObjectStore, ObjectStore};
use zksync_types::{
Expand Down
8 changes: 6 additions & 2 deletions core/lib/env_config/src/snapshots_creator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use zksync_config::SnapshotsCreatorConfig;

use crate::{envy_load, FromEnv};
use crate::{envy_load, object_store::SnapshotsObjectStoreConfig, FromEnv};

impl FromEnv for SnapshotsCreatorConfig {
fn from_env() -> anyhow::Result<Self> {
envy_load("snapshots_creator", "SNAPSHOTS_CREATOR_")
let mut snapshot_creator: SnapshotsCreatorConfig =
envy_load("snapshots_creator", "SNAPSHOTS_CREATOR_")?;

snapshot_creator.object_store = SnapshotsObjectStoreConfig::from_env().map(|a| a.0).ok();
Ok(snapshot_creator)
}
}
Loading