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

Fix pointer issue #839

Merged
merged 2 commits into from
Oct 28, 2024

Conversation

mooselumph
Copy link
Collaborator

@mooselumph mooselumph commented Oct 26, 2024

Why are these changes needed?

Addresses a pointer issue in the below portion of the rate limiter implementation:

// Calculate updated bucket levels
	allowed := true
	for i, size := range d.globalRateParams.BucketSizes {

		// Determine bucket deduction
		deduction := time.Microsecond * time.Duration(1e6*float32(params.BlobSize)/float32(params.Rate)/d.globalRateParams.Multipliers[i])

		prevLevel := bucketParams.BucketLevels[i]

		// Update the bucket level
		bucketParams.BucketLevels[i] = getBucketLevel(bucketParams.BucketLevels[i], size, interval, deduction)
		allowed = allowed && bucketParams.BucketLevels[i] > 0

	}

This code returns the bucketParams struct, which is then push back to the rate limiter's KV store when the call is allowed.

The problem here is that bucketParams.BucketLevels is an array slice and the KV store is an in-memory LRU cache. Even though bucketParams is a copy of the object stored in the KV store, both objects reference the same slice. The code here will directly update the slice, so that when allowed is false, the bucket level is inadvertently updated.

Checks

  • I've made sure the lint is passing in this PR.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, in that case, please comment that they are not relevant.
  • I've checked the new test coverage and the coverage percentage didn't drop.
  • Testing Strategy
    • Unit tests
    • Integration tests
    • This PR is not tested :(

Copy link
Contributor

@pschork pschork left a comment

Choose a reason for hiding this comment

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

RIP mutation roulette. Nice fix. LGTM

Copy link
Contributor

@dmanc dmanc left a comment

Choose a reason for hiding this comment

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

I didn't fully understand how this change will fix the problem. A better description in the PR with an example would be helpful for me.


d.logger.Debug("Bucket level updated", "key", params.RequesterID, "name", params.RequesterName, "prevLevel", prevLevel, "level", bucketParams.BucketLevels[i], "size", size, "interval", interval, "deduction", deduction, "allowed", allowed)
d.logger.Debug("Bucket level updated", "key", params.RequesterID, "name", params.RequesterName, "prevLevel", bucketParams.BucketLevels[i], "level", bucketLevels[i], "size", size, "interval", interval, "deduction", deduction, "allowed", allowed)

// Update metrics only if the requester name is provided. We're making
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we update the metric to use bucketLevels[i]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

@mooselumph
Copy link
Collaborator Author

I didn't fully understand how this change will fix the problem. A better description in the PR with an example would be helpful for me.

I updated the PR description with a better explanation!

@mooselumph mooselumph merged commit 9bd44f4 into Layr-Labs:master Oct 28, 2024
6 checks passed
ian-shim pushed a commit to ian-shim/eigenda that referenced this pull request Oct 29, 2024
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.

5 participants