Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
collection-metrics.h
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 #ifndef IMPALA_UTIL_COLLECTION_METRICS_H
17 #define IMPALA_UTIL_COLLECTION_METRICS_H
18 
19 #include "util/metrics.h"
20 
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <boost/algorithm/string/join.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/accumulators/accumulators.hpp>
27 #include <boost/accumulators/statistics/count.hpp>
28 #include <boost/accumulators/statistics/mean.hpp>
29 #include <boost/accumulators/statistics/min.hpp>
30 #include <boost/accumulators/statistics/max.hpp>
31 #include <boost/accumulators/statistics/variance.hpp>
32 
33 #include "util/pretty-printer.h"
34 
35 namespace impala {
36 
40 
42 template <typename T>
43 class SetMetric : public Metric {
44  public:
45  SetMetric(const std::string& key, const std::set<T>& value,
46  const std::string& description = "") : Metric(key, description), value_(value) { }
47 
49  void Add(const T& item) {
50  boost::lock_guard<boost::mutex> l(lock_);
51  value_.insert(item);
52  }
53 
55  void Remove(const T& item) {
56  boost::lock_guard<boost::mutex> l(lock_);
57  value_.erase(item);
58  }
59 
60  void Reset() { value_.clear(); }
61 
62  virtual void ToJson(rapidjson::Document* document, rapidjson::Value* value) {
63  rapidjson::Value container(rapidjson::kObjectType);
64  AddStandardFields(document, &container);
65  rapidjson::Value metric_list(rapidjson::kArrayType);
66  BOOST_FOREACH(const T& s, value_) {
67  rapidjson::Value entry_value;
68  ToJsonValue(s, TUnit::NONE, document, &entry_value);
69  metric_list.PushBack(entry_value, document->GetAllocator());
70  }
71  container.AddMember("items", metric_list, document->GetAllocator());
72  *value = container;
73  }
74 
75  virtual void ToLegacyJson(rapidjson::Document* document) {
76  rapidjson::Value metric_list(rapidjson::kArrayType);
77  BOOST_FOREACH(const T& s, value_) {
78  rapidjson::Value entry_value;
79  ToJsonValue(s, TUnit::NONE, document, &entry_value);
80  metric_list.PushBack(entry_value, document->GetAllocator());
81  }
82  document->AddMember(key_.c_str(), metric_list, document->GetAllocator());
83  }
84 
85  virtual std::string ToHumanReadable() {
86  std::stringstream out;
87  PrettyPrinter::PrintStringList<std::set<T> >(
88  value_, TUnit::NONE, &out);
89  return out.str();
90  }
91 
92  private:
94  boost::mutex lock_;
95 
97  std::set<T> value_;
98 };
99 
103 //
106 template <typename T>
107 class StatsMetric : public Metric {
108  public:
109  StatsMetric(const std::string& key, const TUnit::type unit,
110  const std::string& description = "") : Metric(key, description), unit_(unit) { }
111 
112  void Update(const T& value) {
113  boost::lock_guard<boost::mutex> l(lock_);
114  value_ = value;
115  acc_(value);
116  }
117 
118  void Reset() {
119  boost::lock_guard<boost::mutex> l(lock_);
120  acc_ = Accumulator();
121  }
122 
123  virtual void ToJson(rapidjson::Document* document, rapidjson::Value* val) {
124  boost::lock_guard<boost::mutex> l(lock_);
125  rapidjson::Value container(rapidjson::kObjectType);
126  AddStandardFields(document, &container);
127  rapidjson::Value units(PrintTUnit(unit_).c_str(), document->GetAllocator());
128  container.AddMember("units", units, document->GetAllocator());
129 
130  container.AddMember("count", boost::accumulators::count(acc_),
131  document->GetAllocator());
132 
133  if (boost::accumulators::count(acc_) > 0) {
134  container.AddMember("last", value_, document->GetAllocator());
135  container.AddMember("min", boost::accumulators::min(acc_),
136  document->GetAllocator());
137  container.AddMember("max", boost::accumulators::max(acc_),
138  document->GetAllocator());
139  container.AddMember("mean", boost::accumulators::mean(acc_),
140  document->GetAllocator());
141  container.AddMember("stddev", sqrt(boost::accumulators::variance(acc_)),
142  document->GetAllocator());
143  }
144  *val = container;
145  }
146 
147  virtual void ToLegacyJson(rapidjson::Document* document) {
148  std::stringstream ss;
149  boost::lock_guard<boost::mutex> l(lock_);
150  rapidjson::Value container(rapidjson::kObjectType);
151  container.AddMember("count", boost::accumulators::count(acc_),
152  document->GetAllocator());
153 
154  if (boost::accumulators::count(acc_) > 0) {
155  container.AddMember("last", value_, document->GetAllocator());
156  container.AddMember("min", boost::accumulators::min(acc_),
157  document->GetAllocator());
158  container.AddMember("max", boost::accumulators::max(acc_),
159  document->GetAllocator());
160  container.AddMember("mean", boost::accumulators::mean(acc_),
161  document->GetAllocator());
162  container.AddMember("stddev", sqrt(boost::accumulators::variance(acc_)),
163  document->GetAllocator());
164  }
165  document->AddMember(key_.c_str(), container, document->GetAllocator());
166  }
167 
168  virtual std::string ToHumanReadable() {
169  std::stringstream out;
170  out << "count: " << boost::accumulators::count(acc_);
171  if (boost::accumulators::count(acc_) > 0) {
172  out << ", last: " << PrettyPrinter::Print(value_, unit_)
173  << ", min: " << PrettyPrinter::Print(boost::accumulators::min(acc_), unit_)
174  << ", max: " << PrettyPrinter::Print(boost::accumulators::max(acc_), unit_)
175  << ", mean: " << PrettyPrinter::Print(boost::accumulators::mean(acc_), unit_)
176  << ", stddev: " << PrettyPrinter::Print(
177  sqrt(boost::accumulators::variance(acc_)), unit_);
178  }
179  return out.str();
180  }
181 
182  private:
184  TUnit::type unit_;
185 
187  boost::mutex lock_;
188 
191 
193  typedef boost::accumulators::accumulator_set<T,
194  boost::accumulators::features<boost::accumulators::tag::mean,
196  boost::accumulators::tag::min,
197  boost::accumulators::tag::max,
198  boost::accumulators::tag::variance> > Accumulator;
200 
201 };
202 
203 };
204 
205 #endif
std::set< T > value_
The set of items.
SetMetric(const std::string &key, const std::set< T > &value, const std::string &description="")
TUnit::type unit_
The units of the values captured in this metric, used when pretty-printing.
const std::string key_
Unique key identifying this metric.
Definition: metrics.h:76
void Remove(const T &item)
Remove an item from this set by value.
void AddStandardFields(rapidjson::Document *document, rapidjson::Value *val)
Definition: metrics.cc:46
std::string PrintTUnit(const TUnit::type &type)
virtual void ToLegacyJson(rapidjson::Document *document)
This method is kept for backwards-compatibility with CM5.0.
T value_
The last value.
boost::mutex lock_
Lock protecting the set.
void Add(const T &item)
Put an item in this set.
boost::accumulators::accumulator_set< T, boost::accumulators::features< boost::accumulators::tag::mean, boost::accumulators::tag::count, boost::accumulators::tag::min, boost::accumulators::tag::max, boost::accumulators::tag::variance > > Accumulator
The set of accumulators that update the statistics on each Update()
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *val)
name, value, human_readable, description
virtual void ToLegacyJson(rapidjson::Document *document)
This method is kept for backwards-compatibility with CM5.0.
static std::string Print(bool value, TUnit::type ignored, bool verbose=false)
virtual std::string ToHumanReadable()
virtual std::string ToHumanReadable()
const std::string & key() const
Definition: metrics.h:71
void Update(const T &value)
Metric whose value is a set of items.
StatsMetric(const std::string &key, const TUnit::type unit, const std::string &description="")
uint64_t count
const std::string & description() const
Definition: metrics.h:72
boost::mutex lock_
Lock protecting the value and the accumulator_set.
TODO: Add ToThrift() for conversion to an RPC-friendly format.
Definition: metrics.h:50
ToJsonValue(const T &value, const TUnit::type unit, rapidjson::Document *document, rapidjson::Value *out_val)
Definition: json-util.h:33
virtual void ToJson(rapidjson::Document *document, rapidjson::Value *value)
name, value, human_readable, description