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

Sort order of ports returned to fix flaky tests #1003

Merged
merged 6 commits into from
Aug 3, 2022

Conversation

kevinearls
Copy link
Member

Signed-off-by: Kevin Earls kearls@redhat.com

This should fix #969. If there is more than one receiver defined, ports may be returned in a different order than that in which they were created. Ideally they would always be in the same order as they are defined, but because Go does not guarantee order when iterating over keys from a map, we cannot guarantee this.

Signed-off-by: Kevin Earls <kearls@redhat.com>
@kevinearls kevinearls requested a review from a team July 25, 2022 12:26
@@ -60,28 +61,34 @@ func ConfigToReceiverPorts(logger logr.Logger, config map[interface{}]interface{
return nil, ErrReceiversNotAMap
}

sortedNames := make([]string, 0, len(receivers))
for name := range receivers {
sortedNames = append(sortedNames, name.(string))
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to check the type just for safety here?

Suggested change
sortedNames = append(sortedNames, name.(string))
v, ok := name.(string)
if !ok {
logger.Warn("receiver does not match type, want: %s, got: %T", "string", name)
continue
}
sortedNames = append(sortedNames, v)

@@ -60,28 +61,34 @@ func ConfigToReceiverPorts(logger logr.Logger, config map[interface{}]interface{
return nil, ErrReceiversNotAMap
}

sortedNames := make([]string, 0, len(receivers))
Copy link
Member

Choose a reason for hiding this comment

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

@kevinearls wouldn't it be cleaner to sort ports at the end of the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe, but I'm not sure it necessary. Originally when I saw this problem it occurred because we were getting receivers in the wrong order. Sorting them alphabetically by name is fairly simple.

The ports, on the other hand, are in an array of corev1.ServicePort. That does not implement methods needed to feed it to sort.Sort(), so sorting those would be non-trivial.

Copy link
Member

@pavolloffay pavolloffay Jul 26, 2022

Choose a reason for hiding this comment

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

What is the root cause of the problem? That the ports are not sorted or receivers? We should use the least error-prone approach that will guarantee always the same result.

Take a look at sort.Slice:

	sort.Slice(ports, func(i, j int) bool {
		return ports[i].Name < ports[j].Name
	})

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem I had was caused by the receivers being in the wrong order, not the ports for a receiver.

Copy link
Member

@pavolloffay pavolloffay Jul 26, 2022

Choose a reason for hiding this comment

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

In other words, the implementation in this PR will break again if ReceiverParser.Ports() returns randomly sorted ports. In fact right now that API does not guarantee the order.

Also, there could be a unit test checking the functionality - or change the existing tests to assume the order of ports matters.

Signed-off-by: Kevin Earls <kearls@redhat.com>
Copy link
Member

@frzifus frzifus left a comment

Choose a reason for hiding this comment

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

up to the others :)

Signed-off-by: Kevin Earls <kearls@redhat.com>
Signed-off-by: Kevin Earls <kearls@redhat.com>
@kevinearls
Copy link
Member Author

@pavolloffay please review and merge if ok

@@ -144,6 +144,39 @@ service:
}
}

func TestReceiverPortsSortedCorrectly(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the test!

It would be better to change the TestExtractPortsFromConfig because it does the same thing - check what ports are returned and just change how the assert is written in that function (see my other comment).


ports, err := adapters.ConfigToReceiverPorts(logger, configuration)
assert.NoError(t, err)
assert.Len(t, ports, 4)
Copy link
Member

Choose a reason for hiding this comment

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

This PR changes the semantics of ConfigToReceiverPorts - the expected result is strictly ordered, hence we can write a nice assert and compare slices directly:

assert.Equal(error, []v1.port{{Name: "foo", Port: 1778944}{...}}, got)

continue
}

if len(rcvrPorts) > 0 {
sort.Slice(ports, func(i, j int) bool {
Copy link
Member

Choose a reason for hiding this comment

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

From all changes in this file only these 3 lines are needed 101-103 and move then right before the return.

Signed-off-by: Kevin Earls <kearls@redhat.com>
@@ -89,5 +90,12 @@ func ConfigToReceiverPorts(logger logr.Logger, config map[interface{}]interface{
ports = append(ports, rcvrPorts...)
}
}

if len(ports) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

the check should not be needed

Signed-off-by: Kevin Earls <kearls@redhat.com>
@pavolloffay pavolloffay merged commit fc3cae5 into open-telemetry:main Aug 3, 2022
@kevinearls kevinearls deleted the fix-flaky-tests branch August 3, 2022 12:32
ItielOlenick pushed a commit to ItielOlenick/opentelemetry-operator that referenced this pull request May 1, 2024
* Sort order of ports returned to fix flaky tests

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Verify type of receiver name

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Sort ports too before returning

Signed-off-by: Kevin Earls <kearls@redhat.com>

* Add unit test

Signed-off-by: Kevin Earls <kearls@redhat.com>

* respond to comments

Signed-off-by: Kevin Earls <kearls@redhat.com>

* fix nits

Signed-off-by: Kevin Earls <kearls@redhat.com>
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.

smoke-simplest e2e test fails intermittently
3 participants