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: Cancel historical backfill process before starting anew #363

Merged

Conversation

morgsmccauley
Copy link
Collaborator

@morgsmccauley morgsmccauley commented Nov 6, 2023

This PR updates Coordinator such that only one Historical Backfill process can exist for a given Indexer Function. Currently, these processes do not have awareness of each other, meaning a developer can trigger multiple simultaneous backfills, with them all writing to the same Redis Stream. The major changes are detailed below.

Streamer/Historical backfill tasks

In this PR, I've introduced the historical_block_processing::Streamer struct. This is used to encapsulate the async task which is spawned for handling the Historical Backfill. I went with "Streamer", as I envision this becoming the single process which handles both historical/real-time messages for a given Indexer, as described in #216 (comment).

Streamer is responsible for starting the async task, and provides a basic API for interacting with it. Right now, that only includes cancelling it. Cancelling a Streamer will drop the existing future, preventing any new messages from being added, and then delete the Redis Stream removing all existing messages.

Indexer Registry Mutex/Async

indexer_registry is the in-memory representation of the given Indexer Registry. Currently, we pass around the MutexGuard to mutate this data, resulting in the lock being held for much longer than is needed, blocking all other consumers from taking the lock. There isn't much contention for this lock, so it isn't a problem right now, but could end up being one in future.

Adding in the Streamer Mutex made mitigating this problem trivial, so I went ahead and made some changes. I've updated the code so that we pass around the Mutex itself, rather than the guard, allowing us to the hold the lock for the least amount of time possible. This required updating most methods within indexer_registry to async, so that we could take the async lock.

With the Mutex being used rather than the MutexGuard, it seemed sensible to add indexer_registry to QueryAPIContext, rather than passing it round individually.

@morgsmccauley morgsmccauley linked an issue Nov 6, 2023 that may be closed by this pull request
@morgsmccauley morgsmccauley force-pushed the 200-stop-historical-processing-on-indexer-redeployment branch from bf90a5f to de1e10b Compare November 7, 2023 01:45
@morgsmccauley morgsmccauley changed the base branch from main to 201-clean-up-sqs-code-in-coordinator-1 November 7, 2023 03:14
@morgsmccauley morgsmccauley changed the title 200 stop historical processing on indexer redeployment feat: Cancel historical backfill process before starting anew Nov 7, 2023
}

pub(crate) async fn process_historical_messages_or_handle_error(
block_height: BlockHeight,
current_block_height: BlockHeight,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got confused about what block_height this actually represents, so ended up renaming it

use unescape::unescape;

use crate::indexer_reducer;
use crate::indexer_reducer::FunctionCallInfo;
use crate::indexer_types::{IndexerFunction, IndexerRegistry};
use indexer_rule_type::indexer_rule::{IndexerRule, IndexerRuleKind, MatchingRule, Status};

pub(crate) fn registry_as_vec_of_indexer_functions(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now done inline

@morgsmccauley morgsmccauley marked this pull request as ready for review November 7, 2023 03:55
@morgsmccauley morgsmccauley requested a review from a team as a code owner November 7, 2023 03:55
.lock()
.await
.entry(function_invocation.account_id.clone())
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also clean up streams here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good call. I'll do that in this issue #346

Base automatically changed from 201-clean-up-sqs-code-in-coordinator-1 to main November 7, 2023 18:13
@morgsmccauley morgsmccauley force-pushed the 200-stop-historical-processing-on-indexer-redeployment branch from 53d599c to b636916 Compare November 7, 2023 19:25
@morgsmccauley morgsmccauley merged commit 0476306 into main Nov 7, 2023
3 checks passed
@morgsmccauley morgsmccauley deleted the 200-stop-historical-processing-on-indexer-redeployment branch November 7, 2023 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop historical processing on indexer redeployment
2 participants