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

Add and use over-allocation metric to decide when to defragment #1000

Merged
merged 1 commit into from
Feb 15, 2021

Conversation

geieredgar
Copy link
Contributor

Previous discussion: #996

Ralith
Ralith previously requested changes Jan 30, 2021
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@Ralith
Copy link
Collaborator

Ralith commented Jan 30, 2021

Another approach might be to keep a separate table of allocations, and map chunks to allocations. That should allow us to track memory overhead precisely by comparing buffered data to net allocation size, but is substantially more complex, so I'm not sure it'd be worthwhile.

@geieredgar geieredgar force-pushed the 981 branch 4 times, most recently from be0b110 to 3bbe101 Compare February 1, 2021 03:48
@geieredgar
Copy link
Contributor Author

Rebased my changes on current main.

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

This implementation looks solid to me! Some minor issues below:

quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@djc
Copy link
Member

djc commented Feb 4, 2021

@Matthias247 would you have a chance to review the current iteration of these changes? (Otherwise we'll probably just merge them; we can always improve on them later, of course.)

@Matthias247
Copy link
Contributor

Will definitely take a look. However it's a bit of a busy week at the moment, so it might be start of next week.

Copy link
Contributor

@Matthias247 Matthias247 left a comment

Choose a reason for hiding this comment

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

The buffered/allocated strategy in the chunks, and taking the defragment decision based on it seem good to me.

However I think defragment migh have a bug and some wrong assumptions (size >= allocation_size) which don't make it as efficient as it could be.

While efficiency can further be improved in follow-ups, the data ordering bug should be checked and fixed.

quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/mod.rs Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@geieredgar geieredgar force-pushed the 981 branch 3 times, most recently from da6e5e8 to fee1e5f Compare February 5, 2021 16:36
@Matthias247
Copy link
Contributor

One more general realization I had: The old mechanism covered the case where a client sent lots of overlapping ranges, and thereby caused excessive memory usage despite having a limited receive buffer. This change as it is won't do that anymore, since all chunks could still have good utilization.

Nevertheless I think the current approach is good too.

Maybe deduplication should happen on insert. But that actually means having to scan all fragments for duplicate ranges, and organizing the buffer more in a form of Vec<(Buffer, Range)> than a heap. Also not perfect in case there are lots of fragments.

@djc
Copy link
Member

djc commented Feb 8, 2021

One more general realization I had: The old mechanism covered the case where a client sent lots of overlapping ranges, and thereby caused excessive memory usage despite having a limited receive buffer. This change as it is won't do that anymore, since all chunks could still have good utilization.

Hmm, that definitely seems like an issue. I think we should still cover that scenario.

@geieredgar
Copy link
Contributor Author

I added two new commits. The first fixes #1009 by adding a deduplicate function that is called when switching from ordered to unordered mode. The second commit adds a end field to Assembler and uses min(self.buffered, self.end - self.bytes_read) instead of only self.buffered for the calculation of over_allocationand threshold. Therefore, if we have a lot of duplicate bytes buffered, deduplicate and defragment will be called,

There is still one flaw that might be abused by a malicious peer: After calling deduplicate, an already defragmented Buffermight be defragmented again, leading to unnecessary allocations. I will add a new commit to fix that problem with a slightly more complex defragment, deduplicate and Buffer.

quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@djc
Copy link
Member

djc commented Feb 11, 2021

@geieredgar please take the opportunity to basically add comments for all of Matthias' questions. Would be good to get the documentation in there since this is obviously a tricky topic and it would be nice to make the explanations easier to access if we ever need to iterate more on this. Plus, it will help make my review easier.

@geieredgar geieredgar force-pushed the 981 branch 2 times, most recently from 1353940 to 01ceb65 Compare February 12, 2021 01:00
@geieredgar
Copy link
Contributor Author

geieredgar commented Feb 12, 2021

@djc Some of Matthias' questions became obsolete after my latest refactor, for the others I added comments.

Copy link
Contributor

@Matthias247 Matthias247 left a comment

Choose a reason for hiding this comment

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

Thanks for the hard work of doing all the changes! This looks a lot simpler now. I just pointed out some style nits and added some questions, but I'm fine with merging as is.

quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@geieredgar geieredgar force-pushed the 981 branch 3 times, most recently from 15455dd to caf5856 Compare February 12, 2021 15:57
Matthias247
Matthias247 previously approved these changes Feb 13, 2021
Copy link
Contributor

@Matthias247 Matthias247 left a comment

Choose a reason for hiding this comment

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

Looks great to me now!
Thanks for all the changes

quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
quinn-proto/src/connection/assembler.rs Outdated Show resolved Hide resolved
@geieredgar geieredgar force-pushed the 981 branch 3 times, most recently from c00ed23 to 71af9f7 Compare February 15, 2021 14:50
@geieredgar geieredgar force-pushed the 981 branch 5 times, most recently from f59b23c to c93eaf4 Compare February 15, 2021 15:31
@djc
Copy link
Member

djc commented Feb 15, 2021

Alright, thanks for seeing this through!

@djc djc merged commit b9eb42e into quinn-rs:main Feb 15, 2021
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