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

fault: use FractionalPercent for percent #3978

Merged
merged 16 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions api/envoy/config/filter/fault/v2/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ api_proto_library_internal(
"//envoy/config/filter/http/fault/v2:__pkg__",
"//envoy/config/filter/network/mongo_proxy/v2:__pkg__",
],
deps = ["//envoy/type:percent"],
)
7 changes: 4 additions & 3 deletions api/envoy/config/filter/fault/v2/fault.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ syntax = "proto3";
package envoy.config.filter.fault.v2;
option go_package = "v2";

import "envoy/type/percent.proto";

import "google/protobuf/duration.proto";

import "validate/validate.proto";
Expand All @@ -22,9 +24,8 @@ message FaultDelay {
// supported.
FaultDelayType type = 1 [(validate.rules).enum.defined_only = true];

// An integer between 0-100 indicating the percentage of operations/connection requests
// on which the delay will be injected.
uint32 percent = 2 [(validate.rules).uint32.lte = 100];
// The percentage of operations/connection requests on which the delay will be injected.
envoy.type.FractionalPercent percent = 2;
Copy link
Member

Choose a reason for hiding this comment

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

We can't just change this. You will need to deprecate the old percent and add a new one.

Copy link
Member

Choose a reason for hiding this comment

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

call the new one as percentage. And i think there is also some protoc validation with it


oneof fault_delay_secifier {
option (validate.required) = true;
Expand Down
14 changes: 12 additions & 2 deletions api/envoy/type/percent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,25 @@ message FractionalPercent {
// **Example**: 1/100 = 1%.
HUNDRED = 0;

// 1,000.
//
// **Example**: 1/1000 = 0.1%.
THOUSAND = 1;

// 10,000.
//
// **Example**: 1/10000 = 0.01%.
TEN_THOUSAND = 1;
Copy link
Member

Choose a reason for hiding this comment

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

These can't be changed. You will need to add new ones at the end. Are they really necessary though? Seems unnecessary to me with TEN_THOUSAND and MILLION. I would revert these changes personally.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in f105661.

TEN_THOUSAND = 2;

// 100,000.
//
// **Example**: 1/100000 = 0.001%.
HUNDRED_THOUSAND = 3;

// 1,000,000.
//
// **Example**: 1/1000000 = 0.0001%.
MILLION = 2;
MILLION = 4;
}

// Specifies the denominator. If the denominator specified is less than the numerator, the final
Expand Down
10 changes: 6 additions & 4 deletions docs/root/api-v1/http_filters/fault_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ Delay

{
"type" : "...",
"fixed_delay_percent" : "...",
"fixed_delay_percent" : {
Copy link
Member

Choose a reason for hiding this comment

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

We don't accept v1 changes anymore. Please revert all changes to v1 code, schema, docs, etc.

"numerator" : "...",
"denominator" : "..."
},
"fixed_duration_ms" : "..."
}

Expand All @@ -86,9 +89,8 @@ type:
injected. Currently only *fixed* delay type (step function) is supported.

fixed_delay_percent:
*(required, integer)* The percentage of requests that will
be delayed for the duration specified by *fixed_duration_ms*. Valid
values range from 0 to 100.
*(required, envoy.type.FractionalPercent)* The percentage of requests that will be delayed for
the duration specified by *fixed_duration_ms*.

Copy link
Member

Choose a reason for hiding this comment

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

Skip all changes to this file

fixed_duration_ms:
*(required, integer)* The delay duration in milliseconds. Must be greater than 0.
9 changes: 6 additions & 3 deletions docs/root/api-v1/network_filters/mongo_proxy_filter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ incoming data up until the timer event fires will be a part of the delay.

{
"fixed_delay": {
"percent": "...",
"percent": {
"numerator": "...",
"denominator": "..."
},
"duration_ms": "..."
}
}

percent
*(required, integer)* Probability of an eligible MongoDB operation to be affected by the
injected fault when there is no active fault. Valid values are integers in a range of [0, 100].
*(required, envoy.type.FractionalPercent)* Probability of an eligible MongoDB operation to be
affected by the injected fault when there is no active fault.
Copy link
Member

Choose a reason for hiding this comment

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

and this as well


duration_ms
*(required, integer)* Non-negative delay duration in milliseconds.
Expand Down
21 changes: 17 additions & 4 deletions include/envoy/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ class Snapshot {
virtual bool featureEnabled(const std::string& key, uint64_t default_value,
uint64_t random_value) const PURE;

/**
* Test if a feature is enabled using the built in random generator and total number of buckets
* for sampling.
* @param key supplies the feature key to lookup.
* @param default_value supplies the default value that will be used if either the feature key
* does not exist or it is not an integer.
* @param num_buckets control max number of buckets for sampling. Sampled value will be in a range
* of [0, num_buckets).
* @return true if the feature is enabled.
*/
virtual bool sampleFeatureEnabled(const std::string& key, uint64_t default_value,
Copy link
Member

Choose a reason for hiding this comment

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

nit: I see why you had to change the name here, but IMO "sample" is a bit strange in the sense that the other featureEnabled() functions also do sampling. A few options here:

  1. Get rid of the versions of featureEnabled() that don't require specifying number of buckets. Larger change.
  2. Some other name. featureEnabledEx ? (Bad, but not sure what would be better).

Copy link
Member Author

@venilnoronha venilnoronha Aug 8, 2018

Choose a reason for hiding this comment

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

@mattklein123 thanks for reviewing! I've addressed your other comments in f197fe2, and I'm working on this one now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Implemented option 1 in f98bcc0.

uint64_t num_buckets) const PURE;

/**
* Test if a feature is enabled using a supplied stable random value and total number of buckets
* for sampling.
Expand All @@ -112,12 +125,12 @@ class Snapshot {
* does not exist or it is not an integer.
* @param random_value supplies the stable random value to use for determining whether the feature
* is enabled.
* @param control max number of buckets for sampling. Sampled value will be in a range of
* [0, num_buckets).
* @param num_buckets control max number of buckets for sampling. Sampled value will be in a range
* of [0, num_buckets).
* @return true if the feature is enabled.
*/
virtual bool featureEnabled(const std::string& key, uint64_t default_value, uint64_t random_value,
uint64_t num_buckets) const PURE;
virtual bool sampleFeatureEnabled(const std::string& key, uint64_t default_value,
uint64_t random_value, uint64_t num_buckets) const PURE;
Copy link
Member

Choose a reason for hiding this comment

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

@mattklein123 how are you guys doing fractional percent with runtimes? IOW, do you need runtime support for fractional percent? If not, all changes to this file can be eliminated

Copy link
Member Author

Choose a reason for hiding this comment

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

I think access_log_impl uses runtime for sampling the feature status as seen here. Should we just stick to a similar approach for faults?

Copy link
Member

Choose a reason for hiding this comment

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

Looking again, why is this being renamed ?

Copy link
Member Author

@venilnoronha venilnoronha Aug 1, 2018

Choose a reason for hiding this comment

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

Because of a signature conflict between the routines highlighted here.


/**
* Fetch raw runtime data based on key.
Expand Down
2 changes: 1 addition & 1 deletion source/common/access_log/access_log_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ bool RuntimeFilter::evaluate(const RequestInfo::RequestInfo&,
random_value = random_.random();
}

return runtime_.snapshot().featureEnabled(
return runtime_.snapshot().sampleFeatureEnabled(
runtime_key_, percent_.numerator(), random_value,
ProtobufPercentHelper::fractionalPercentDenominatorToInt(percent_));
}
Expand Down
7 changes: 4 additions & 3 deletions source/common/config/filter_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ void FilterJson::translateMongoProxy(
JSON_UTIL_SET_STRING(json_config, proto_config, access_log);
if (json_config.hasObject("fault")) {
const auto json_fault = json_config.getObject("fault")->getObject("fixed_delay");
const auto json_percent = json_fault->getObject("percent");
auto* delay = proto_config.mutable_delay();

delay->set_type(envoy::config::filter::fault::v2::FaultDelay::FIXED);
delay->set_percent(static_cast<uint32_t>(json_fault->getInteger("percent")));
JSON_UTIL_SET_FRACTIONALPERCENT(*json_percent, *delay, percent);
JSON_UTIL_SET_DURATION_FROM_FIELD(*json_fault, *delay, fixed_delay, duration);
}
}
Expand All @@ -276,9 +276,10 @@ void FilterJson::translateFaultFilter(
}

if (!json_config_delay->empty()) {
const auto json_percent = json_config_delay->getObject("fixed_delay_percent");
auto* delay = proto_config.mutable_delay();
delay->set_type(envoy::config::filter::fault::v2::FaultDelay::FIXED);
delay->set_percent(static_cast<uint32_t>(json_config_delay->getInteger("fixed_delay_percent")));
JSON_UTIL_SET_FRACTIONALPERCENT(*json_percent, *delay, percent);
JSON_UTIL_SET_DURATION_FROM_FIELD(*json_config_delay, *delay, fixed_delay, fixed_duration);
}

Expand Down
15 changes: 15 additions & 0 deletions source/common/config/json_utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@
Protobuf::util::TimeUtil::MillisecondsToDuration((json).getInteger(#src_field "_ms"))); \
} \
} while (0)

// Set a envoy.type.FractionalPercent compatible field in a protobuf message with the corresponding
// numerator and denominator values from a JSON object if the fields are set in the JSON object.
#define JSON_UTIL_SET_FRACTIONALPERCENT(json, message, field_name) \
do { \
if ((json).hasObject("numerator")) { \
(message).mutable_##field_name()->set_numerator( \
static_cast<uint32_t>((json).getInteger("numerator"))); \
} \
if ((json).hasObject("denominator")) { \
(message).mutable_##field_name()->set_denominator( \
ProtobufPercentHelper::stringToFractionalPercentDenominatorType( \
(json).getString("denominator"))); \
} \
} while (0)
6 changes: 3 additions & 3 deletions source/common/http/conn_manager_utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ void ConnectionManagerUtility::mutateTracingRequestHeader(Http::HeaderMap& reque
UuidUtils::setTraceableUuid(x_request_id, UuidTraceStatus::Client);
} else if (request_headers.EnvoyForceTrace()) {
UuidUtils::setTraceableUuid(x_request_id, UuidTraceStatus::Forced);
} else if (runtime.snapshot().featureEnabled("tracing.random_sampling",
config.tracingConfig()->random_sampling_, result,
10000)) {
} else if (runtime.snapshot().sampleFeatureEnabled("tracing.random_sampling",
config.tracingConfig()->random_sampling_,
result, 10000)) {
UuidUtils::setTraceableUuid(x_request_id, UuidTraceStatus::Sampled);
}
}
Expand Down
34 changes: 28 additions & 6 deletions source/common/json/config_schemas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,20 @@ const std::string Json::Schema::MONGO_PROXY_NETWORK_FILTER_SCHEMA(R"EOF(
"type" : "object",
"properties" : {
"percent" : {
"type" : "integer",
"minimum" : 0,
"maximum" : 100
Copy link
Member

Choose a reason for hiding this comment

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

skip file

"type" : "object",
"properties" : {
"numerator" : {
"type" : "integer",
"minimum" : 0,
"maximum" : 100
},
"denominator" : {
"type" : "string",
"enum" : ["HUNDRED", "THOUSAND", "TEN_THOUSAND", "HUNDRED_THOUSAND", "MILLION"]
}
},
"required": ["numerator", "denominator"],
"additionalProperties" : false
},
"duration_ms" : {
"type" : "integer",
Expand Down Expand Up @@ -1022,9 +1033,20 @@ const std::string Json::Schema::FAULT_HTTP_FILTER_SCHEMA(R"EOF(
"enum" : ["fixed"]
},
"fixed_delay_percent" : {
"type" : "integer",
"minimum" : 0,
"maximum" : 100
"type" : "object",
"properties" : {
"numerator" : {
"type" : "integer",
"minimum" : 0,
"maximum" : 100
},
"denominator" : {
"type" : "string",
"enum" : ["HUNDRED", "THOUSAND", "TEN_THOUSAND", "HUNDRED_THOUSAND", "MILLION"]
}
},
"required": ["numerator", "denominator"],
"additionalProperties" : false
},
"fixed_duration_ms" : {
"type" : "integer",
Expand Down
22 changes: 22 additions & 0 deletions source/common/protobuf/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,34 @@ uint64_t convertPercent(double percent, uint64_t max_value) {
return max_value * (percent / 100.0);
}

envoy::type::FractionalPercent::DenominatorType
stringToFractionalPercentDenominatorType(const std::string denominator) {
if (denominator == "HUNDRED") {
return envoy::type::FractionalPercent::HUNDRED;
} else if (denominator == "THOUSAND") {
return envoy::type::FractionalPercent::THOUSAND;
} else if (denominator == "TEN_THOUSAND") {
return envoy::type::FractionalPercent::TEN_THOUSAND;
} else if (denominator == "HUNDRED_THOUSAND") {
return envoy::type::FractionalPercent::HUNDRED_THOUSAND;
} else if (denominator == "MILLION") {
return envoy::type::FractionalPercent::MILLION;
} else {
// Checked by schema.
NOT_REACHED_GCOVR_EXCL_LINE;
}
}

uint64_t fractionalPercentDenominatorToInt(const envoy::type::FractionalPercent& percent) {
switch (percent.denominator()) {
case envoy::type::FractionalPercent::HUNDRED:
return 100;
case envoy::type::FractionalPercent::THOUSAND:
return 1000;
case envoy::type::FractionalPercent::TEN_THOUSAND:
return 10000;
case envoy::type::FractionalPercent::HUNDRED_THOUSAND:
return 100000;
case envoy::type::FractionalPercent::MILLION:
return 1000000;
default:
Expand Down
8 changes: 8 additions & 0 deletions source/common/protobuf/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ namespace ProtobufPercentHelper {
uint64_t checkAndReturnDefault(uint64_t default_value, uint64_t max_value);
uint64_t convertPercent(double percent, uint64_t max_value);

/**
* Convert a string to a fractional percent DenominatorType enum value.
* @param denominator supplies the denominator string to convert.
* @return the converted DenominatorType.
*/
envoy::type::FractionalPercent::DenominatorType
stringToFractionalPercentDenominatorType(const std::string denominator);

/**
* Convert a fractional percent denominator enum into an integer.
* @param percent supplies percent to convert.
Expand Down
2 changes: 1 addition & 1 deletion source/common/router/router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool FilterUtility::shouldShadow(const ShadowPolicy& policy, Runtime::Loader& ru
}

if (!policy.runtimeKey().empty() &&
!runtime.snapshot().featureEnabled(policy.runtimeKey(), 0, stable_random, 10000UL)) {
!runtime.snapshot().sampleFeatureEnabled(policy.runtimeKey(), 0, stable_random, 10000UL)) {
return false;
}

Expand Down
11 changes: 8 additions & 3 deletions source/common/runtime/runtime_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ std::string RandomGeneratorImpl::uuid() {
return std::string(uuid, UUID_LENGTH);
}

bool SnapshotImpl::featureEnabled(const std::string& key, uint64_t default_value,
uint64_t random_value, uint64_t num_buckets) const {
bool SnapshotImpl::sampleFeatureEnabled(const std::string& key, uint64_t default_value,
uint64_t num_buckets) const {
return sampleFeatureEnabled(key, default_value, generator_.random(), num_buckets);
}

bool SnapshotImpl::sampleFeatureEnabled(const std::string& key, uint64_t default_value,
Copy link
Member

Choose a reason for hiding this comment

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

Skip renaming and overload old function name ?

Copy link
Member

Choose a reason for hiding this comment

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

Ignore

uint64_t random_value, uint64_t num_buckets) const {
return random_value % num_buckets < std::min(getInteger(key, default_value), num_buckets);
}

Expand All @@ -165,7 +170,7 @@ bool SnapshotImpl::featureEnabled(const std::string& key, uint64_t default_value

bool SnapshotImpl::featureEnabled(const std::string& key, uint64_t default_value,
uint64_t random_value) const {
return featureEnabled(key, default_value, random_value, 100);
return sampleFeatureEnabled(key, default_value, random_value, 100);
}

Copy link
Member

Choose a reason for hiding this comment

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

These two functions are not equivalent.. The stable random value is not same as number of buckets above. Am I missing something here>?

Copy link
Member Author

Choose a reason for hiding this comment

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

random_value and num_buckets aren't being used interchangeably. Either we pass both random_value and num_buckets to featureEnabled, or we only pass num_buckets. There's no way to pass random_value without num_buckets.

const std::string& SnapshotImpl::get(const std::string& key) const {
Expand Down
6 changes: 4 additions & 2 deletions source/common/runtime/runtime_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ class SnapshotImpl : public Snapshot, public ThreadLocal::ThreadLocalObject {
std::vector<OverrideLayerConstPtr>&& layers);

// Runtime::Snapshot
bool featureEnabled(const std::string& key, uint64_t default_value, uint64_t random_value,
uint64_t num_buckets) const override;
bool sampleFeatureEnabled(const std::string& key, uint64_t default_value,
uint64_t num_buckets) const override;
bool sampleFeatureEnabled(const std::string& key, uint64_t default_value, uint64_t random_value,
uint64_t num_buckets) const override;
bool featureEnabled(const std::string& key, uint64_t default_value) const override;
bool featureEnabled(const std::string& key, uint64_t default_value,
uint64_t random_value) const override;
Expand Down
10 changes: 6 additions & 4 deletions source/extensions/filters/http/fault/fault_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ Http::FilterHeadersStatus FaultFilter::decodeHeaders(Http::HeaderMap& headers, b
}

bool FaultFilter::isDelayEnabled() {
bool enabled = config_->runtime().snapshot().featureEnabled(DELAY_PERCENT_KEY,
fault_settings_->delayPercent());
bool enabled = config_->runtime().snapshot().sampleFeatureEnabled(
DELAY_PERCENT_KEY, fault_settings_->delayPercent().numerator(),
ProtobufPercentHelper::fractionalPercentDenominatorToInt(fault_settings_->delayPercent()));

if (!downstream_cluster_delay_percent_key_.empty()) {
enabled |= config_->runtime().snapshot().featureEnabled(downstream_cluster_delay_percent_key_,
fault_settings_->delayPercent());
enabled |= config_->runtime().snapshot().sampleFeatureEnabled(
downstream_cluster_delay_percent_key_, fault_settings_->delayPercent().numerator(),
ProtobufPercentHelper::fractionalPercentDenominatorToInt(fault_settings_->delayPercent()));
}

return enabled;
Expand Down
12 changes: 6 additions & 6 deletions source/extensions/filters/http/fault/fault_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ class FaultSettings : public Router::RouteSpecificFilterConfig {
return fault_filter_headers_;
}
uint64_t abortPercent() const { return abort_percent_; }
uint64_t delayPercent() const { return fixed_delay_percent_; }
envoy::type::FractionalPercent delayPercent() const { return fixed_delay_percent_; }
uint64_t delayDuration() const { return fixed_duration_ms_; }
uint64_t abortCode() const { return http_status_; }
const std::string& upstreamCluster() const { return upstream_cluster_; }
const std::unordered_set<std::string>& downstreamNodes() const { return downstream_nodes_; }

private:
uint64_t abort_percent_{}; // 0-100
uint64_t http_status_{}; // HTTP or gRPC return codes
uint64_t fixed_delay_percent_{}; // 0-100
uint64_t fixed_duration_ms_{}; // in milliseconds
std::string upstream_cluster_; // restrict faults to specific upstream cluster
uint64_t abort_percent_{}; // 0-100
uint64_t http_status_{}; // HTTP or gRPC return codes
envoy::type::FractionalPercent fixed_delay_percent_{}; // 0-100
uint64_t fixed_duration_ms_{}; // in milliseconds
std::string upstream_cluster_; // restrict faults to specific upstream cluster
std::vector<Http::HeaderUtility::HeaderData> fault_filter_headers_;
std::unordered_set<std::string> downstream_nodes_{}; // Inject failures for specific downstream
};
Expand Down
Loading