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