-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
outlier: tooling for success rate ejection #618
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
41f5a06
outlier: tooling for success rate ejection
ce8eb86
docs for admin port
edaf832
update
6ddc858
update
99df9b4
link
1f3ff02
Merge branch 'master' into outlier-sr-tooling
5953641
update
24cedc8
docs
84a8f97
update
b1e5845
return
3825f62
coverage
7d3f240
nit
08f1fcd
sentence
a401824
Merge branch 'master' into outlier-sr-tooling
0bd0487
comments
c103237
format
eaedc71
format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,34 +48,16 @@ class DetectorHostSink { | |
* @return the last time this host was unejected, if the host has been unejected previously. | ||
*/ | ||
virtual const Optional<SystemTime>& lastUnejectionTime() PURE; | ||
}; | ||
|
||
typedef std::unique_ptr<DetectorHostSink> DetectorHostSinkPtr; | ||
|
||
enum class EjectionType { Consecutive5xx, SuccessRate }; | ||
|
||
/** | ||
* Sink for outlier detection event logs. | ||
*/ | ||
class EventLogger { | ||
public: | ||
virtual ~EventLogger() {} | ||
|
||
/** | ||
* Log an ejection event. | ||
* @param host supplies the host that generated the event. | ||
* @param type supplies the type of the event. | ||
* @return the success rate of the host in the last calculated interval, in the range 0-100. | ||
* -1 means that the host did not have enough request volume to calculate success rate | ||
* or the cluster did not have enough hosts to run through success rate outlier ejection. | ||
*/ | ||
virtual void logEject(HostDescriptionConstSharedPtr host, EjectionType type) PURE; | ||
|
||
/** | ||
* Log an unejection event. | ||
* @param host supplies the host that generated the event. | ||
*/ | ||
virtual void logUneject(HostDescriptionConstSharedPtr host) PURE; | ||
virtual double successRate() const PURE; | ||
}; | ||
|
||
typedef std::shared_ptr<EventLogger> EventLoggerSharedPtr; | ||
typedef std::unique_ptr<DetectorHostSink> DetectorHostSinkPtr; | ||
|
||
/** | ||
* Interface for an outlier detection engine. Uses per host data to determine which hosts in a | ||
|
@@ -95,9 +77,53 @@ class Detector { | |
* changes state (either ejected or brought back in) due to outlier status. | ||
*/ | ||
virtual void addChangedStateCb(ChangeStateCb cb) PURE; | ||
|
||
/** | ||
* Returns the average success rate of the hosts in the Detector for the last aggregation | ||
* interval. | ||
* @return the average success rate, or -1 if there were not enough hosts with enough request | ||
* volume to proceed with success rate based outlier ejection. | ||
*/ | ||
virtual double successRateAverage() const PURE; | ||
|
||
/** | ||
* Returns the success rate threshold used in the last interval. The threshold is used to eject | ||
* hosts based on their success rate. | ||
* @return the threshold, or -1 if there were not enough hosts with enough request volume to | ||
* proceed with success rate based outlier ejection. | ||
*/ | ||
virtual double successRateEjectionThreshold() const PURE; | ||
}; | ||
|
||
typedef std::shared_ptr<Detector> DetectorSharedPtr; | ||
|
||
enum class EjectionType { Consecutive5xx, SuccessRate }; | ||
|
||
/** | ||
* Sink for outlier detection event logs. | ||
*/ | ||
class EventLogger { | ||
public: | ||
virtual ~EventLogger() {} | ||
|
||
/** | ||
* Log an ejection event. | ||
* @param host supplies the host that generated the event. | ||
* @param detector supplies the detector that is doing the ejection. | ||
* @param type supplies the type of the event. | ||
* @param enforced is true if the ejection took place; false, if only logging took place. | ||
*/ | ||
virtual void logEject(HostDescriptionConstSharedPtr host, Detector& detector, EjectionType type, | ||
bool enforced) PURE; | ||
|
||
/** | ||
* Log an unejection event. | ||
* @param host supplies the host that generated the event. | ||
*/ | ||
virtual void logUneject(HostDescriptionConstSharedPtr host) PURE; | ||
}; | ||
|
||
typedef std::shared_ptr<EventLogger> EventLoggerSharedPtr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: new line after type def |
||
|
||
} // Outlier | ||
} // Upstream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,8 @@ DetectorImpl::DetectorImpl(const Cluster& cluster, const Json::Object& json_conf | |
: config_(json_config), dispatcher_(dispatcher), runtime_(runtime), time_source_(time_source), | ||
stats_(generateStats(cluster.info()->statsScope())), | ||
interval_timer_(dispatcher.createTimer([this]() -> void { onIntervalTimer(); })), | ||
event_logger_(event_logger) {} | ||
event_logger_(event_logger), success_rate_average_(-1), success_rate_ejection_threshold_(-1) { | ||
} | ||
|
||
DetectorImpl::~DetectorImpl() { | ||
for (auto host : host_sinks_) { | ||
|
@@ -185,7 +186,11 @@ void DetectorImpl::ejectHost(HostSharedPtr host, EjectionType type) { | |
runCallbacks(host); | ||
|
||
if (event_logger_) { | ||
event_logger_->logEject(host, type); | ||
event_logger_->logEject(host, *this, type, true); | ||
} | ||
} else { | ||
if (event_logger_) { | ||
event_logger_->logEject(host, *this, type, false); | ||
} | ||
} | ||
} else { | ||
|
@@ -240,7 +245,7 @@ void DetectorImpl::onConsecutive5xxWorker(HostSharedPtr host) { | |
// outliers. | ||
const double Utility::SUCCESS_RATE_STDEV_FACTOR = 1.9; | ||
|
||
double Utility::successRateEjectionThreshold( | ||
Utility::EjectionPair Utility::successRateEjectionThreshold( | ||
double success_rate_sum, const std::vector<HostSuccessRatePair>& valid_success_rate_hosts) { | ||
// This function is using mean and standard deviation as statistical measures for outlier | ||
// detection. First the mean is calculated by dividing the sum of success rate data over the | ||
|
@@ -266,7 +271,7 @@ double Utility::successRateEjectionThreshold( | |
variance /= valid_success_rate_hosts.size(); | ||
double stdev = std::sqrt(variance); | ||
|
||
return mean - (SUCCESS_RATE_STDEV_FACTOR * stdev); | ||
return {mean, (mean - (SUCCESS_RATE_STDEV_FACTOR * stdev))}; | ||
} | ||
|
||
void DetectorImpl::processSuccessRateEjections() { | ||
|
@@ -277,6 +282,10 @@ void DetectorImpl::processSuccessRateEjections() { | |
std::vector<HostSuccessRatePair> valid_success_rate_hosts; | ||
double success_rate_sum = 0; | ||
|
||
// Reset the Detector's success rate mean and stdev. | ||
success_rate_average_ = -1; | ||
success_rate_ejection_threshold_ = -1; | ||
|
||
// Exit early if there are not enough hosts. | ||
if (host_sinks_.size() < success_rate_minimum_hosts) { | ||
return; | ||
|
@@ -286,7 +295,6 @@ void DetectorImpl::processSuccessRateEjections() { | |
valid_success_rate_hosts.reserve(host_sinks_.size()); | ||
|
||
for (const auto& host : host_sinks_) { | ||
host.second->updateCurrentSuccessRateBucket(); | ||
// Don't do work if the host is already ejected. | ||
if (!host.first->healthFlagGet(Host::HealthFlag::FAILED_OUTLIER_CHECK)) { | ||
Optional<double> host_success_rate = | ||
|
@@ -296,15 +304,18 @@ void DetectorImpl::processSuccessRateEjections() { | |
valid_success_rate_hosts.emplace_back( | ||
HostSuccessRatePair(host.first, host_success_rate.value())); | ||
success_rate_sum += host_success_rate.value(); | ||
host.second->successRate(host_success_rate.value()); | ||
} | ||
} | ||
} | ||
|
||
if (valid_success_rate_hosts.size() >= success_rate_minimum_hosts) { | ||
double ejection_threshold = | ||
Utility::EjectionPair ejection_pair = | ||
Utility::successRateEjectionThreshold(success_rate_sum, valid_success_rate_hosts); | ||
success_rate_average_ = ejection_pair.success_rate_average_; | ||
success_rate_ejection_threshold_ = ejection_pair.ejection_threshold_; | ||
for (const auto& host_success_rate_pair : valid_success_rate_hosts) { | ||
if (host_success_rate_pair.success_rate_ < ejection_threshold) { | ||
if (host_success_rate_pair.success_rate_ < success_rate_ejection_threshold_) { | ||
stats_.ejections_success_rate_.inc(); | ||
ejectHost(host_success_rate_pair.host_, EjectionType::SuccessRate); | ||
} | ||
|
@@ -317,6 +328,12 @@ void DetectorImpl::onIntervalTimer() { | |
|
||
for (auto host : host_sinks_) { | ||
checkHostForUneject(host.first, host.second, now); | ||
|
||
// Need to update the writer bucket to keep the data valid. | ||
host.second->updateCurrentSuccessRateBucket(); | ||
// Refresh host success rate stat for the /clusters endpoint. If there is a new valid value, it | ||
// will get updated in processSuccessRateEjections(). | ||
host.second->successRate(-1); | ||
} | ||
|
||
processSuccessRateEjections(); | ||
|
@@ -330,25 +347,55 @@ void DetectorImpl::runCallbacks(HostSharedPtr host) { | |
} | ||
} | ||
|
||
void EventLoggerImpl::logEject(HostDescriptionConstSharedPtr host, EjectionType type) { | ||
void EventLoggerImpl::logEject(HostDescriptionConstSharedPtr host, Detector& detector, | ||
EjectionType type, bool enforced) { | ||
// TODO(mattklein123): Log friendly host name (e.g., instance ID or DNS name). | ||
// clang-format off | ||
static const std::string json = | ||
static const std::string json_5xx = | ||
std::string("{{") + | ||
"\"time\": \"{}\", " + | ||
"\"secs_since_last_action\": \"{}\", " + | ||
"\"cluster\": \"{}\", " + | ||
"\"upstream_url\": \"{}\", " + | ||
"\"action\": \"eject\", " + | ||
"\"type\": \"{}\", " + | ||
"\"num_ejections\": {}" + | ||
"\"num_ejections\": \"{}\", " + | ||
"\"enforced\": \"{}\"" + | ||
"}}\n"; | ||
|
||
static const std::string json_success_rate = | ||
std::string("{{") + | ||
"\"time\": \"{}\", " + | ||
"\"secs_since_last_action\": \"{}\", " + | ||
"\"cluster\": \"{}\", " + | ||
"\"upstream_url\": \"{}\", " + | ||
"\"action\": \"eject\", " + | ||
"\"type\": \"{}\", " + | ||
"\"num_ejections\": \"{}\", " + | ||
"\"enforced\": \"{}\", " + | ||
"\"host_success_rate\": \"{}\", " + | ||
"\"cluster_average_success_rate\": \"{}\", " + | ||
"\"cluster_success_rate_ejection_threshold\": \"{}\"" + | ||
"}}\n"; | ||
// clang-format on | ||
SystemTime now = time_source_.currentSystemTime(); | ||
file_->write(fmt::format(json, AccessLogDateTimeFormatter::fromTime(now), | ||
secsSinceLastAction(host->outlierDetector().lastUnejectionTime(), now), | ||
host->cluster().name(), host->address()->asString(), typeToString(type), | ||
host->outlierDetector().numEjections())); | ||
|
||
switch (type) { | ||
case EjectionType::Consecutive5xx: | ||
file_->write(fmt::format(json_5xx, AccessLogDateTimeFormatter::fromTime(now), | ||
secsSinceLastAction(host->outlierDetector().lastUnejectionTime(), now), | ||
host->cluster().name(), host->address()->asString(), | ||
typeToString(type), host->outlierDetector().numEjections(), enforced)); | ||
break; | ||
case EjectionType::SuccessRate: | ||
file_->write(fmt::format(json_success_rate, AccessLogDateTimeFormatter::fromTime(now), | ||
secsSinceLastAction(host->outlierDetector().lastUnejectionTime(), now), | ||
host->cluster().name(), host->address()->asString(), | ||
typeToString(type), host->outlierDetector().numEjections(), enforced, | ||
host->outlierDetector().successRate(), detector.successRateAverage(), | ||
detector.successRateEjectionThreshold())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add break here for next person that comes along. |
||
break; | ||
} | ||
} | ||
|
||
void EventLoggerImpl::logUneject(HostDescriptionConstSharedPtr host) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you actually look at these docs rendered? This is almost definitely not correct and will look broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I rendered them and they look correct