Skip to content
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

Feat: add support of READONLY and READWRITE #2173

Merged
merged 7 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
3 changes: 2 additions & 1 deletion src/cluster/cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,8 @@ Status Cluster::CanExecByMySelf(const redis::CommandAttributes *attributes, cons
}

if (myself_ && myself_->role == kClusterSlave && !(attributes->flags & redis::kCmdWrite) &&
nodes_.find(myself_->master_id) != nodes_.end() && nodes_[myself_->master_id] == slots_nodes_[slot]) {
nodes_.find(myself_->master_id) != nodes_.end() && nodes_[myself_->master_id] == slots_nodes_[slot] &&
this->cluster_mode_ == Cluster::ClusterMode::READONLY) {
return Status::OK(); // My master is serving this slot
}

Expand Down
9 changes: 9 additions & 0 deletions src/cluster/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class SyncMigrateContext;

class Cluster {
public:
enum class ClusterMode { READONLY, READWRITE };

explicit Cluster(Server *srv, std::vector<std::string> binds, int port);
Status SetClusterNodes(const std::string &nodes_str, int64_t version, bool force);
Status GetClusterNodes(std::string *nodes_str);
Expand All @@ -92,6 +94,11 @@ class Cluster {

static bool SubCommandIsExecExclusive(const std::string &subcommand);

Status SetClusterMode(ClusterMode mode) {
cluster_mode_ = mode;
return Status::OK();
}

private:
std::string genNodesDescription();
std::string genNodesInfo();
Expand All @@ -111,4 +118,6 @@ class Cluster {

std::map<int, std::string> migrated_slots_;
std::set<int> imported_slots_;

ClusterMode cluster_mode_{ClusterMode::READONLY};
LiuYuHui marked this conversation as resolved.
Show resolved Hide resolved
};
21 changes: 19 additions & 2 deletions src/commands/cmd_cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,25 @@ static uint64_t GenerateClusterFlag(const std::vector<std::string> &args) {
return 0;
}

class CommandReadOnly : public Commander {
public:
Status Execute(Server *srv, Connection *conn, std::string *output) override {
*output = redis::SimpleString("READONLY");
LiuYuHui marked this conversation as resolved.
Show resolved Hide resolved
return srv->cluster->SetClusterMode(Cluster::ClusterMode::READONLY);
}
};

class CommandReadWrite : public Commander {
public:
Status Execute(Server *srv, Connection *conn, std::string *output) override {
*output = redis::SimpleString("READWRITE");
LiuYuHui marked this conversation as resolved.
Show resolved Hide resolved
return srv->cluster->SetClusterMode(Cluster::ClusterMode::READWRITE);
}
};

REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandCluster>("cluster", -2, "cluster no-script", 0, 0, 0, GenerateClusterFlag),
MakeCmdAttr<CommandClusterX>("clusterx", -2, "cluster no-script", 0, 0, 0,
GenerateClusterFlag), )
MakeCmdAttr<CommandClusterX>("clusterx", -2, "cluster no-script", 0, 0, 0, GenerateClusterFlag),
MakeCmdAttr<CommandReadOnly>("readonly", 1, "cluster no-multi", 0, 0, 0),
MakeCmdAttr<CommandReadWrite>("readwrite", 1, "cluster no-multi", 0, 0, 0), )

} // namespace redis
14 changes: 14 additions & 0 deletions tests/gocase/integration/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,18 @@ func TestClusterMultiple(t *testing.T) {
require.ErrorContains(t, rdb[1].Do(ctx, "EXEC").Err(), "EXECABORT")
require.Equal(t, "no-multi", rdb[1].Get(ctx, util.SlotTable[0]).Val())
})

t.Run("requests on cluster are ok when enable readonly", func(t *testing.T) {

require.NoError(t, rdb[3].Do(ctx, "READONLY").Err())
require.NoError(t, rdb[2].Set(ctx, util.SlotTable[8192], 8192, 0).Err())
util.WaitForOffsetSync(t, rdb[2], rdb[3])
// request node3 that serves slot 8192, that's ok
require.Equal(t, "8192", rdb[3].Get(ctx, util.SlotTable[8192]).Val())

require.NoError(t, rdb[3].Do(ctx, "READWRITE").Err())

// when enable READWRITE, request node3 that serves slot 8192, that's not ok
util.ErrorRegexp(t, rdb[3].Get(ctx, util.SlotTable[8192]).Err(), fmt.Sprintf("MOVED 8192.*%d.*", srv[2].Port()))
})
}