MySQL 9.3.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
slow_query_monitor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2024, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_MRS_SLOW_QUERY_MONITOR_H_
27#define ROUTER_SRC_REST_MRS_SRC_MRS_SLOW_QUERY_MONITOR_H_
28
29#include <chrono>
30#include <functional>
31#include <list>
32#include <memory>
33#include <mutex>
34#include <string>
35#include <thread>
39#include "mrs/configuration.h"
41
42namespace mrs {
43namespace database {
44
45constexpr const int64_t k_default_sql_query_timeout_ms = 2000;
46
48 public:
49 using TimeType = std::chrono::time_point<std::chrono::system_clock>;
51
55
56 void execute(const std::function<void()> &fn, MySQLSession *conn,
57 int64_t timeout_ms = -1);
58
59 void start();
60 void stop();
61 void reset();
62
63 void configure(const std::string &options);
64
65 int64_t default_timeout() const { return default_sql_timeout_ms_; }
66
67 private:
68 struct ActiveQuery {
70 bool killed = false;
71 uint64_t connection_id = 0;
72
74 };
75
76 using ActiveQueryList = std::list<ActiveQuery>;
77 using ActiveQueryListIt = ActiveQueryList::iterator;
78
79 ActiveQueryListIt on_query_start(MySQLSession *conn, int64_t timeout_ms = -1);
81
82 void run();
83 bool wait_until_next_timeout(int64_t next_timeout);
84
85 void kill_session(
87 unsigned long conn_id);
88 int64_t check_queries();
89
91
93
94 std::thread monitor_thread_;
98
99 class Waitable : public WaitableMonitor<void *> {
100 public:
102 using Parent::WaitableMonitor;
103 };
105
108};
109
110} // namespace database
111} // namespace mrs
112
113#endif // ROUTER_SRC_REST_MRS_SRC_MRS_SLOW_QUERY_MONITOR_H_
Monitor can be waited for.
Definition: monitor.h:62
Definition: wait_variable.h:34
Definition: counted_mysql_session.h:38
Definition: mysql_cache_manager.h:87
Definition: configuration.h:47
Definition: slow_query_monitor.h:99
Definition: slow_query_monitor.h:47
void on_query_end(ActiveQueryListIt query)
Definition: slow_query_monitor.cc:154
void execute(const std::function< void()> &fn, MySQLSession *conn, int64_t timeout_ms=-1)
Definition: slow_query_monitor.cc:98
std::chrono::time_point< std::chrono::system_clock > TimeType
Definition: slow_query_monitor.h:49
void kill_session(const collector::CountedMySQLSession::ConnectionParameters &params, unsigned long conn_id)
Definition: slow_query_monitor.cc:161
Waitable waitable_
Definition: slow_query_monitor.h:104
collector::MysqlCacheManager * cache_manager_
Definition: slow_query_monitor.h:96
void start()
Definition: slow_query_monitor.cc:216
WaitableVariable< State > state_
Definition: slow_query_monitor.h:97
ActiveQueryList::iterator ActiveQueryListIt
Definition: slow_query_monitor.h:77
void run()
Definition: slow_query_monitor.cc:239
void stop()
Definition: slow_query_monitor.cc:223
int64_t check_queries()
Definition: slow_query_monitor.cc:180
void reset()
Definition: slow_query_monitor.cc:234
~SlowQueryMonitor()
Definition: slow_query_monitor.cc:214
std::list< ActiveQuery > ActiveQueryList
Definition: slow_query_monitor.h:76
State
Definition: slow_query_monitor.h:92
@ k_running
Definition: slow_query_monitor.h:92
@ k_idle
Definition: slow_query_monitor.h:92
@ k_stopped
Definition: slow_query_monitor.h:92
@ k_initializing
Definition: slow_query_monitor.h:92
ActiveQueryList active_queries_
Definition: slow_query_monitor.h:107
bool wait_until_next_timeout(int64_t next_timeout)
Definition: slow_query_monitor.cc:262
uint64_t default_sql_timeout_ms_
Definition: slow_query_monitor.h:90
void configure(const std::string &options)
Definition: slow_query_monitor.cc:88
SlowQueryMonitor(const mrs::Configuration &configuration, collector::MysqlCacheManager *cache)
Definition: slow_query_monitor.cc:84
ActiveQueryListIt on_query_start(MySQLSession *conn, int64_t timeout_ms=-1)
Definition: slow_query_monitor.cc:118
std::thread monitor_thread_
Definition: slow_query_monitor.h:94
std::mutex active_queries_mutex_
Definition: slow_query_monitor.h:106
int64_t default_timeout() const
Definition: slow_query_monitor.h:65
const mrs::Configuration configuration_
Definition: slow_query_monitor.h:95
static char * query
Definition: myisam_ftdump.cc:47
constexpr const int64_t k_default_sql_query_timeout_ms
Definition: slow_query_monitor.h:45
Definition: authorize_manager.h:48
Definition: options.cc:57
required string configuration
Definition: replication_asynchronous_connection_failover.proto:49
Definition: counted_mysql_session.h:41
Definition: slow_query_monitor.h:68
uint64_t connection_id
Definition: slow_query_monitor.h:71
bool killed
Definition: slow_query_monitor.h:70
MySQLSession * conn
Definition: slow_query_monitor.h:69
TimeType max_time
Definition: slow_query_monitor.h:73