Skip to content

Commit

Permalink
Move protobuf conversion code to utils. (#58)
Browse files Browse the repository at this point in the history
* Move protobuf conversation code to utils.

* fix format
  • Loading branch information
qiwzhang authored Apr 21, 2017
1 parent 7b78421 commit 01e4071
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 82 deletions.
2 changes: 2 additions & 0 deletions mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ cc_library(
"src/transport.h",
"utils/md5.cc",
"utils/md5.h",
"utils/protobuf.cc",
"utils/protobuf.h",
"utils/status_test_util.h",
],
hdrs = [
Expand Down
5 changes: 0 additions & 5 deletions mixerclient/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,14 @@ struct MixerClientOptions {

// Constructor with specified option values.
MixerClientOptions(const CheckOptions& check_options,
const ReportOptions& report_options,
const QuotaOptions& quota_options)
: check_options(check_options),
report_options(report_options),
quota_options(quota_options),
transport(nullptr) {}

// Check options.
CheckOptions check_options;

// Report options.
ReportOptions report_options;

// Quota options.
QuotaOptions quota_options;

Expand Down
49 changes: 5 additions & 44 deletions mixerclient/include/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,62 +63,23 @@ struct CheckOptions {
std::vector<std::string> cache_keys;
};

// Options controlling report behavior.
struct ReportOptions {
// Default constructor.
ReportOptions() : num_entries(10000), flush_interval_ms(1000) {}

// Constructor.
// cache_entries is the maximum number of cache entries that can be kept in
// the cache. Cache is disabled when cache_entries <= 0.
// flush_cache_entry_interval_ms is the maximum milliseconds before
// report requests are flushed to the server. The cache entry is deleted after
// the flush.
ReportOptions(int cache_entries, int flush_cache_entry_interval_ms)
: num_entries(cache_entries),
flush_interval_ms(flush_cache_entry_interval_ms) {}

// Maximum number of cache entries kept in the cache.
// Set to 0 will disable caching.
const int num_entries;

// Maximum milliseconds before report requests are flushed to the
// server. The flush is triggered by a timer.
const int flush_interval_ms;
};

// Options controlling quota behavior.
struct QuotaOptions {
// Default constructor.
QuotaOptions()
: num_entries(10000), flush_interval_ms(500), expiration_ms(1000) {}
QuotaOptions() : num_entries(10000), expiration_ms(600000) {}

// Constructor.
// cache_entries is the maximum number of cache entries that can be kept in
// the cache. Cache is disabled when cache_entries <= 0.
// flush_cache_entry_interval_ms is the maximum milliseconds before an
// quota request needs to send to remote server again.
// response_expiration_ms is the maximum milliseconds before a cached quota
// response is invalidated. We make sure that it is at least
// flush_cache_entry_interval_ms + 1.
QuotaOptions(int cache_entries, int flush_cache_entry_interval_ms,
int response_expiration_ms)
: num_entries(cache_entries),
flush_interval_ms(flush_cache_entry_interval_ms),
expiration_ms(std::max(flush_cache_entry_interval_ms + 1,
response_expiration_ms)) {}
// expiration_ms is the maximum milliseconds an idle cached quota is removed.
QuotaOptions(int cache_entries, int expiration_ms)
: num_entries(cache_entries), expiration_ms(expiration_ms) {}

// Maximum number of cache entries kept in the cache.
// Set to 0 will disable caching.
const int num_entries;

// Maximum milliseconds before quota requests are flushed to the
// server. The flush is triggered by a quota request.
const int flush_interval_ms;

// Maximum milliseconds before a cached quota response should be deleted. The
// deletion is triggered by a timer. This value must be larger than
// flush_interval_ms.
// Maximum milliseconds before an idle cached quota should be deleted.
const int expiration_ms;
};

Expand Down
7 changes: 7 additions & 0 deletions mixerclient/prefetch/quota_prefetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ void QuotaPrefetchImpl::OnResponse(SlotId slot_id, int req_amount,
<< ", expire: " << expiration.count() << ", id: " << slot_id
<< std::endl;

// resp_amount of -1 indicates any network failures.
// Use fail open policy to handle any netowrk failures.
if (resp_amount == -1) {
resp_amount = req_amount;
expiration = milliseconds(kMaxExpirationInMs);
}

Slot* slot = nullptr;
if (slot_id != 0) {
// The prefetched amount was added to the available queue
Expand Down
3 changes: 1 addition & 2 deletions mixerclient/prefetch/quota_prefetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ class QuotaPrefetch {

// Define the transport.
// The callback function after quota allocation is done from the server.
// Set amount = -1 If there are any network failures.
typedef std::function<void(int amount, std::chrono::milliseconds expiration,
Tick t)>
DoneFunc;
// The transport function to send quota allocation to server.
// Rate limiting usually follows fail-open policy. If there are any network
// failures, the caller should grant the full request amount.
typedef std::function<void(int amount, DoneFunc fn, Tick t)> TransportFunc;

// virtual destructor.
Expand Down
23 changes: 1 addition & 22 deletions mixerclient/src/attribute_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,14 @@
* limitations under the License.
*/
#include "src/attribute_context.h"
#include "google/protobuf/timestamp.pb.h"
#include "utils/protobuf.h"

using ::google::protobuf::Duration;
using ::google::protobuf::Map;
using ::google::protobuf::Timestamp;

namespace istio {
namespace mixer_client {
namespace {

// Convert timestamp from time_point to Timestamp
Timestamp CreateTimestamp(std::chrono::system_clock::time_point tp) {
Timestamp time_stamp;
long long nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(
tp.time_since_epoch())
.count();

time_stamp.set_seconds(nanos / 1000000000);
time_stamp.set_nanos(nanos % 1000000000);
return time_stamp;
}

Duration CreateDuration(std::chrono::nanoseconds value) {
Duration duration;
duration.set_seconds(value.count() / 1000000000);
duration.set_nanos(value.count() % 1000000000);
return duration;
}

// Compare two string maps to check
// 1) any removed keys: ones in the old, but not in the new.
// 2) Get all key/value pairs which are the same in the two maps.
Expand Down
7 changes: 1 addition & 6 deletions mixerclient/src/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "src/client_impl.h"
#include <sstream>
#include "utils/protobuf.h"

using ::istio::mixer::v1::CheckResponse;
using ::istio::mixer::v1::ReportResponse;
Expand All @@ -23,11 +23,6 @@ using ::google::protobuf::util::error::Code;

namespace istio {
namespace mixer_client {
namespace {
Status ConvertRpcStatus(const ::google::rpc::Status &status) {
return Status(static_cast<Code>(status.code()), status.message());
}
} // namespace

MixerClientImpl::MixerClientImpl(const MixerClientOptions &options)
: options_(options) {
Expand Down
4 changes: 1 addition & 3 deletions mixerclient/src/client_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ class MixerClientImplTest : public ::testing::Test, public TransportInterface {
MixerClientOptions options(
CheckOptions(1 /*entries */, 500 /* refresh_interval_ms */,
1000 /* expiration_ms */),
ReportOptions(1 /* entries */, 500 /*flush_interval_ms*/),
QuotaOptions(1 /* entries */, 500 /*flush_interval_ms*/,
1000 /* expiration_ms */));
QuotaOptions(1 /* entries */, 600000 /* expiration_ms */));
options.transport = this;
client_ = CreateMixerClient(options);
}
Expand Down
56 changes: 56 additions & 0 deletions mixerclient/utils/protobuf.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Copyright 2017 Istio Authors. 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.
*/

#include "protobuf.h"

using namespace std::chrono;

using ::google::protobuf::Duration;
using ::google::protobuf::Timestamp;

using ::google::protobuf::util::Status;
using ::google::protobuf::util::error::Code;

namespace istio {
namespace mixer_client {

// Convert timestamp from time_point to Timestamp
Timestamp CreateTimestamp(system_clock::time_point tp) {
Timestamp time_stamp;
long long nanos = duration_cast<nanoseconds>(tp.time_since_epoch()).count();

time_stamp.set_seconds(nanos / 1000000000);
time_stamp.set_nanos(nanos % 1000000000);
return time_stamp;
}

Duration CreateDuration(nanoseconds value) {
Duration duration;
duration.set_seconds(value.count() / 1000000000);
duration.set_nanos(value.count() % 1000000000);
return duration;
}

milliseconds ToMilliseonds(const Duration& duration) {
return duration_cast<milliseconds>(seconds(duration.seconds())) +
duration_cast<milliseconds>(nanoseconds(duration.nanos()));
}

Status ConvertRpcStatus(const ::google::rpc::Status& status) {
return Status(static_cast<Code>(status.code()), status.message());
}

} // namespace mixer_client
} // namespace istio
44 changes: 44 additions & 0 deletions mixerclient/utils/protobuf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright 2017 Istio Authors. 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.
*/

#ifndef MIXER_CLIENT_UTILS_PROTOBUF_H_
#define MIXER_CLIENT_UTILS_PROTOBUF_H_

#include "google/protobuf/stubs/status.h"
#include "google/protobuf/timestamp.pb.h"
#include "mixer/v1/service.grpc.pb.h"

namespace istio {
namespace mixer_client {

// Convert system_clock time to protobuf timestamp
::google::protobuf::Timestamp CreateTimestamp(
std::chrono::system_clock::time_point tp);

// Convert from chrono duration to protobuf duration.
::google::protobuf::Duration CreateDuration(std::chrono::nanoseconds value);

// Convert from prtoobuf duration to chrono duration.
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);

} // namespace mixer_client
} // namespace istio

#endif // MIXER_CLIENT_CLIENT_UTILS_PROTOBUF_H_

0 comments on commit 01e4071

Please sign in to comment.