Nordix / hiredis-cluster

C client library for Valkey/Redis Cluster. This project is used and sponsored by Ericsson. It is a fork of the now unmaintained hiredis-vip.
BSD 3-Clause "New" or "Revised" License
87 stars 42 forks source link

Support custom commands defined in Redis modules #162

Closed selcukaltinay closed 7 months ago

selcukaltinay commented 1 year ago

I'm trying to use redis modules with hiredis-cluster. In previous version of hiredis-cluster, it is possible to add commands to command.h in order to use the redis commands like FT.CREATE which comes from redisearch. But in the last version, there is a script which is named as gencommands.py to produce cmddef.h. As far as I know, I need to add commands to this file to produce QUERY to send redis server. But I couldn't have make it for RediSearch and RedisJSON. I'm giving the json which is located in redisjson and redisearch as an argument my I'm taking an error. How can I use redisearch and redisjson with hiredis-cluster?

zuiderkwast commented 1 year ago

It's a good question. We didn't think about custom module commands when we rewrote command.h, but of course we need to support this.

I think we need to provide a way to extend the command definition, either at compile time or in runtime. We have to think about the best way to do this.

In the meantime, there is one workaround: You can use redisClusterGetNodeByKey and then redisClusterCommandToNode (or the similar functions for async API) to send any command to the specified node.

selcukaltinay commented 1 year ago

Thanks for answer. I tried to make your suggestions like this

std::string key = "{my_key}"; cluster_node node = redisClusterGetNodeByKey(cc, key); reply = (redisReply *) redisClusterCommandToNode(cc, node, strCommand.c_str());

But I'm taking "Resource Temporarily Unavailable" error when I try to run the program

Thanks again

zuiderkwast commented 1 year ago

I'm not very good with C++ but I guess key needs to be a char * instead of a std:string. Can this be the problem?

Or maybe "Resource Temporarily Unavailable" means there is a temporary network problem, your Redis cluster is not available?

bjosv commented 1 year ago

Maybe its a copy&paste error but you should need to give key.c_str() to redisClusterGetNodeByKey() too, then if you are using the async api (?) you should use redisClusterAsyncCommandToNode() (already mentioned above.. but just mentioning it again since you see a EAGAIN "Resource Temporarily Unavailable" )

selcukaltinay commented 1 year ago

The resource unavailable problem fixed with using placeholders as %s, command instead of giving full string with c_str method. And also suggested work around worked fine. Thanks for answers

zuiderkwast commented 1 year ago

Ah, the docs mentions that command is separated by spaces, but it is not very clear. Perhaps we should also add an example that you can't do

    // Wrong:
    reply = redisClusterCommand(clustercontext, "SET foo \"Hello my name is Mr. Hankey\"");

    // Correct:
    reply = redisClusterCommand(clustercontext, "SET foo %s", "Hello my name is Mr. Hankey");

This could be improved in hiredis README as well...

Anyway, this issue is resolved so I'm closing it.