moves elasticsearch initialization to `FinalizedState::new_with_debug()`

This commit is contained in:
Arya 2024-06-06 20:24:13 -04:00
parent 26c0e5dca8
commit 281668bd82
2 changed files with 30 additions and 70 deletions

View File

@ -24,15 +24,6 @@ use std::{
time::{Duration, Instant},
};
#[cfg(feature = "elasticsearch")]
use elasticsearch::{
auth::Credentials::Basic,
cert::CertificateValidation,
http::transport::{SingleNodeConnectionPool, TransportBuilder},
http::Url,
Elasticsearch,
};
use futures::future::FutureExt;
use tokio::sync::{oneshot, watch};
use tower::{util::BoxService, Service, ServiceExt};
@ -319,29 +310,7 @@ impl StateService {
checkpoint_verify_concurrency_limit: usize,
) -> (Self, ReadStateService, LatestChainTip, ChainTipChange) {
let timer = CodeTimer::start();
#[cfg(feature = "elasticsearch")]
let finalized_state = {
let conn_pool = SingleNodeConnectionPool::new(
Url::parse(config.elasticsearch_url.as_str())
.expect("configured elasticsearch url is invalid"),
);
let transport = TransportBuilder::new(conn_pool)
.cert_validation(CertificateValidation::None)
.auth(Basic(
config.clone().elasticsearch_username,
config.clone().elasticsearch_password,
))
.build()
.expect("elasticsearch transport builder should not fail");
let elastic_db = Some(Elasticsearch::new(transport));
FinalizedState::new(&config, network, elastic_db)
};
#[cfg(not(feature = "elasticsearch"))]
let finalized_state = { FinalizedState::new(&config, network) };
timer.finish(module_path!(), line!(), "opening finalized state database");
let timer = CodeTimer::start();
@ -1959,36 +1928,13 @@ pub fn init_read_only(
ZebraDb,
tokio::sync::watch::Sender<NonFinalizedState>,
) {
let finalized_state = { FinalizedState::new_with_debug(&config, network, true, true) };
let (non_finalized_state_sender, non_finalized_state_receiver) =
tokio::sync::watch::channel(NonFinalizedState::new(network));
#[cfg(feature = "elasticsearch")]
let finalized_state = {
let conn_pool = SingleNodeConnectionPool::new(
Url::parse(config.elasticsearch_url.as_str())
.expect("configured elasticsearch url is invalid"),
);
let transport = TransportBuilder::new(conn_pool)
.cert_validation(CertificateValidation::None)
.auth(Basic(
config.clone().elasticsearch_username,
config.clone().elasticsearch_password,
))
.build()
.expect("elasticsearch transport builder should not fail");
let elastic_db = Some(Elasticsearch::new(transport));
FinalizedState::new_with_debug(&config, network, true, elastic_db, true)
};
#[cfg(not(feature = "elasticsearch"))]
let finalized_state = { FinalizedState::new_with_debug(&config, network, true, true) };
let db = finalized_state.db.clone();
(
ReadStateService::new(&finalized_state, None, non_finalized_state_receiver),
db,
finalized_state.db.clone(),
non_finalized_state_sender,
)
}

View File

@ -139,19 +139,8 @@ pub struct FinalizedState {
impl FinalizedState {
/// Returns an on-disk database instance for `config`, `network`, and `elastic_db`.
/// If there is no existing database, creates a new database on disk.
pub fn new(
config: &Config,
network: &Network,
#[cfg(feature = "elasticsearch")] elastic_db: Option<elasticsearch::Elasticsearch>,
) -> Self {
Self::new_with_debug(
config,
network,
false,
#[cfg(feature = "elasticsearch")]
elastic_db,
false,
)
pub fn new(config: &Config, network: &Network) -> Self {
Self::new_with_debug(config, network, false, false)
}
/// Returns an on-disk database instance with the supplied production and debug settings.
@ -162,9 +151,34 @@ impl FinalizedState {
config: &Config,
network: &Network,
debug_skip_format_upgrades: bool,
#[cfg(feature = "elasticsearch")] elastic_db: Option<elasticsearch::Elasticsearch>,
read_only: bool,
) -> Self {
#[cfg(feature = "elasticsearch")]
let elastic_db = {
use elasticsearch::{
auth::Credentials::Basic,
cert::CertificateValidation,
http::transport::{SingleNodeConnectionPool, TransportBuilder},
http::Url,
Elasticsearch,
};
let conn_pool = SingleNodeConnectionPool::new(
Url::parse(config.elasticsearch_url.as_str())
.expect("configured elasticsearch url is invalid"),
);
let transport = TransportBuilder::new(conn_pool)
.cert_validation(CertificateValidation::None)
.auth(Basic(
config.clone().elasticsearch_username,
config.clone().elasticsearch_password,
))
.build()
.expect("elasticsearch transport builder should not fail");
Some(Elasticsearch::new(transport))
};
let db = ZebraDb::new(
config,
STATE_DATABASE_KIND,