sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.6k stars 347 forks source link

[FEATURE] update slot mapping when module command returns READONLY response #584

Closed bknolinski closed 13 hours ago

bknolinski commented 2 months ago

Is your feature request related to a problem? Please describe. We're using redis modules to implement a "write" command that updates data. After a server fails over to the replica, a request to the former master returns "READONLY You can't write against a read only replica." Since this isn't a MOVED or ASK response, the slot mapping does not get updated and requests continue to fail.

Describe the solution you'd like The error handling should be updated for this case.
My suggestion is to add a new ReadOnly error class as shown by the patch code below which is provided the behavior requested:

--- a/src/sw/redis++/shards.h 2024-01-31 09:00:54.000000000 -0600 +++ b/src/sw/redis++/shards.h 2024-06-13 10:28:53.222535347 -0500 @@ -122,6 +122,19 @@ virtual ~SlotUncoveredError() override = default; };

+class ReadOnlyError : public ReplyError { +public:

+class ReadOnlyError; + void throw_error(const redisContext &context, const std::string &err_info);

void throw_error(const redisReply &reply); diff a/src/sw/redis++/errors.cpp b/src/sw/redis++/errors.cpp --- a/src/sw/redis++/errors.cpp 2024-01-31 09:00:54.000000000 -0600 +++ b/src/sw/redis++/errors.cpp 2024-06-13 10:31:51.353083607 -0500 @@ -29,7 +29,8 @@

std::unordered_map<std::string, ReplyErrorType> error_map = { {"MOVED", ReplyErrorType::MOVED},

sewenew commented 1 week ago

Sorry, somehow, I missed this message.

The latest code on master branch updates the underlying slot-node mapping every ClusterOptions::slot_map_refresh_interval time interval (by default, 10 seconds). So I think your problem can be solved by updating to the latest code. Please have a try.

Also the latest code should be more CPU friendly.

Regards

sewenew commented 13 hours ago

Since there's no update, I'll close this issue. If you still have problem with it, feel free to reopen it.

Regards