Skip to content

Commit

Permalink
moves elasticsearch initialization to FinalizedState::new_with_debug()
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Jun 7, 2024
1 parent 26c0e5d commit 281668b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 70 deletions.
58 changes: 2 additions & 56 deletions zebra-state/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
)
}
Expand Down
42 changes: 28 additions & 14 deletions zebra-state/src/service/finalized_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit 281668b

Please sign in to comment.