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

client.FromLabelAdaptersToLabels breaks contract of labels.Labels #1813

Closed
pstibrany opened this issue Nov 12, 2019 · 7 comments
Closed

client.FromLabelAdaptersToLabels breaks contract of labels.Labels #1813

pstibrany opened this issue Nov 12, 2019 · 7 comments
Labels
keepalive Skipped by stale bot

Comments

@pstibrany
Copy link
Contributor

func FromLabelAdaptersToLabels(ls []LabelAdapter) labels.Labels {

labels.Labels is supposed to be sorted set of labels. Conversion from []LabelAdapter is unsafe unless order is checked first.

@bboreham
Copy link
Contributor

There's various other ways to resolve this - could comment that it doesn't check that contact, or comment that it must only be called with sorted LabelAdapter.

Could you say a bit about where this caused a problem?

@pstibrany
Copy link
Contributor Author

Yes, documenting break of contract or required preconditions is one way of fixing it.

I'm not sure if it is a problem in Cortex (on quick check, it doesn't seem to be), but I've found several unsafe usages in Loki.

@pstibrany
Copy link
Contributor Author

Same problem in

func FromLabelAdaptersToLabels(input []client.LabelAdapter) labels.Labels {
(with slightly different types) (@pracucci)

@pracucci
Copy link
Contributor

Same problem in

func FromLabelAdaptersToLabels(input []client.LabelAdapter) labels.Labels {

(with slightly different types) (@pracucci)

@pstibrany May you elaborate a bit more on the issue, please? labels.Labels is []Label and the tsdb.FromLabelAdaptersToLabels() iterates over the input list of LabelAdapter keeping the same ordering in the output list of Labels so, as far as the input is correctly sorted, the output is as well. What am I missing?

@pstibrany
Copy link
Contributor Author

pstibrany commented Nov 19, 2019

as far as the input is correctly sorted, the output is as well.

This is exactly the problem. labels.Labels is documented as: "Labels is a sorted set of labels.", and there may be code relying on this property. However, FromLabelAdaptersToLabels doesn't check for it, and will happily generate unsorted labels.Labels instance, thus breaking contract of labels.Labels. (Personally, I prefer code to be correct first, fast second. In this instance, it's easy to fix. In compact.go file, code tries to be smart, at the expense of being incorrect).

@stale
Copy link

stale bot commented Feb 3, 2020

This issue has been automatically marked as stale because it has not had any activity in the past 30 days. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Feb 3, 2020
@pracucci pracucci added keepalive Skipped by stale bot and removed stale labels Feb 3, 2020
@pstibrany
Copy link
Contributor Author

There is now a comment on the function warning about this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keepalive Skipped by stale bot
Projects
None yet
Development

No branches or pull requests

3 participants