This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
forked from matter-labs/zksync-era
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(en): Switch EN to use node framework (matter-labs#2427)
## What ❔ This PR ports remaining parts of the EN to node framework and makes framework the default way to run it. In more detail: - Config for the health check limits are now set from config. - EN and rust metrics are now exposed; the protocol version update task now runs. -⚠️ Connection pool healthcheck was removed. It was controversial initially, its usefulness is not clear, it was supposed to be refactored a year ago but didn't, and it wasn't working well when testing. See [linear issue](https://linear.app/matterlabs/issue/PLA-255/revamp-db-connection-health-check) for more context. - Tests were reworked to use node framework; some refactoring was also applied to reduce boilerplate. - Additional tests were added to check for invalid EN configurations. -⚠️ Node framework was made the default way to run the EN. There is also a hook to force EN to run the old way, so that we don't have to rollback over small issues. ## Why ❔ - Part of switch to the node framework. ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zk fmt` and `zk lint`.
- Loading branch information
Showing
37 changed files
with
982 additions
and
528 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::time::Duration; | ||
|
||
use zksync_dal::{ConnectionPool, Core, CoreDal as _}; | ||
use zksync_node_framework::{ | ||
implementations::resources::pools::{MasterPool, PoolResource}, | ||
FromContext, IntoContext, StopReceiver, Task, TaskId, WiringError, WiringLayer, | ||
}; | ||
use zksync_shared_metrics::rustc::RUST_METRICS; | ||
use zksync_types::{L1ChainId, L2ChainId}; | ||
|
||
use super::EN_METRICS; | ||
|
||
#[derive(Debug)] | ||
pub struct ExternalNodeMetricsLayer { | ||
pub l1_chain_id: L1ChainId, | ||
pub l2_chain_id: L2ChainId, | ||
pub postgres_pool_size: u32, | ||
} | ||
|
||
#[derive(Debug, FromContext)] | ||
pub struct Input { | ||
pub master_pool: PoolResource<MasterPool>, | ||
} | ||
|
||
#[derive(Debug, IntoContext)] | ||
pub struct Output { | ||
#[context(task)] | ||
pub task: ProtocolVersionMetricsTask, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl WiringLayer for ExternalNodeMetricsLayer { | ||
type Input = Input; | ||
type Output = Output; | ||
|
||
fn layer_name(&self) -> &'static str { | ||
"external_node_metrics" | ||
} | ||
|
||
async fn wire(self, input: Self::Input) -> Result<Self::Output, WiringError> { | ||
RUST_METRICS.initialize(); | ||
EN_METRICS.observe_config(self.l1_chain_id, self.l2_chain_id, self.postgres_pool_size); | ||
|
||
let pool = input.master_pool.get_singleton().await?; | ||
let task = ProtocolVersionMetricsTask { pool }; | ||
Ok(Output { task }) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ProtocolVersionMetricsTask { | ||
pool: ConnectionPool<Core>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Task for ProtocolVersionMetricsTask { | ||
fn id(&self) -> TaskId { | ||
"en_protocol_version_metrics".into() | ||
} | ||
|
||
async fn run(self: Box<Self>, mut stop_receiver: StopReceiver) -> anyhow::Result<()> { | ||
const QUERY_INTERVAL: Duration = Duration::from_secs(10); | ||
|
||
while !*stop_receiver.0.borrow_and_update() { | ||
let maybe_protocol_version = self | ||
.pool | ||
.connection() | ||
.await? | ||
.protocol_versions_dal() | ||
.last_used_version_id() | ||
.await; | ||
if let Some(version) = maybe_protocol_version { | ||
EN_METRICS.protocol_version.set(version as u64); | ||
} | ||
|
||
tokio::time::timeout(QUERY_INTERVAL, stop_receiver.0.changed()) | ||
.await | ||
.ok(); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.