-
Notifications
You must be signed in to change notification settings - Fork 83
Expose a stream with indexer state changes #352
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
Merged
joshstevens19
merged 4 commits into
joshstevens19:master
from
desfero:pawel/stream-indexer-state-change-events
Jan 6, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,46 @@ | ||
| /// Events emitted during the indexing process for external consumption. | ||
| #[derive(Debug, Clone)] | ||
| pub enum RindexerEvent { | ||
| /// The indexing process has completed indexing historical events (happens every restart of the indexer) | ||
| HistoricalIndexingCompleted, | ||
| } | ||
|
|
||
| /// A handle to subscribe to indexer events | ||
| #[derive(Clone)] | ||
| pub struct RindexerEventStream { | ||
| tx: tokio::sync::broadcast::Sender<RindexerEvent>, | ||
| } | ||
|
|
||
| impl Default for RindexerEventStream { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl RindexerEventStream { | ||
| pub fn new() -> Self { | ||
| let (tx, _) = tokio::sync::broadcast::channel(100); | ||
| Self { tx } | ||
| } | ||
|
|
||
| /// Subscribe to indexer events | ||
| pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<RindexerEvent> { | ||
| self.tx.subscribe() | ||
| } | ||
| } | ||
|
|
||
| /// A handle to subscribe to indexer events | ||
| #[derive(Clone)] | ||
| pub struct RindexerEventEmitter { | ||
| tx: tokio::sync::broadcast::Sender<RindexerEvent>, | ||
| } | ||
|
|
||
| impl RindexerEventEmitter { | ||
| pub fn from_stream(stream: RindexerEventStream) -> Self { | ||
| Self { tx: stream.tx.clone() } | ||
| } | ||
|
|
||
| pub fn emit(&self, event: RindexerEvent) { | ||
| let _ = self.tx.send(event); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,14 @@ | ||
| use std::time::Duration; | ||
|
|
||
| pub fn format_duration(duration: Duration) -> String { | ||
| let secs = duration.as_secs(); | ||
| let hours = secs / 3600; | ||
| let minutes = (secs % 3600) / 60; | ||
| let seconds = secs % 60; | ||
|
|
||
| match (hours, minutes) { | ||
| (h, m) if h > 0 => format!("{}h {}m {}s", h, m, seconds), | ||
| (0, m) if m > 0 => format!("{}m {}s", m, seconds), | ||
| _ => format!("{}s", seconds), | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ use tokio::signal; | |
| use tracing::{error, info}; | ||
|
|
||
| use crate::database::clickhouse::setup::SetupClickhouseError; | ||
| use crate::events::RindexerEventEmitter; | ||
| use crate::indexer::start::{start_historical_indexing, start_live_indexing}; | ||
| use crate::{ | ||
| api::{start_graphql_server, GraphqlOverrideSettings, StartGraphqlServerError}, | ||
| database::postgres::{ | ||
|
|
@@ -17,7 +19,7 @@ use crate::{ | |
| health::start_health_server, | ||
| indexer::{ | ||
| no_code::{setup_no_code, SetupNoCodeError}, | ||
| start::{start_indexing, StartIndexingError}, | ||
| start::StartIndexingError, | ||
| ContractEventDependencies, ContractEventDependenciesMapFromRelationshipsError, | ||
| }, | ||
| initiate_shutdown, | ||
|
|
@@ -27,12 +29,13 @@ use crate::{ | |
| storage::RelationshipsAndIndexersError, | ||
| yaml::{read_manifest, ReadManifestError}, | ||
| }, | ||
| setup_clickhouse, setup_info_logger, | ||
| setup_clickhouse, setup_info_logger, RindexerEventStream, | ||
| }; | ||
|
|
||
| pub struct IndexingDetails { | ||
| pub registry: EventCallbackRegistry, | ||
| pub trace_registry: TraceCallbackRegistry, | ||
| pub event_stream: Option<RindexerEventStream>, | ||
| } | ||
|
|
||
| pub struct StartDetails<'a> { | ||
|
|
@@ -250,14 +253,13 @@ pub async fn start_rindexer(details: StartDetails<'_>) -> Result<(), StartRindex | |
| let mut dependencies: Vec<ContractEventDependencies> = | ||
| ContractEventDependencies::parse(&manifest); | ||
|
|
||
| let processed_network_contracts = start_indexing( | ||
| let processed_network_contracts = start_historical_indexing( | ||
| &manifest, | ||
| project_path, | ||
| &dependencies, | ||
| // we index all the historic data first before then applying FKs | ||
| !relationships.is_empty(), | ||
| indexing_details.registry.complete(), | ||
| indexing_details.trace_registry.complete(), | ||
| indexing_details.event_stream.map(RindexerEventEmitter::from_stream), | ||
| ) | ||
| .await?; | ||
|
|
||
|
|
@@ -269,30 +271,27 @@ pub async fn start_rindexer(details: StartDetails<'_>) -> Result<(), StartRindex | |
| // need to handle this | ||
| info!("Applying constraints relationships back to the database as historic resync is complete"); | ||
| Relationship::apply_all(&relationships).await?; | ||
| } | ||
|
|
||
| if manifest.has_any_contracts_live_indexing() { | ||
| info!("Starting live indexing now relationship re-applied.."); | ||
|
|
||
| if dependencies.is_empty() { | ||
| dependencies = | ||
| ContractEventDependencies::map_from_relationships(&relationships)?; | ||
| } else { | ||
| info!("Manual dependency_events found, skipping auto-applying the dependency_events with the relationships"); | ||
| } | ||
|
|
||
| start_indexing( | ||
| &manifest, | ||
| project_path, | ||
| &dependencies, | ||
| false, | ||
| indexing_details | ||
| .registry | ||
| .reapply_after_historic(processed_network_contracts), | ||
| indexing_details.trace_registry.complete(), | ||
| ) | ||
| .await | ||
| .map_err(StartRindexerError::CouldNotStartIndexing)?; | ||
| if manifest.has_any_contracts_live_indexing() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fixes the issue that was affecting historical indexing when there was no relationship Now historical indexing duration log is always there which makes codebase more cohesive |
||
| if dependencies.is_empty() { | ||
| dependencies = | ||
| ContractEventDependencies::map_from_relationships(&relationships)?; | ||
| } else { | ||
| info!("Manual dependency_events found, skipping auto-applying the dependency_events with the relationships"); | ||
| } | ||
|
|
||
| start_live_indexing( | ||
| &manifest, | ||
| project_path, | ||
| &dependencies, | ||
| indexing_details | ||
| .registry | ||
| .reapply_after_historic(processed_network_contracts), | ||
| indexing_details.trace_registry.complete(), | ||
| ) | ||
| .await | ||
| .map_err(StartRindexerError::CouldNotStartIndexing)?; | ||
| } | ||
|
|
||
| // Do not need now with the main shutdown keeping around in-case | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just nit, we don't need to loop through all values in the array