OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
gendb_statistics.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3 //
4 
5 #include "gendb_statistics.h"
6 
7 namespace GenDb {
8 
9 // DbTableStatistics::TableStats
10 void DbTableStatistics::TableStats::Update(bool write, bool fail,
11  bool back_pressure, uint64_t num) {
12  if (write) {
13  if (fail) {
15  } else if (back_pressure) {
17  } else {
18  num_writes_ += num;
19  }
20  } else {
21  if (fail) {
23  } else {
24  num_reads_ += num;
25  }
26  }
27 }
28 
29 void DbTableStatistics::TableStats::Get(const std::string &table_name,
30  DbTableInfo *info) const {
31  info->set_table_name(table_name);
32  info->set_reads(num_reads_);
33  info->set_read_fails(num_read_fails_);
34  info->set_writes(num_writes_);
35  info->set_write_fails(num_write_fails_);
36  info->set_write_back_pressure_fails(num_write_back_pressure_fails_);
37 }
38 
39 // DbTableStatistics
40 void DbTableStatistics::Update(const std::string &table_name,
41  bool write, bool fail, bool back_pressure, uint64_t num) {
42  TableStatsMap::iterator it = table_stats_map_.find(table_name);
43  if (it == table_stats_map_.end()) {
44  it = (table_stats_map_.insert(table_name, new TableStats)).first;
45  }
46  TableStats *table_stats = it->second;
47  table_stats->Update(write, fail, back_pressure, num);
48  // Update cumulative table stats as well
49  it = table_stats_cumulative_map_.find(table_name);
50  if (it == table_stats_cumulative_map_.end()) {
51  it = (table_stats_cumulative_map_.insert(table_name, new TableStats)).first;
52  }
53  table_stats = it->second;
54  table_stats->Update(write, fail, back_pressure, num);
55 }
56 
57 void DbTableStatistics::GetInternal(std::vector<GenDb::DbTableInfo> *vdbti,
58  bool cumulative) const {
59  TableStatsMap stats_map;
60  if (cumulative) {
61  stats_map = table_stats_cumulative_map_ ;
62  } else {
63  stats_map = table_stats_map_;
64  }
65  for (TableStatsMap::const_iterator it = stats_map.begin();
66  it != stats_map.end(); it++) {
67  const TableStats *table_stats(it->second);
68  GenDb::DbTableInfo dbti;
69  table_stats->Get(it->first, &dbti);
70  vdbti->push_back(dbti);
71  }
72 }
73 
74 void DbTableStatistics::GetDiffs(std::vector<GenDb::DbTableInfo> *vdbti) {
75  GetInternal(vdbti, false);
76  table_stats_map_.clear();
77 }
78 
79 void DbTableStatistics::GetCumulative(std::vector<GenDb::DbTableInfo> *vdbti)
80  const {
81  GetInternal(vdbti, true);
82 }
83 
84 // IfErrors
85 void IfErrors::GetInternal(DbErrors *db_errors) const {
86  db_errors->set_write_tablespace_fails(write_tablespace_fails_);
87  db_errors->set_read_tablespace_fails(read_tablespace_fails_);
88  db_errors->set_write_table_fails(write_column_family_fails_);
89  db_errors->set_read_table_fails(read_column_family_fails_);
90  db_errors->set_write_column_fails(write_column_fails_);
91  db_errors->set_write_batch_column_fails(write_batch_column_fails_);
92  db_errors->set_read_column_fails(read_column_fails_);
93 }
94 
96  write_tablespace_fails_ = 0;
97  read_tablespace_fails_ = 0;
98  write_column_family_fails_ = 0;
99  read_column_family_fails_ = 0;
100  write_column_fails_ = 0;
101  write_batch_column_fails_ = 0;
102  read_column_fails_ = 0;
103 }
104 
105 void IfErrors::GetDiffs(DbErrors *db_errors) {
106  GetInternal(db_errors);
107  Clear();
108 }
109 
110 void IfErrors::GetCumulative(DbErrors *db_errors) const {
111  GetInternal(db_errors);
112 }
113 
114 
116  switch (type) {
118  write_tablespace_fails_++;
119  break;
121  read_tablespace_fails_++;
122  break;
124  write_column_family_fails_++;
125  break;
127  read_column_family_fails_++;
128  break;
130  write_column_fails_++;
131  break;
133  write_batch_column_fails_++;
134  break;
136  read_column_fails_++;
137  break;
138  default:
139  break;
140  }
141 }
142 
143 // GenDbIfStats
144 void GenDbIfStats::IncrementTableStatsInternal(const std::string &table_name,
145  bool write, bool fail, bool back_pressure, uint64_t num) {
146  table_stats_.Update(table_name, write, fail, back_pressure, num);
147 }
148 
150  const std::string &table_name) {
151  switch (op) {
153  break;
155  IncrementTableStatsInternal(table_name, true, false, false, 1);
156  break;
158  IncrementTableStatsInternal(table_name, true, true, false, 1);
159  break;
161  IncrementTableStatsInternal(table_name, true, true, true, 1);
162  break;
164  IncrementTableStatsInternal(table_name, false, false, false, 1);
165  break;
167  IncrementTableStatsInternal(table_name, false, true, false, 1);
168  break;
169  default:
170  break;
171  }
172 }
173 
174 void GenDbIfStats::IncrementTableWrite(const std::string &table_name) {
175  IncrementTableStatsInternal(table_name, true, false, false, 1);
176 }
177 
178 void GenDbIfStats::IncrementTableWrite(const std::string &table_name,
179  uint64_t num_writes) {
180  IncrementTableStatsInternal(table_name, true, false, false, num_writes);
181 }
182 
183 void GenDbIfStats::IncrementTableWriteFail(const std::string &table_name) {
184  IncrementTableStatsInternal(table_name, true, true, false, 1);
185 }
186 
187 void GenDbIfStats::IncrementTableWriteFail(const std::string &table_name,
188  uint64_t num_writes) {
189  IncrementTableStatsInternal(table_name, true, true, false, num_writes);
190 }
191 
193  const std::string &table_name) {
194  IncrementTableStatsInternal(table_name, true, false, true, 1);
195 }
196 
197 void GenDbIfStats::IncrementTableRead(const std::string &table_name) {
198  IncrementTableStatsInternal(table_name, false, false, false, 1);
199 }
200 
201 void GenDbIfStats::IncrementTableRead(const std::string &table_name,
202  uint64_t num_reads) {
203  IncrementTableStatsInternal(table_name, false, false, false, num_reads);
204 }
205 
206 void GenDbIfStats::IncrementTableReadFail(const std::string &table_name) {
207  IncrementTableStatsInternal(table_name, false, true, false, 1);
208 }
209 
210 void GenDbIfStats::IncrementTableReadFail(const std::string &table_name,
211  uint64_t num_reads) {
212  IncrementTableStatsInternal(table_name, false, true, false, num_reads);
213 }
214 
216  const std::string &table_name) {
217  IncrementTableStatsInternal(table_name, false, false, true, 1);
218 }
219 
221  errors_.Increment(etype);
222  cumulative_errors_.Increment(etype);
223 }
224 
225 void GenDbIfStats::GetDiffs(std::vector<DbTableInfo> *vdbti, DbErrors *dbe) {
226  // Get diff cfstats
227  table_stats_.GetDiffs(vdbti);
228  // Get diff errors
229  errors_.GetDiffs(dbe);
230 }
231 
232 void GenDbIfStats::GetCumulative(std::vector<DbTableInfo> *vdbti,
233  DbErrors *dbe) const {
234  // Get cumulative cfstats
235  table_stats_.GetCumulative(vdbti);
236  // Get cumulative errors
237  cumulative_errors_.GetCumulative(dbe);
238 }
239 
240 } // namespace GenDb
void IncrementTableReadFail(const std::string &table_name)
void GetCumulative(std::vector< GenDb::DbTableInfo > *vdbti) const
void IncrementTableWriteFail(const std::string &table_name)
void IncrementTableStats(TableOp op, const std::string &table_name)
void GetDiffs(GenDb::DbErrors *dbe)
TableStatsMap table_stats_cumulative_map_
void IncrementErrors(IfErrors::Type type)
uint8_t type
Definition: load_balance.h:109
void IncrementTableReadBackPressureFail(const std::string &table_name)
void IncrementTableRead(const std::string &table_name)
boost::ptr_map< const std::string, TableStats > TableStatsMap
void IncrementTableStatsInternal(const std::string &table_name, bool write, bool fail, bool back_pressure, uint64_t num)
void GetInternal(GenDb::DbErrors *dbe) const
void Update(bool write, bool fail, bool back_pressure, uint64_t num)
void GetInternal(std::vector< GenDb::DbTableInfo > *vdbti, bool cumulative) const
void GetDiffs(std::vector< GenDb::DbTableInfo > *vdbti, GenDb::DbErrors *dbe)
void Increment(Type type)
void Update(const std::string &table_name, bool write, bool fail, bool back_pressure, uint64_t num)
void GetDiffs(std::vector< GenDb::DbTableInfo > *vdbti)
TableStatsMap table_stats_map_
void GetCumulative(GenDb::DbErrors *dbe) const
void GetCumulative(std::vector< GenDb::DbTableInfo > *vdbti, GenDb::DbErrors *dbe) const
void Get(const std::string &table_name, GenDb::DbTableInfo *dbti) const
void IncrementTableWrite(const std::string &table_name)
void IncrementTableWriteBackPressureFail(const std::string &table_name)