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

feat: improve rollback mechanism #4

Merged
merged 1 commit into from
Oct 9, 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
40 changes: 26 additions & 14 deletions src/event_reader_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ pub enum ResyncOrder {
Historical,
}

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum Rollback {
#[default]
None,
Beginning,
Signature(SolanaSignature),
}

#[derive(derive_builder::Builder)]
pub struct EventsReader<TransactionConsumerFn, EventRecipient, E>
where
Expand Down Expand Up @@ -126,8 +134,8 @@ where
pub resync_signatures_chunk_size: Option<usize>,
pub resync_ptr_setter: Arc<dyn Send + Sync + Fn(u64) -> BoxFuture<'static, Result<()>>>,
pub resync_order: ResyncOrder,
#[builder(default = "Arc::new(RwLock::new(None))")]
pub resync_rollback: Arc<RwLock<Option<SolanaSignature>>>,
#[builder(default = "Arc::new(RwLock::new(Rollback::None))")]
pub resync_rollback: Arc<RwLock<Rollback>>,
pub live_events_transaction_request_param: TransactionRequestParams,
}

Expand Down Expand Up @@ -488,18 +496,22 @@ where
self: &Arc<Self>,
last_transaction: Option<SolanaSignature>,
) -> Result<()> {
if let Some(last_transaction) = self
.resync_rollback
.write()
.ok()
.and_then(|mut write| {
write.take().map(|tx| {
info!("Found rollback to {tx} transaction");
tx
})
})
.or(last_transaction)
{
let next_resync_ptr = match self.resync_rollback.write().as_deref() {
Ok(Rollback::Beginning) => {
info!("Reset last resynced tx");
self.local_storage
.reset_last_resynced_transaction(&self.program_id)?;
return Ok(());
}
Ok(Rollback::None) => last_transaction,
Err(err) => {
error!("Error while lock rollback: {err:?}");
last_transaction
}
Ok(Rollback::Signature(signature)) => Some(*signature),
};

if let Some(last_transaction) = next_resync_ptr {
info!("Set last resynced tx to {last_transaction} transaction");
self.local_storage
.set_last_resynced_transaction(&self.program_id, &last_transaction)?;
Expand Down
14 changes: 14 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ pub trait ResyncedTransactionsPtrStorage: RegisterTransaction {
program_id: &Pubkey,
transaction: &SolanaSignature,
) -> Result<(), <Self as RegisterTransaction>::Error>;

fn reset_last_resynced_transaction(
&self,
program_id: &Pubkey,
) -> Result<(), <Self as RegisterTransaction>::Error>;
}

#[cfg(feature = "rocksdb")]
Expand Down Expand Up @@ -185,5 +190,14 @@ pub mod rocksdb {

Ok(())
}

fn reset_last_resynced_transaction(
&self,
program_id: &Pubkey,
) -> Result<(), <Self as RegisterTransaction>::Error> {
self.delete([&program_id.to_bytes()[..], LAST_RESYNCED_SUFFIX].concat())?;

Ok(())
}
}
}
Loading