Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
15640b6
feat(command): add 'reply' subcommand to control reply mode in Comman…
DCjanus May 6, 2025
72f9a29
refactor(reply): replace SKIP mode with skip next reply functionality…
DCjanus May 6, 2025
4b269be
Merge branch 'apache:unstable' into unstable
DCjanus May 7, 2025
4687b86
refactor(reply): update reply mode handling to use enum values for cl…
DCjanus May 7, 2025
f9968ca
refactor(reply): rename reply modes for better clarity and consistency
DCjanus May 7, 2025
6864380
test(reply): add tests for CLIENT REPLY mode switching behavior
DCjanus May 7, 2025
2af9f7a
test(reply): enhance tests for CLIENT REPLY mode with additional ECHO…
DCjanus May 7, 2025
b4e8655
refactor(reply): improve comment formatting for reply modes in redis_…
DCjanus May 7, 2025
1278cee
test(reply): update MustRead assertions to use MustReadBulkString for…
DCjanus May 7, 2025
2e31c0e
fix(test): remove unnecessary quotes in MustReadBulkString assertions…
DCjanus May 7, 2025
e45a4e6
Merge branch 'apache:unstable' into unstable
DCjanus May 7, 2025
1ba4efa
Merge branch 'unstable' into unstable
DCjanus May 8, 2025
f958e8b
Merge branch 'unstable' into unstable
PragmaTwice May 8, 2025
d20a56b
Merge remote-tracking branch 'upstream/unstable' into unstable
DCjanus May 8, 2025
ca8f0af
refactor(cmd_server): simplify reply mode handling in CommandClient
DCjanus May 8, 2025
01f38e5
refactor(redis_connection): unify reply mode handling by removing SKI…
DCjanus May 8, 2025
aadc678
Merge branch 'unstable' into unstable
PragmaTwice May 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class CommandClient : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
subcommand_ = util::ToLower(args[1]);
// subcommand: getname id kill list info setname
// subcommand: getname id kill list info setname reply
if ((subcommand_ == "id" || subcommand_ == "getname" || subcommand_ == "list" || subcommand_ == "info") &&
args.size() == 2) {
return Status::OK();
Expand All @@ -412,6 +412,23 @@ class CommandClient : public Commander {
return Status::OK();
}

if (subcommand_ == "reply") {
if (args.size() != 3) {
return {Status::RedisParseErr, errInvalidSyntax};
}
auto mode_str = util::ToLower(args[2]);
if (mode_str == "on") {
reply_mode_ = redis::Connection::ReplyMode::ON;
} else if (mode_str == "off") {
reply_mode_ = redis::Connection::ReplyMode::OFF;
} else if (mode_str == "skip") {
reply_mode_ = redis::Connection::ReplyMode::SKIP;
} else {
return {Status::RedisParseErr, errInvalidSyntax};
}
return Status::OK();
}

if ((subcommand_ == "kill")) {
if (args.size() == 2) {
return {Status::RedisParseErr, errInvalidSyntax};
Expand Down Expand Up @@ -464,7 +481,7 @@ class CommandClient : public Commander {
}
return Status::OK();
}
return {Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|INFO|KILL ip:port|GETNAME|SETNAME"};
return {Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|INFO|KILL ip:port|GETNAME|SETNAME|REPLY"};
}

Status Execute([[maybe_unused]] engine::Context &ctx, Server *srv, Connection *conn, std::string *output) override {
Expand Down Expand Up @@ -497,15 +514,22 @@ class CommandClient : public Commander {
*output = redis::RESP_OK;
}
return Status::OK();
} else if (subcommand_ == "reply") {
conn->SetReplyMode(reply_mode_);
if (reply_mode_ != redis::Connection::ReplyMode::SKIP) {
*output = redis::RESP_OK;
}
return Status::OK();
}

return {Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|INFO|KILL ip:port|GETNAME|SETNAME"};
return {Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|INFO|KILL ip:port|GETNAME|SETNAME|REPLY"};
}

private:
std::string addr_;
std::string conn_name_;
std::string subcommand_;
redis::Connection::ReplyMode reply_mode_ = redis::Connection::ReplyMode::ON;
bool skipme_ = false;
int64_t kill_type_ = 0;
uint64_t id_ = 0;
Expand Down
7 changes: 7 additions & 0 deletions src/server/redis_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ void Connection::OnEvent(bufferevent *bev, int16_t events) {
}

void Connection::Reply(const std::string &msg) {
if (reply_mode_ == ReplyMode::SKIP) {
reply_mode_ = ReplyMode::ON;
return;
}
if (reply_mode_ == ReplyMode::OFF) {
return;
}
owner_->srv->stats.IncrOutboundBytes(msg.size());
redis::Reply(bufferevent_get_output(bev_), msg);
}
Expand Down
12 changes: 12 additions & 0 deletions src/server/redis_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ class Connection : public EvbufCallbackBase<Connection> {
kAsking = 1 << 10,
};

enum class ReplyMode {
ON, // Always reply to every command (default)
OFF, // Never reply to any command
SKIP // Skip reply for the next command, then automatically switch back to ON
};

explicit Connection(bufferevent *bev, Worker *owner);
~Connection();

Expand Down Expand Up @@ -181,6 +187,10 @@ class Connection : public EvbufCallbackBase<Connection> {
std::set<std::string> watched_keys;
std::atomic<bool> watched_keys_modified = false;

// Reply mode getter/setter
void SetReplyMode(ReplyMode mode) { reply_mode_ = mode; }
ReplyMode GetReplyMode() const { return reply_mode_; }

private:
uint64_t id_ = 0;
std::atomic<int> flags_ = 0;
Expand Down Expand Up @@ -215,6 +225,8 @@ class Connection : public EvbufCallbackBase<Connection> {

bool importing_ = false;
RESP protocol_version_ = RESP::v2;

ReplyMode reply_mode_ = ReplyMode::ON;
};

} // namespace redis
34 changes: 34 additions & 0 deletions tests/gocase/unit/introspection/introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,40 @@ func TestIntrospection(t *testing.T) {
require.NoError(t, rdb.Do(ctx, "SET", "key", "value").Err())
require.EqualValues(t, 1, rdb.Do(ctx, "MOVE", "key", "0").Val())
})

// Test CLIENT REPLY subcommand behaviors
t.Run("CLIENT REPLY mode switching", func(t *testing.T) {
c := srv.NewTCPClient()
defer func() { require.NoError(t, c.Close()) }()

// Should reply by default
require.NoError(t, c.WriteArgs("ECHO", "default"))
c.MustReadBulkString(t, "default")

// Set to OFF, following commands should not reply
require.NoError(t, c.WriteArgs("CLIENT", "REPLY", "OFF"))
require.NoError(t, c.WriteArgs("ECHO", "off"))
// No reply expected here, do not read

// Set back to ON, commands should reply again
require.NoError(t, c.WriteArgs("CLIENT", "REPLY", "ON"))
c.MustRead(t, "+OK")
require.NoError(t, c.WriteArgs("ECHO", "on"))
c.MustReadBulkString(t, "on")

// Set to SKIP, next command should not reply, then reply resumes
require.NoError(t, c.WriteArgs("CLIENT", "REPLY", "SKIP"))
// No reply expected here, do not read

require.NoError(t, c.WriteArgs("ECHO", "skip1"))
// No reply expected here, do not read

require.NoError(t, c.WriteArgs("ECHO", "skip2"))
c.MustReadBulkString(t, "skip2")

require.NoError(t, c.WriteArgs("ECHO", "skip3"))
c.MustReadBulkString(t, "skip3")
})
}

func TestMultiServerIntrospection(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions tests/gocase/util/tcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func (c *TCPClient) MustReadStrings(t testing.TB, s []string) {
}
}

func (c *TCPClient) MustReadBulkString(t testing.TB, s string) {
r, err := c.ReadLine()
require.NoError(t, err)
require.Equal(t, "$"+strconv.Itoa(len(s)), r)

r, err = c.ReadLine()
require.NoError(t, err)
require.Equal(t, s, r)
}

func (c *TCPClient) MustReadStringsWithKey(t testing.TB, key string, s []string) {
r, err := c.ReadLine()
require.NoError(t, err)
Expand Down
Loading