-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the outline of an outlier detection framework. It doesn't actually do anything right now. I want to get this deployed just to make sure that the basic flows look good and then I will start work on actual detection algorithms.
- Loading branch information
1 parent
2a80f8f
commit 673bcc9
Showing
22 changed files
with
410 additions
and
52 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
|
||
#include "envoy/common/pure.h" | ||
|
||
namespace Upstream { | ||
|
||
class Host; | ||
typedef std::shared_ptr<Host> HostPtr; | ||
|
||
/** | ||
* Sink for per host data. Proxy filters should send pertinent data when available. | ||
*/ | ||
class OutlierDetectorHostSink { | ||
public: | ||
virtual ~OutlierDetectorHostSink() {} | ||
|
||
/** | ||
* Add an HTTP response code for a host. | ||
*/ | ||
virtual void putHttpResponseCode(uint64_t code) PURE; | ||
|
||
/** | ||
* Add a response time for a host (in this case response time is generic and might be used for | ||
* different operations including HTTP, Mongo, Redis, etc.). | ||
*/ | ||
virtual void putResponseTime(std::chrono::milliseconds time) PURE; | ||
}; | ||
|
||
typedef std::unique_ptr<OutlierDetectorHostSink> OutlierDetectorHostSinkPtr; | ||
|
||
/** | ||
* Interface for an outlier detection engine. Uses per host data to determine which hosts in a | ||
* cluster are outliers and should be ejected. | ||
*/ | ||
class OutlierDetector { | ||
public: | ||
virtual ~OutlierDetector() {} | ||
|
||
/** | ||
* Outlier detection change state callback. | ||
*/ | ||
typedef std::function<void(HostPtr host)> ChangeStateCb; | ||
|
||
/** | ||
* Add a changed state callback to the detector. The callback will be called whenever any host | ||
* changes state (either ejected or brought back in) due to outlier status. | ||
*/ | ||
virtual void addChangedStateCb(ChangeStateCb cb) PURE; | ||
}; | ||
|
||
typedef std::unique_ptr<OutlierDetector> OutlierDetectorPtr; | ||
|
||
} // 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
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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "outlier_detection_impl.h" | ||
|
||
#include "common/common/assert.h" | ||
|
||
namespace Upstream { | ||
|
||
OutlierDetectorPtr OutlierDetectorImplFactory::createForCluster(Cluster& cluster, | ||
const Json::Object& cluster_config, | ||
Event::Dispatcher& dispatcher) { | ||
// Right now we don't support any configuration but in order to make the config backwards | ||
// compatible we just look for an empty object. | ||
if (cluster_config.hasObject("outlier_detection")) { | ||
return OutlierDetectorPtr{new OutlierDetectorImpl(cluster, dispatcher)}; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
OutlierDetectorImpl::OutlierDetectorImpl(Cluster& cluster, Event::Dispatcher&) { | ||
for (HostPtr host : cluster.hosts()) { | ||
addHostSink(host); | ||
} | ||
|
||
cluster.addMemberUpdateCb([this](const std::vector<HostPtr>& hosts_added, | ||
const std::vector<HostPtr>& hosts_removed) -> void { | ||
for (HostPtr host : hosts_added) { | ||
addHostSink(host); | ||
} | ||
|
||
for (HostPtr host : hosts_removed) { | ||
ASSERT(host_sinks_.count(host) == 1); | ||
host_sinks_.erase(host); | ||
} | ||
}); | ||
} | ||
|
||
void OutlierDetectorImpl::addHostSink(HostPtr host) { | ||
ASSERT(host_sinks_.count(host) == 0); | ||
OutlierDetectorHostSinkImpl* sink = new OutlierDetectorHostSinkImpl(); | ||
host_sinks_[host] = sink; | ||
host->setOutlierDetector(OutlierDetectorHostSinkPtr{sink}); | ||
} | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#pragma once | ||
|
||
#include "envoy/upstream/outlier_detection.h" | ||
#include "envoy/upstream/upstream.h" | ||
|
||
#include "common/json/json_loader.h" | ||
|
||
namespace Upstream { | ||
|
||
/** | ||
* Null host sink implementation. | ||
*/ | ||
class OutlierDetectorHostSinkNullImpl : public OutlierDetectorHostSink { | ||
public: | ||
// Upstream::OutlierDetectorHostSink | ||
void putHttpResponseCode(uint64_t) override {} | ||
void putResponseTime(std::chrono::milliseconds) override {} | ||
}; | ||
|
||
/** | ||
* Factory for creating a detector from a JSON configuration. | ||
*/ | ||
class OutlierDetectorImplFactory { | ||
public: | ||
static OutlierDetectorPtr createForCluster(Cluster& cluster, const Json::Object& cluster_config, | ||
Event::Dispatcher& dispatcher); | ||
}; | ||
|
||
/** | ||
* Implementation of OutlierDetectorHostSink for the generic detector. | ||
*/ | ||
class OutlierDetectorHostSinkImpl : public OutlierDetectorHostSink { | ||
public: | ||
// Upstream::OutlierDetectorHostSink | ||
void putHttpResponseCode(uint64_t) override {} | ||
void putResponseTime(std::chrono::milliseconds) override {} | ||
}; | ||
|
||
/** | ||
* An implementation of an outlier detector. In the future we may support multiple outlier detection | ||
* implementations with different configuration. For now, as we iterate everything is contained | ||
* within this implementation. | ||
*/ | ||
class OutlierDetectorImpl : public OutlierDetector { | ||
public: | ||
OutlierDetectorImpl(Cluster& cluster, Event::Dispatcher& dispatcher); | ||
|
||
// Upstream::OutlierDetector | ||
void addChangedStateCb(ChangeStateCb cb) override { callbacks_.push_back(cb); } | ||
|
||
private: | ||
void addHostSink(HostPtr host); | ||
|
||
std::list<ChangeStateCb> callbacks_; | ||
std::unordered_map<HostPtr, OutlierDetectorHostSinkImpl*> host_sinks_; | ||
}; | ||
|
||
} // 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
Oops, something went wrong.