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

shuffle targets during parse phase #343

Merged
merged 2 commits into from
May 15, 2024
Merged

Conversation

dedelala
Copy link

Description

@rjlaine found an issue where if the list of hosts changes e.g. from a scale up the new hosts may not end up in the list of target hosts. We can fix this by doing a shuffle sort during the parse phase.

@@ -82,7 +82,6 @@ type JSONGateResolverBuilder struct {
targets map[string][]targetHost
resolvers []*JSONGateResolver

rand *rand.Rand
Copy link
Author

Choose a reason for hiding this comment

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

I think it's ok to just use the global rand in the rand package here. We actually introduced a bug when we added GetTargets because rand.Rand is not safe to use concurrently (the top level package funcs are safe)

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's fine, but then we need to call rand.Seed during Init somewhere

Copy link
Collaborator

@rjlaine rjlaine May 14, 2024

Choose a reason for hiding this comment

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

rand.Seed was deprecated in go1.20, so if we want to rebase off a current branch, we might want to avoid it. If possible, I'd like to avoid using package level global instances like rand.globalRandGenerator because they can be poisoned by other packages. If concurrency is a problem, could we use a rand.Rand and a sync.Mutex?

Copy link
Author

Choose a reason for hiding this comment

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

noted, rewritten to use a rand.Rand with a sync.Mutex

@rjlaine
Copy link
Collaborator

rjlaine commented May 14, 2024

Confirmed, this does fix the issue when I re-run tests.

j := head + b.rand.Intn(tail-head+1)
s.mu.Lock()
j := head + s.rand.Intn(tail-head+1)
s.mu.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is likely irrelevant since we're not ever calling this under load, but my .02 would be that we don't need to take and drop the lock so often, and instead just taking it once at the beginning of sort and dropping at end would be fine.

Copy link
Collaborator

@demmer demmer left a comment

Choose a reason for hiding this comment

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

LGTM overall

@dedelala dedelala merged commit 62f39e4 into vtgateproxy May 15, 2024
152 of 241 checks passed
@dedelala dedelala deleted the esme-always-shuffle branch May 15, 2024 23:20
dedelala added a commit that referenced this pull request May 30, 2024
* shuffle targets during parse phase

* don't use global rand
dedelala added a commit that referenced this pull request Jul 30, 2024
* shuffle targets during parse phase

* don't use global rand
dedelala added a commit that referenced this pull request Sep 9, 2024
* shuffle targets during parse phase

* don't use global rand
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.

3 participants