From 34627f4266e8bc0c002226402f0cba561a6f45e2 Mon Sep 17 00:00:00 2001 From: Abdul Basit Date: Mon, 3 Jun 2024 19:58:53 +0500 Subject: [PATCH] revert if failed to store fetched resource --- src/data_source/fetching/block.rs | 3 +++ src/data_source/fetching/leaf.rs | 3 +++ src/data_source/fetching/vid.rs | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/data_source/fetching/block.rs b/src/data_source/fetching/block.rs index 31e060b19..a290cda56 100644 --- a/src/data_source/fetching/block.rs +++ b/src/data_source/fetching/block.rs @@ -277,6 +277,9 @@ where { let mut storage = self.fetcher.storage.write().await; if let Err(err) = store_block(&mut *storage, block).await { + // Rollback the transaction if insert fails + // This prevents subsequent queries from failing, as they would be part of the same transaction block. + storage.revert().await; // It is unfortunate if this fails, but we can still proceed by returning // the block that we fetched, keeping it in memory. Simply log the error and // move on. diff --git a/src/data_source/fetching/leaf.rs b/src/data_source/fetching/leaf.rs index 49c828aa0..7585554e7 100644 --- a/src/data_source/fetching/leaf.rs +++ b/src/data_source/fetching/leaf.rs @@ -216,6 +216,9 @@ where tracing::info!("fetched leaf {height}"); let mut storage = fetcher.storage.write().await; if let Err(err) = store_leaf(&mut *storage, leaf).await { + // Rollback the transaction if insert fails + // This prevents subsequent queries from failing, as they would be part of the same transaction block. + storage.revert().await; // It is unfortunate if this fails, but we can still proceed by // returning the leaf that we fetched, keeping it in memory. // Simply log the error and move on. diff --git a/src/data_source/fetching/vid.rs b/src/data_source/fetching/vid.rs index 5cddd24d5..aaa52adaa 100644 --- a/src/data_source/fetching/vid.rs +++ b/src/data_source/fetching/vid.rs @@ -203,6 +203,9 @@ where { let mut storage = self.fetcher.storage.write().await; if let Err(err) = store_vid_common(&mut *storage, common).await { + // Rollback the transaction if insert fails + // This prevents subsequent queries from failing, as they would be part of the same transaction block. + storage.revert().await; // It is unfortunate if this fails, but we can still proceed by returning // the block that we fetched, keeping it in memory. Simply log the error and // move on.