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

runtime: throw exception in debug mode that condition is always true when fractionalPercent numerator > denominator #12068

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions source/common/runtime/runtime_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ bool SnapshotImpl::featureEnabled(absl::string_view key,
percent = default_value;
}

// When numerator > denominator condition is always evaluates to TRUE
// It becomes hard to debug why configuration does not work in case of wrong numerator.
// Log debug message that numerator is invalid.
uint64_t denominator_value =
ProtobufPercentHelper::fractionalPercentDenominatorToInt(percent.denominator());
if (percent.numerator() > denominator_value) {
ENVOY_LOG(debug,
"WARNING runtime key '{}': numerator ({}) > denominator ({}), condition always "
"evaluates to true",
key, percent.numerator(), denominator_value);
}

return ProtobufPercentHelper::evaluateFractionalPercent(percent, random_value);
}

Expand Down
1 change: 1 addition & 0 deletions test/common/runtime/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ envoy_cc_test(
"//test/mocks/thread_local:thread_local_mocks",
"//test/mocks/upstream:upstream_mocks",
"//test/test_common:environment_lib",
"//test/test_common:logging_lib",
"@envoy_api//envoy/config/bootstrap/v3:pkg_cc_proto",
"@envoy_api//envoy/config/core/v3:pkg_cc_proto",
"@envoy_api//envoy/service/discovery/v3:pkg_cc_proto",
Expand Down
20 changes: 20 additions & 0 deletions test/common/runtime/runtime_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "test/mocks/thread_local/mocks.h"
#include "test/mocks/upstream/mocks.h"
#include "test/test_common/environment.h"
#include "test/test_common/logging.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
Expand Down Expand Up @@ -684,6 +685,25 @@ TEST_F(StaticLoaderImplTest, ProtoParsing) {
EXPECT_EQ(2, store_.gauge("runtime.num_layers", Stats::Gauge::ImportMode::NeverImport).value());
}

TEST_F(StaticLoaderImplTest, InvalidNumerator) {
base_ = TestUtility::parseYaml<ProtobufWkt::Struct>(R"EOF(
invalid_numerator:
numerator: 111
denominator: HUNDRED
)EOF");
setup();

envoy::type::v3::FractionalPercent fractional_percent;

// There is no assertion here - when numerator is invalid
// featureEnabled() will just drop debug log line.
EXPECT_CALL(generator_, random()).WillOnce(Return(500000));
ggreenway marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_LOG_CONTAINS("debug",
"runtime key 'invalid_numerator': numerator (111) > denominator (100), "
"condition always evaluates to true",
loader_->snapshot().featureEnabled("invalid_numerator", fractional_percent));
}
belyalov marked this conversation as resolved.
Show resolved Hide resolved

TEST_F(StaticLoaderImplTest, RuntimeFromNonWorkerThreads) {
// Force the thread to be considered a non-worker thread.
tls_.registered_ = false;
Expand Down