Skip to content

Commit da09787

Browse files
authored
Moves logging into reader_fsm (#332)
* Removes logging all the reader actions, and logs specific messages inside the reader_fsm instead * Adds constructors to reader actions * Makes reader_fsm use connection_state * Refactors reader_fsm tests * Moves exec_fsm action printing to test code
1 parent f683e36 commit da09787

File tree

7 files changed

+330
-223
lines changed

7 files changed

+330
-223
lines changed

include/boost/redis/connection.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,15 @@ struct reader_op {
245245
public:
246246
reader_op(connection_impl<Executor>& conn) noexcept
247247
: conn_{&conn}
248-
, fsm_{conn.st_.mpx}
249248
{ }
250249

251250
template <class Self>
252251
void operator()(Self& self, system::error_code ec = {}, std::size_t n = 0)
253252
{
254253
for (;;) {
255-
auto act = fsm_.resume(n, ec, self.get_cancellation_state().cancelled());
256-
257-
conn_->st_.logger.on_fsm_resume(act);
254+
auto act = fsm_.resume(conn_->st_, n, ec, self.get_cancellation_state().cancelled());
258255

259256
switch (act.type_) {
260-
case reader_fsm::action::type::needs_more:
261257
case reader_fsm::action::type::read_some:
262258
{
263259
auto const buf = conn_->st_.mpx.get_prepared_read_buffer();

include/boost/redis/detail/connection_logger.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#ifndef BOOST_REDIS_CONNECTION_LOGGER_HPP
88
#define BOOST_REDIS_CONNECTION_LOGGER_HPP
99

10-
#include <boost/redis/detail/reader_fsm.hpp>
1110
#include <boost/redis/logger.hpp>
1211
#include <boost/redis/response.hpp>
1312

@@ -38,7 +37,7 @@ class connection_logger {
3837
void on_connect(system::error_code const& ec, std::string_view unix_socket_ep);
3938
void on_ssl_handshake(system::error_code const& ec);
4039
void on_write(system::error_code const& ec, std::size_t n);
41-
void on_fsm_resume(reader_fsm::action const& action);
40+
void on_read(system::error_code const& ec, std::size_t n);
4241
void on_setup(system::error_code const& ec, generic_response const& resp);
4342
void log(logger::level lvl, std::string_view msg);
4443
void log(logger::level lvl, std::string_view msg1, std::string_view msg2);

include/boost/redis/detail/reader_fsm.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#ifndef BOOST_REDIS_READER_FSM_HPP
88
#define BOOST_REDIS_READER_FSM_HPP
9+
10+
#include <boost/redis/detail/connection_state.hpp>
911
#include <boost/redis/detail/multiplexer.hpp>
1012

1113
#include <boost/asio/cancellation_type.hpp>
@@ -23,27 +25,40 @@ class reader_fsm {
2325
enum class type
2426
{
2527
read_some,
26-
needs_more,
2728
notify_push_receiver,
2829
done,
2930
};
3031

31-
type type_ = type::done;
32-
std::size_t push_size_ = 0u;
33-
system::error_code ec_ = {};
34-
};
32+
action(type t, std::size_t push_size = 0u) noexcept
33+
: type_(t)
34+
, push_size_(push_size)
35+
{ }
3536

36-
explicit reader_fsm(multiplexer& mpx) noexcept;
37+
action(system::error_code ec) noexcept
38+
: type_(type::done)
39+
, ec_(ec)
40+
{ }
41+
42+
static action notify_push_receiver(std::size_t bytes)
43+
{
44+
return {type::notify_push_receiver, bytes};
45+
}
46+
47+
type type_;
48+
std::size_t push_size_{};
49+
system::error_code ec_;
50+
};
3751

3852
action resume(
53+
connection_state& st,
3954
std::size_t bytes_read,
4055
system::error_code ec,
4156
asio::cancellation_type_t cancel_state);
4257

58+
reader_fsm() = default;
59+
4360
private:
4461
int resume_point_{0};
45-
action::type next_read_type_ = action::type::read_some;
46-
multiplexer* mpx_ = nullptr;
4762
std::pair<consume_result, std::size_t> res_{consume_result::needs_more, 0u};
4863
};
4964

include/boost/redis/impl/connection_logger.ipp

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
#include <boost/redis/detail/connection_logger.hpp>
8-
#include <boost/redis/detail/exec_fsm.hpp>
98
#include <boost/redis/logger.hpp>
109

1110
#include <boost/asio/ip/tcp.hpp>
@@ -16,35 +15,6 @@
1615

1716
namespace boost::redis::detail {
1817

19-
#define BOOST_REDIS_READER_SWITCH_CASE(elem) \
20-
case reader_fsm::action::type::elem: return "reader_fsm::action::type::" #elem
21-
22-
#define BOOST_REDIS_EXEC_SWITCH_CASE(elem) \
23-
case exec_action_type::elem: return "exec_action_type::" #elem
24-
25-
auto to_string(reader_fsm::action::type t) noexcept -> char const*
26-
{
27-
switch (t) {
28-
BOOST_REDIS_READER_SWITCH_CASE(read_some);
29-
BOOST_REDIS_READER_SWITCH_CASE(needs_more);
30-
BOOST_REDIS_READER_SWITCH_CASE(notify_push_receiver);
31-
BOOST_REDIS_READER_SWITCH_CASE(done);
32-
default: return "action::type::<invalid type>";
33-
}
34-
}
35-
36-
auto to_string(exec_action_type t) noexcept -> char const*
37-
{
38-
switch (t) {
39-
BOOST_REDIS_EXEC_SWITCH_CASE(setup_cancellation);
40-
BOOST_REDIS_EXEC_SWITCH_CASE(immediate);
41-
BOOST_REDIS_EXEC_SWITCH_CASE(done);
42-
BOOST_REDIS_EXEC_SWITCH_CASE(notify_writer);
43-
BOOST_REDIS_EXEC_SWITCH_CASE(wait_for_response);
44-
default: return "exec_action_type::<invalid type>";
45-
}
46-
}
47-
4818
inline void format_tcp_endpoint(const asio::ip::tcp::endpoint& ep, std::string& to)
4919
{
5020
// This formatting is inspired by Asio's endpoint operator<<
@@ -161,21 +131,20 @@ void connection_logger::on_write(system::error_code const& ec, std::size_t n)
161131
logger_.fn(logger::level::info, msg_);
162132
}
163133

164-
void connection_logger::on_fsm_resume(reader_fsm::action const& action)
134+
void connection_logger::on_read(system::error_code const& ec, std::size_t bytes_read)
165135
{
166136
if (logger_.lvl < logger::level::debug)
167137
return;
168138

169-
std::string msg;
170-
msg += "(";
171-
msg += to_string(action.type_);
172-
msg += ", ";
173-
msg += std::to_string(action.push_size_);
174-
msg += ", ";
175-
msg += action.ec_.message();
176-
msg += ")";
139+
msg_ = "Reader task: ";
140+
msg_ += std::to_string(bytes_read);
141+
msg_ += " bytes read";
142+
if (ec) {
143+
msg_ += ", error: ";
144+
format_error_code(ec, msg_);
145+
}
177146

178-
logger_.fn(logger::level::debug, msg);
147+
logger_.fn(logger::level::debug, msg_);
179148
}
180149

181150
void connection_logger::on_setup(system::error_code const& ec, generic_response const& resp)

include/boost/redis/impl/reader_fsm.ipp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* accompanying file LICENSE.txt)
55
*/
66

7+
#include <boost/redis/detail/connection_state.hpp>
78
#include <boost/redis/detail/coroutine.hpp>
89
#include <boost/redis/detail/multiplexer.hpp>
910
#include <boost/redis/detail/reader_fsm.hpp>
@@ -14,11 +15,8 @@
1415

1516
namespace boost::redis::detail {
1617

17-
reader_fsm::reader_fsm(multiplexer& mpx) noexcept
18-
: mpx_{&mpx}
19-
{ }
20-
2118
reader_fsm::action reader_fsm::resume(
19+
connection_state& st,
2220
std::size_t bytes_read,
2321
system::error_code ec,
2422
asio::cancellation_type_t cancel_state)
@@ -28,53 +26,64 @@ reader_fsm::action reader_fsm::resume(
2826

2927
for (;;) {
3028
// Prepare the buffer for the read operation
31-
ec = mpx_->prepare_read();
29+
ec = st.mpx.prepare_read();
3230
if (ec) {
33-
return {action::type::done, 0, ec};
31+
st.logger.trace("Reader task: error in prepare_read", ec);
32+
return {ec};
3433
}
3534

3635
// Read
37-
BOOST_REDIS_YIELD(resume_point_, 1, next_read_type_)
36+
st.logger.trace("Reader task: issuing read");
37+
BOOST_REDIS_YIELD(resume_point_, 1, action::type::read_some)
38+
39+
// Check for cancellations
40+
if (is_terminal_cancel(cancel_state)) {
41+
st.logger.trace("Reader task: cancelled (1)");
42+
return {asio::error::operation_aborted};
43+
}
44+
45+
// Log what we read
46+
st.logger.on_read(ec, bytes_read);
3847

3948
// Process the bytes read, even if there was an error
40-
mpx_->commit_read(bytes_read);
49+
st.mpx.commit_read(bytes_read);
4150

4251
// Check for read errors
4352
if (ec) {
4453
// TODO: If an error occurred but data was read (i.e.
4554
// bytes_read != 0) we should try to process that data and
4655
// deliver it to the user before calling cancel_run.
47-
return {action::type::done, bytes_read, ec};
48-
}
49-
50-
// Check for cancellations
51-
if (is_terminal_cancel(cancel_state)) {
52-
return {action::type::done, 0u, asio::error::operation_aborted};
56+
return {ec};
5357
}
5458

5559
// Process the data that we've read
56-
next_read_type_ = action::type::read_some;
57-
while (mpx_->get_read_buffer_size() != 0) {
58-
res_ = mpx_->consume(ec);
60+
while (st.mpx.get_read_buffer_size() != 0) {
61+
res_ = st.mpx.consume(ec);
5962

6063
if (ec) {
6164
// TODO: Perhaps log what has not been consumed to aid
6265
// debugging.
63-
return {action::type::done, res_.second, ec};
66+
st.logger.trace("Reader task: error processing message", ec);
67+
return {ec};
6468
}
6569

6670
if (res_.first == consume_result::needs_more) {
67-
next_read_type_ = action::type::needs_more;
71+
st.logger.trace("Reader task: incomplete message received");
6872
break;
6973
}
7074

7175
if (res_.first == consume_result::got_push) {
72-
BOOST_REDIS_YIELD(resume_point_, 2, action::type::notify_push_receiver, res_.second)
73-
if (ec) {
74-
return {action::type::done, 0u, ec};
75-
}
76+
BOOST_REDIS_YIELD(resume_point_, 2, action::notify_push_receiver(res_.second))
77+
// Check for cancellations
7678
if (is_terminal_cancel(cancel_state)) {
77-
return {action::type::done, 0u, asio::error::operation_aborted};
79+
st.logger.trace("Reader task: cancelled (2)");
80+
return {asio::error::operation_aborted};
81+
}
82+
83+
// Check for other errors
84+
if (ec) {
85+
st.logger.trace("Reader task: error notifying push receiver", ec);
86+
return {ec};
7887
}
7988
} else {
8089
// TODO: Here we should notify the exec operation that
@@ -89,7 +98,7 @@ reader_fsm::action reader_fsm::resume(
8998
}
9099

91100
BOOST_ASSERT(false);
92-
return {action::type::done, 0, system::error_code()};
101+
return {system::error_code()};
93102
}
94103

95104
} // namespace boost::redis::detail

test/test_exec_fsm.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
#include <boost/asio/cancellation_type.hpp>
1414
#include <boost/asio/error.hpp>
15+
#include <boost/assert.hpp>
1516
#include <boost/core/lightweight_test.hpp>
1617
#include <boost/system/error_code.hpp>
17-
#include <boost/assert.hpp>
18+
19+
#include "sansio_utils.hpp"
1820

1921
#include <cstddef>
2022
#include <memory>
2123
#include <ostream>
2224
#include <utility>
2325

24-
#include "sansio_utils.hpp"
25-
2626
using namespace boost::redis;
2727
namespace asio = boost::asio;
2828
using detail::exec_fsm;
@@ -33,11 +33,24 @@ using detail::exec_action;
3333
using boost::system::error_code;
3434
using boost::asio::cancellation_type_t;
3535

36+
#define BOOST_REDIS_EXEC_SWITCH_CASE(elem) \
37+
case exec_action_type::elem: return "exec_action_type::" #elem
38+
39+
static auto to_string(exec_action_type t) noexcept -> char const*
40+
{
41+
switch (t) {
42+
BOOST_REDIS_EXEC_SWITCH_CASE(setup_cancellation);
43+
BOOST_REDIS_EXEC_SWITCH_CASE(immediate);
44+
BOOST_REDIS_EXEC_SWITCH_CASE(done);
45+
BOOST_REDIS_EXEC_SWITCH_CASE(notify_writer);
46+
BOOST_REDIS_EXEC_SWITCH_CASE(wait_for_response);
47+
default: return "exec_action_type::<invalid type>";
48+
}
49+
}
50+
3651
// Operators
3752
namespace boost::redis::detail {
3853

39-
extern auto to_string(exec_action_type t) noexcept -> char const*;
40-
4154
std::ostream& operator<<(std::ostream& os, exec_action_type type)
4255
{
4356
os << to_string(type);

0 commit comments

Comments
 (0)