From cba172dd78e67906772088e6a6fc5002f0e3db6f Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Fri, 24 Mar 2017 17:23:57 -0700 Subject: [PATCH 1/2] Update README on how to enable check cache. --- src/envoy/mixer/README.md | 35 +++++++++++++++++++++++++++++--- src/envoy/mixer/config.cc | 31 ++++++++++++++-------------- src/envoy/mixer/config.h | 1 + src/envoy/mixer/http_control.cc | 31 ++++++++++++++++++++++++---- src/envoy/mixer/repositories.bzl | 2 +- 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/src/envoy/mixer/README.md b/src/envoy/mixer/README.md index 493b2d185cf..b72613ecdff 100644 --- a/src/envoy/mixer/README.md +++ b/src/envoy/mixer/README.md @@ -73,6 +73,7 @@ This filter will intercept all HTTP requests and call Mixer. Here is its config: }, "quota_name": "RequestCount", "quota_amount": "1", + "check_cache_expiration_in_seconds": "600", "check_cache_keys": [ "request.host", "request.path", @@ -88,13 +89,20 @@ Notes: * quota_name, quota_amount are used for making quota call. quota_amount is default to 1 if missing. * check_cache_keys is to cache check calls. If missing or empty, check calls are not cached. -By default, mixer filter forwards attributes and does not invoke mixer server. You can customize this behavior per HTTP route by supplying an opaque config in the route config: +## HTTP Route opaque config +By default, mixer filter only forwards attributes and does not call mixer server. This behavior can be changed per HTTP route by supplying an opaque config: ``` - "opaque_config": { + "routes": [ + { + "timeout_ms": 0, + "prefix": "/", + "cluster": "service1", + "opaque_config": { "mixer_control": "on", "mixer_forward": "off" - } + } + } ``` This route opaque config reverts the behavior by sending requests to mixer server but not forwarding any attributes. @@ -111,3 +119,24 @@ Mixer client can be configured to make Quota call for all requests. If "quota_n ## How to pass some attributes from client proxy to mixer. Usually client proxy is not configured to call mixer (it can be enabled in the route opaque_config). Client proxy can pass some attributes to mixer by using "forward_attributes" field. Its attributes will be sent to the upstream proxy (the server proxy). If the server proxy is calling mixer, these attributes will be sent to the mixer. + + +## How to enable cache for Check calls + +Check calls can be cached. By default, it is not enabled. It can be enabled by supplying non-empty "check_cache_keys" string list in the mixer filter config. Only these attributes in the Check request, their keys and values, are used to calculate the key for the cache lookup. If it is a cache hit, the cached response will be used. The cached response will be expired in 5 minutes by default. It can be overrided by supplying "check_cache_expiration_in_seconds" in the mixer filter config. + +Following is a sample mixer filter config to enable check cache: +``` + "check_cache_expiration_in_seconds": "600", + "check_cache_keys": [ + "request.host", + "request.path", + "source.labels", + "request.headers/:method", + "origin.user" + ] +``` + +For the string map attributes in the above example: +1) "request.headers" attribute is a string map, "request.headers/:method" cache key means only its ":method" key and value are used for cache key. +2) "source.labels" attribute is a string map, "source.labels" cache key means all key value pairs for the string map will be used. diff --git a/src/envoy/mixer/config.cc b/src/envoy/mixer/config.cc index 5675a5a311f..8e06b1e9c2b 100644 --- a/src/envoy/mixer/config.cc +++ b/src/envoy/mixer/config.cc @@ -22,21 +22,22 @@ namespace Mixer { namespace { // The Json object name for mixer-server. -const std::string kJsonNameMixerServer("mixer_server"); +const std::string kMixerServer("mixer_server"); // The Json object name for static attributes. -const std::string kJsonNameMixerAttributes("mixer_attributes"); +const std::string kMixerAttributes("mixer_attributes"); // The Json object name to specify attributes which will be forwarded // to the upstream istio proxy. -const std::string kJsonNameForwardAttributes("forward_attributes"); +const std::string kForwardAttributes("forward_attributes"); // The Json object name for quota name and amount. -const std::string kJsonNameQuotaName("quota_name"); -const std::string kJsonNameQuotaAmount("quota_amount"); +const std::string kQuotaName("quota_name"); +const std::string kQuotaAmount("quota_amount"); // The Json object name for check cache keys. -const std::string kJsonNameCheckCacheKeys("check_cache_keys"); +const std::string kCheckCacheKeys("check_cache_keys"); +const std::string kCheckCacheExpiration("check_cache_expiration_in_seconds"); void ReadString(const Json::Object& json, const std::string& name, std::string* value) { @@ -67,28 +68,28 @@ void ReadStringVector(const Json::Object& json, const std::string& name, } // namespace void MixerConfig::Load(const Json::Object& json) { - ReadString(json, kJsonNameMixerServer, &mixer_server); + ReadString(json, kMixerServer, &mixer_server); - ReadStringMap(json, kJsonNameMixerAttributes, &mixer_attributes); - ReadStringMap(json, kJsonNameForwardAttributes, &forward_attributes); + ReadStringMap(json, kMixerAttributes, &mixer_attributes); + ReadStringMap(json, kForwardAttributes, &forward_attributes); - ReadString(json, kJsonNameQuotaName, "a_name); - ReadString(json, kJsonNameQuotaAmount, "a_amount); + ReadString(json, kQuotaName, "a_name); + ReadString(json, kQuotaAmount, "a_amount); - ReadStringVector(json, kJsonNameCheckCacheKeys, &check_cache_keys); + ReadStringVector(json, kCheckCacheKeys, &check_cache_keys); + ReadString(json, kCheckCacheExpiration, &check_cache_expiration); } void MixerConfig::ExtractQuotaAttributes(Attributes* attr) const { if (!quota_name.empty()) { - attr->attributes[ ::istio::mixer_client::kQuotaName] = + attr->attributes[Attributes::kQuotaName] = Attributes::StringValue(quota_name); int64_t amount = 1; // default amount to 1. if (!quota_amount.empty()) { amount = std::stoi(quota_amount); } - attr->attributes[ ::istio::mixer_client::kQuotaAmount] = - Attributes::Int64Value(amount); + attr->attributes[Attributes::kQuotaAmount] = Attributes::Int64Value(amount); } } diff --git a/src/envoy/mixer/config.h b/src/envoy/mixer/config.h index ee301aba2b5..fc08f33c439 100644 --- a/src/envoy/mixer/config.h +++ b/src/envoy/mixer/config.h @@ -41,6 +41,7 @@ struct MixerConfig { // The attribute names for check cache. std::vector check_cache_keys; + std::string check_cache_expiration; // Load the config from envoy config. void Load(const Json::Object& json); diff --git a/src/envoy/mixer/http_control.cc b/src/envoy/mixer/http_control.cc index b44cf707172..1eeea39b570 100644 --- a/src/envoy/mixer/http_control.cc +++ b/src/envoy/mixer/http_control.cc @@ -23,8 +23,12 @@ #include "src/envoy/mixer/utils.h" using ::google::protobuf::util::Status; +using ::istio::mixer_client::CheckOptions; using ::istio::mixer_client::Attributes; using ::istio::mixer_client::DoneFunc; +using ::istio::mixer_client::MixerClientOptions; +using ::istio::mixer_client::ReportOptions; +using ::istio::mixer_client::QuotaOptions; namespace Http { namespace Mixer { @@ -45,6 +49,27 @@ const std::string kResponseLatency = "response.latency"; const std::string kResponseSize = "response.size"; const std::string kResponseTime = "response.time"; +// Check cache size: 10000 cache entries. +const int kCheckCacheEntries = 10000; +// Default check cache expired in 5 minutes. +const int kCheckCacheExpirationInSeconds = 300; + +CheckOptions GetCheckOptions(const MixerConfig& config) { + int expiration = kCheckCacheExpirationInSeconds; + if (!config.check_cache_expiration.empty()) { + expiration = std::stoi(config.check_cache_expiration); + } + + // Remove expired items from cache 1 second later. + CheckOptions options(kCheckCacheEntries, expiration * 1000, + (expiration + 1) * 1000); + + options.cache_keys.insert(config.check_cache_keys.begin(), + config.check_cache_keys.end()); + + return options; +} + void SetStringAttribute(const std::string& name, const std::string& value, Attributes* attr) { if (!value.empty()) { @@ -110,11 +135,9 @@ void FillRequestInfoAttributes(const AccessLog::RequestInfo& info, HttpControl::HttpControl(const MixerConfig& mixer_config) : mixer_config_(mixer_config) { - ::istio::mixer_client::MixerClientOptions options; + MixerClientOptions options(GetCheckOptions(mixer_config), ReportOptions(), + QuotaOptions()); options.mixer_server = mixer_config_.mixer_server; - options.check_options.cache_keys.insert( - mixer_config_.check_cache_keys.begin(), - mixer_config_.check_cache_keys.end()); mixer_client_ = ::istio::mixer_client::CreateMixerClient(options); mixer_config_.ExtractQuotaAttributes("a_attributes_); diff --git a/src/envoy/mixer/repositories.bzl b/src/envoy/mixer/repositories.bzl index ffa2a20409f..a5c7ba1e9de 100644 --- a/src/envoy/mixer/repositories.bzl +++ b/src/envoy/mixer/repositories.bzl @@ -15,7 +15,7 @@ ################################################################################ # -MIXER_CLIENT = "0e6f858bc7b52dc8f143f46ac32afc79d504e8a4" +MIXER_CLIENT = "dfcccd33151046c569b27d66f0c6a8bc82f977b3" def mixer_client_repositories(bind=True): native.git_repository( From eb5beae59234c3a2cd6244ee9209b48528f69a7b Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Sat, 25 Mar 2017 18:27:18 -0700 Subject: [PATCH 2/2] Update the comment. --- src/envoy/mixer/README.md | 9 +++++---- src/envoy/mixer/http_control.cc | 3 +-- src/envoy/mixer/repositories.bzl | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/envoy/mixer/README.md b/src/envoy/mixer/README.md index b72613ecdff..48229a3631e 100644 --- a/src/envoy/mixer/README.md +++ b/src/envoy/mixer/README.md @@ -86,11 +86,11 @@ Notes: * mixer_server is required * mixer_attributes: these attributes will be sent to the mixer in both Check and Report calls. * forward_attributes: these attributes will be forwarded to the upstream istio/proxy. It will send them to mixer in Check and Report calls. -* quota_name, quota_amount are used for making quota call. quota_amount is default to 1 if missing. +* quota_name, quota_amount are used for making quota call. quota_amount defaults to 1. * check_cache_keys is to cache check calls. If missing or empty, check calls are not cached. ## HTTP Route opaque config -By default, mixer filter only forwards attributes and does not call mixer server. This behavior can be changed per HTTP route by supplying an opaque config: +By default, the mixer filter only forwards attributes and does not call mixer server. This behavior can be changed per HTTP route by supplying an opaque config: ``` "routes": [ @@ -123,9 +123,10 @@ Usually client proxy is not configured to call mixer (it can be enabled in the r ## How to enable cache for Check calls -Check calls can be cached. By default, it is not enabled. It can be enabled by supplying non-empty "check_cache_keys" string list in the mixer filter config. Only these attributes in the Check request, their keys and values, are used to calculate the key for the cache lookup. If it is a cache hit, the cached response will be used. The cached response will be expired in 5 minutes by default. It can be overrided by supplying "check_cache_expiration_in_seconds" in the mixer filter config. +Check calls can be cached. By default, it is not enabled. It can be enabled by supplying non-empty "check_cache_keys" string list in the mixer filter config. Only these attributes in the Check request, their keys and values, are used to calculate the key for the cache lookup. If it is a cache hit, the cached response will be used. +The cached response will be expired in 5 minutes by default. It can be overrided by supplying "check_cache_expiration_in_seconds" in the mixer filter config. The Check response from the mixer has an expiration field. If it is filled, it will be used. By design, the mixer will control the cache expiration time. -Following is a sample mixer filter config to enable check cache: +Following is a sample mixer filter config to enable the Check call cache: ``` "check_cache_expiration_in_seconds": "600", "check_cache_keys": [ diff --git a/src/envoy/mixer/http_control.cc b/src/envoy/mixer/http_control.cc index 1eeea39b570..8283482dff3 100644 --- a/src/envoy/mixer/http_control.cc +++ b/src/envoy/mixer/http_control.cc @@ -64,8 +64,7 @@ CheckOptions GetCheckOptions(const MixerConfig& config) { CheckOptions options(kCheckCacheEntries, expiration * 1000, (expiration + 1) * 1000); - options.cache_keys.insert(config.check_cache_keys.begin(), - config.check_cache_keys.end()); + options.cache_keys = config.check_cache_keys; return options; } diff --git a/src/envoy/mixer/repositories.bzl b/src/envoy/mixer/repositories.bzl index a5c7ba1e9de..4382e71f921 100644 --- a/src/envoy/mixer/repositories.bzl +++ b/src/envoy/mixer/repositories.bzl @@ -15,7 +15,7 @@ ################################################################################ # -MIXER_CLIENT = "dfcccd33151046c569b27d66f0c6a8bc82f977b3" +MIXER_CLIENT = "c5d857e28bfcc53f20f59466b464f99526737545" def mixer_client_repositories(bind=True): native.git_repository(