-
Notifications
You must be signed in to change notification settings - Fork 82
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
Migrate Default Processor to the SDK #567
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1d2223a
Init
dermanyang cfb0220
Implement storer
dermanyang 786de18
correct function input
dermanyang 2d63d98
Merge branch 'main' into sdy/migrate-default
dermanyang ae2635e
Formatting + docstrings
dermanyang 676b647
qualify async_trait
dermanyang 2399873
merge
dermanyang 087fced
Remove async block
dermanyang 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 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
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,139 @@ | ||
use crate::{ | ||
config::{ | ||
db_config::DbConfig, indexer_processor_config::IndexerProcessorConfig, | ||
processor_config::ProcessorConfig, | ||
}, | ||
steps::{ | ||
common::get_processor_status_saver, | ||
default_processor::{default_extractor::DefaultExtractor, default_storer::DefaultStorer}, | ||
}, | ||
utils::{ | ||
chain_id::check_or_update_chain_id, | ||
database::{new_db_pool, run_migrations, ArcDbPool}, | ||
starting_version::get_starting_version, | ||
}, | ||
}; | ||
use anyhow::Result; | ||
use aptos_indexer_processor_sdk::{ | ||
aptos_indexer_transaction_stream::{TransactionStream, TransactionStreamConfig}, | ||
builder::ProcessorBuilder, | ||
common_steps::{ | ||
TransactionStreamStep, VersionTrackerStep, DEFAULT_UPDATE_PROCESSOR_STATUS_SECS, | ||
}, | ||
traits::{processor_trait::ProcessorTrait, IntoRunnableStep}, | ||
}; | ||
use async_trait::async_trait; | ||
use processor::worker::TableFlags; | ||
use tracing::{debug, info}; | ||
|
||
pub struct DefaultProcessor { | ||
pub config: IndexerProcessorConfig, | ||
pub db_pool: ArcDbPool, | ||
} | ||
|
||
impl DefaultProcessor { | ||
pub async fn new(config: IndexerProcessorConfig) -> Result<Self> { | ||
match config.db_config { | ||
DbConfig::PostgresConfig(ref postgres_config) => { | ||
let conn_pool = new_db_pool( | ||
&postgres_config.connection_string, | ||
Some(postgres_config.db_pool_size), | ||
) | ||
.await | ||
.map_err(|e| { | ||
anyhow::anyhow!( | ||
"Failed to create connection pool for PostgresConfig: {:?}", | ||
e | ||
) | ||
})?; | ||
|
||
Ok(Self { | ||
config, | ||
db_pool: conn_pool, | ||
}) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ProcessorTrait for DefaultProcessor { | ||
fn name(&self) -> &'static str { | ||
self.config.processor_config.name() | ||
} | ||
|
||
async fn run_processor(&self) -> Result<()> { | ||
// Run migrations | ||
match self.config.db_config { | ||
DbConfig::PostgresConfig(ref postgres_config) => { | ||
run_migrations( | ||
postgres_config.connection_string.clone(), | ||
self.db_pool.clone(), | ||
) | ||
.await; | ||
}, | ||
} | ||
|
||
// Merge the starting version from config and the latest processed version from the DB | ||
let starting_version = get_starting_version(&self.config, self.db_pool.clone()).await?; | ||
|
||
// Check and update the ledger chain id to ensure we're indexing the correct chain | ||
let grpc_chain_id = TransactionStream::new(self.config.transaction_stream_config.clone()) | ||
.await? | ||
.get_chain_id() | ||
.await?; | ||
check_or_update_chain_id(grpc_chain_id as i64, self.db_pool.clone()).await?; | ||
|
||
let processor_config = match self.config.processor_config.clone() { | ||
ProcessorConfig::DefaultProcessor(processor_config) => processor_config, | ||
_ => { | ||
return Err(anyhow::anyhow!( | ||
"Invalid processor config for DefaultProcessor: {:?}", | ||
self.config.processor_config | ||
)) | ||
}, | ||
}; | ||
let channel_size = processor_config.channel_size; | ||
let deprecated_table_flags = TableFlags::from_set(&processor_config.deprecated_tables); | ||
|
||
// Define processor steps | ||
let transaction_stream = TransactionStreamStep::new(TransactionStreamConfig { | ||
starting_version: Some(starting_version), | ||
..self.config.transaction_stream_config.clone() | ||
}) | ||
.await?; | ||
let default_extractor = DefaultExtractor { | ||
deprecated_table_flags, | ||
}; | ||
let default_storer = DefaultStorer::new(self.db_pool.clone(), processor_config); | ||
let version_tracker = VersionTrackerStep::new( | ||
get_processor_status_saver(self.db_pool.clone(), self.config.clone()), | ||
DEFAULT_UPDATE_PROCESSOR_STATUS_SECS, | ||
); | ||
|
||
// Connect processor steps together | ||
let (_, buffer_receiver) = ProcessorBuilder::new_with_inputless_first_step( | ||
transaction_stream.into_runnable_step(), | ||
) | ||
.connect_to(default_extractor.into_runnable_step(), channel_size) | ||
.connect_to(default_storer.into_runnable_step(), channel_size) | ||
.connect_to(version_tracker.into_runnable_step(), channel_size) | ||
.end_and_return_output_receiver(channel_size); | ||
|
||
// (Optional) Parse the results | ||
loop { | ||
match buffer_receiver.recv().await { | ||
Ok(txn_context) => { | ||
debug!( | ||
"Finished processing versions [{:?}, {:?}]", | ||
txn_context.metadata.start_version, txn_context.metadata.end_version, | ||
); | ||
}, | ||
Err(e) => { | ||
info!("No more transactions in channel: {:?}", e); | ||
break 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod default_processor; | ||
pub mod events_processor; | ||
pub mod fungible_asset_processor; |
74 changes: 74 additions & 0 deletions
74
rust/sdk-processor/src/steps/default_processor/default_extractor.rs
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,74 @@ | ||
use aptos_indexer_processor_sdk::{ | ||
aptos_protos::transaction::v1::Transaction, | ||
traits::{async_step::AsyncRunType, AsyncStep, NamedStep, Processable}, | ||
types::transaction_context::TransactionContext, | ||
utils::errors::ProcessorError, | ||
}; | ||
use async_trait::async_trait; | ||
use processor::{ | ||
db::common::models::default_models::{ | ||
block_metadata_transactions::BlockMetadataTransactionModel, | ||
move_tables::{CurrentTableItem, TableItem, TableMetadata}, | ||
}, | ||
processors::default_processor::process_transactions, | ||
worker::TableFlags, | ||
}; | ||
pub const MIN_TRANSACTIONS_PER_RAYON_JOB: usize = 64; | ||
|
||
pub struct DefaultExtractor | ||
where | ||
Self: Sized + Send + 'static, | ||
{ | ||
pub deprecated_table_flags: TableFlags, | ||
} | ||
|
||
#[async_trait] | ||
impl Processable for DefaultExtractor { | ||
type Input = Vec<Transaction>; | ||
type Output = ( | ||
Vec<BlockMetadataTransactionModel>, | ||
Vec<TableItem>, | ||
Vec<CurrentTableItem>, | ||
Vec<TableMetadata>, | ||
); | ||
type RunType = AsyncRunType; | ||
|
||
async fn process( | ||
&mut self, | ||
transactions: TransactionContext<Vec<Transaction>>, | ||
) -> Result< | ||
Option< | ||
TransactionContext<( | ||
Vec<BlockMetadataTransactionModel>, | ||
Vec<TableItem>, | ||
Vec<CurrentTableItem>, | ||
Vec<TableMetadata>, | ||
)>, | ||
>, | ||
ProcessorError, | ||
> { | ||
let flags = self.deprecated_table_flags; | ||
let (block_metadata_transactions, table_items, current_table_items, table_metadata) = | ||
tokio::task::spawn_blocking(move || process_transactions(transactions.data, flags)) | ||
.await | ||
.expect("Failed to spawn_blocking for TransactionModel::from_transactions"); | ||
|
||
Ok(Some(TransactionContext { | ||
data: ( | ||
block_metadata_transactions, | ||
table_items, | ||
current_table_items, | ||
table_metadata, | ||
), | ||
metadata: transactions.metadata, | ||
})) | ||
} | ||
} | ||
|
||
impl AsyncStep for DefaultExtractor {} | ||
|
||
impl NamedStep for DefaultExtractor { | ||
fn name(&self) -> String { | ||
"DefaultExtractor".to_string() | ||
} | ||
} |
Oops, something went wrong.
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.
This spawn_blocking may be unnecessary. SDK runs AsyncStep inside of its own thread :) https://github.com/aptos-labs/aptos-indexer-processors-sdk/blob/faf0469441fc27fd62bff4eed0dfab89e0bd5ddd/aptos-indexer-processors-sdk/sdk/src/traits/async_step.rs#L88
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.
oh nice good catch