`QueryInfo::end_time` should always be greater than or equal to `start_time`

Signed-off-by: Wazir Ahmed <wazir@proxysql.com>
pull/5099/head
Wazir Ahmed 8 months ago
parent daf1570c5d
commit 8d29742e20

@ -96,8 +96,33 @@ class Query_Info {
void end();
char *get_digest_text();
bool is_select_NOT_for_update();
void set_end_time(unsigned long long time);
};
/**
* @brief Assigns query end time.
* @details In addition to being a setter for end_time member variable, this
* method ensures that end_time is always greater than or equal to start_time.
* Refer https://github.com/sysown/proxysql/issues/4950 for more details.
* @param time query end time
*/
inline void Query_Info::set_end_time(unsigned long long time) {
end_time = time;
#ifndef CLOCK_MONOTONIC_RAW
if (start_time <= end_time)
return;
// If start_time is greater than end_time, assign current monotonic time
end_time = monotonic_time();
if (start_time <= end_time)
return;
// If start_time is still greater than end_time, set the difference to 0
end_time = start_time;
#endif // CLOCK_MONOTONIC_RAW
}
/**
* @class MySQL_Session
* @brief Manages a client session, including query parsing, backend connections, and state transitions.

@ -179,12 +179,37 @@ public:
void end();
char* get_digest_text();
bool is_select_NOT_for_update();
void set_end_time(unsigned long long time);
private:
void reset_extended_query_info();
void init(unsigned char* _p, int len, bool header = false);
};
/**
* @brief Assigns query end time.
* @details In addition to being a setter for end_time member variable, this
* method ensures that end_time is always greater than or equal to start_time.
* Refer https://github.com/sysown/proxysql/issues/4950 for more details.
* @param time query end time
*/
inline void PgSQL_Query_Info::set_end_time(unsigned long long time) {
end_time = time;
#ifndef CLOCK_MONOTONIC_RAW
if (start_time <= end_time)
return;
// If start_time is greater than end_time, assign current monotonic time
end_time = monotonic_time();
if (start_time <= end_time)
return;
// If start_time is still greater than end_time, set the difference to 0
end_time = start_time;
#endif // CLOCK_MONOTONIC_RAW
}
class PgSQL_Session : public Base_Session<PgSQL_Session, PgSQL_Data_Stream, PgSQL_Backend, PgSQL_Thread> {
private:
using PktType = std::variant<std::unique_ptr<PgSQL_Parse_Message>,std::unique_ptr<PgSQL_Describe_Message>,

@ -3322,7 +3322,7 @@ void MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
l_free(pkt.size,pkt.ptr);
client_myds->DSS=STATE_SLEEP;
status=WAITING_CLIENT_DATA;
CurrentQuery.end_time=thread->curtime;
CurrentQuery.set_end_time(thread->curtime);
CurrentQuery.end();
} else {
mybe=find_or_create_backend(current_hostgroup);
@ -7527,7 +7527,7 @@ unsigned long long MySQL_Session::IdleTime() {
void MySQL_Session::LogQuery(MySQL_Data_Stream *myds, const unsigned int myerrno, const char * errmsg) {
// we need to access statistics before calling CurrentQuery.end()
// so we track the time here
CurrentQuery.end_time=thread->curtime;
CurrentQuery.set_end_time(thread->curtime);
if (qpo) {
if (qpo->log==1) {

@ -4885,7 +4885,7 @@ unsigned long long PgSQL_Session::IdleTime() {
void PgSQL_Session::LogQuery(PgSQL_Data_Stream* myds) {
// we need to access statistics before calling CurrentQuery.end()
// so we track the time here
CurrentQuery.end_time = thread->curtime;
CurrentQuery.set_end_time(thread->curtime);
if (qpo) {
if (qpo->log == 1) {

Loading…
Cancel
Save