-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reimplement the ‘monitor' command by delete its thread #1551
reimplement the ‘monitor' command by delete its thread #1551
Conversation
@@ -411,7 +411,8 @@ class PikaServer { | |||
/* | |||
* Monitor used | |||
*/ | |||
std::unique_ptr<PikaMonitorThread> pika_monitor_thread_; | |||
mutable pstd::Mutex monitor_mutex_protector_; | |||
std::set<std::weak_ptr<PikaClientConn>, std::owner_less<std::weak_ptr<PikaClientConn>>> pika_monitor_clients_; | |||
|
|||
/* | |||
* Rsync used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch removes the PikaMonitorThread
class and adds a std::set
data member to keep track of monitor clients. The HasMonitorClients()
method is also updated to be marked const
. Additionally, a mutex is added to protect access to the pika_monitor_clients_
set.
It's difficult to provide more detailed feedback without seeing the entirety of the codebase and how this change fits into it. However, some suggestions for further improvement could include verifying that the changes have been thoroughly tested, and possibly implementing move semantics for the PikaClientConn
type to improve performance when inserting elements into the std::set
.
return; // Monitor thread will return "OK" | ||
|
||
g_pika_server->AddMonitorClient(std::dynamic_pointer_cast<PikaClientConn>(conn_repl)); | ||
res_.SetRes(CmdRes::kOk); | ||
} | ||
|
||
void DbsizeCmd::DoInitial() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that a block of code has been removed in the MonitorCmd::Do
function that was responsible for moving the connection from one thread to another thread. It would be better to review why this change was made and if it has any impact on the functionality.
Also, the current implementation assumes that the cast std::dynamic_pointer_cast<PikaClientConn>(conn_repl)
always succeeds, so it might be worth adding some additional checks or error handling logic.
Regarding the DbsizeCmd::DoInitial
function, nothing stands out as problematic or in need of improvement based on the provided code patch.
} | ||
|
||
void PikaServer::AddMonitorClient(std::shared_ptr<PikaClientConn> client_ptr) { | ||
pika_monitor_thread_->AddMonitorClient(client_ptr); | ||
std::unique_lock lock(monitor_mutex_protector_); | ||
pika_monitor_clients_.insert(client_ptr); | ||
} | ||
|
||
void PikaServer::SlowlogTrim() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch removes the PikaMonitorThread
object and its associated functions, replacing it with a new approach to broadcast monitor messages to connected clients.
Improvement suggestions:
- Add comments explaining the purpose of each function.
- Use
std::shared_ptr
instead of raw pointers to manage memory inAddMonitorClient
. - Consider adding error handle for the
std::unique_lock
calls inAddMonitorClient
,HasMonitorClients
, andAddMonitorMessage
. - Consider adding a timeout for the monitor message delivery mechanism to avoid blocking in case of client disconnections or network issues.
- The code seems to have removed some
pika_monitor_thread_
related functionalities but did not update some of the functions called byPikaServer
. Ensure any unnecessary elements are removed from the code.
@@ -411,7 +411,8 @@ class PikaServer { | |||
/* | |||
* Monitor used | |||
*/ | |||
std::unique_ptr<PikaMonitorThread> pika_monitor_thread_; | |||
mutable pstd::Mutex monitor_mutex_protector_; | |||
std::set<std::weak_ptr<PikaClientConn>, std::owner_less<std::weak_ptr<PikaClientConn>>> pika_monitor_clients_; | |||
|
|||
/* | |||
* Rsync used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch adds a std::set
to keep track of monitor clients and replaces the non-const bool HasMonitorClients()
function with a const version. Additionally, a new mutex called monitor_mutex_protector_
is added to protect the monitor thread and its clients.
Overall, the changes seem reasonable and should not introduce any bugs. However, it would be useful to see the PikaMonitorThread
class to ensure that the modifications are consistent with the overall design.
A minor improvement suggestion would be to use std::shared_ptr
instead of std::weak_ptr
in pika_monitor_clients_
, which would prevent dangling pointer issues if the client object is destroyed while still being accessed from the set. While the owner_less
is used to sort std::weak_ptr
, it only guarantees strict weak ordering among expired pointers, but does not check for expiring pointers when inserting/deleting, making "use-after-free" bugs possible.
return; // Monitor thread will return "OK" | ||
|
||
g_pika_server->AddMonitorClient(std::dynamic_pointer_cast<PikaClientConn>(conn_repl)); | ||
res_.SetRes(CmdRes::kOk); | ||
} | ||
|
||
void DbsizeCmd::DoInitial() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the patch removed some code related to moving a network connection out of a server thread, replacing it with directly adding a client to the monitor and setting a response to "OK". As long as it is intended to remove that specific functionality, there shouldn't be any bug risks. However, without knowing the context of the application, it's hard to suggest any improvements.
if (client_ptr) { | ||
std::unique_lock lock(monitor_mutex_protector_); | ||
pika_monitor_clients_.insert(client_ptr); | ||
} | ||
} | ||
|
||
void PikaServer::SlowlogTrim() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch removes the pika_monitor_thread_
member variable and related functions, and instead introduces a monitor_mutex_protector_
and uses a new container pika_monitor_clients_
to store monitor clients.
This change could potentially reduce complexity and improve thread safety. However, it is impossible to assess bug risk without knowledge of the broader system context and testing.
One improvement suggestion would be to use std::shared_ptr
instead of raw pointers or weak_ptr when managing client connections, as this could improve memory management and simplify the code.
@@ -411,7 +411,8 @@ class PikaServer { | |||
/* | |||
* Monitor used | |||
*/ | |||
std::unique_ptr<PikaMonitorThread> pika_monitor_thread_; | |||
mutable pstd::Mutex monitor_mutex_protector_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is better to use RWLock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make no difference.
src/pika_server.cc
Outdated
auto cli = (*it).lock(); | ||
if (cli) { | ||
cli->WriteResp(msg); | ||
auto status = cli->SendReply(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's possible SendReply not in lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it's a improvement if there exists too many monitor clients, fix it now.
@@ -411,7 +411,8 @@ class PikaServer { | |||
/* | |||
* Monitor used | |||
*/ | |||
std::unique_ptr<PikaMonitorThread> pika_monitor_thread_; | |||
mutable pstd::Mutex monitor_mutex_protector_; | |||
std::set<std::weak_ptr<PikaClientConn>, std::owner_less<std::weak_ptr<PikaClientConn>>> pika_monitor_clients_; | |||
|
|||
/* | |||
* Rsync used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch includes adding a set of monitor clients to PikaServer class, replacing the old implementation using PikaMonitorThread. The new implementation uses a mutex to protect the access to the set, and the set contains weak pointers to PikaClientConn objects instead of shared pointers. No bug risk is apparent from this patch.
A suggestion for improvement is to use a shared_mutex instead of a Mutex for monitor_mutex_protector_, as multiple read accesses can be made safely and concurrently.
return; // Monitor thread will return "OK" | ||
|
||
g_pika_server->AddMonitorClient(std::dynamic_pointer_cast<PikaClientConn>(conn_repl)); | ||
res_.SetRes(CmdRes::kOk); | ||
} | ||
|
||
void DbsizeCmd::DoInitial() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code patch you provided removes some code that moves a connection object from one thread to another and replaces it with a simpler approach of adding the connection directly to the monitor client list. This seems like a reasonable simplification if it meets the requirements of the application.
One potential improvement could be to add error handling for the case where conn_repl
is not actually an instance of PikaClientConn
. You could add a check before casting it to PikaClientConn
to avoid a potential runtime error.
For the DbsizeCmd
function, no changes are shown, so it's unclear what the purpose of this function is. It may be worthwhile to review this function as well, especially if it relates to the functionality being changed in the MonitorCmd
function.
if (client_ptr) { | ||
std::unique_lock lock(monitor_mutex_protector_); | ||
pika_monitor_clients_.insert(client_ptr); | ||
} | ||
} | ||
|
||
void PikaServer::SlowlogTrim() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code review suggests the following:
- Removing
pika_monitor_thread_
object creation as it is no longer being used. - In
ClientKillAll()
, remove the call topika_monitor_thread_->ThreadClientKill()
since the object is being removed and this function is no longer needed. - In
ClientKill()
, remove the call topika_monitor_thread_->ThreadClientKill(ip_port)
since the object is being removed and this function is no longer needed. - In
ClientList()
, remove the call topika_monitor_thread_->ThreadClientList(clients)
since the object is being removed and this function is no longer needed. - Replace
pika_monitor_thread_->HasMonitorClients()
with a new implementationHasMonitorClients()
that uses a mutex-protected set ofpika_monitor_clients_
to check if there are any monitor clients connected. Lock the mutex before accessing the set. - Add a
monitor_mutex_protector_
mutex to protect access to thepika_monitor_clients_
set. - In
AddMonitorMessage()
, send the message to all connected monitor clients by iterating over thepika_monitor_clients_
set, passing each client the message as a string, constructing a+
-prefixed,CR LF
-terminated Redis protocol message format. Lock themonitor_mutex_protector_
before iterating over the set. - In
AddMonitorClient()
, add the givenclient_ptr
to thepika_monitor_clients_
set. Lock themonitor_mutex_protector_
before adding the client.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.