diff --git a/src/istio/mixerclient/attribute_compressor.cc b/src/istio/mixerclient/attribute_compressor.cc index 701c1672098..c267504c501 100644 --- a/src/istio/mixerclient/attribute_compressor.cc +++ b/src/istio/mixerclient/attribute_compressor.cc @@ -58,6 +58,11 @@ class MessageDictionary { const std::vector& GetWords() const { return message_words_; } + void Clear() { + message_words_.clear(); + message_dict_.clear(); + } + private: const GlobalDictionary& global_dict_; @@ -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 diff --git a/src/istio/mixerclient/attribute_compressor.h b/src/istio/mixerclient/attribute_compressor.h index 8ba2e6b3107..f6c9c0203ec 100644 --- a/src/istio/mixerclient/attribute_compressor.h +++ b/src/istio/mixerclient/attribute_compressor.h @@ -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. diff --git a/src/istio/mixerclient/attribute_compressor_test.cc b/src/istio/mixerclient/attribute_compressor_test.cc index 721d2bb07bd..614381bb16c 100644 --- a/src/istio/mixerclient/attribute_compressor_test.cc +++ b/src/istio/mixerclient/attribute_compressor_test.cc @@ -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 diff --git a/src/istio/mixerclient/report_batch.cc b/src/istio/mixerclient/report_batch.cc index c41da3b17a8..5bb07ef436f 100644 --- a/src/istio/mixerclient/report_batch.cc +++ b/src/istio/mixerclient/report_batch.cc @@ -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) {} @@ -41,10 +42,6 @@ ReportBatch::~ReportBatch() { Flush(); } void ReportBatch::Report(const Attributes& request) { std::lock_guard 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(); @@ -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 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(); @@ -80,6 +76,8 @@ void ReportBatch::FlushWithLock() { } } }); + + batch_compressor_->Clear(); } void ReportBatch::Flush() { diff --git a/src/istio/mixerclient/report_batch_test.cc b/src/istio/mixerclient/report_batch_test.cc index 9cfcfb72242..06cc7156ded 100644 --- a/src/istio/mixerclient/report_batch_test.cc +++ b/src/istio/mixerclient/report_batch_test.cc @@ -65,7 +65,7 @@ class ReportBatchTest : public ::testing::Test { }; } - MockReportTransport mock_report_transport_; + ::testing::NiceMock mock_report_transport_; MockTimer* mock_timer_; AttributeCompressor compressor_; std::unique_ptr batch_;