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

chore(spool): Enable periodic unspool #2993

Merged
merged 13 commits into from
Jan 25, 2024
Merged

chore(spool): Enable periodic unspool #2993

merged 13 commits into from
Jan 25, 2024

Conversation

olksdr
Copy link
Contributor

@olksdr olksdr commented Jan 23, 2024

Currently in order to trigger unspool of buffered envelopes the new project state update must be received from the upstream, which sometimes can take longer time.

To make unspool action a bit more predictable let's enable periodic unspool initiated by project cache. This way:

  • spool service does not have to know when to unspool
  • spool service just has to serve the request and communicate back the results
  • project cache will take care of the initiating the action and only queue the envelopes when certain criteria are met

part of: https://github.com/getsentry/team-ingest/issues/267

@olksdr olksdr self-assigned this Jan 23, 2024
@olksdr olksdr requested a review from a team as a code owner January 23, 2024 17:15
relay-config/src/config.rs Outdated Show resolved Hide resolved
relay-server/src/services/project_cache.rs Outdated Show resolved Hide resolved

// If there is nothing spooled, schedule the next check a little bit later.
if keys.is_empty() {
self.schedule_unspool();
Copy link
Member

Choose a reason for hiding this comment

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

Is this to schedule the unspool before resetting?

Can we return in this branch to make it clear, e.g.

self.schedule_unspool();
self.buffer_unspool_backoff.reset();
return;

Copy link
Member

Choose a reason for hiding this comment

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

nit: Should the reset of the backoff be part of schedule_unspool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, since if there is nothing to unspool, I want to wait a little bit longer before trying to unspool again. Once the unspool finishes with anything, we reset the backoff to initial state (which currently 100ms)

relay-server/src/services/project_cache.rs Outdated Show resolved Hide resolved
relay-server/src/services/project_cache.rs Outdated Show resolved Hide resolved
relay-server/src/services/project_cache.rs Outdated Show resolved Hide resolved
@@ -913,6 +895,48 @@ impl ProjectCacheBroker {
}
}

/// Returns backoff timeout for an unspool attempt.
fn next_unspool_attempt(&mut self) -> Duration {
self.config.spool_envelopes_unspool_interval() + self.buffer_unspool_backoff.next_backoff()
Copy link
Member

Choose a reason for hiding this comment

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

nit: Instead of adding an interval to a backoff, could we use ExponentialBackoff instead of RetryBackoff and initialize it with an initial_interval of self.config.spool_envelopes_unspool_interval()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you mean to use backoff::ExponentialBackoff directly?

We have a little better interface and we also could add some functions to make the setting of things for it more convenient.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. We could make RetryBackoff more flexible (i.e. accept a custom initial delay), but that's out of scope for this PR.

relay-server/src/services/project_cache.rs Outdated Show resolved Hide resolved

// If there is nothing spooled, schedule the next check a little bit later.
if keys.is_empty() {
self.schedule_unspool();
Copy link
Member

Choose a reason for hiding this comment

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

nit: Should the reset of the backoff be part of schedule_unspool?

if self
.projects
.get(project_key)
.map_or(false, |state| state.valid_state().is_some())
Copy link
Member

Choose a reason for hiding this comment

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

Is the project config actually being refreshed anywhere now? In the previous impl we had .get_cached_state(project_cache, false) which did an implicit fetch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will be refreshed as it was before like normal refresh operation.
Here I just want to check for the valid states, and if there are try to unspool them.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. It might not be related to the goal of this PR but I feel like we should trigger a prefetch at this point. Otherwise we still depend on incoming traffic to get old data unspooled, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's a valid point.
I've switched to using get_cached_state instead, which will trigger the refresh of the project state if it's not valid or expired.

@@ -683,8 +663,10 @@ impl ProjectCacheBroker {
no_cache,
);

if !state.invalid() {
self.dequeue(project_key);
// Schedule unspool if nothing is running at the moment.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we check here too whether there are enough buffer guards available?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, the check is done in handle_periodic_unspool now.

@olksdr olksdr requested review from jjbayer, iker-barriocanal, Dav1dde and a team January 24, 2024 14:40
@@ -913,6 +895,48 @@ impl ProjectCacheBroker {
}
}

/// Returns backoff timeout for an unspool attempt.
fn next_unspool_attempt(&mut self) -> Duration {
self.config.spool_envelopes_unspool_interval() + self.buffer_unspool_backoff.next_backoff()
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. We could make RetryBackoff more flexible (i.e. accept a custom initial delay), but that's out of scope for this PR.

if self
.projects
.get(project_key)
.map_or(false, |state| state.valid_state().is_some())
Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. It might not be related to the goal of this PR but I feel like we should trigger a prefetch at this point. Otherwise we still depend on incoming traffic to get old data unspooled, right?

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -1268,4 +1309,89 @@ mod tests {
assert!(buffer_rx.try_recv().is_err())
}
}

#[tokio::test]
async fn periodic_unspool() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jjbayer Added a test, if you want to have a look.

Copy link
Member

Choose a reason for hiding this comment

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

Nice!

@olksdr olksdr merged commit c892dd9 into master Jan 25, 2024
20 checks passed
@olksdr olksdr deleted the chore/unspool-re-tune branch January 25, 2024 10:37
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.

4 participants