Skip to content

Conversation

@mjc1283
Copy link
Contributor

@mjc1283 mjc1283 commented Oct 30, 2019

Previously, GetDecision of ExperimentWhitelistService was incorrectly using variation key to look up variations in the Variations map of an Experiment struct, in which keys are variation IDs, not variation keys.

With this fix, we iterate over variations to find the one with the desired key. To avoid needing to search all variations, we can add a map keyed by variation key as a follow up.

Matt Carroll added 2 commits October 29, 2019 18:04
@mjc1283 mjc1283 removed their assignment Oct 30, 2019
return decision, nil
// TODO(Matt): Add a VariationsByKey map to the Experiment struct, and use it to look up Variation by key
for _, variation := range decisionContext.Experiment.Variations {
variation := variation
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this reassignment necessary? Can't you just use the variation assigned by the loop iterator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh... @pawels-optimizely thoughts on this? I won't block the merge on this 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Appears to be a valid catch for potentially hard to track down bugs. I also just learned something about go range loops :) In this particular case I believe we were safe since we're immediately returning with the current variation, but might as well be in the habit of "pinning".

Copy link
Contributor

@pawels-optimizely pawels-optimizely Oct 30, 2019

Choose a reason for hiding this comment

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

I don't think we need it here , but if linters are complaining then that's fine. that assignment is needed only for goroutines , and we are handling that in sidedoor properly : https://github.com/optimizely/sidedoor/blob/master/pkg/admin/handlers/admin_entities.go#L64

Copy link
Contributor

Choose a reason for hiding this comment

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

@pawels-optimizely The assignment is not just needed for go routines, that is just a common place it can manifest itself. See the copies example in scopelint readme.

Copy link
Contributor

@pawels-optimizely pawels-optimizely Oct 30, 2019

Choose a reason for hiding this comment

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

yeah, goroutines are the most common example. I missed the reference (&) in Matt's code.
Any time you take the reference of a range you need that extra init. That's why I usually use the value (not the reference) coming from the range.

Copy link
Contributor

@mikeproeng37 mikeproeng37 left a comment

Choose a reason for hiding this comment

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

Ship it

Copy link
Contributor

@mikecdavis mikecdavis left a comment

Choose a reason for hiding this comment

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

Thanks for the turnaround on this! Minor comment on the test case.

}

// Test an experiment whose key differs from its id
func (s *ExperimentWhitelistServiceTestSuite) TestWhitelistIncludesDecisionForExpWithKey() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure we need a new test for this or if we update the previous test fixture to have a differing id, key pair.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll delete the new test and change the old test data.

return decision, nil
// TODO(Matt): Add a VariationsByKey map to the Experiment struct, and use it to look up Variation by key
for _, variation := range decisionContext.Experiment.Variations {
variation := variation
Copy link
Contributor

Choose a reason for hiding this comment

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

Appears to be a valid catch for potentially hard to track down bugs. I also just learned something about go range loops :) In this particular case I believe we were safe since we're immediately returning with the current variation, but might as well be in the habit of "pinning".

}

// Experiment with a whitelist, and variation ids are not the same as keys
const testExpWhitelistKey2 = "test_experiment_whitelist_2"
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar comment above about not needing a new fixture.

@mjc1283 mjc1283 merged commit f05273e into master Oct 30, 2019
@mjc1283 mjc1283 deleted the mcarroll/fix-experiment-whitelist branch October 30, 2019 15:36
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