From 7e07b5ad8915887fbabb5f85831559b99e8f726c Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Thu, 17 Aug 2017 08:51:30 -0700 Subject: [PATCH] Converts mixer server INTERNAL code properly. (#96) --- mixerclient/src/check_cache.cc | 13 +++++++++++-- mixerclient/src/check_cache.h | 10 +++++++++- mixerclient/utils/protobuf.cc | 4 ---- mixerclient/utils/protobuf.h | 4 ---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/mixerclient/src/check_cache.cc b/mixerclient/src/check_cache.cc index d25c65307938..740f75e29fbf 100644 --- a/mixerclient/src/check_cache.cc +++ b/mixerclient/src/check_cache.cc @@ -28,7 +28,7 @@ namespace mixer_client { void CheckCache::CacheElem::CacheElem::SetResponse( const CheckResponse& response, Tick time_now) { if (response.has_precondition()) { - status_ = ConvertRpcStatus(response.precondition().status()); + status_ = parent_.ConvertRpcStatus(response.precondition().status()); if (response.precondition().has_valid_duration()) { expire_time_ = @@ -144,7 +144,7 @@ Status CheckCache::CacheResponse(const std::string& signature, return lookup.value()->status(); } - CacheElem* cache_elem = new CacheElem(response, time_now); + CacheElem* cache_elem = new CacheElem(*this, response, time_now); cache_->Insert(signature, cache_elem, 1); return cache_elem->status(); } @@ -160,5 +160,14 @@ Status CheckCache::FlushAll() { return Status::OK; } +Status CheckCache::ConvertRpcStatus(const ::google::rpc::Status& status) const { + // If server status code is INTERNAL, check network_fail_open flag. + if (status.code() == Code::INTERNAL && options_.network_fail_open) { + return Status::OK; + } else { + return Status(static_cast(status.code()), status.message()); + } +} + } // namespace mixer_client } // namespace istio diff --git a/mixerclient/src/check_cache.h b/mixerclient/src/check_cache.h index a2b8f470f39a..cfeb8e91d6f1 100644 --- a/mixerclient/src/check_cache.h +++ b/mixerclient/src/check_cache.h @@ -96,9 +96,15 @@ class CheckCache { // Usually called at destructor. ::google::protobuf::util::Status FlushAll(); + // Convert from grpc status to protobuf status. + ::google::protobuf::util::Status ConvertRpcStatus( + const ::google::rpc::Status& status) const; + class CacheElem { public: - CacheElem(const ::istio::mixer::v1::CheckResponse& response, Tick time) { + CacheElem(const CheckCache& parent, + const ::istio::mixer::v1::CheckResponse& response, Tick time) + : parent_(parent) { SetResponse(response, time); } @@ -113,6 +119,8 @@ class CheckCache { ::google::protobuf::util::Status status() const { return status_; } private: + // To the parent cache object. + const CheckCache& parent_; // The check status for the last check request. ::google::protobuf::util::Status status_; // Cache item should not be used after it is expired. diff --git a/mixerclient/utils/protobuf.cc b/mixerclient/utils/protobuf.cc index fcf414425c17..b8a675b09706 100644 --- a/mixerclient/utils/protobuf.cc +++ b/mixerclient/utils/protobuf.cc @@ -55,10 +55,6 @@ milliseconds ToMilliseonds(const Duration& duration) { duration_cast(nanoseconds(duration.nanos())); } -Status ConvertRpcStatus(const ::google::rpc::Status& status) { - return Status(static_cast(status.code()), status.message()); -} - bool InvalidDictionaryStatus(const Status& status) { return status.error_code() == Code::INVALID_ARGUMENT && status.error_message().starts_with(kInvalidDictionaryErrorPrefix); diff --git a/mixerclient/utils/protobuf.h b/mixerclient/utils/protobuf.h index f84fd90f87c1..1a7bfafc2e38 100644 --- a/mixerclient/utils/protobuf.h +++ b/mixerclient/utils/protobuf.h @@ -37,10 +37,6 @@ ::google::protobuf::Duration CreateDuration(std::chrono::nanoseconds value); std::chrono::milliseconds ToMilliseonds( const ::google::protobuf::Duration& duration); -// Convert from grpc status to protobuf status. -::google::protobuf::util::Status ConvertRpcStatus( - const ::google::rpc::Status& status); - bool InvalidDictionaryStatus(const ::google::protobuf::util::Status& status); } // namespace mixer_client