ob_query_response_time.cpp 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include "ob_query_response_time.h"
#include "observer/omt/ob_tenant_timezone_mgr.h"
#include "observer/omt/ob_tenant_config_mgr.h"

using namespace oceanbase;
using namespace oceanbase::common;
using namespace oceanbase::share::schema;

namespace oceanbase {
namespace observer {

ObRSTUtility::ObRSTUtility():base_(0)
{
    max_dec_value_ = MILLION;
    for(int i= 0; OB_QRT_POSITIVE_LENGTH > i; ++i)
        max_dec_value_ *= 10;
    setup(OB_QRT_DEFAULT_BASE);
}

ObRSTUtility::~ObRSTUtility()
{}

int ObRSTUtility::setup(uint base)
{
    int ret = OB_SUCCESS;
    if (base < 2) {
        ret = OB_INVALID_ARGUMENT;
    } else if(base != base_) {
        base_= base;
        
        uint64_t value = MILLION;
        negative_count_= 0;
        while(value > 0){
            negative_count_ += 1;
            value /= base_;
        }
        negative_count_ -= 1;

        value = MILLION;
        positive_count_ = 0;
        while(value < max_dec_value_){
            positive_count_ += 1;
            value *= base_;
        }
        bound_count_ = negative_count_ + positive_count_;

        value = MILLION;
        for(uint i = 0; i < negative_count_; ++i) {
            value /= base_;
            bound_[negative_count_ - i - 1] = value;
        }

        value = MILLION;
        for(uint i = 0; i < positive_count_; ++i) {
            bound_[negative_count_ + i] = value;
            value *= base_;
        }
    }
    return 0;
}

ObRSTTimeCollector::ObRSTTimeCollector():mutex_()
{
    flush();
}
ObRSTTimeCollector::~ObRSTTimeCollector()
{}

int ObRSTTimeCollector::flush()
{
    for(int i = 0; i < OB_QRT_OVERALL_COUNT + 1; i++) {
        count_[i] = 0;
        total_[i] = 0;
    }
    return 0;
}

int ObRSTTimeCollector::collect(uint64_t time)
{
    int i = 0;
    for(int count = utility_.bound_count(); count > i; ++i) {
        if(utility_.bound(i) > time) {
            count_[i]++;
            total_[i] += time;
            break;
        }
    }
    return 0;
}

ObRSTCollector::ObRSTCollector():inited_(false)
{}

ObRSTCollector::~ObRSTCollector(){
    inited_ = false;
    collector_map_.clear();
}

ObRSTCollector& ObRSTCollector::get_instance(){
    static ObRSTCollector collector_instance_;
    if (!collector_instance_.inited_){
        collector_instance_.init();
    }
    return collector_instance_;
}

int ObRSTCollector::init()
{
  int ret = OB_SUCCESS;
  if (inited_){
    ret = OB_INIT_TWICE;
  } else if (!collector_map_.created() &&
        OB_FAIL(collector_map_.create(
            common::OB_MAX_SERVER_TENANT_CNT, ObModIds::OB_HASH_BUCKET, ObModIds::OB_HASH_NODE))) {
      ret = OB_NOT_INIT;
      SQL_ENG_LOG(WARN, "create time collector map failed", K(ret));
    } else {
        inited_ = true;
    } 
  return ret;
}

int ObRSTCollector::collect_query_response_time(uint64_t tenant_id, uint64_t time){
    int ret = OB_SUCCESS;
    omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
    if(tenant_config->query_response_time_stats){
        ObRSTTimeCollector* time_collector;
        if (OB_FAIL(ret = collector_map_.get_refactored(tenant_id, time_collector))){
          SERVER_LOG(WARN, "time collector of the tenant does not exist", K(tenant_id), K(time), K(ret));
        } else {
            lib::ObMutexGuard guard(time_collector->mutex_);
            if(OB_FAIL(ret = time_collector->collect(time))){
                SERVER_LOG(WARN, "time collector of the tenant collect time failed", K(tenant_id), K(time), K(ret));
            }
        }
    }
    return ret;
}

int ObRSTCollector::flush_query_response_time(uint64_t tenant_id,const ObString& is_enable) {
    int ret = OB_SUCCESS;
    bool is_enable_value = false;
    bool is_valid = false;
    is_enable_value = ObConfigBoolParser::get(is_enable.ptr(), is_valid);
    omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
    if (!is_valid) {
        ret = OB_INVALID_ARGUMENT;
        SERVER_LOG(WARN, "invalid bool str", K(ret), K(is_enable), K(tenant_id));
    } else if (is_enable_value) {
        ObRSTTimeCollector* time_collector;
        if (OB_FAIL(ret = collector_map_.get_refactored(tenant_id, time_collector))){
            SERVER_LOG(WARN, "time collector of the tenant does not exist", K(ret), K(tenant_id));
        } else {
            lib::ObMutexGuard guard(time_collector->mutex_);
            if (OB_FAIL(ret = time_collector->setup(tenant_config->query_response_time_range_base))){
                SERVER_LOG(WARN, "time collector of the tenant set range base failed", K(ret), K(tenant_id));
            } else if (OB_FAIL(ret = time_collector->flush())){
                SERVER_LOG(WARN, "time collector of the tenant flush failed", K(ret), K(tenant_id));
            } 
        }    
    }
    return ret;
}

int ObRSTCollector::enable_query_response_time(uint64_t tenant_id){
    int ret = OB_SUCCESS;
L
LINxiansheng 已提交
179 180 181 182
    omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
    if(tenant_config->query_response_time_stats){
        SERVER_LOG(INFO, "query_response_time_stats already turn on", K(ret), K(tenant_id));
    } else if (OB_FAIL(ret = collector_map_.set_refactored(tenant_id, new ObRSTTimeCollector()))) {
183 184 185 186 187 188 189 190 191
        if (OB_HASH_EXIST == ret) {
            ret = OB_ERR_ALREADY_EXISTS;
        }
    }
    return ret;
}

int ObRSTCollector::free_query_response_time(uint64_t tenant_id){
    int ret = OB_SUCCESS;
L
LINxiansheng 已提交
192 193 194 195
    omt::ObTenantConfigGuard tenant_config(TENANT_CONF(tenant_id));
    if(!tenant_config->query_response_time_stats){
        SERVER_LOG(INFO, "query_response_time_stats already turn off", K(ret), K(tenant_id));
    } else if (OB_FAIL(collector_map_.erase_refactored(tenant_id))) {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        SERVER_LOG(WARN,"erase the time collector failed", K(tenant_id));
    }
    return ret;
}

int ObRSTCollector::control_query_response_time(uint64_t tenant_id, const ObString& is_enable){
    int ret = OB_SUCCESS;
    bool is_enable_value = false;
    bool is_valid = false;
    is_enable_value = ObConfigBoolParser::get(is_enable.ptr(), is_valid);
    if (is_enable_value) {
        if (OB_FAIL(ret = enable_query_response_time(tenant_id))){
            SERVER_LOG(WARN, "enable the query response time failed", K(ret),K(tenant_id));
        }
    } else if (OB_FAIL(ret = free_query_response_time(tenant_id))){
        SERVER_LOG(WARN, "free the query response time failed", K(ret),K(tenant_id));
    }
    return ret;
}

}  // namespace observer
}  // namespace oceanbase