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

Implement batch keys in OSS #1344

Merged

Conversation

BrunoScheufler
Copy link
Contributor

@BrunoScheufler BrunoScheufler commented May 10, 2024

Description

This pull request introduces a new key option on the function batching configuration. When storing batches, the batch pointer now includes a hashed representation of the evaluated expression. Unlike the previous implementation where one function would have at most one associated active batch, batch keys create many batches per function.

Motivation

Batch keys enable event aggregation or grouping by tenants: Events could be counted for user notifications, or processed together to enable other features.

Type of change (choose one)

  • Chore (refactors, upgrades, etc.)
  • Bug fix (non-breaking change that fixes an issue)
  • Security fix (non-breaking change that fixes a potential vulnerability)
  • Docs
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality not to work as expected)

Checklist

Check our Pull Request Guidelines

Copy link

vercel bot commented May 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
ui ⬜️ Ignored (Inspect) Visit Preview May 22, 2024 10:48am

Copy link

linear bot commented May 10, 2024

Str("expression", *fn.EventBatch.Key).
Interface("event", evt.Map()).
Msg("error evaluating batch key expression")
return "<invalid>", nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

if it's invalid, maybe just return it as the default fn.ID.String() instead?
hardcoding "<invalid>" here is no different from just using the function ID.

maybe also return an error just in case. I suspect it's not going to be used anytime soon, but hopefully we can surface this kind of invalid expression errors to users better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated this to return an error and use the fn identifier as fallback, just to clarify, should we pass this up already or silently ignore the error? I guess it does make sense to warn early and condition users to use valid expressions returning strings, what do you think?

Copy link
Collaborator

@darwin67 darwin67 May 22, 2024

Choose a reason for hiding this comment

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

We don't have a way to pass this error back up to the user atm.
It's the same for all expression related evaluation, and we should bubble the evaluation error up somehow so users are aware.

But regardless we don't want to fail the execution because there's an invalid expression since there are no ways to recover from that and runs are essentially lost at that point.

@BrunoScheufler BrunoScheufler force-pushed the bruno/inn-3010-implement-batch-keys-in-oss-executor branch from 4a935df to 0083a3a Compare May 14, 2024 21:13
@BrunoScheufler BrunoScheufler force-pushed the bruno/inn-3010-implement-batch-keys-in-oss-executor branch from 0083a3a to a3ea6b7 Compare May 21, 2024 07:17
@BrunoScheufler BrunoScheufler marked this pull request as ready for review May 21, 2024 07:42
@BrunoScheufler BrunoScheufler self-assigned this May 21, 2024
Copy link
Collaborator

@darwin67 darwin67 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! Nice tests as well.
Have a couple of comments and suggestions.
Only need you to add the CEL expr check in IsValid, others are minor suggestions that are not blocking.

Approving the PR so you can merge it once you made the changes.

if str, ok := out.(string); ok {
return str, nil
}
return fmt.Sprintf("%v", out), nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

ratelimit hashes the key before returning it. could be a something to copy if it makes sense, and can save you that type assertion above.

// RateLimitKey returns the rate limiting key given a function ID, rate limit config,
// and incoming event data.
func RateLimitKey(ctx context.Context, id uuid.UUID, c inngest.RateLimit, evt map[string]any) (string, error) {
if c.Key == nil {
return id.String(), nil
}
eval, err := expressions.NewExpressionEvaluator(ctx, *c.Key)
if err != nil {
return "", ErrEvaluatingRateLimitExpression
}
res, _, err := eval.Evaluate(ctx, expressions.NewData(map[string]any{"event": evt}))
if err != nil {
return "", ErrEvaluatingRateLimitExpression
}
// Take a checksum of this data. It doesn't matter if this is a map or a string;
// as long as we're consistent here.
return hash(res, id), nil
}
func hash(res any, id uuid.UUID) string {
ui := xxhash.Sum64String(fmt.Sprintf("%v", res))
sum := strconv.FormatUint(ui, 36)
return fmt.Sprintf("%s-%s", id, sum)
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see you're already base64 encoding it, so it might not matter.

pkg/execution/batch/redis.go Outdated Show resolved Hide resolved
pkg/inngest/batch.go Show resolved Hide resolved
tests/golang/batch_test.go Outdated Show resolved Hide resolved
tests/golang/batch_test.go Outdated Show resolved Hide resolved
@BrunoScheufler BrunoScheufler merged commit 5274c20 into main May 22, 2024
12 checks passed
@BrunoScheufler BrunoScheufler deleted the bruno/inn-3010-implement-batch-keys-in-oss-executor branch May 22, 2024 11:15
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.

None yet

2 participants