redis-plus-plus icon indicating copy to clipboard operation
redis-plus-plus copied to clipboard

Provide a way to iterate the nodes in a cluster shard so people can implement key scans on clusters

Open masariello opened this issue 8 months ago • 3 comments

I would like to be able to SCAN keys in a cluster as in the below example code.

std::unordered_set<std::string> scan(std::string const& glob_expr)
{
  std::unordered_set<std::string> result;
  auto scan_node = [&](Redis& redis)
  {
    auto cursor = 0LL;
    do {
      cursor = redis.scan(cursor, glob_expr, [&](std::string const& k) { result.insert(k); });
    } while (cursor != 0);
  };

  ShardsPool shards_pool(...);
  for(auto const& slot : shards_pool->shards())
  {
    auto pool = shards_pool->fetch(slot.second());
    auto node = Redis(std::make_shared<GuardedConnection>(pool));
    scan_node(node));
  }
  return result;
}

The above uses the Redis::Redis(const GuardedConnectionSPtr &) which is private, so obviously not the cleanest approach.

A better way would be to add the following RedisCluster methods, please.

Shards RedisCluster::shards() const
{
  return pool_.shards();
}

ConnectionPoolSPtr RedisCluster::fetch(const Node & node)
{
  return pool_.fetch(node);
}

Thank you!

masariello avatar Nov 13 '23 12:11 masariello

In fact, this feature is on the way. However, there's some problem with the async interface, and I'm considering how to do error handling elegantly. The interface is more or less like the following:

auto r = RedisCluster("redis://127.0.0.1:7000");
r.for_each([](Redis &r) {
                      // *r* is a Redis instance connecting a node, and you can do stuff with it for each node.
                 });

Regards

sewenew avatar Nov 13 '23 13:11 sewenew

Looks lovely. The thinner the prettier. Thank you!

masariello avatar Nov 13 '23 14:11 masariello

RedisCluster::for_each has been merged into the master branch. Please take a try. If you have any problem, feel free to let me know. for_each for AsyncRedisCluster is still on the way.

Regards

sewenew avatar Nov 19 '23 13:11 sewenew