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

cleanup: replace ad-hoc [0, 1] value types with UnitFloat #14081

Merged
merged 7 commits into from
Dec 1, 2020

Conversation

akonradi
Copy link
Contributor

Commit Message: Use specialized IntervalType to guarantee bounds
Additional Description:
Add IntervalValue and its specialization UnitFloat, and use them to
guarantee that ScaledMinimum's scale_factor_ is in the range [0, 1].
Also do the same for OverloadActionState, and replace
ScaledTimerManagerImpl::DurationScaleFactor with UnitFloat, which reduces
space usage since double precision wasn't necessary.

Risk Level: low
Testing: ran tests
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

/assign @KBaichoo

Add IntervalValue and its specialization UnitFloat, and use them to
guarantee that ScaledMinimum's scale_factor_ is in the range [0, 1].
Also do the same for OverloadActionState, and replace
ScaledTimerManagerImpl::DurationScaleFactor with UnitFloat, which is a
functional no-op.

Signed-off-by: Alex Konradi <akonradi@google.com>
Copy link
Contributor

@KBaichoo KBaichoo left a comment

Choose a reason for hiding this comment

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

Neat use of templates to enforce the range. LGTM

namespace Envoy {

// Template helper type that represents a closed interval with the given minimum and maximum values.
template <typename T, T MinValue, T MaxValue> struct Interval {
Copy link
Contributor

Choose a reason for hiding this comment

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

super nit: Could call this closedInterval since it represents a closed interval in particular

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the name is still "Interval"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to make the change to the value type since it didn't seem helpful to give the interval type an opinion on whether it is open or closed - that is, the value type is templated over a thing with a min and a max, which sounds like it could apply to both an open or closed interval (though "bounds" might be better for an open interval).

Signed-off-by: Alex Konradi <akonradi@google.com>
@akonradi
Copy link
Contributor Author

/assign @antoniovicente

T value_;
};

using UnitFloat = ClosedIntervalValue<float, Interval<int, 0, 1>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you avoid mixing float and int in this definition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No; float template parameters aren't allowed until C++20.

Copy link
Contributor

Choose a reason for hiding this comment

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

Darn. Ok.
Worth a comment/todo to clean this up once C++ 20 is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

source/common/common/interval_value.h Show resolved Hide resolved
namespace Envoy {

// Template helper type that represents a closed interval with the given minimum and maximum values.
template <typename T, T MinValue, T MaxValue> struct Interval {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the name is still "Interval"

antoniovicente
antoniovicente previously approved these changes Nov 20, 2020
T value_;
};

using UnitFloat = ClosedIntervalValue<float, Interval<int, 0, 1>>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Darn. Ok.
Worth a comment/todo to clean this up once C++ 20 is here?

Signed-off-by: Alex Konradi <akonradi@google.com>
antoniovicente
antoniovicente previously approved these changes Nov 23, 2020
Signed-off-by: Alex Konradi <akonradi@google.com>
CI complained about coverage, so try adding tests

Signed-off-by: Alex Konradi <akonradi@google.com>
…t-interval-type

Signed-off-by: Alex Konradi <akonradi@google.com>
Signed-off-by: Alex Konradi <akonradi@google.com>
@akonradi
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

Retrying Azure Pipelines:
Retried failed jobs in: envoy-presubmit

🐱

Caused by: a #14081 (comment) was created by @akonradi.

see: more, trace.

@akonradi
Copy link
Contributor Author

akonradi commented Dec 1, 2020

@jmarantz can you assign a non-Google reviewer or merge if this seems trivial enough?

@jmarantz
Copy link
Contributor

jmarantz commented Dec 1, 2020

Pretty cool stuff. I think @mattklein123 might want to take a look; maybe there. would be other applications of this new type.

Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

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

Nice. Thanks for following up on this random request of mine. There are probably are plenty of other places we can use this but nothing immediately comes to mind so we can wait until something comes up!

@mattklein123 mattklein123 merged commit 2dc72a9 into envoyproxy:master Dec 1, 2020
mpuncel added a commit to mpuncel/envoy that referenced this pull request Dec 2, 2020
* master: (70 commits)
  upstream: avoid reset after end_stream in TCP HTTP upstream (envoyproxy#14106)
  bazelci: add fuzz coverage (envoyproxy#14179)
  dependencies: allowlist CVE-2020-8277 to prevent false positives. (envoyproxy#14228)
  cleanup: replace ad-hoc [0, 1] value types with UnitFloat (envoyproxy#14081)
  Update docs for skywalking tracer (envoyproxy#14210)
  Fix some errors in the switch statement when decode dubbo response (envoyproxy#14207)
  Windows: enable tests and envoy-static.exe pdb file (envoyproxy#13688)
  http: add Kill Request HTTP filter (envoyproxy#14170)
  dependencies: fix release_dates error behavior. (envoyproxy#14216)
  thrift filter: support skip decoding data after metadata in the thrift message (envoyproxy#13592)
  update cares (envoyproxy#14213)
  docs: clarify behavior of hedge_on_per_try_timeout (envoyproxy#12983)
  repokitteh: add support for randomized auto-assign. (envoyproxy#14185)
  [grpc] validate grpc config for illegal characters (envoyproxy#14129)
  server: Return nullopt when process_context is nullptr (envoyproxy#14181)
  [Windows] Fix thrift proxy tests (envoyproxy#13220)
  kafka: add missing unit tests (envoyproxy#14195)
  doc: mention gperftools explicitly in PPROF.md (envoyproxy#14199)
  Removed `--use-fake-symbol-table` option. (envoyproxy#14178)
  filter contract: clarification around local replies (envoyproxy#14193)
  ...

Signed-off-by: Michael Puncel <mpuncel@squareup.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.

5 participants