Skip to content
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

all: Make graphman chain call-cache remove safer #4397

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub trait ChainStore: Send + Sync + 'static {
) -> Result<Vec<transaction_receipt::LightTransactionReceipt>, StoreError>;

/// Clears call cache of the chain for the given `from` and `to` block number.
async fn clear_call_cache(&self, from: Option<i32>, to: Option<i32>) -> Result<(), Error>;
async fn clear_call_cache(&self, from: BlockNumber, to: BlockNumber) -> Result<(), Error>;
}

pub trait EthereumCallCache: Send + Sync + 'static {
Expand Down
40 changes: 31 additions & 9 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clap::{Parser, Subcommand};
use config::PoolSize;
use git_testament::{git_testament, render_testament};
use graph::bail;
use graph::prelude::BLOCK_NUMBER_MAX;
use graph::{data::graphql::effort::LoadManager, prelude::chrono, prometheus::Registry};
use graph::{
log::logger,
Expand Down Expand Up @@ -455,14 +457,19 @@ pub enum ChainCommand {
pub enum CallCacheCommand {
/// Remove the call cache of the specified chain.
///
/// If block numbers are not mentioned in `--from` and `--to`, then all the call cache will be
/// removed.
/// Either remove entries in the range `--from` and `--to`, or remove
/// the entire cache with `--remove-entire-cache`. Removing the entire
/// cache can reduce indexing performance significantly and should
/// generally be avoided.
Remove {
/// Remove the entire cache
#[clap(long, conflicts_with_all = &["from", "to"])]
remove_entire_cache: bool,
/// Starting block number
#[clap(long, short)]
#[clap(long, short, conflicts_with = "remove-entire-cache", requires = "to")]
from: Option<i32>,
/// Ending block number
#[clap(long, short)]
#[clap(long, short, conflicts_with = "remove-entire-cache", requires = "from")]
to: Option<i32>,
},
}
Expand Down Expand Up @@ -1187,12 +1194,27 @@ async fn main() -> anyhow::Result<()> {
let chain_store = ctx.chain_store(&chain_name)?;
truncate(chain_store, force)
}
CallCache { method, chain_name } => match method {
CallCacheCommand::Remove { from, to } => {
let chain_store = ctx.chain_store(&chain_name)?;
commands::chain::clear_call_cache(chain_store, from, to).await
CallCache { method, chain_name } => {
match method {
CallCacheCommand::Remove {
from,
to,
remove_entire_cache,
} => {
let chain_store = ctx.chain_store(&chain_name)?;
if !remove_entire_cache && from.is_none() && to.is_none() {
bail!("you must specify either --from and --to or --remove-entire-cache");
}
let (from, to) = if remove_entire_cache {
(0, BLOCK_NUMBER_MAX)
} else {
// Clap makes sure that this does not panic
(from.unwrap(), to.unwrap())
};
commands::chain::clear_call_cache(chain_store, from, to).await
}
}
},
}
}
}
Stats(cmd) => {
Expand Down
9 changes: 6 additions & 3 deletions node/src/manager/commands/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ pub async fn list(primary: ConnectionPool, store: Arc<BlockStore>) -> Result<(),

pub async fn clear_call_cache(
chain_store: Arc<ChainStore>,
from: Option<i32>,
to: Option<i32>,
from: i32,
to: i32,
) -> Result<(), Error> {
println!(
"Removing entries for blocks from {from} to {to} from the call cache for `{}`",
chain_store.chain
);
chain_store.clear_call_cache(from, to).await?;
println!("The call cache has cleared");
Ok(())
}

Expand Down
80 changes: 30 additions & 50 deletions store/postgres/src/chain_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,66 +1050,43 @@ mod data {
pub(super) fn clear_call_cache(
&self,
conn: &PgConnection,
from: Option<i32>,
to: Option<i32>,
head: BlockNumber,
from: BlockNumber,
to: BlockNumber,
) -> Result<(), Error> {
if from.is_none() && to.is_none() {
// If both `from` and `to` arguments are equal to `None`, then truncation should be
// preferred over deletion as it is a faster operation.
if from <= 0 && to >= head {
// We are removing the entire cache. Truncating is much
// faster in that case
self.truncate_call_cache(conn)?;
return Ok(());
}
match self {
Storage::Shared => {
use public::eth_call_cache as cache;
let mut delete_stmt = diesel::delete(cache::table).into_boxed();
if let Some(from) = from {
delete_stmt = delete_stmt.filter(cache::block_number.ge(from));
}
if let Some(to) = to {
delete_stmt = delete_stmt.filter(cache::block_number.le(to))
}
delete_stmt.execute(conn).map_err(Error::from)?;
diesel::delete(
cache::table
.filter(cache::block_number.ge(from))
.filter(cache::block_number.le(to)),
)
.execute(conn)
.map_err(Error::from)?;
Ok(())
}
Storage::Private(Schema { call_cache, .. }) => match (from, to) {
Storage::Private(Schema { call_cache, .. }) => {
// Because they are dynamically defined, our private call cache tables can't
// implement all the required traits for deletion. This means we can't use Diesel
// DSL with them and must rely on the `sql_query` function instead.
(Some(from), None) => {
let query =
format!("delete from {} where block_number >= $1", call_cache.qname);
sql_query(query)
.bind::<Integer, _>(from)
.execute(conn)
.map_err(Error::from)?;
Ok(())
}
(None, Some(to)) => {
let query =
format!("delete from {} where block_number <= $1", call_cache.qname);
sql_query(query)
.bind::<Integer, _>(to)
.execute(conn)
.map_err(Error::from)?;
Ok(())
}
(Some(from), Some(to)) => {
let query = format!(
"delete from {} where block_number >= $1 and block_number <= $2",
call_cache.qname
);
sql_query(query)
.bind::<Integer, _>(from)
.bind::<Integer, _>(to)
.execute(conn)
.map_err(Error::from)?;
Ok(())
}
(None, None) => {
unreachable!("truncation was handled at the beginning of this function");
}
},
let query = format!(
"delete from {} where block_number >= $1 and block_number <= $2",
call_cache.qname
);
sql_query(query)
.bind::<Integer, _>(from)
.bind::<Integer, _>(to)
.execute(conn)
.map_err(Error::from)
.map(|_| ())
}
}
}

Expand Down Expand Up @@ -1820,9 +1797,12 @@ impl ChainStoreTrait for ChainStore {
.await
}

async fn clear_call_cache(&self, from: Option<i32>, to: Option<i32>) -> Result<(), Error> {
async fn clear_call_cache(&self, from: BlockNumber, to: BlockNumber) -> Result<(), Error> {
let conn = self.get_conn()?;
self.storage.clear_call_cache(&conn, from, to)
if let Some(head) = self.chain_head_block(&self.chain)? {
self.storage.clear_call_cache(&conn, head, from, to)?;
}
Ok(())
}

async fn transaction_receipts_in_block(
Expand Down