sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

Command "CLUSTER SHARDS" #458

Closed mlanzuisi closed 1 year ago

mlanzuisi commented 1 year ago

Hello,

I tried sending a generic command to a single server of a Redis Cluster (instance 0 of an array) with the following code:

std::vector val = m_redis_servers[0]->command<std::vector>("CLUSTER", "SHARDS");

copying from examples, but I receive this error:

terminate called after throwing an instance of 'sw::redis::ParseError' what(): expect STRING or STATUS or VERB or BIGNUM reply, but got ARRAY reply

and if I try using the std::array structure it is not compiling. Did I do something wrong?

Environment:

sewenew commented 1 year ago

The code you posted cannot compile, i.e. not specifying template type for std::vector please give the exact code you used. Also please give the Redis server info, e.g. version. Since different version might return different shards info.

Regards

mlanzuisi commented 1 year ago

Sorry, I was making tests and I used the wrong copy/paste. This is the correct code:

std::vector\<OptionalString> val = m_redis_servers[0]->command\<std::vector\<OptionalString>>("CLUSTER", "SHARDS");

that returns the error. Here the redis server info

Server

redis_version:7.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:a7db51f3965d7ff7 redis_mode:cluster os:Linux 3.10.0-1160.88.1.el7.x86_64 x86_64

sewenew commented 1 year ago

CLUSTER SHARDS returns a nested array reply, instead of an array of strings. So you need to call the command with a template parameter that can hold the nested array.

using SlotInfo = std::pair<std::string, std::string>;
using NodeInfo = std::unordered_map<std::string, std::variant<std::string, long long>>;
using Result = std::vector<std::variant<std::string, std::vector<NodeInfo>, SlotInfo>>;
auto result = r.command<std::vector<Result>>("CLUSTER", "SHARDS");

Check cluster shards doc, c++ std::variant, and redis-plus-plus doc for more info.

Regards

sewenew commented 1 year ago

Since there's no update, I'll close the issue

Regards

mlanzuisi commented 1 year ago

I could not test it so far, so I didn't add anything, Thank you so much for the answer.