Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ref(actix): Migrate Aggregator actor to new tokio runtime [INGEST-1645] #1508

Merged
merged 25 commits into from
Oct 11, 2022

Conversation

olksdr
Copy link
Contributor

@olksdr olksdr commented Oct 2, 2022

Let's convert Metric Aggregator actor to tokio runtime.

In this PR:

  • Create AggregatorService which runs on tokio runtime
  • Some cleanup of the old messages and handlers
  • Make sure all the Rust tests are working

Some open questions:

  • how to test the new service when the both old and new runtime is required - like handling the shutdown message from the. controller?
  • currently, just to keep changes to minimum, I use following
     if let Some(registry) = REGISTRY.get() {
                    registry
                        .aggregator
                        .send(InsertMetrics::new(self.project_key, metrics))
                }
    
    to get the aggregator from the registry and then send the message to it. This must be done better - share the channels with dependants?

@olksdr olksdr self-assigned this Oct 2, 2022
@olksdr olksdr requested a review from a team October 2, 2022 11:22
Copy link
Contributor

@flub flub left a comment

Choose a reason for hiding this comment

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

It seems there's still an lot of actix code left in here? Is that intentional?

relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
let receiver = TestReceiver::default();
// Create the new tokio runtime where we start AggregatorService
let rt = tokio::runtime::Runtime::new().unwrap();
// Run in the old actix system for receiver
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really still need the old runtime for this test? Can we not convert TestReceiver to tokio as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but in production we still have project_cache actor which uses old system and we still have to test it.
Once we convert that actor to the new tokio, we can change all of this as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, but these tests are to make sure the aggregation actor works i think, not to make sure that the callers/senders mess something up?

relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
iter::{FromIterator, FusedIterator},
mem,
time::{Duration, Instant},
};

use actix::prelude::*;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be removed, should it not?

@@ -1388,15 +1437,16 @@ impl<T: Iterator<Item = Bucket>> FusedIterator for CappedBucketIter<T> {}
/// all elapsed buckets belonging to the same [`ProjectKey`] are flushed together.
///
/// Receivers must implement a handler for the [`FlushBuckets`] message.
pub struct Aggregator {
pub struct AggregatorService {
config: AggregatorConfig,
buckets: HashMap<BucketKey, QueuedBucket>,
receiver: Recipient<FlushBuckets>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Recipient is still something actix, why is that still here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This service still interacts with project_cache actor which runs on the old actix. I wanted to keep these changes to the minimum and then when the project_cache is converted we can remove this Recipient and maybe replace it with mpsc channel?

I can look into it and maybe introduce changes in this PR, if that's better for the progress of migration.

Copy link
Contributor

Choose a reason for hiding this comment

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

the approach has been to use compat senders during the transition, I think that makes the most sense here as well. the code here will become easier to grasp if it is fully converted

Copy link
Member

@jjbayer jjbayer left a comment

Choose a reason for hiding this comment

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

Looks good to me in general, just some questions for my own understanding.

relay-server/src/actors/health_check.rs Outdated Show resolved Hide resolved
relay-server/src/actors/project.rs Outdated Show resolved Hide resolved
relay-server/src/actors/health_check.rs Outdated Show resolved Hide resolved
relay-system/src/compat.rs Outdated Show resolved Hide resolved
pub partition_key: Option<u64>,
/// The buckets to be flushed.
pub buckets: Vec<Bucket>,
}
Copy link
Member

Choose a reason for hiding this comment

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

It's a good idea to reorder these declarations, but it also makes the diff hard to read. What if we keep them where they were, and do the reordering in a separate PR that does not change anything functionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this is difficult to parse I can move those structs back. I decided to move all in one place so it either to see how the API of this service looks like and one can see all the messages and impls for them in one place.

relay-server/src/actors/project.rs Outdated Show resolved Hide resolved
relay-server/src/service.rs Outdated Show resolved Hide resolved
relay-system/src/compat.rs Outdated Show resolved Hide resolved
relay-system/src/controller.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
relay-metrics/src/aggregation.rs Outdated Show resolved Hide resolved
@jan-auer jan-auer changed the title ref(metrics): Migrate Aggregator actor to new tokio runtime [INGEST-1645] ref(actix): Migrate Aggregator actor to new tokio runtime [INGEST-1645] Oct 7, 2022
@olksdr olksdr requested a review from jan-auer October 8, 2022 06:37
@olksdr olksdr requested review from jjbayer and flub October 8, 2022 06:37
@@ -1,12 +1,11 @@
use std::collections::BTreeMap;
use std::fmt;
use std::{collections::BTreeMap, fmt};
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpicking, but what makes this stuff gratuitously change like this? is it some tooling? what's wrong with the existing style?

FWIW while it's not explicitly called out in our how to write rust the example suggests the existing style: https://www.notion.so/sentry/HOWTO-Code-Rust-at-Sentry-7b35f165b10b4492bb95ebe1471a9ada#4f8cc9ec744c472ebcc89791400d519c

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically VIM setup I did to merge all the imports if they are coming from the same crate. I will revert it to the previous state.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, is this vim-specific or some rustfmt or rust-analyzer setting that I don't know about? How does this work? I'm curious :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My config for rustfmt contains

imports_granularity = "Crate"

and then in vim I run following when I want to auto format the file:

> rustfmt +nightly  <path to file>

Copy link
Member

@jan-auer jan-auer Oct 10, 2022

Choose a reason for hiding this comment

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

In VSCode / rust-analyzer, I use this:

{
  "rust-analyzer.imports.granularity.group": "module",
  "rust-analyzer.imports.prefix": "crate"
}

Will probably check that in. Unfortunately, that doesn't create separate a import group for all relay_* crates, but we can consider removing that convention.

Generally, we run with rustfmt defaults and no overrides.

partition_key,
buckets: batch,
};
compat::send_to_recipient(receiver, flush_buckets);
Copy link
Contributor

Choose a reason for hiding this comment

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

cool, i agree this is the right way to do this :)

fn handle_shutdown(&mut self, message: &Option<Shutdown>) {
if let Some(message) = message {
if message.timeout.is_some() {
self.state = AggregatorState::ShuttingDown;
Copy link
Contributor

Choose a reason for hiding this comment

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

there used to be a log message here, not sure if it's important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already have another message when the aggregator will stop, I don't see the reason to add more logs here.

relay-metrics/src/aggregation.rs Show resolved Hide resolved
Copy link
Member

@jan-auer jan-auer left a comment

Choose a reason for hiding this comment

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

Looks good, thank you!

In tests there are still some broken indentation fixes, if you look at the diff including whitespace you will see them. It would be great to get those reverted before merging this.

Copy link
Contributor

@flub flub left a comment

Choose a reason for hiding this comment

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

same comment on all the indentation stuff - would be great to not pollute the commit history with noise like that

@olksdr olksdr merged commit c3a1d4c into master Oct 11, 2022
@olksdr olksdr deleted the feat/aggr-to-tokio branch October 11, 2022 05:51
@olksdr olksdr mentioned this pull request Oct 17, 2022
30 tasks
@HazAT HazAT added this to the Upgrade Tokio in Relay milestone Nov 21, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants