ObStatManager & ObStatManager::operate(const ObStatManager &operand, OpFunc op) { if (server_type_ == operand.server_type_) { for (int32_t mod = 0; mod < OB_MAX_MOD_NUMBER; mod++) { for (int32_t i = 0; i < table_stats_[mod].get_array_index(); i++) { ObStat *stat = table_stats_[mod].at(i); ObStat *operand_stat = NULL; int has_table = operand.get_stat(stat->get_mod_id(), stat->get_table_id(), operand_stat); int64_t op_value = 0; // if operand has status value of same table, then apply op, otherwise keep old values. if (OB_SUCCESS == has_table && NULL != operand_stat) { for (int32_t index = 0; index < ObStat::MAX_STATICS_PER_TABLE; ++index) { op_value = op(stat->get_value(index) , operand_stat->get_value(index)); stat->set_value(index, op_value); } } } } } return *this; }
int ObStatManager::inc(const uint64_t mod_id, const uint64_t table_id, const int32_t index, const int64_t inc_value) { int ret = OB_ERROR; bool need_add_new = true; assert(mod_id < OB_MAX_MOD_NUMBER); for (int32_t i = 0; table_id != OB_INVALID_ID && mod_id != OB_INVALID_ID && i < table_stats_[mod_id].get_array_index(); i++) { if (data_holder_[mod_id][i].get_table_id() == table_id) { need_add_new = false; ret = data_holder_[mod_id][i].inc(index, inc_value); } } if (need_add_new && table_id != OB_INVALID_ID && mod_id != OB_INVALID_ID) { ObStat stat; stat.set_mod_id(mod_id); stat.set_table_id(table_id); ret = stat.set_value(index, inc_value); if (OB_SUCCESS == ret) { if ((ret = add_new_stat(stat)) == OB_ENTRY_EXIST) { ret = inc(mod_id, table_id, index, inc_value); } } } return ret; }
int ObStatManager::add_new_stat(const ObStat& stat) { int ret = OB_SUCCESS; uint64_t table_id = stat.get_table_id(); uint64_t mod_id = stat.get_mod_id(); if (table_id != OB_INVALID_ID && mod_id != OB_INVALID_ID) { tbsys::CThreadGuard guard(&lock_); for (int32_t i = 0; i < table_stats_[mod_id].get_array_index(); i++) { if (data_holder_[mod_id][i].get_table_id() == table_id) { ret = OB_ENTRY_EXIST; break; } } if (ret != OB_ENTRY_EXIST) { if (!table_stats_[mod_id].push_back(stat)) { TBSYS_LOG(WARN, "too much tables"); ret = OB_ERROR; } } } return ret; }
int ObStatManager::inc(const uint64_t table_id, const int32_t index, const int64_t inc_value) { int ret = OB_ERROR; bool need_add_new = true; for (int32_t i = 0; table_id != OB_INVALID_ID && i < table_stats_.get_array_index(); i++) { if (data_holder_[i].get_table_id() == table_id) { need_add_new = false; ret = data_holder_[i].inc(index, inc_value); } } if (need_add_new && table_id != OB_INVALID_ID) { ObStat stat; stat.set_table_id(table_id); ret = stat.set_value(index, inc_value); if (OB_SUCCESS == ret) { if (!table_stats_.push_back(stat)) { TBSYS_LOG(WARN, "too much tables"); ret = OB_ERROR; } } } return ret; }
TEST(ObStat,Serialize) { ObStat stat; stat.set_table_id(1111); stat.set_value(3, 20); stat.inc(3); char buff[1024]; int64_t pos = 0; EXPECT_EQ(1111, stat.get_table_id()); EXPECT_EQ(21, stat.get_value(3)); EXPECT_TRUE(OB_SUCCESS == stat.serialize(buff, 1024, pos)); pos = 0; ObStat stat2; EXPECT_TRUE(OB_SUCCESS == stat2.deserialize(buff, 1024, pos)); EXPECT_EQ(1111, stat2.get_table_id()); EXPECT_EQ(21, stat2.get_value(3)); }
int ObStatManager::get_scanner(ObNewScanner &scanner) const { int ret = OB_SUCCESS; int64_t last_used_mod = OB_INVALID_ID; char ipbuf[OB_IP_STR_BUFF] = {0}; server_.ip_to_string(ipbuf, sizeof (ipbuf)); ObString ipstr = ObString::make_string(ipbuf); const int32_t port = server_.get_port(); ObString server_name = ObString::make_string(print_role(get_server_type())); int64_t total_stat_cnt = 0; /* create row_desc */ ObRowDesc row_desc; int32_t column_id = OB_APP_MIN_COLUMN_ID; row_desc.add_column_desc(OB_ALL_SERVER_STAT_TID, column_id++); row_desc.add_column_desc(OB_ALL_SERVER_STAT_TID, column_id++); row_desc.add_column_desc(OB_ALL_SERVER_STAT_TID, column_id++); row_desc.add_column_desc(OB_ALL_SERVER_STAT_TID, column_id++); row_desc.add_column_desc(OB_ALL_SERVER_STAT_TID, column_id++); row_desc.set_rowkey_cell_count(4); ObRow row; ObObj obj; // drop mod id info for (int32_t mod = 0; mod < OB_MAX_MOD_NUMBER; mod++) { ObStat stat; total_stat_cnt += stat_cnt_[mod]; // drop table id info for (int32_t i = 0; i < table_stats_[mod].get_array_index(); i++) { for (int32_t j = 0; j < stat_cnt_[mod]; j++) { stat.inc(j, table_stats_[mod].at(i)->get_value(j)); } } for (int32_t i = 0; i < stat_cnt_[mod]; i++) { column_id = OB_APP_MIN_COLUMN_ID; row.set_row_desc(row_desc); row.reset(false, ObRow::DEFAULT_NULL); /* server type */ obj.set_type(ObVarcharType); obj.set_varchar(server_name); row.set_cell(OB_ALL_SERVER_STAT_TID, column_id++, obj); /* server ip */ obj.set_type(ObVarcharType); obj.set_varchar(ipstr); row.set_cell(OB_ALL_SERVER_STAT_TID, column_id++, obj); /* port */ obj.set_type(ObIntType); obj.set_int(port); row.set_cell(OB_ALL_SERVER_STAT_TID, column_id++, obj); /* key */ obj.set_type(ObVarcharType); obj.set_varchar(ObString::make_string(get_name(mod, i))); row.set_cell(OB_ALL_SERVER_STAT_TID, column_id++, obj); /* value */ obj.set_type(ObIntType); obj.set_int(stat.get_value(i)); row.set_cell(OB_ALL_SERVER_STAT_TID, column_id++, obj); scanner.add_row(row); // for last rowkey last_used_mod = mod; } } /* last rowkey */ if (total_stat_cnt > 0) { static ObObj rk_objs[4]; rk_objs[0].set_type(ObVarcharType); rk_objs[0].set_varchar(server_name); rk_objs[1].set_type(ObVarcharType); rk_objs[1].set_varchar(ipstr); rk_objs[2].set_type(ObIntType); rk_objs[2].set_int(port); rk_objs[3].set_type(ObVarcharType); rk_objs[3].set_varchar(ObString::make_string(get_name(last_used_mod, stat_cnt_[last_used_mod] - 1))); ObRowkey rowkey(rk_objs, 4); scanner.set_last_row_key(rowkey); } /* fullfilled */ scanner.set_is_req_fullfilled(true, total_stat_cnt); return ret; }