From bfa5c440d6e59aacb4d1b4b0ff674483541c72ba Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Wed, 15 Feb 2017 15:27:47 -0800 Subject: [PATCH 1/3] Add metric_cost in config. --- contrib/endpoints/src/api_manager/config.cc | 30 +++++++++++++++++++ contrib/endpoints/src/api_manager/config.h | 2 ++ .../endpoints/src/api_manager/method_impl.h | 11 +++++++ 3 files changed, 43 insertions(+) diff --git a/contrib/endpoints/src/api_manager/config.cc b/contrib/endpoints/src/api_manager/config.cc index b0a1b85d119..dda4feb8be2 100644 --- a/contrib/endpoints/src/api_manager/config.cc +++ b/contrib/endpoints/src/api_manager/config.cc @@ -113,6 +113,36 @@ MethodInfoImpl *Config::GetOrCreateMethodInfoImpl(const string &name, return i->second.get(); } +bool Config::LoadQuotaRule(ApiManagerEnvInterface *env) { + for (const auto &rule : service_.quota().metric_rules()) { + auto method = utils::FindOrNull(method_map_, rule.selector()); + if (method) { + for (auto &metric_cost : rule.metric_costs()) { + (*method)->add_metric_cost(metric_cost.first, metric_cost.second); + } + } else { + env->LogDebug("Method not Found."); + } + } + + for (const auto &rule : service_.quota().rules()) { + auto method = utils::FindOrNull(method_map_, rule.selector()); + if (method) { + for (const auto &group_name : rule.groups()) { + for (const auto &group : service_.quota().groups()) { + if (group.name() == group_name.group()) { + for (const auto &limit : group.limits()) { + (*method)->add_metric_cost(limit.metric(), group_name.cost()); + } + } + } + } + } + } + + return true; +} + bool Config::LoadHttpMethods(ApiManagerEnvInterface *env, PathMatcherBuilder *pmb) { std::set all_urls, urls_with_options; diff --git a/contrib/endpoints/src/api_manager/config.h b/contrib/endpoints/src/api_manager/config.h index 9a56d16d745..53c6cf39f2c 100644 --- a/contrib/endpoints/src/api_manager/config.h +++ b/contrib/endpoints/src/api_manager/config.h @@ -111,6 +111,8 @@ class Config { // Load SystemParameters info to MethodInfo. bool LoadSystemParameters(ApiManagerEnvInterface *env); + bool LoadQuotaRule(ApiManagerEnvInterface *env); + // Gets the MethodInfoImpl creating it if necessary MethodInfoImpl *GetOrCreateMethodInfoImpl(const std::string &name, const std::string &api_name, diff --git a/contrib/endpoints/src/api_manager/method_impl.h b/contrib/endpoints/src/api_manager/method_impl.h index e5739d639bc..f05e7806c80 100644 --- a/contrib/endpoints/src/api_manager/method_impl.h +++ b/contrib/endpoints/src/api_manager/method_impl.h @@ -62,6 +62,10 @@ class MethodInfoImpl : public MethodInfo { const std::string &backend_address() const { return backend_address_; } + const std::map &metric_cost_map() const { + return metric_cost_map_; + } + const std::string &rpc_method_full_name() const { return rpc_method_full_name_; } @@ -90,6 +94,10 @@ class MethodInfoImpl : public MethodInfo { url_query_parameters_[name].push_back(url_query_parameter); } + void add_metric_cost(const std::string &metric, int64_t cost) { + metric_cost_map_[metric] = cost; + } + // After add all system parameters, lookup some of them to cache // their lookup results. void process_system_parameters(); @@ -175,6 +183,9 @@ class MethodInfoImpl : public MethodInfo { // Whether the response is streaming or not. bool response_streaming_; + + // map of metric and its cost + std::map metric_cost_map_; }; typedef std::unique_ptr MethodInfoImplPtr; From ad8cccf2547e20f531019f0f83c6e11b3dd6de2b Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Thu, 16 Feb 2017 14:18:24 -0800 Subject: [PATCH 2/3] Remove group rules. --- contrib/endpoints/src/api_manager/config.cc | 17 +---------------- contrib/endpoints/src/api_manager/method_impl.h | 15 ++++++++------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/contrib/endpoints/src/api_manager/config.cc b/contrib/endpoints/src/api_manager/config.cc index dda4feb8be2..50504083100 100644 --- a/contrib/endpoints/src/api_manager/config.cc +++ b/contrib/endpoints/src/api_manager/config.cc @@ -121,22 +121,7 @@ bool Config::LoadQuotaRule(ApiManagerEnvInterface *env) { (*method)->add_metric_cost(metric_cost.first, metric_cost.second); } } else { - env->LogDebug("Method not Found."); - } - } - - for (const auto &rule : service_.quota().rules()) { - auto method = utils::FindOrNull(method_map_, rule.selector()); - if (method) { - for (const auto &group_name : rule.groups()) { - for (const auto &group : service_.quota().groups()) { - if (group.name() == group_name.group()) { - for (const auto &limit : group.limits()) { - (*method)->add_metric_cost(limit.metric(), group_name.cost()); - } - } - } - } + env->LogError("Metric rule with selector is mismatched."); } } diff --git a/contrib/endpoints/src/api_manager/method_impl.h b/contrib/endpoints/src/api_manager/method_impl.h index f05e7806c80..d6eaba33724 100644 --- a/contrib/endpoints/src/api_manager/method_impl.h +++ b/contrib/endpoints/src/api_manager/method_impl.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "contrib/endpoints/include/api_manager/method.h" #include "contrib/endpoints/src/api_manager/utils/stl_util.h" @@ -62,8 +63,8 @@ class MethodInfoImpl : public MethodInfo { const std::string &backend_address() const { return backend_address_; } - const std::map &metric_cost_map() const { - return metric_cost_map_; + const std::vector> &metric_cost_vector() const { + return metric_cost_vector_; } const std::string &rpc_method_full_name() const { @@ -95,7 +96,7 @@ class MethodInfoImpl : public MethodInfo { } void add_metric_cost(const std::string &metric, int64_t cost) { - metric_cost_map_[metric] = cost; + metric_cost_vector_.push_back(std::make_pair(metric, cost)); } // After add all system parameters, lookup some of them to cache @@ -147,13 +148,13 @@ class MethodInfoImpl : public MethodInfo { // such as API Key)? bool allow_unregistered_calls_; // Issuers to allowed audiences map. - std::map > issuer_audiences_map_; + std::map> issuer_audiences_map_; // system parameter map of parameter name to http_header name. - std::map > http_header_parameters_; + std::map> http_header_parameters_; // system parameter map of parameter name to url query parameter name. - std::map > url_query_parameters_; + std::map> url_query_parameters_; // all the names of system query parameters std::set system_query_parameter_names_; @@ -185,7 +186,7 @@ class MethodInfoImpl : public MethodInfo { bool response_streaming_; // map of metric and its cost - std::map metric_cost_map_; + std::vector> metric_cost_vector_; }; typedef std::unique_ptr MethodInfoImplPtr; From 66be3527d0d03b0169971c902e5ee349fa0be0d8 Mon Sep 17 00:00:00 2001 From: Qian Sun Date: Thu, 16 Feb 2017 16:25:40 -0800 Subject: [PATCH 3/3] Call loadQuotaConfig in config::create. --- contrib/endpoints/src/api_manager/config.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/contrib/endpoints/src/api_manager/config.cc b/contrib/endpoints/src/api_manager/config.cc index 50504083100..bcc0e3babee 100644 --- a/contrib/endpoints/src/api_manager/config.cc +++ b/contrib/endpoints/src/api_manager/config.cc @@ -121,7 +121,9 @@ bool Config::LoadQuotaRule(ApiManagerEnvInterface *env) { (*method)->add_metric_cost(metric_cost.first, metric_cost.second); } } else { - env->LogError("Metric rule with selector is mismatched."); + env->LogError("Metric rule with selector " + rule.selector() + + "is mismatched."); + return false; } } @@ -458,6 +460,9 @@ std::unique_ptr Config::Create(ApiManagerEnvInterface *env, if (!config->LoadBackends(env)) { return nullptr; } + if (!config->LoadQuotaRule(env)) { + return nullptr; + } return config; }