[timer] print user-defined timer thread name in log

This commit is contained in:
obdev 2025-01-02 06:15:05 +00:00 committed by ob-robot
parent 0bf3e81dd5
commit 7b128a1015
6 changed files with 78 additions and 13 deletions

View File

@ -521,6 +521,7 @@ const int64_t OB20_PROTOCOL_EXTRA_INFO_LENGTH = 4; // for the length of extra i
const int16_t OB20_PROTOCOL_VERSION_VALUE = 20;
const int OB_THREAD_NAME_BUF_LEN = 16;
const int OB_EXTENED_THREAD_NAME_BUF_LEN = 32;
enum ObCSProtocolType
{
@ -2615,6 +2616,21 @@ OB_INLINE char* ob_get_origin_thread_name()
return ori_tname;
}
OB_INLINE char* ob_get_extended_thread_name()
{
thread_local char ext_tname[oceanbase::OB_EXTENED_THREAD_NAME_BUF_LEN] = {0};
return ext_tname;
}
OB_INLINE char* ob_get_tname_v2()
{
char *ret_tname = ob_get_extended_thread_name();
if ('\0' == ret_tname[0]) {
ret_tname = ob_get_tname();
}
return ret_tname;
}
static const char* PARALLEL_DDL_THREAD_NAME = "DDLPQueueTh";
static const char* REPLAY_SERVICE_THREAD_NAME = "ReplaySrv";
@ -2642,6 +2658,7 @@ OB_INLINE uint64_t ob_set_thread_tenant_id(uint64_t tenant_id)
#define GETTID() ob_gettid()
#define GETTNAME() ob_get_tname()
#define GETTNAME_V2() ob_get_tname_v2()
#define GET_TENANT_ID() ob_get_tenant_id()
#define gettid GETTID
#define GET_CLUSTER_ID() ob_get_cluster_id()

View File

@ -747,7 +747,7 @@ int ObLogger::log_head(const int64_t ts,
"|%s|%s|%s|%d|%lu|%ld|%s|%s|%s|%s:%d|",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec, tv.tv_usec, errstr_[level], mod_name, dba_event, errcode,
GET_TENANT_ID(), GETTID(), GETTNAME(), ObCurTraceId::get_trace_id_str(),
GET_TENANT_ID(), GETTID(), GETTNAME_V2(), ObCurTraceId::get_trace_id_str(),
function, base_file_name, line);
} else {
if (level == OB_LOG_LEVEL_DBA_ERROR
@ -762,7 +762,7 @@ int ObLogger::log_head(const int64_t ts,
"[%04d-%02d-%02d %02d:%02d:%02d.%06ld] "
"[%ld][%s][T%lu][%s] ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec, tv.tv_usec, GETTID(), GETTNAME(), GET_TENANT_ID(), ObCurTraceId::get_trace_id_str());
tm.tm_sec, tv.tv_usec, GETTID(), GETTNAME_V2(), GET_TENANT_ID(), ObCurTraceId::get_trace_id_str());
} else {
constexpr int cluster_id_buf_len = 8;
char cluster_id_buf[cluster_id_buf_len] = {'\0'};
@ -772,7 +772,7 @@ int ObLogger::log_head(const int64_t ts,
"%-5s %s%s (%s:%d) [%ld][%s]%s[T%lu][%s] [lt=%ld]%s ",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec, tv.tv_usec, errstr_[level], mod_name, function,
base_file_name, line, GETTID(), GETTNAME(), is_arb_replica_ ? cluster_id_buf : "",
base_file_name, line, GETTID(), GETTNAME_V2(), is_arb_replica_ ? cluster_id_buf : "",
is_arb_replica_ ? GET_ARB_TENANT_ID() : GET_TENANT_ID(), ObCurTraceId::get_trace_id_str(),
last_logging_cost_time_us_, errcode_buf);
}

View File

@ -26,9 +26,8 @@ namespace common
using namespace obutil;
using namespace lib;
int ObTimer::init(const char* thread_name, const ObMemAttr &attr)
int ObTimer::init(const char* timer_name, const ObMemAttr &attr)
{
UNUSED(thread_name);
UNUSED(attr);
int ret = OB_SUCCESS;
if (ObTimerService::get_instance().is_never_started()) {
@ -42,6 +41,7 @@ int ObTimer::init(const char* thread_name, const ObMemAttr &attr)
} else {
is_inited_ = true;
is_stopped_ = false;
ObTimerUtil::copy_buff(timer_name_, sizeof(timer_name_), timer_name);
}
return ret;
}
@ -99,6 +99,7 @@ void ObTimer::destroy()
wait();
is_inited_ = false;
timer_service_ = nullptr;
timer_name_[0] = '\0';
}
}

