diff --git a/include/maxscale/backend.hh b/include/maxscale/backend.hh index 078c3cc09..262f7beef 100644 --- a/include/maxscale/backend.hh +++ b/include/maxscale/backend.hh @@ -20,6 +20,7 @@ #include #include +#include namespace maxscale @@ -323,6 +324,12 @@ public: return m_uri.c_str(); } + void select_started(); + void select_ended(); + + int64_t num_selects() const; + const maxbase::StopWatch& session_timer() const; + const maxbase::IntervalTimer& select_timer() const; private: /** * Internal state of the backend @@ -357,6 +364,10 @@ private: SessionCommandList m_session_commands; /**< List of session commands that are * to be executed on this backend server */ std::string m_uri; /**< The combined address and port */ + + maxbase::StopWatch m_session_timer; + maxbase::IntervalTimer m_select_timer; + int64_t m_num_selects = 0; }; typedef std::shared_ptr SBackend; diff --git a/include/maxscale/session_stats.hh b/include/maxscale/session_stats.hh new file mode 100644 index 000000000..d5c9984d1 --- /dev/null +++ b/include/maxscale/session_stats.hh @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ +#pragma once + +#include + + +#include + +#include +#include +#include + +namespace maxscale +{ + +/** ServerStats is a class to hold server statistics associated with a router */ +class ServerStats +{ + +public: + struct CurrentStats + { + maxbase::Duration ave_session_dur; + double ave_session_active_pct; + int64_t ave_session_selects; + int64_t total_queries; + int64_t total_read_queries; + int64_t total_write_queries; + }; + + void start_session(); + void end_session(maxbase::Duration sess_duration, maxbase::Duration active_duration, int64_t num_selects); + + CurrentStats current_stats() const; + + ServerStats& operator+=(const ServerStats& rhs); + + // These are exactly what they were in struct ServerStats, in readwritesplit.hh. + // (well, but I changed uint64_t to int64_t). + // TODO make these private, and add functions to modify them. + int64_t total = 0; + int64_t read = 0; + int64_t write = 0; + +private: + maxbase::CumulativeAverage m_ave_session_dur; + maxbase::CumulativeAverage m_ave_active_dur; + maxbase::CumulativeAverage m_num_ave_session_selects; +}; + +using SrvStatMap = std::map; +} diff --git a/server/core/backend.cc b/server/core/backend.cc index 2d8f93681..f8ee909c3 100644 --- a/server/core/backend.cc +++ b/server/core/backend.cc @@ -259,3 +259,29 @@ bool Backend::write_stored_command() return rval; } + +const maxbase::StopWatch& Backend::session_timer() const +{ + return m_session_timer; +} + +const maxbase::IntervalTimer& Backend::select_timer() const +{ + return m_select_timer; +} + +void Backend::select_started() +{ + m_select_timer.start_interval(); +} + +void Backend::select_ended() +{ + m_select_timer.end_interval(); + ++m_num_selects; +} + +int64_t Backend::num_selects() const +{ + return m_num_selects; +} diff --git a/server/core/session_stats.cc b/server/core/session_stats.cc new file mode 100644 index 000000000..0a813b513 --- /dev/null +++ b/server/core/session_stats.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2022-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include +#include + +void maxscale::ServerStats::start_session() +{ + // nop for now +} + +void maxscale::ServerStats::end_session(maxbase::Duration sess_duration, + maxbase::Duration active_duration, + int64_t num_selects) +{ + m_ave_session_dur.add(sess_duration.secs()); + m_ave_active_dur.add(active_duration.secs()); + m_num_ave_session_selects.add(num_selects); +} + +maxscale::ServerStats& maxscale::ServerStats::operator+=(const maxscale::ServerStats& rhs) +{ + total += rhs.total; + read += rhs.read; + write += rhs.write; + m_ave_session_dur += rhs.m_ave_session_dur; + m_ave_active_dur += rhs.m_ave_active_dur; + m_num_ave_session_selects += rhs.m_num_ave_session_selects; + + return *this; +} + +maxscale::ServerStats::CurrentStats maxscale::ServerStats::current_stats() const +{ + double sess_secs = m_ave_session_dur.average(); + double active_secs = m_ave_active_dur.average(); + double active = 100 * active_secs / sess_secs; + + return {maxbase::Duration(sess_secs), + active, + static_cast(m_num_ave_session_selects.average()), + total, + read, + write}; +}