diff --git a/WORKSPACE b/WORKSPACE index bbafd205918..95dcdde5cdc 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -31,16 +31,34 @@ googletest_repositories() load( "//contrib/endpoints:repositories.bzl", "grpc_repositories", - "mixerapi_repositories", + "mixer_client_repositories", "servicecontrol_client_repositories", ) grpc_repositories() -mixerapi_repositories() +mixer_client_repositories() servicecontrol_client_repositories() +# Workaround for Bazel > 0.4.0 since it needs newer protobuf.bzl from: +# https://github.com/google/protobuf/pull/2246 +# Do not use this git_repository for anything else than protobuf.bzl +new_git_repository( + name = "protobuf_bzl", + # Injecting an empty BUILD file to prevent using any build target + build_file_content = "", + commit = "05090726144b6e632c50f47720ff51049bfcbef6", + remote = "https://github.com/google/protobuf.git", +) + +load( + "@mixerclient_git//:repositories.bzl", + "mixerapi_repositories", +) + +mixerapi_repositories(protobuf_repo="@protobuf_bzl//") + load( "//src/envoy:repositories.bzl", "envoy_repositories", diff --git a/contrib/endpoints/repositories.bzl b/contrib/endpoints/repositories.bzl index f3df61722a2..7b77aa91c5a 100644 --- a/contrib/endpoints/repositories.bzl +++ b/contrib/endpoints/repositories.bzl @@ -334,51 +334,15 @@ def servicecontrol_client_repositories(bind=True): actual = "@servicecontrol_client_git//:service_control_client_lib", ) -def mixerapi_repositories(protobuf_repo="@protobuf_git//", bind=True): - BUILD = """ -# Copyright 2016 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -################################################################################ -# -licenses(["notice"]) - -load("{}:protobuf.bzl", "cc_proto_library") - -cc_proto_library( - name = "mixer_api_cc_proto", - srcs = glob( - ["mixer/api/v1/*.proto"], - ), - default_runtime = "//external:protobuf", - protoc = "//external:protoc", - visibility = ["//visibility:public"], - deps = [ - "//external:cc_wkt_protos", - "//external:servicecontrol", - ], -) -""".format(protobuf_repo) - - native.new_git_repository( - name = "mixerapi_git", - commit = "fc5a396185edc72d06d1937f30a8148a37d4fc1b", - remote = "https://github.com/istio/api.git", - build_file_content = BUILD, +def mixer_client_repositories(bind=True): + native.git_repository( + name = "mixerclient_git", + commit = "1569430f1e27b31e23c029c6bec0d8d5062d9e55", + remote = "https://github.com/istio/mixerclient.git", ) + if bind: native.bind( - name = "mixer_api_cc_proto", - actual = "@mixerapi_git//:mixer_api_cc_proto", + name = "mixer_client_lib", + actual = "@mixerclient_git//:mixer_client_lib", ) diff --git a/contrib/endpoints/src/api_manager/mixer/BUILD b/contrib/endpoints/src/api_manager/mixer/BUILD index 8082c61e201..affe09c7ddd 100644 --- a/contrib/endpoints/src/api_manager/mixer/BUILD +++ b/contrib/endpoints/src/api_manager/mixer/BUILD @@ -32,11 +32,11 @@ cc_library( ], }), deps = [ - "//external:grpc++", - "//external:mixer_api_cc_proto", - "//external:protobuf", "//contrib/endpoints/src/api_manager:impl_headers", "//contrib/endpoints/src/api_manager/service_control", "//contrib/endpoints/src/api_manager/utils", + "//external:grpc++", + "//external:mixer_client_lib", + "//external:protobuf", ], ) diff --git a/contrib/endpoints/src/api_manager/mixer/mixer.cc b/contrib/endpoints/src/api_manager/mixer/mixer.cc index 0c0bb5d793e..47f6c4a8a2f 100644 --- a/contrib/endpoints/src/api_manager/mixer/mixer.cc +++ b/contrib/endpoints/src/api_manager/mixer/mixer.cc @@ -14,169 +14,103 @@ */ #include "contrib/endpoints/src/api_manager/mixer/mixer.h" -#include -#include "mixer/api/v1/service.pb.h" - using ::google::api_manager::utils::Status; -using ::google::protobuf::util::error::Code; -using ::google::protobuf::Map; +using ::istio::mixer_client::Attributes; namespace google { namespace api_manager { namespace mixer { namespace { -const char kMixerServiceName[] = "istio.mixer.v1.Mixer"; - -enum AttributeIndex { - ATTR_SERVICE_NAME = 0, - ATTR_PEER_ID, - ATTR_OPERATION_NAME, - ATTR_API_KEY, - ATTR_RESPONSE_CODE, - ATTR_URL, - ATTR_LOCATION, - ATTR_API_NAME, - ATTR_API_VERSION, - ATTR_API_METHOD, - ATTR_REQUEST_SIZE, - ATTR_RESPONSE_SIZE, - ATTR_LOG_MESSAGE, -}; - -struct AttributeDict { - int index; - std::string name; -} kAttributeNames[] = { - { - ATTR_SERVICE_NAME, "serviceName", - }, - { - ATTR_PEER_ID, "peerId", - }, - { - ATTR_OPERATION_NAME, "operationName", - }, - { - ATTR_API_KEY, "apiKey", - }, - { - ATTR_RESPONSE_CODE, "responseCode", - }, - { - ATTR_URL, "URL", - }, - { - ATTR_LOCATION, "location", - }, - { - ATTR_API_NAME, "apiName", - }, - { - ATTR_API_VERSION, "apiVersion", - }, - { - ATTR_API_METHOD, "apiMethod", - }, - { - ATTR_REQUEST_SIZE, "requestSize", - }, - { - ATTR_RESPONSE_SIZE, "responesSize", - }, - { - ATTR_LOG_MESSAGE, "logMessage", - }, -}; - -void SetAttributeDict(Map* dict) { - for (auto attr : kAttributeNames) { - (*dict)[attr.index] = attr.name; - } +const std::string kAttrNameServiceName = "serviceName"; +const std::string kAttrNamePeerId = "peerId"; +const std::string kAttrNameOperationName = "operationName"; +const std::string kAttrNameApiKey = "apiKey"; +const std::string kAttrNameResponseCode = "responseCode"; +const std::string kAttrNameURL = "url"; +const std::string kAttrNameLocation = "location"; +const std::string kAttrNameApiName = "apiName"; +const std::string kAttrNameApiVersion = "apiVersion"; +const std::string kAttrNameApiMethod = "apiMethod"; +const std::string kAttrNameRequestSize = "requestSize"; +const std::string kAttrNameResponseSize = "responseSize"; +const std::string kAttrNameLogMessage = "logMessage"; + +Attributes::Value StringValue(const std::string& str) { + Attributes::Value v; + v.type = Attributes::Value::STRING; + v.str_v = str; + return v; } -void CovertToPb(const service_control::CheckRequestInfo& info, - const std::string& service_name, - ::istio::mixer::v1::Attributes* attr) { - SetAttributeDict(attr->mutable_dictionary()); - - auto* str_attrs = attr->mutable_string_attributes(); - (*str_attrs)[ATTR_SERVICE_NAME] = service_name; - (*str_attrs)[ATTR_PEER_ID] = "Proxy"; - (*str_attrs)[ATTR_OPERATION_NAME] = info.operation_name; - (*str_attrs)[ATTR_API_KEY] = info.api_key; +Attributes::Value Int64Value(int64_t value) { + Attributes::Value v; + v.type = Attributes::Value::INT64; + v.value.int64_v = value; + return v; } -void CovertToPb(const service_control::ReportRequestInfo& info, - const std::string& service_name, - ::istio::mixer::v1::Attributes* attr) { - SetAttributeDict(attr->mutable_dictionary()); +void FillCheckAttributes(const service_control::CheckRequestInfo& info, + const std::string& service_name, + ::istio::mixer_client::Attributes* attr) { + attr->attributes[kAttrNameServiceName] = StringValue(service_name); + attr->attributes[kAttrNamePeerId] = StringValue("Proxy"); + attr->attributes[kAttrNameOperationName] = StringValue(info.operation_name); + attr->attributes[kAttrNameApiKey] = StringValue(info.api_key); +} - auto* str_attrs = attr->mutable_string_attributes(); - (*str_attrs)[ATTR_SERVICE_NAME] = service_name; - (*str_attrs)[ATTR_PEER_ID] = "Proxy"; - (*str_attrs)[ATTR_OPERATION_NAME] = info.operation_name; - (*str_attrs)[ATTR_API_KEY] = info.api_key; +void FillReportAttributes(const service_control::ReportRequestInfo& info, + const std::string& service_name, + ::istio::mixer_client::Attributes* attr) { + attr->attributes[kAttrNameServiceName] = StringValue(service_name); + attr->attributes[kAttrNamePeerId] = StringValue("Proxy"); + attr->attributes[kAttrNameOperationName] = StringValue(info.operation_name); + attr->attributes[kAttrNameApiKey] = StringValue(info.api_key); - (*str_attrs)[ATTR_URL] = info.url; - (*str_attrs)[ATTR_LOCATION] = info.location; + attr->attributes[kAttrNameURL] = StringValue(info.url); + attr->attributes[kAttrNameLocation] = StringValue(info.location); - (*str_attrs)[ATTR_API_NAME] = info.api_name; - (*str_attrs)[ATTR_API_VERSION] = info.api_version; - (*str_attrs)[ATTR_API_METHOD] = info.api_method; + attr->attributes[kAttrNameApiName] = StringValue(info.api_name); + attr->attributes[kAttrNameApiVersion] = StringValue(info.api_version); + attr->attributes[kAttrNameApiMethod] = StringValue(info.api_method); - (*str_attrs)[ATTR_LOG_MESSAGE] = info.log_message; + attr->attributes[kAttrNameLogMessage] = StringValue(info.log_message); - auto* int_attrs = attr->mutable_int64_attributes(); - (*int_attrs)[ATTR_RESPONSE_CODE] = info.response_code; - (*int_attrs)[ATTR_REQUEST_SIZE] = info.request_size; - (*int_attrs)[ATTR_RESPONSE_SIZE] = info.response_size; + attr->attributes[kAttrNameResponseCode] = Int64Value(info.response_code); + attr->attributes[kAttrNameRequestSize] = Int64Value(info.request_size); + attr->attributes[kAttrNameResponseSize] = Int64Value(info.response_size); } } // namespace Mixer::Mixer(ApiManagerEnvInterface* env, const Config* config) - : env_(env), request_index_(0), config_(config) {} + : env_(env), config_(config) {} Mixer::~Mixer() {} -Status Mixer::Init() { return Status::OK; } +Status Mixer::Init() { + ::istio::mixer_client::MixerClientOptions options; + options.mixer_server = + config_->server_config()->mixer_options().mixer_server(); + mixer_client_ = ::istio::mixer_client::CreateMixerClient(options); + return Status::OK; +} Status Mixer::Close() { return Status::OK; } Status Mixer::Report(const service_control::ReportRequestInfo& info) { - std::unique_ptr grpc_request(new GRPCRequest([this]( - Status status, std::string&& body) { - if (status.ok()) { - // Handle 200 response - ::istio::mixer::v1::ReportResponse response; - if (!response.ParseFromString(body)) { - status = - Status(Code::INVALID_ARGUMENT, std::string("Invalid response")); - env_->LogError(std::string("Failed parse report response: ") + body); - } - env_->LogInfo(std::string("Report response: ") + response.DebugString()); - } else { - env_->LogError(std::string("Failed to call Mixer::report, Error: ") + - status.ToString()); - } - })); - - ::istio::mixer::v1::ReportRequest request; - request.set_request_index(++request_index_); - CovertToPb(info, config_->service_name(), request.mutable_attribute_update()); - env_->LogInfo(std::string("Send Report: ") + request.DebugString()); - - std::string request_body; - request.SerializeToString(&request_body); - - grpc_request - ->set_server(config_->server_config()->mixer_options().mixer_server()) - .set_service(kMixerServiceName) - .set_method("Report") - .set_body(request_body); - - env_->RunGRPCRequest(std::move(grpc_request)); + ::istio::mixer_client::Attributes attributes; + FillReportAttributes(info, config_->service_name(), &attributes); + env_->LogInfo("Send Report: "); + mixer_client_->Report( + attributes, [this](const ::google::protobuf::util::Status& status) { + if (status.ok()) { + env_->LogInfo("Report response: OK"); + } else { + env_->LogError(std::string("Failed to call Mixer::report, Error: ") + + status.ToString()); + } + }); return Status::OK; } @@ -185,40 +119,23 @@ void Mixer::Check( cloud_trace::CloudTraceSpan* parent_span, std::function on_done) { - std::unique_ptr grpc_request(new GRPCRequest([this, on_done]( - Status status, std::string&& body) { - if (status.ok()) { - // Handle 200 response - ::istio::mixer::v1::CheckResponse response; - if (!response.ParseFromString(body)) { - status = - Status(Code::INVALID_ARGUMENT, std::string("Invalid response")); - env_->LogError(std::string("Failed parse check response: ") + body); - } - env_->LogInfo(std::string("Check response: ") + response.DebugString()); - } else { - env_->LogError(std::string("Failed to call Mixer::check, Error: ") + - status.ToString()); - } - service_control::CheckResponseInfo info; - on_done(status, info); - })); - - ::istio::mixer::v1::CheckRequest request; - request.set_request_index(++request_index_); - CovertToPb(info, config_->service_name(), request.mutable_attribute_update()); - env_->LogInfo(std::string("Send Check: ") + request.DebugString()); - - std::string request_body; - request.SerializeToString(&request_body); - - grpc_request - ->set_server(config_->server_config()->mixer_options().mixer_server()) - .set_service(kMixerServiceName) - .set_method("Check") - .set_body(request_body); - - env_->RunGRPCRequest(std::move(grpc_request)); + ::istio::mixer_client::Attributes attributes; + FillCheckAttributes(info, config_->service_name(), &attributes); + env_->LogInfo("Send Check: "); + mixer_client_->Check( + attributes, + [this, on_done](const ::google::protobuf::util::Status& status) { + if (status.ok()) { + env_->LogInfo("Check response: OK"); + } else { + env_->LogError(std::string("Failed to call Mixer::check, Error: ") + + status.ToString()); + } + service_control::CheckResponseInfo info; + on_done(Status(status.error_code(), status.error_message(), + Status::SERVICE_CONTROL), + info); + }); } Status Mixer::GetStatistics(service_control::Statistics* esp_stat) const { diff --git a/contrib/endpoints/src/api_manager/mixer/mixer.h b/contrib/endpoints/src/api_manager/mixer/mixer.h index 0318a5f06f1..4cf2d1f54a7 100644 --- a/contrib/endpoints/src/api_manager/mixer/mixer.h +++ b/contrib/endpoints/src/api_manager/mixer/mixer.h @@ -18,6 +18,7 @@ #include "contrib/endpoints/include/api_manager/env_interface.h" #include "contrib/endpoints/src/api_manager/config.h" #include "contrib/endpoints/src/api_manager/service_control/interface.h" +#include "include/client.h" namespace google { namespace api_manager { @@ -51,9 +52,10 @@ class Mixer : public service_control::Interface { // The Api Manager environment interface. ApiManagerEnvInterface* env_; - int64_t request_index_; - + // The config. const Config* config_; + // The mixer client + std::unique_ptr<::istio::mixer_client::MixerClient> mixer_client_; }; } // namespace mixer diff --git a/src/envoy/prototype/api_manager_filter.cc b/src/envoy/prototype/api_manager_filter.cc index 2fca48de23c..4ccd9e6d120 100644 --- a/src/envoy/prototype/api_manager_filter.cc +++ b/src/envoy/prototype/api_manager_filter.cc @@ -214,13 +214,16 @@ class Instance : public Http::StreamFilter, status.ToJson()); if (!status.ok() && state_ != Responded) { state_ = Responded; - Utility::sendLocalReply(*decoder_callbacks_, Code(status.HttpCode()), - status.ToJson()); + decoder_callbacks_->dispatcher().post([this, status]() { + Utility::sendLocalReply(*decoder_callbacks_, Code(status.HttpCode()), + status.ToJson()); + }); return; } state_ = Complete; if (!initiating_call_) { - decoder_callbacks_->continueDecoding(); + decoder_callbacks_->dispatcher().post( + [this]() { decoder_callbacks_->continueDecoding(); }); } } diff --git a/src/envoy/prototype/envoy-esp.conf b/src/envoy/prototype/envoy-esp.conf index 11690bed565..d3ec54f405d 100644 --- a/src/envoy/prototype/envoy-esp.conf +++ b/src/envoy/prototype/envoy-esp.conf @@ -73,18 +73,6 @@ } ] }, - { - "name": "mixer_server", - "connect_timeout_ms": 5000, - "type": "strict_dns", - "lb_type": "round_robin", - "features": "http2", - "hosts": [ - { - "url": "tcp://localhost:9091" - } - ] - }, { "name": "api_manager", "connect_timeout_ms": 5000, diff --git a/src/envoy/prototype/server_config.pb.txt b/src/envoy/prototype/server_config.pb.txt index 9703886cc9e..19cb6cbb2c0 100644 --- a/src/envoy/prototype/server_config.pb.txt +++ b/src/envoy/prototype/server_config.pb.txt @@ -2,5 +2,5 @@ cloud_tracing_config { force_disable: true } mixer_options { - mixer_server: "mixer_server" -} \ No newline at end of file + mixer_server: "localhost:9091" +}