View File

@ -23,14 +23,19 @@ namespace common
class ObTimer
{
friend class TaskToken;
public:
ObTimer()
: is_inited_(false), is_stopped_(true), timer_service_(nullptr), run_wrapper_(nullptr)
: is_inited_(false),
is_stopped_(true),
timer_service_(nullptr),
run_wrapper_(nullptr)
{
timer_name_[0] = '\0';
timer_service_ = &(ObTimerService::get_instance());
}
~ObTimer();
int init(const char* thread_name = nullptr,
int init(const char* timer_name = nullptr,
const ObMemAttr &attr = ObMemAttr(OB_SERVER_TENANT_ID, "timer")); // init and start
bool inited() const;
int start(); // only start
@ -59,6 +64,7 @@ private:
private:
bool is_inited_;
bool is_stopped_;
char timer_name_[OB_THREAD_NAME_BUF_LEN];
ObTimerService *timer_service_;
lib::IRunWrapper *run_wrapper_;
};

View File

@ -57,12 +57,14 @@ TaskToken::TaskToken(
const int64_t dt)
: timer_(timer), task_(task), scheduled_time_(st), delay_(dt)
{
char *buf = task_type_;
int buf_len = sizeof(task_type_);
if (task != NULL) {
strncpy(buf, typeid(*task).name(), buf_len);
task_type_[0] = '\0';
timer_name_[0] = '\0';
if (nullptr != task) {
ObTimerUtil::copy_buff(task_type_, sizeof(task_type_), typeid(*task).name());
}
if (nullptr != timer) {
ObTimerUtil::copy_buff(timer_name_, sizeof(timer_name_), timer->timer_name_);
}
buf[buf_len - 1] = '\0';
}
TaskToken::TaskToken(const ObTimer *timer, ObTimerTask *task)
@ -97,9 +99,11 @@ void ObTimerTaskThreadPool::handle(void *task_token)
thread_id = GETTID();
ObTimerMonitor::get_instance().start_task(thread_id, start_time, token->delay_, token->task_);
}
token->task_->runTimerTask();
THIS_WORKER.set_timeout_ts(INT64_MAX); // reset timeout to INT64_MAX
ObCurTraceId::reset(); // reset trace_id
set_ext_tname(token);
token->task_->runTimerTask();
clear_ext_tname(); // reset ext_tname
const int64_t end_time = ::oceanbase::common::ObTimeUtility::current_time();
const int64_t elapsed_time = end_time - start_time;
if (do_timeout_check) {
@ -113,6 +117,26 @@ void ObTimerTaskThreadPool::handle(void *task_token)
}
}
void ObTimerTaskThreadPool::set_ext_tname(const TaskToken *token)
{
if (nullptr != token) {
char *ext_tname = ob_get_extended_thread_name();
const char *tname = ob_get_tname();
if (nullptr != ext_tname && nullptr != tname) {
IGNORE_RETURN databuff_printf(ext_tname, OB_EXTENED_THREAD_NAME_BUF_LEN,
"%s_%s", tname, token->timer_name_);
}
}
}
void ObTimerTaskThreadPool::clear_ext_tname()
{
char *ext_tname = ob_get_extended_thread_name();
if (nullptr != ext_tname) {
ext_tname[0] = '\0';
}
}
ObTimerService::ObTimerService(uint64_t tenant_id /* = OB_SERVER_TENANT_ID */)
: is_never_started_(true),
is_stopped_(true),

View File

@ -34,6 +34,19 @@ namespace common
{
extern uint64_t mtl_get_id();
class ObTimerUtil
{
public:
static void copy_buff(char *buf, int64_t buf_len, const char *value)
{
if (nullptr != buf && nullptr != value && '\0' != value[0] && buf_len > 0) {
strncpy(buf, value, buf_len);
buf[buf_len - 1] = '\0';
}
}
};
class ObTimerTask
{
public:
@ -72,6 +85,7 @@ public:
TO_STRING_KV(KP(this), KP_(timer), KP_(task), K_(task_type), K_(scheduled_time), K_(delay));
public:
char task_type_[128];
char timer_name_[OB_THREAD_NAME_BUF_LEN];
const ObTimer *timer_;
ObTimerTask *task_;
int64_t scheduled_time_;
@ -87,6 +101,9 @@ public:
virtual void handle(void *task_token) override;
ObTimerTaskThreadPool(const ObTimerTaskThreadPool &) = delete;
ObTimerTaskThreadPool &operator=(const ObTimerTaskThreadPool &) = delete;
private:
static void set_ext_tname(const TaskToken *token);
static void clear_ext_tname();
private:
ObTimerService &service_;
private: