fix: add retry logic in rocksdb

This commit is contained in:
Ludo Galabru
2023-05-01 21:54:05 -04:00
parent 64f621534e
commit 247df2088a
3 changed files with 22 additions and 9 deletions

View File

@@ -755,7 +755,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
let mut missing_blocks = vec![];
for i in 1..=780000 {
if find_block_at_block_height(i, &blocks_db_rw).is_none() {
if find_block_at_block_height(i, 3, &blocks_db_rw).is_none() {
println!("Missing block {i}");
missing_blocks.push(i);
}

View File

@@ -86,7 +86,7 @@ pub async fn scan_bitcoin_chainstate_via_http_using_predicate(
if let Ok(blocks_db) =
open_readonly_hord_db_conn_rocks_db(&config.expected_cache_path(), &ctx)
{
if find_block_at_block_height(end_block as u32, &blocks_db).is_none() {
if find_block_at_block_height(end_block as u32, 3, &blocks_db).is_none() {
hord_blocks_requires_update = true;
}
}

View File

@@ -482,13 +482,26 @@ pub fn find_last_block_inserted(blocks_db: &DB) -> u32 {
}
}
pub fn find_block_at_block_height(block_height: u32, blocks_db: &DB) -> Option<CompactedBlock> {
match blocks_db.get(block_height.to_be_bytes()) {
Ok(Some(ref res)) => {
let res = CompactedBlock::deserialize(&mut std::io::Cursor::new(&res)).unwrap();
Some(res)
pub fn find_block_at_block_height(
block_height: u32,
retry: u8,
blocks_db: &DB,
) -> Option<CompactedBlock> {
let mut attempt = 0;
loop {
match blocks_db.get(block_height.to_be_bytes()) {
Ok(Some(ref res)) => {
let res = CompactedBlock::deserialize(&mut std::io::Cursor::new(&res)).unwrap();
return Some(res);
}
_ => {
attempt += 1;
std::thread::sleep(std::time::Duration::from_secs(1));
if attempt > retry {
return None;
}
}
}
_ => None,
}
}
@@ -1036,7 +1049,7 @@ pub fn retrieve_satoshi_point_using_local_storage(
hops += 1;
let block = match local_block_cache.get(&ordinal_block_number) {
Some(block) => block,
None => match find_block_at_block_height(ordinal_block_number, &blocks_db) {
None => match find_block_at_block_height(ordinal_block_number, 3, &blocks_db) {
Some(block) => {
local_block_cache.insert(ordinal_block_number, block);
local_block_cache.get(&ordinal_block_number).unwrap()