Example #1
0
TEST(ObStatManager,Serialize)
{
  ObStatManager stat_manager(ObStatManager::SERVER_TYPE_CHUNK);
  stat_manager.set_value(1001, 1, 1);
  stat_manager.set_value(1002, 2, 2);
  stat_manager.set_value(1001, 2, 2);
  stat_manager.inc(1002, 2);
  stat_manager.inc(1001, 3, 5);
  stat_manager.inc(1001, 3, 5);

  ObStatManager::const_iterator it = stat_manager.begin();
  EXPECT_EQ(1001, it->get_table_id());
  EXPECT_EQ(1, it->get_value(1));
  EXPECT_EQ(2, it->get_value(2));
  EXPECT_EQ(10, it->get_value(3));
  it++;
  EXPECT_EQ(1002, it->get_table_id());
  EXPECT_EQ(3, it->get_value(2));
  EXPECT_EQ(0, it->get_value(1));
  it++;
  EXPECT_EQ(stat_manager.end(), it);

  char buff[1024];
  int64_t pos = 0;
  EXPECT_TRUE(OB_SUCCESS == stat_manager.serialize(buff, 1024, pos));
  pos = 0;
  ObStatManager m2(ObStatManager::SERVER_TYPE_MERGE);
  EXPECT_TRUE(OB_SUCCESS == m2.deserialize(buff, 1024, pos));
  EXPECT_EQ(ObStatManager::SERVER_TYPE_CHUNK, m2.get_server_type());

  it = m2.begin();
  EXPECT_EQ(1001, it->get_table_id());
  EXPECT_EQ(1, it->get_value(1));
  EXPECT_EQ(2, it->get_value(2));
  EXPECT_EQ(10, it->get_value(3));
  it++;
  EXPECT_EQ(1002, it->get_table_id());
  EXPECT_EQ(3, it->get_value(2));
  EXPECT_EQ(0, it->get_value(1));
  it++;
  EXPECT_EQ(m2.end(), it);


}
    int32_t ObAppStats::refresh()
    {
      int ret = 0;
      ObRole server_type = store_.diff.get_server_type();

      store_.current.reset();
      store_.current.set_server_type(server_type);


      if (server_type == OB_ROOTSERVER)
      {
        ret = sum_cluster_stats(server_type, root_server_array_);
      }
      else if (server_type == OB_UPDATESERVER)
      {
        ObStatManager node_stats;
        node_stats.set_server_type(server_type);

        for (int i = 0; i < root_server_array_.get_array_index(); ++i)
        {
          if (i == master_index_)
          {
            ObClientRpcStub rs_stub;
            ObClientRpcStub ups_stub;
            ObServer update_server;

            if (OB_SUCCESS != (ret = rs_stub.initialize(*root_server_array_.at(i),
                &GFactory::get_instance().get_base_client().get_client_mgr())))
            {
              fprintf(stderr, "initialize rs stub failed\n");
              break;
            }
            else if (OB_SUCCESS != (ret = rs_stub.get_update_server(update_server)))
            {
              fprintf(stderr, "get update server addr from rs failed\n");
              break;
            }
            else if (OB_SUCCESS != (ret = ups_stub.initialize(update_server,
                    &GFactory::get_instance().get_base_client().get_client_mgr())))
            {
              fprintf(stderr, "initialize ups stub failed\n");
              break;
            }
            else if (OB_SUCCESS != (ret = ups_stub.fetch_stats(node_stats)))
            {
              fprintf(stderr, "fetch ups stats failed\n");
              break;
            }
          }

          ret = sum_ups_cluster_stats(*root_server_array_.at(i), server_type);
          if (OB_SUCCESS != ret) break;
        }

        // set apply stat. value
        ObStatManager::const_iterator it = store_.current.begin(mod_);
        ObStat *stat_item = NULL;;
        while (it != store_.current.end(mod_))
        {
          for (int index = UPS_MASTER_START_INDEX; index <= UPS_MASTER_END_INDEX; ++index)
          {
            node_stats.get_stat(it->get_mod_id(), it->get_table_id(), stat_item);
            if (NULL != stat_item)
              const_cast<ObStat*>(it)->set_value(index, stat_item->get_value(index));
          }
          ++it;
        }
      }
      else if (server_type == OB_CHUNKSERVER
          || server_type == OB_MERGESERVER)
      {
        for (int i = 0; i < root_server_array_.get_array_index(); ++i)
        {
          ret = sum_cs_cluster_stats(*root_server_array_.at(i), server_type);
          if (OB_SUCCESS != ret) break;
        }
      }

      return ret;
    }