forked from dragonflydb/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): support monitor command - user can set connection to
monitor all commands running on all other connections (dragonflydb#344). Signed-off-by: Boaz Sade <[email protected]>
- Loading branch information
Showing
10 changed files
with
103 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,14 +88,16 @@ std::string MonitorTimestamp() { | |
} | ||
|
||
auto CmdEntryToMonitorFormat(std::string_view str) -> std::string { | ||
// This code is based on Redis impl for it at [email protected] | ||
std::string result = absl::StrCat("\""); | ||
|
||
for (auto c : str) { | ||
switch (c) { | ||
case '\\': | ||
absl::StrAppend(&result, "\\\\"); | ||
break; | ||
case '"': | ||
absl::StrAppend(&result, "\\"); | ||
result += c; | ||
absl::StrAppend(&result, "\\\""); | ||
break; | ||
case '\n': | ||
absl::StrAppend(&result, "\\n"); | ||
|
@@ -116,7 +118,7 @@ auto CmdEntryToMonitorFormat(std::string_view str) -> std::string { | |
if (isprint(c)) { | ||
result += c; | ||
} else { | ||
absl::StrAppend(&result, "\\x", absl::Hex((unsigned char)c, absl::kSpacePad2)); | ||
absl::StrAppend(&result, "\\x", absl::Hex((unsigned char)c, absl::kZeroPad2)); | ||
} | ||
break; | ||
} | ||
|
@@ -162,15 +164,17 @@ void MonitorCmd(bool admin_cmd, ConnectionContext* connection, CmdArgList args) | |
// enabled on them - see https://redis.io/commands/monitor/ | ||
const auto& my_monitors = ServerState::tlocal()->Monitors(); | ||
if (!(my_monitors.Empty() || admin_cmd)) { | ||
util::fibers_ext::BlockingCounter bc(my_monitors.Size()); | ||
// We have connections waiting to get the info on the last command, send it to them | ||
auto monitor_msg = MakeMonitorMessage(connection->conn_state, connection->owner(), args); | ||
LOG(WARNING) << "sending command '" << monitor_msg << "' to the clients that registered on it"; | ||
shard_set->pool()->Await([&monitor_msg, bc](unsigned idx, util::ProactorBase*) { | ||
ServerState::tlocal()->Monitors().Send(monitor_msg, bc, idx); | ||
// Note that this is accepted by value for lifetime reasons | ||
// we want to have our own copy since we are assuming that | ||
// 1. there will be not to many connections that we in monitor state | ||
// 2. we need to have for each of them each own copy for thread safe reasons | ||
VLOG(1) << "sending command '" << monitor_msg << "' to the clients that registered on it"; | ||
shard_set->pool()->Await([monitor_msg](unsigned idx, util::ProactorBase*) { | ||
ServerState::tlocal()->Monitors().Send(monitor_msg, idx); | ||
}); | ||
bc.Wait(); // Wait for all the messages to be sent. | ||
// we need to un-borrow what we used, do this here | ||
|
||
shard_set->pool()->Await( | ||
[](unsigned idx, util::ProactorBase*) { ServerState::tlocal()->Monitors().Release(idx); }); | ||
} | ||
|
@@ -1090,7 +1094,8 @@ bool CheckWatchedKeyExpiry(ConnectionContext* cntx, const CommandRegistry& regis | |
|
||
// The comparison can still be true even if a key expired due to another one being created. | ||
// So we have to check the watched_dirty flag, which is set if a key expired. | ||
return watch_exist_count.load() == exec_info.watched_existed && !exec_info.watched_dirty.load(memory_order_relaxed); | ||
return watch_exist_count.load() == exec_info.watched_existed && | ||
!exec_info.watched_dirty.load(memory_order_relaxed); | ||
} | ||
|
||
// Check if exec_info watches keys on dbs other than db_indx. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters