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

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

Open
masariello opened this issue Nov 13, 2023 · 3 comments

Comments

@masariello
Copy link

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!

@sewenew
Copy link
Owner

sewenew commented Nov 13, 2023

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

@masariello
Copy link
Author

Looks lovely. The thinner the prettier. Thank you!

@sewenew
Copy link
Owner

sewenew commented Nov 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants