Commit 87d4dc84 authored by hrishi18pathak's avatar hrishi18pathak

make auto discoverability of nodes synchronous, actually fix issue 300

parent 6b317395
......@@ -1203,11 +1203,31 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
}
const CommandFlags flags = CommandFlags.NoRedirect | CommandFlags.HighPriority;
var available = new Task<bool>[endpoints.Count];
var servers = new ServerEndPoint[available.Length];
List<ServerEndPoint> masters = new List<ServerEndPoint>(endpoints.Count);
bool useTieBreakers = !string.IsNullOrWhiteSpace(configuration.TieBreaker);
var tieBreakers = useTieBreakers ? new Task<string>[endpoints.Count] : null;
ServerEndPoint[] servers = null;
Task<string>[] tieBreakers = null;
bool encounteredConnectedServer = false;
Stopwatch watch = null;
int iterCount = first ? 2 : 1;
// this is fix for https://github.com/StackExchange/StackExchange.Redis/issues/300
// auto discoverability of cluster nodes is made synchronous.
// we try to connect to endpoints specified inside the user provided configuration
// and when we encounter one such endpoint to which we are able to successfully connect,
// we get the list of cluster nodes from this endpoint and try to proactively connect
// to these nodes instead of relying on auto configure
for (int iter = 0; iter < iterCount; ++iter)
{
if (endpoints == null) break;
var available = new Task<bool>[endpoints.Count];
tieBreakers = useTieBreakers ? new Task<string>[endpoints.Count] : null;
servers = new ServerEndPoint[available.Length];
RedisKey tieBreakerKey = useTieBreakers ? (RedisKey)configuration.TieBreaker : default(RedisKey);
for (int i = 0; i < available.Length; i++)
{
Trace("Testing: " + Format.ToString(endpoints[i]));
......@@ -1232,11 +1252,13 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
}
}
LogLocked(log, "Allowing endpoints {0} to respond...", TimeSpan.FromMilliseconds(configuration.ConnectTimeout));
Trace("Allowing endpoints " + TimeSpan.FromMilliseconds(configuration.ConnectTimeout) + " to respond...");
await WaitAllIgnoreErrorsAsync(available, configuration.ConnectTimeout, log).ForAwait();
List<ServerEndPoint> masters = new List<ServerEndPoint>(available.Length);
watch = watch ?? Stopwatch.StartNew();
var remaining = configuration.ConnectTimeout - checked((int)watch.ElapsedMilliseconds);
LogLocked(log, "Allowing endpoints {0} to respond...", TimeSpan.FromMilliseconds(remaining));
Trace("Allowing endpoints " + TimeSpan.FromMilliseconds(remaining) + " to respond...");
await WaitAllIgnoreErrorsAsync(available, remaining, log).ForAwait();
EndPointCollection updatedEndpointCollection = null;
for (int i = 0; i < available.Length; i++)
{
var task = available[i];
......@@ -1263,7 +1285,14 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
{
servers[i].ClearUnselectable(UnselectableFlags.DidNotRespond);
LogLocked(log, "{0} returned with success", Format.ToString(endpoints[i]));
UpdateClusterConfigIfNeeded(server, log);
if (!encounteredConnectedServer)
{
// we have encountered a connected server for the first time.
// so we will get list of other nodes from this server using "CLUSTER NODES" command
// and try to connect to these other nodes in the next iteration
encounteredConnectedServer = true;
updatedEndpointCollection = GetEndpointsFromClusterNodes(server, log);
}
// count the server types
switch (server.ServerType)
{
......@@ -1314,6 +1343,16 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
}
}
if (encounteredConnectedServer)
{
endpoints = updatedEndpointCollection;
}
else
{
break; // will be retried by the outer do while loop
}
}
if (clusterCount == 0)
{
// set the serverSelectionStrategy
......@@ -1419,33 +1458,23 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
}
}
private void UpdateClusterConfigIfNeeded(ServerEndPoint server, TextWriter log)
private EndPointCollection GetEndpointsFromClusterNodes(ServerEndPoint server, TextWriter log)
{
var message = Message.Create(-1, CommandFlags.None, RedisCommand.CLUSTER, RedisLiterals.NODES);
ClusterConfiguration clusterConfig = null;
try
{
clusterConfig = this.ExecuteSyncImpl(message, ResultProcessor.ClusterNodes, server);
return new EndPointCollection(clusterConfig.Nodes.Select(node => node.EndPoint).ToList());
}
catch (Exception ex)
{
if (ex.Message.Contains("ERR This instance has cluster support disabled"))
{
LogLocked(log, "Cluster support disabled. Continuing without updating cluster config...");
return;
}
LogLocked(log, "Encountered error while updating cluster config: " + ex.Message);
}
if (clusterConfig != null)
{
this.UpdateClusterRange(clusterConfig);
LogLocked(log, "Updated cluster config");
return null;
}
}
private void ResetAllNonConnected()
{
var snapshot = serverSnapshot;
......
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
......@@ -9,6 +10,14 @@ namespace StackExchange.Redis
/// </summary>
public sealed class EndPointCollection : Collection<EndPoint>
{
public EndPointCollection() : base()
{
}
public EndPointCollection(IList<EndPoint> endpoints) : base(endpoints)
{
}
/// <summary>
/// Format an endpoint
/// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment