Skip to content

Commit

Permalink
add --remove-block-data flag
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed Sep 22, 2023
1 parent a818c19 commit 80e2488
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
6 changes: 6 additions & 0 deletions packages/fuel-indexer-database/postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ pub async fn load_block_data(
Ok(blocks)
}

pub async fn remove_block_data(
conn: &mut PoolConnection<Postgres>,
) -> sqlx::Result<usize> {
execute_query(conn, format!("DELETE FROM index_block_data;")).await
}

/// Return all indexers registered to this indexer serivce.
#[cfg_attr(feature = "metrics", metrics)]
pub async fn all_registered_indexers(
Expand Down
11 changes: 11 additions & 0 deletions packages/fuel-indexer-database/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ pub async fn load_block_data(
}
}

/// Remove all stored `BlockData` from the database.
pub async fn remove_block_data(
conn: &mut IndexerConnection,
) -> sqlx::Result<usize> {
match conn {
IndexerConnection::Postgres(ref mut c) => {
postgres::remove_block_data(c).await
}
}
}

/// Return all indexers registered to this indexer serivce.
pub async fn all_registered_indexers(
conn: &mut IndexerConnection,
Expand Down
7 changes: 7 additions & 0 deletions packages/fuel-indexer-lib/src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ pub struct IndexerArgs {
help = "Store blocks in the database and use these stored blocks to fast-forward an indexer starting up."
)]
pub enable_block_store: bool,

/// Remove all stored blocks. Use this flag together with --enable-block-store to redownload block data afresh.
#[clap(
long,
help = "Remove all stored blocks. Use this flag together with --enable-block-store to redownload block data afresh."
)]
pub remove_stored_blocks: bool,
}

#[derive(Debug, Parser, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions packages/fuel-indexer-lib/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Default for IndexerArgs {
accept_sql_queries: defaults::ACCEPT_SQL,
block_page_size: defaults::NODE_BLOCK_PAGE_SIZE,
enable_block_store: defaults::ENABLE_BLOCK_STORE,
remove_stored_blocks: defaults::REMOVE_STORED_BLOCKS,
}
}
}
Expand Down Expand Up @@ -138,6 +139,7 @@ pub struct IndexerConfig {
pub accept_sql_queries: bool,
pub node_block_page_size: usize,
pub enable_block_store: bool,
pub remove_stored_blocks: bool,
}

impl Default for IndexerConfig {
Expand All @@ -160,6 +162,7 @@ impl Default for IndexerConfig {
accept_sql_queries: defaults::ACCEPT_SQL,
node_block_page_size: defaults::NODE_BLOCK_PAGE_SIZE,
enable_block_store: defaults::ENABLE_BLOCK_STORE,
remove_stored_blocks: defaults::REMOVE_STORED_BLOCKS,
}
}
}
Expand Down Expand Up @@ -242,6 +245,7 @@ impl From<IndexerArgs> for IndexerConfig {
accept_sql_queries: args.accept_sql_queries,
node_block_page_size: args.block_page_size,
enable_block_store: args.enable_block_store,
remove_stored_blocks: args.remove_stored_blocks,
};

config
Expand Down Expand Up @@ -330,6 +334,7 @@ impl From<ApiServerArgs> for IndexerConfig {
accept_sql_queries: args.accept_sql_queries,
node_block_page_size: defaults::NODE_BLOCK_PAGE_SIZE,
enable_block_store: defaults::ENABLE_BLOCK_STORE,
remove_stored_blocks: defaults::REMOVE_STORED_BLOCKS
};

config
Expand Down
3 changes: 3 additions & 0 deletions packages/fuel-indexer-lib/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,6 @@ pub const ACCEPT_SQL: bool = false;

/// Store blocks in the database and use these stored blocks to fast-forward an indexer starting up.
pub const ENABLE_BLOCK_STORE: bool = false;

/// Remove all stored blocks from the database.
pub const REMOVE_STORED_BLOCKS: bool = false;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ OPTIONS:

--enable-block-store
Store blocks in the database and use these stored blocks to fast-forward an indexer
starting it up.
starting up.

--fuel-node-host <FUEL_NODE_HOST>
Host of the running Fuel node. [default: localhost]
Expand Down Expand Up @@ -101,6 +101,10 @@ OPTIONS:
--remove-data
When replacing an indexer, also remove the indexed data.

--remove-stored-blocks
Remove all stored blocks. Use this flag together with --enable-block-store to redownload
block data afresh.

--replace-indexer
Whether to allow replacing an existing indexer. If not specified, an attempt to deploy
over an existing indexer results in an error.
Expand Down
26 changes: 16 additions & 10 deletions packages/fuel-indexer/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,24 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {

info!("Configuration: {:?}", config);

#[allow(unused)]
let (tx, rx) = channel::<ServiceRequest>(defaults::SERVICE_REQUEST_CHANNEL_SIZE);

let pool = IndexerConnectionPool::connect(&config.database.to_string()).await?;

if config.remove_stored_blocks {
info!("Removing stored blocks.");
let mut conn = pool.acquire().await?;
let count = queries::remove_block_data(&mut conn).await?;
info!("Succesfully removed {count} blocks.");
}

if config.enable_block_store {
subsystems.spawn(crate::service::create_block_sync_task(
config.clone(),
pool.clone(),
));
};

let (tx, rx) = channel::<ServiceRequest>(defaults::SERVICE_REQUEST_CHANNEL_SIZE);

if config.run_migrations {
let mut c = pool.acquire().await?;
queries::run_migration(&mut c).await?;
Expand Down Expand Up @@ -148,13 +161,6 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {
}
});

if config.enable_block_store {
subsystems.spawn(crate::service::create_block_sync_task(
config.clone(),
pool.clone(),
));
};

#[cfg(feature = "fuel-core-lib")]
{
use fuel_core::service::{Config, FuelService};
Expand Down

0 comments on commit 80e2488

Please sign in to comment.