StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Auto discover cluster nodes

Open thepirat000 opened this issue 11 years ago • 7 comments

Does StackExchange.Redis supports auto-discover configuration for cluster, like the redis-cli -c option?

Currently, to connect to a cluster I need to specify all the nodes ip:port to the configuration string.

So for example if I have 6 nodes in different machines all listening to port 7000, to connect to the cluster I need to do:

var config = "10.0.0.1:7000, 10.0.0.2:7000, 10.0.0.3:7000, 10.0.0.4:7000, 10.0.0.5:7000, 10.0.0.6:7000";
var cnn = ConnectionMultiplexer.Connect(config);

But since the cluster configuration can be retrieved from any of the nodes, this seems unnecessary. Actually I can "discover" the complete cluster configuration by calling the GetEndPoints method, like this:

// Connect to one of the nodes to discover all the endpoints
var config = "10.0.0.1:7000";
using (var discoverCnn = ConnectionMultiplexer.Connect(config))
{
    Thread.Sleep(2000);
    config = string.Join(",", discoverCnn.GetEndPoints(false).Select(ep => ep.ToString()));
}
// Now "config" has all the cluster endpoints comma separated
var realCnn = ConnectionMultiplexer.Connect(config);
var db = realCnn.GetDatabase();

Is there a reliable way of doing this?

The ultimate goal is to prevent the client need to know the complete list of cluster nodes to connect to that cluster.

thepirat000 avatar Nov 04 '15 17:11 thepirat000

You shouldn't have to do that - any 1 should be sufficient (although 2 is probably a good idea for HA) - this is actually why there is a boolean flag on GetEndpoints. If this has stopped working: it is a bug. It is absolutely meant to expand its list of endpoints automatically. On 4 Nov 2015 9:44 am, "Federico Daniel Colombo" [email protected] wrote:

Does StackExchange.Redis supports auto-discover configuration for cluster, like the redis-cli -c option?

Currently, to connect to a cluster I need to specify all the nodes ip:port to the configuration string.

So for example if I have 6 nodes in different machines all listening to port 7000, to connect to the cluster I need to do:

var config = "10.0.0.1:7000, 10.0.0.2:7000, 10.0.0.3:7000, 10.0.0.4:7000, 10.0.0.5:7000, 10.0.0.6:7000";var cnn = ConnectionMultiplexer.Connect(config);

But since the cluster configuration can be retrieved from any of the nodes, this seems unnecessary. Actually I can "discover" the complete cluster configuration by calling the GetEndPoints method, like this:

// Connect to one of the nodes to discover all the endpointsvar config = "10.0.0.1:7000";using (var discoverCnn = ConnectionMultiplexer.Connect(config)) { Thread.Sleep(2000); config = string.Join(",", discoverCnn.GetEndPoints(false).Select(ep => ep.ToString())); }// Now "config" has all the cluster endpoints comma separatedvar realCnn = ConnectionMultiplexer.Connect(config);var db = realCnn.GetDatabase();

Is there a reliable way of doing this?

The ultimate goal is to prevent the client need to know the complete list of cluster nodes to connect to that cluster.

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/300.

mgravell avatar Nov 05 '15 02:11 mgravell

Oh, you're right. I've been experiencing MOVED exceptions when using just one endpoint, but it was because the snapshot was being constructed.

var config = "192.168.15.11:7000";
var cnn = ConnectionMultiplexer.Connect(config);
var db = cnn.GetDatabase();
var a = db.StringGet("a").ToString();   // <-- This will throw MOVED exception

But if I wait for a second after the call to connect, the same works perfectly:

var config = "192.168.15.11:7000";
var cnn = ConnectionMultiplexer.Connect(config);
Thread.Sleep(1000);
var db = cnn.GetDatabase();
var a = db.StringGet("a").ToString();   

Is there any way to know when the snapshot has finished constructing?

thepirat000 avatar Nov 05 '15 19:11 thepirat000

Yes, I was in one of the MS buildings talking to the Azure Redis Cache team about that yesterday, and the timing issue sounds the same as our conclusion. We're going to find a way of fixing that. On 5 Nov 2015 11:07 am, "Federico Daniel Colombo" [email protected] wrote:

Oh, you're right. I've been experiencing MOVED exceptions when using just one endpoint, but it was because the snapshot was being constructed.

var config = "192.168.15.11:7000";var cnn = ConnectionMultiplexer.Connect(config);var db = cnn.GetDatabase();var a = db.StringGet("a").ToString(); // <-- This will throw MOVED exception

But if I wait for a second after the call to connect, the same works perfectly:

var config = "192.168.15.11:7000";var cnn = ConnectionMultiplexer.Connect(config); Thread.Sleep(1000);var db = cnn.GetDatabase();var a = db.StringGet("a").ToString();

Is there any way to know when the snapshot has finished constructing?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/300#issuecomment-154155545 .

mgravell avatar Nov 05 '15 20:11 mgravell

Hi @mgravell , I got the same problem while using Azure Redis Cache. Can I have some backup plans to handle this temporary?

huangcd avatar Dec 01 '15 07:12 huangcd

My current workaround is to Thread.Sleep a couple of seconds after ConnectionMultiplexer.Connect.

thepirat000 avatar Dec 01 '15 16:12 thepirat000

I have raised a PR (see above) for fixing this issue. Please take a look

Thanks

hrishi18pathak avatar Dec 01 '15 22:12 hrishi18pathak

Resolved via #312

NickCraver avatar Jan 27 '16 11:01 NickCraver