iBet uBet web content aggregator. Adding the entire web to your favor.
iBet uBet web content aggregator. Adding the entire web to your favor.



Link to original content: https://github.com/MobileTeleSystems/data-rentgen/pull/92
[DOP-20958] Consume events in batches by dolfinus · Pull Request #92 · MobileTeleSystems/data-rentgen · GitHub
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

[DOP-20958] Consume events in batches #92

Merged
merged 1 commit into from
Nov 1, 2024
Merged

Conversation

dolfinus
Copy link
Member

@dolfinus dolfinus commented Oct 25, 2024

Change Summary

Make consumer fetching a list of OpenLineage events instead of fetching them one-by-one:

  • Add DTO property unique_key and method merge. This allows tracking multiple versions of the same DTO, and then merge into one DTO having all the non-null fields. For example, all RunDTOs with same id are merged into one RunDTO with non-null created_at and ended_at, and so on.
  • Add BatchExtractResult class which tracks each type of DTO, merges old items with new ones, and then allows to iterate other merged items.
  • Move all extraction logic from consumer to dedicated function extract_batch. It iterates other list of OpenLineage events (e.g. sequence of STARTED+RUNNING+SUCCESS) into a final one (SUCCESS with started_at + ended_at + ...).
  • Combine all DB logic inside consumer into one function which just iterates other BatchExtractResult, and creates all the extracted items in database. All cross-DTO links are resolved by BatchExtractResult. For example, when dataset_dto.id = ... is populated from database model, all InputDTOs and OutputDTOs with the same dataset got the same dataset id, which simplifies consumer logic.

This reduces the number of IO operations a lot - instead of sequence of INSERT+UPDATE+UPDATE+UPDATE+... statements, we perform only one INSERT/UPDATE per unique object.

Benchmarks were run in the same environment (4 FastStream workers, 4 Kafka partitions) and the amount of data (239k events, 632MB with ZSTD compression).

Before this change:

  • Consuming all events: 63 min, ~62 RPS.
  • Postgres network usage: 611MB (input) / 764MB (output).
  • Postgres IO: 30.3MB (read) / 2.81GB (write)

After this change:

  • Consuming all events: 6.6 min, ~600 RPS.
  • Postgres network usage: 167MB (input) / 104MB (output).
  • Postgres IO: 21.3MB (read) / 1.63GB (write)

So x10 in RPS, -86% of Postgres requests, -47% of IO.

Related issue number

Checklist

  • Commit message and PR title is comprehensive
  • Keep the change as small as possible
  • Unit and integration tests for the changes exist
  • Tests pass on CI and coverage does not decrease
  • Documentation reflects the changes where applicable
  • docs/changelog/next_release/<pull request or issue id>.<change type>.rst file added describing change
    (see CONTRIBUTING.rst for details.)
  • My PR is ready to review.

@dolfinus dolfinus self-assigned this Oct 25, 2024
Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 95.60811% with 13 lines in your changes missing coverage. Please review.

Project coverage is 91.86%. Comparing base (4e615d1) to head (6de41ef).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
data_rentgen/consumer/extractors/batch.py 97.33% 0 Missing and 4 partials ⚠️
data_rentgen/dto/run.py 66.66% 2 Missing and 2 partials ⚠️
data_rentgen/dto/input.py 83.33% 1 Missing and 1 partial ⚠️
data_rentgen/dto/output.py 83.33% 1 Missing and 1 partial ⚠️
data_rentgen/dto/user.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop      #92      +/-   ##
===========================================
+ Coverage    91.69%   91.86%   +0.16%     
===========================================
  Files          151      152       +1     
  Lines         3071     3256     +185     
  Branches       217      235      +18     
===========================================
+ Hits          2816     2991     +175     
- Misses         198      202       +4     
- Partials        57       63       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dolfinus dolfinus changed the title [DOP-20958] Convert consumer to batching [DOP-20958] Consume events in batches Oct 25, 2024
@dolfinus dolfinus requested a review from TiGrib October 28, 2024 13:18
@dolfinus dolfinus marked this pull request as ready for review October 28, 2024 13:18
@dolfinus dolfinus merged commit 3588222 into develop Nov 1, 2024
13 checks passed
@dolfinus dolfinus deleted the feature/DOP-20958 branch November 1, 2024 07: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.

2 participants