MySQL 9.4.0
Source Code Documentation
metadata_logger.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 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, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19 * the GNU General Public License, version 2.0, 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 Foundation, Inc.,
23 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#ifndef ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_METADATA_LOGGER_H_
27#define ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_METADATA_LOGGER_H_
28
29#include <list>
30#include <memory>
31#include <queue>
32#include <string>
33#include <thread>
35#include "helper/json/text_to.h"
36#include "mrs/configuration.h"
40
41namespace mrs {
42namespace database {
43
45 public:
47 using LogRecordBuffer = std::queue<LogRecord>;
48
49 void log(const LogRecord &record);
50
51 virtual ~BufferedLogger() {}
52
53 protected:
54 virtual bool flush_records(LogRecordBuffer &records) = 0;
55
56 void start_flush_thread();
57 void stop_flush_thread();
58
59 void flush_thread_run();
60
61 virtual size_t get_buffer_size() const = 0;
63
65 std::mutex mtx_;
66 std::condition_variable buffer_not_full_cv_;
67 std::condition_variable flush_thread_cv_;
68 std::thread flush_thread_;
70
71 uint64_t dropped_logs_{0};
72};
73
75 public:
77
78 struct Options {
79 static constexpr size_t kMinBufferSize = 1;
80 static constexpr size_t kMaxBufferSize = 10'000;
81 static constexpr size_t kDefaultBufferSize = 500;
82 static constexpr auto kMinFlushInterval = std::chrono::seconds(1);
83 static constexpr auto kMaxFlushInterval = std::chrono::seconds(86'400);
84 static constexpr auto kDefaultFlushInterval = std::chrono::seconds(10);
85
86 std::optional<LogLevel> log_level;
87 std::optional<size_t> buffer_size{kDefaultBufferSize};
88 std::optional<std::chrono::seconds> flush_interval{kDefaultFlushInterval};
89
90 bool operator==(const Options &) const = default;
91 };
92
94 void deinit();
95
98
100 const mrs::database::MrsSchemaVersion &schema_ver);
101
104 void stop();
105
107 static MetadataLogger instance_;
108
109 return instance_;
110 }
111
112 protected:
115 }
116
117 size_t get_buffer_size() const override {
119 }
120
122 return logger_options_.flush_interval.value_or(
124 }
125
126 private:
128 bool flush_records(LogRecordBuffer &records) override;
129 void reconfigure(const Options &config);
133 const mrs::database::MrsSchemaVersion &schema_ver);
134
135 // whether the logging to metadata is enabled in the configuration
136 [[nodiscard]] bool is_enabled() const { return initialized_; }
137
138 // log level configured statically
140
141 // current logger configuration
143
144 std::shared_ptr<mysql_harness::logging::ExternalHandler> handler_;
147
148 bool initialized_{false};
149};
150
151} // namespace database
152} // namespace mrs
153
154#endif // ROUTER_SRC_MYSQL_REST_SERVICE_SRC_MRS_DATABASE_METADATA_LOGGER_H_
Definition: mysql_cache_manager.h:87
Definition: configuration.h:49
Definition: metadata_logger.h:44
void stop_flush_thread()
Definition: metadata_logger.cc:111
virtual bool flush_records(LogRecordBuffer &records)=0
virtual ~BufferedLogger()
Definition: metadata_logger.h:51
virtual size_t get_buffer_size() const =0
virtual std::chrono::seconds get_flush_interval() const =0
uint64_t dropped_logs_
Definition: metadata_logger.h:71
void flush_thread_run()
Definition: metadata_logger.cc:121
void start_flush_thread()
Definition: metadata_logger.cc:105
std::queue< LogRecord > LogRecordBuffer
Definition: metadata_logger.h:47
std::mutex mtx_
Definition: metadata_logger.h:65
LogRecordBuffer buffered_records_
Definition: metadata_logger.h:64
bool flush_thread_is_running_
Definition: metadata_logger.h:69
std::condition_variable flush_thread_cv_
Definition: metadata_logger.h:67
std::condition_variable buffer_not_full_cv_
Definition: metadata_logger.h:66
std::thread flush_thread_
Definition: metadata_logger.h:68
void log(const LogRecord &record)
Definition: metadata_logger.cc:82
Definition: metadata_logger.h:74
bool is_enabled() const
Definition: metadata_logger.h:136
bool initialized_
Definition: metadata_logger.h:148
size_t get_buffer_size() const override
Definition: metadata_logger.h:117
void check_dynamic_config(mysqlrouter::MySQLSession *session)
Definition: metadata_logger.cc:284
Options logger_options_
Definition: metadata_logger.h:142
const mrs::Configuration * configuration_
Definition: metadata_logger.h:146
void report_dropped_logs(mysqlrouter::MySQLSession *session)
Definition: metadata_logger.cc:434
void stop()
Definition: metadata_logger.cc:381
static MetadataLogger & instance()
Definition: metadata_logger.h:106
void deinit()
Definition: metadata_logger.cc:275
void start(const mrs::Configuration *configuration, collector::MysqlCacheManager *cache)
Definition: metadata_logger.cc:361
void reconfigure(const Options &config)
Definition: metadata_logger.cc:308
void on_metadata_version_change(const mrs::database::MrsSchemaVersion &schema_ver)
Definition: metadata_logger.cc:344
std::shared_ptr< mysql_harness::logging::ExternalHandler > handler_
Definition: metadata_logger.h:144
void on_metadata_available(const mrs::database::MrsSchemaVersion &schema_ver, mysqlrouter::MySQLSession *session)
Definition: metadata_logger.cc:329
LogLevel static_log_level_
Definition: metadata_logger.h:139
collector::MysqlCacheManager * cache_manager_
Definition: metadata_logger.h:145
void init(mysql_harness::logging::LogLevel log_level)
Definition: metadata_logger.cc:266
LogLevel get_log_level() const
Definition: metadata_logger.h:113
bool check_metadata_version_supported(const mrs::database::MrsSchemaVersion &schema_ver)
Definition: metadata_logger.cc:316
MetadataLogger()
Definition: metadata_logger.cc:264
std::chrono::seconds get_flush_interval() const override
Definition: metadata_logger.h:121
bool flush_records(LogRecordBuffer &records) override
Definition: metadata_logger.cc:389
Definition: mysql_session.h:157
Logging interface for using and extending the logging subsystem.
static int record
Definition: mysqltest.cc:195
static loglevel log_level(const Sql_condition *condition)
Definition: histogram.cc:1650
constexpr pos_type Options
Definition: method.h:48
std::chrono::seconds seconds
Definition: authorize_manager.cc:68
Definition: authorize_manager.h:48
LogLevel
Log level values.
Definition: logging.h:90
required string configuration
Definition: replication_asynchronous_connection_failover.proto:49
Definition: metadata_logger.h:78
static constexpr auto kDefaultFlushInterval
Definition: metadata_logger.h:84
static constexpr size_t kMinBufferSize
Definition: metadata_logger.h:79
std::optional< LogLevel > log_level
Definition: metadata_logger.h:86
bool operator==(const Options &) const =default
static constexpr auto kMinFlushInterval
Definition: metadata_logger.h:82
static constexpr size_t kDefaultBufferSize
Definition: metadata_logger.h:81
static constexpr auto kMaxFlushInterval
Definition: metadata_logger.h:83
static constexpr size_t kMaxBufferSize
Definition: metadata_logger.h:80
std::optional< std::chrono::seconds > flush_interval
Definition: metadata_logger.h:88
std::optional< size_t > buffer_size
Definition: metadata_logger.h:87
Definition: query_version.h:37
Log record containing information collected by the logging system.
Definition: logging.h:182