Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/istio/mixerclient/attribute_compressor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class MessageDictionary {

const std::vector<std::string>& GetWords() const { return message_words_; }

void Clear() {
message_words_.clear();
message_dict_.clear();
}

private:
const GlobalDictionary& global_dict_;

Expand Down Expand Up @@ -121,28 +126,31 @@ void CompressByDict(const Attributes& attributes, MessageDictionary& dict,
class BatchCompressorImpl : public BatchCompressor {
public:
BatchCompressorImpl(const GlobalDictionary& global_dict)
: dict_(global_dict), report_(new ::istio::mixer::v1::ReportRequest) {
report_->set_global_word_count(global_dict.size());
: dict_(global_dict) {
report_.set_global_word_count(global_dict.size());
}

void Add(const Attributes& attributes) override {
CompressedAttributes pb;
CompressByDict(attributes, dict_, &pb);
pb.GetReflection()->Swap(report_->add_attributes(), &pb);
CompressByDict(attributes, dict_, report_.add_attributes());
}

int size() const override { return report_->attributes_size(); }
int size() const override { return report_.attributes_size(); }

std::unique_ptr<::istio::mixer::v1::ReportRequest> Finish() override {
const ::istio::mixer::v1::ReportRequest& Finish() override {
for (const std::string& word : dict_.GetWords()) {
report_->add_default_words(word);
report_.add_default_words(word);
}
return std::move(report_);
return report_;
}

void Clear() override {
dict_.Clear();
report_.Clear();
}

private:
MessageDictionary dict_;
std::unique_ptr<::istio::mixer::v1::ReportRequest> report_;
::istio::mixer::v1::ReportRequest report_;
};

} // namespace
Expand Down
5 changes: 4 additions & 1 deletion src/istio/mixerclient/attribute_compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class BatchCompressor {
virtual int size() const = 0;

// Finish the batch and create the batched report request.
virtual std::unique_ptr<::istio::mixer::v1::ReportRequest> Finish() = 0;
virtual const ::istio::mixer::v1::ReportRequest& Finish() = 0;

// Reset the object data.
virtual void Clear() = 0;
};

// Compress attributes.
Expand Down
6 changes: 3 additions & 3 deletions src/istio/mixerclient/attribute_compressor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,14 @@ TEST_F(AttributeCompressorTest, BatchCompressTest) {
auto report_pb = batch_compressor->Finish();

std::string out_str;
TextFormat::PrintToString(*report_pb, &out_str);
TextFormat::PrintToString(report_pb, &out_str);
GOOGLE_LOG(INFO) << "===" << out_str << "===";

::istio::mixer::v1::ReportRequest expected_report_pb;
ASSERT_TRUE(
TextFormat::ParseFromString(kReportAttributes, &expected_report_pb));
report_pb->set_global_word_count(221);
EXPECT_TRUE(MessageDifferencer::Equals(*report_pb, expected_report_pb));
report_pb.set_global_word_count(221);
EXPECT_TRUE(MessageDifferencer::Equals(report_pb, expected_report_pb));
}

} // namespace
Expand Down
16 changes: 7 additions & 9 deletions src/istio/mixerclient/report_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ReportBatch::ReportBatch(const ReportOptions& options,
transport_(transport),
timer_create_(timer_create),
compressor_(compressor),
batch_compressor_(compressor.CreateBatchCompressor()),
total_report_calls_(0),
total_remote_report_calls_(0) {}

Expand All @@ -41,10 +42,6 @@ ReportBatch::~ReportBatch() { Flush(); }
void ReportBatch::Report(const Attributes& request) {
std::lock_guard<std::mutex> lock(mutex_);
++total_report_calls_;
if (!batch_compressor_) {
batch_compressor_ = compressor_.CreateBatchCompressor();
}

batch_compressor_->Add(request);
if (batch_compressor_->size() >= options_.max_batch_entries) {
FlushWithLock();
Expand All @@ -59,19 +56,18 @@ void ReportBatch::Report(const Attributes& request) {
}

void ReportBatch::FlushWithLock() {
if (!batch_compressor_) {
if (batch_compressor_->size() == 0) {
return;
}

++total_remote_report_calls_;
std::unique_ptr<ReportRequest> request = batch_compressor_->Finish();
batch_compressor_.reset();
if (timer_) {
timer_->Stop();
}

++total_remote_report_calls_;
auto request = batch_compressor_->Finish();
ReportResponse* response = new ReportResponse;
transport_(*request, response, [this, response](const Status& status) {
transport_(request, response, [this, response](const Status& status) {
delete response;
if (!status.ok()) {
GOOGLE_LOG(ERROR) << "Mixer Report failed with: " << status.ToString();
Expand All @@ -80,6 +76,8 @@ void ReportBatch::FlushWithLock() {
}
}
});

batch_compressor_->Clear();
}

void ReportBatch::Flush() {
Expand Down
2 changes: 1 addition & 1 deletion src/istio/mixerclient/report_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ReportBatchTest : public ::testing::Test {
};
}

MockReportTransport mock_report_transport_;
::testing::NiceMock<MockReportTransport> mock_report_transport_;
MockTimer* mock_timer_;
AttributeCompressor compressor_;
std::unique_ptr<ReportBatch> batch_;
Expand Down