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: collect less sample_events for reporting #4615

Closed
wants to merge 1 commit into from

Conversation

satishrudderstack
Copy link
Contributor

@satishrudderstack satishrudderstack commented Apr 24, 2024

Description

Current Behaviour

  • We generate multiple reports in a minute for the same group (source_id, dest_id, event_name, status_code, reported_by ... etc). For each report, we save sample_event and sample_response. On Reporting Service, we have hourly aggregates and we pick last sample_event for each group. We are collecting too many sample_events on Rudder Server

New Behaviour

  • We are having a in-memory map to track whether a sample_event is captured for a group. We use hash of grouping columns as key and bool as value in this map. This map resets every eventSamplingDuration seconds (typically < 1 h)

Analysis of sample_events we are collecting is documented in this linear ticket

Linear Ticket

https://linear.app/rudderstack/issue/OBS-461/collect-less-sample-events-on-rudder-server

completes OBS-461

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@satishrudderstack satishrudderstack marked this pull request as draft April 24, 2024 09:54
Copy link
Contributor

coderabbitai bot commented Apr 24, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@satishrudderstack satishrudderstack marked this pull request as ready for review April 24, 2024 12:52

func (groupingColumns GroupingColumns) generateHash() string {
data := groupingColumns.WorkspaceID + groupingColumns.SourceDefinitionID + groupingColumns.SourceCategory + groupingColumns.SourceID + groupingColumns.DestinationDefinitionID + groupingColumns.DestinationID + groupingColumns.SourceTaskRunID + groupingColumns.SourceJobID + groupingColumns.SourceJobRunID + groupingColumns.TransformationID + groupingColumns.TransformationVersionID + groupingColumns.TrackingPlanID + strconv.Itoa(groupingColumns.TrackingPlanVersion) + groupingColumns.InPU + groupingColumns.PU + groupingColumns.Status + strconv.FormatBool(groupingColumns.TerminalState) + strconv.FormatBool(groupingColumns.InitialState) + strconv.Itoa(groupingColumns.StatusCode) + groupingColumns.EventName + groupingColumns.EventType + groupingColumns.ErrorType
hash := md5.Sum([]byte(data))
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest using an hashing algorithm like murmur3, since it faster/more efficient than md5 and only slightly worse with collisions.

}
}

func (es *EventSampler) IsSampleEventCollected(groupingColumns string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

[minor]

Suggested change
func (es *EventSampler) IsSampleEventCollected(groupingColumns string) bool {
func (es *EventSampler) Exists(key string) bool {

return exists
}

func (es *EventSampler) MarkSampleEventAsCollected(groupingColumns string) {
Copy link
Member

Choose a reason for hiding this comment

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

[minor]

Suggested change
func (es *EventSampler) MarkSampleEventAsCollected(groupingColumns string) {
func (es *EventSampler) Mark(groupingColumns string) {

Comment on lines +38 to +48
func (es *EventSampler) StartResetLoop() error {
go func() {
for {
time.Sleep(es.resetDuration.Load())
es.mu.Lock()
es.collectedSamples = make(map[string]bool)
es.mu.Unlock()
}
}()
return nil
}
Copy link
Member

Choose a reason for hiding this comment

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

Couple of thoughts here:

No lifecycle control

The go routine spins on background and runs for ever. This can create go-routine and memory leaks.

Ideally you want to do the following for every exported method:

  1. Ability to stop the execution of the loop, usually we can do that by passing a ctx
  2. Ability to wait until the go routine stop
  3. Ability to interrupt the sleep

One way this can be achieved:

Suggested change
func (es *EventSampler) StartResetLoop() error {
go func() {
for {
time.Sleep(es.resetDuration.Load())
es.mu.Lock()
es.collectedSamples = make(map[string]bool)
es.mu.Unlock()
}
}()
return nil
}
func (es *EventSampler) RunCleanup(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return
case <-time.After(1 * time.Second):
es.mu.Lock()
es.collectedSamples = make(map[string]bool)
es.mu.Unlock()
}
}
}

Memory allocations

The plan is to reset the map every time, but this can cause a lot memory to be dereference and GCed. However, most of the keys we are deleting are going to be added again, doing more memory allocations.

It might be better to store a time.Time instead of boolean and do two things:

  • during check if exist, check if a key is expired
  • you can keep the go routine to clean up keys that being a expired for a longer time

Or even better you can use the existing cachettl implementation, which clean-ups keys async during lookup.

When it comes to perfromance, it is always better to run benchmark to verify. Given the cardinality of keys we are planning to handle, it is worth considering.

@satishrudderstack satishrudderstack marked this pull request as draft April 24, 2024 14:05
Copy link

This PR is considered to be stale. It has been open 20 days with no further activity thus it is going to be closed in 7 days. To avoid such a case please consider removing the stale label manually or add a comment to the PR.

@github-actions github-actions bot added the Stale label May 15, 2024
@github-actions github-actions bot closed this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants