Skip to content

Commit

Permalink
Working query impl
Browse files Browse the repository at this point in the history
  • Loading branch information
emostov committed Oct 2, 2020
1 parent 791f0cf commit b3ba021
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 0 additions & 2 deletions bin/polkadot-archive/test_conf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ cache_size = 128
block_workers = 8
# Number of 64KB Heap Pages to allocate for WASM execution
wasm_pages = 2048
# Maximum number of blocks to index at a time. Useful for preventing cpu usage spikes.
max_block_load = 100_000

db_host = "localhost"
db_port = "5432"
Expand Down
23 changes: 13 additions & 10 deletions src/actors/workers/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,29 +103,32 @@ where
/// gets any blocks that are missing from database and indexes those.
/// sets the `last_max` value.
async fn re_index(&mut self) -> Result<()> {
let mut conn = self.db.send(GetState::Conn.into()).await?.await?.conn();
let mut conn = self.db.send(GetState::Conn.into()).await?.await?.conn();
let cur_max = if let Some(m) = queries::max_block(&mut conn).await? {
m
} else {
// if the query return `None` the the blocks table is empty
m
} else {
// if the query return `None` the the blocks table is empty
log::info!("{} missing blocks", 0);
return Ok(());
}
};

let mut missing_blocks = 0;
let mut min = self.last_max;
loop {
let batch = queries::missing_blocks_min_max(&mut conn, self.last_max, self.max_block_load).await?;
log::info!("batch len: {}", batch.len());
let batch =
queries::missing_blocks_min_max(&mut conn, min, self.max_block_load).await?;
log::info!("batch len: {}", batch.len());
if batch.len() > 0 {
missing_blocks += batch.len();
min += self.max_block_load;
self.collect_and_send(move |n| batch.contains(&n)).await?;
} else {
break;
}
}

self.last_max = cur_max;
}
self.last_max = cur_max;
log::info!("{} missing blocks", missing_blocks);

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/actors/workers/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<B: BlockT + Unpin> Metadata<B> {
{
let hash = blk.inner.block.header().hash();
self.meta_checker(blk.spec, hash).await?;
self.addr.send(blk.into()).await?;
self.addr.send(blk.into()).await?.await;
Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/database/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub(crate) async fn missing_blocks(conn: &mut PgConnection) -> Result<Vec<u32>>
.collect())
}

/// Find missing blocks from the relational database betwen numbers `min` and
/// MAX(block_num). LIMIT result to length `max_block_load`.
pub(crate) async fn missing_blocks_min_max(
conn: &mut PgConnection,
min: u32,
Expand Down

0 comments on commit b3ba021

Please sign in to comment.