-
-
Notifications
You must be signed in to change notification settings - Fork 983
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
Master nodes not discovered #338
Comments
Hi @danieleteti, Indeed iterating over That said, trying to delete a subset of the keys spread over the keyspace is a bit tricky with redis-cluster but still not impossible. First of all I suggest avoiding This is a quick example of a function that fetches the current slots map from redis-cluster in order to extract the list of master nodes from it, then it creates a new connection for each master node and uses it to create a new client (for convenience instead of using function cluster_iterate(\Predis\ClientInterface $client, $callback)
{
$clientClass = get_class($client);
$clientOptions = $client->getOptions();
$cluster = $client->getConnection();
$connector = $cluster->getConnectionFactory();
$slotsmap = $cluster->getSlotsMap() ?: $cluster->askSlotsMap();
foreach (array_unique($slotsmap) as $node) {
$nodeConnection = $connector->create($node);
$nodeClient = new $clientClass($nodeConnection, $clientOptions);
$callback($nodeClient);
}
} A script that deletes the keys matching a certain pattern on each node of the cluster would then look like this: $clusterClient = new \Predis\Client(['tcp://127.0.0.1:7000'], ['cluster' => 'redis']);
cluster_iterate($clusterClient, function ($nodeClient) {
$keyspace = new \Predis\Collection\Iterator\Keyspace($nodeClient, "cache:*");
foreach ($keyspace as $key) {
$nodeClient->del($key);
}
}); Everything considered I think this is the best possible compromise, but this approach still requires a few considerations:
|
Thank you for your example. Currently I finished to ask a CLUSTER NODES to redis and then create a specific predis instance with only the master nodes. I'll change KEYS to SCAN ASAP, but still I need a way to be sure that predis is talking only with master nodes. $config = [my nodes array with some masters and some slaves];
$client = new PRedis($config);
//Here I've to be sure that $client is connected only to master nodes... With my solution I've to reimplement a microredisclient to issue a CLUSTER NODES to understand how the cluster is and the crete predis. Is there a way to let predis understand itself and let it connects only to master nodes? Still thank you. |
Why putting slaves at all in By the way if you want to get these info "manually" from a node of the cluster, I think it's easier to use |
Just a quick update: after commit 922e56b (which means starting with Predis v1.1.0 when it will be released) the function function cluster_iterate(\Predis\ClientInterface $client, $callback)
{
$clientClass = get_class($client);
foreach ($client->getConnection() as $nodeConnection) {
$nodeClient = new $clientClass($nodeConnection, $client->getOptions());
$callback($nodeClient);
}
} Basically |
Improvements have been made for v1.1.0 so I'm closing this issue, anyway feel free to post further comments on the matter if you need more clarifications. EDIT: just pushed the last change for Predis v1.1 with ecab7e4 to make it possible to iterate each node of an underlying traversable aggregate connection simply by doing a foreach ($client as $nodeClient) {
$nodeClient->flushdb();
} |
In a redis-cluster with 3 master and 3 slave (one for each) I have that some master could become a slave because of a master fault or simply a restart.
Now, how I can discover which are the current master nodes?
I've to delete a key which starts with a particupar pattern (let's say cache:aaa*) on all the nodes.
This is what I currently do:
But this code doesn't search for actual masters. It simply uses the configured list of node in the predis constructor.
How I can get all these keys?
The text was updated successfully, but these errors were encountered: