Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c08cdb5

Browse files
committed
Document RedisSentinel public API
1 parent 6bf595e commit c08cdb5

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public class RedisSentinel : IRedisSentinel
1818
{
1919
protected static readonly ILog Log = LogManager.GetLogger(typeof(RedisSentinel));
2020

21-
public Func<string[], string[], IRedisClientsManager> RedisManagerFactory { get; set; }
22-
2321
public static string DefaultMasterName = "mymaster";
2422
public static string DefaultAddress = "127.0.0.1:26379";
2523

@@ -39,28 +37,92 @@ public string MasterName
3937
private RedisSentinelWorker worker;
4038
private static int MaxFailures = 5;
4139

40+
/// <summary>
41+
/// Change to use a different IRedisClientsManager
42+
/// </summary>
43+
public Func<string[], string[], IRedisClientsManager> RedisManagerFactory { get; set; }
44+
45+
/// <summary>
46+
/// Configure the Redis Connection String to use for a Redis Client Host
47+
/// </summary>
48+
public Func<string, string> HostFilter { get; set; }
49+
50+
/// <summary>
51+
/// The configured Redis Client Manager this Sentinel managers
52+
/// </summary>
4253
public IRedisClientsManager RedisManager { get; set; }
54+
55+
/// <summary>
56+
/// Fired when Sentinel fails over the Redis Client Manager to a new master
57+
/// </summary>
4358
public Action<IRedisClientsManager> OnFailover { get; set; }
59+
60+
/// <summary>
61+
/// Fired when the Redis Sentinel Worker connection fails
62+
/// </summary>
4463
public Action<Exception> OnWorkerError { get; set; }
64+
65+
/// <summary>
66+
/// Fired when the Sentinel worker receives a message from the Sentinel Subscription
67+
/// </summary>
4568
public Action<string, string> OnSentinelMessageReceived { get; set; }
4669

70+
/// <summary>
71+
/// Map the internal IP's returned by Sentinels to its external IP
72+
/// </summary>
4773
public Dictionary<string, string> IpAddressMap { get; set; }
4874

75+
/// <summary>
76+
/// Whether to routinely scan for other sentinel hosts (default true)
77+
/// </summary>
4978
public bool ScanForOtherSentinels { get; set; }
5079

51-
private DateTime lastSentinelsRefresh;
80+
/// <summary>
81+
/// What interval to scan for other sentinel hosts (default 10 mins)
82+
/// </summary>
5283
public TimeSpan RefreshSentinelHostsAfter { get; set; }
84+
private DateTime lastSentinelsRefresh;
5385

54-
public TimeSpan WaitBetweenSentinelLookups { get; set; }
55-
public TimeSpan MaxWaitBetweenSentinelLookups { get; set; }
86+
/// <summary>
87+
/// How long to wait after failing before connecting to next redis instance (default 250ms)
88+
/// </summary>
89+
public TimeSpan WaitBetweenFailedHosts { get; set; }
90+
91+
/// <summary>
92+
/// How long to retry connecting to hosts before throwing (default 60 secs)
93+
/// </summary>
94+
public TimeSpan MaxWaitBetweenFailedHosts { get; set; }
95+
96+
/// <summary>
97+
/// How long to wait after consecutive failed connection attempts to master before forcing
98+
/// a Sentinel to failover the current master (default 60 secs)
99+
/// </summary>
56100
public TimeSpan WaitBeforeForcingMasterFailover { get; set; }
101+
102+
/// <summary>
103+
/// The Max Connection time for Sentinel Worker (default 100ms)
104+
/// </summary>
57105
public int SentinelWorkerConnectTimeoutMs { get; set; }
106+
107+
/// <summary>
108+
/// The Max TCP Socket Receive time for Sentinel Worker (default 100ms)
109+
/// </summary>
58110
public int SentinelWorkerReceiveTimeoutMs { get; set; }
111+
112+
/// <summary>
113+
/// The Max TCP Socket Send time for Sentinel Worker (default 100ms)
114+
/// </summary>
59115
public int SentinelWorkerSendTimeoutMs { get; set; }
60116

117+
/// <summary>
118+
/// Reset client connections when Sentinel reports redis instance is subjectively down (default true)
119+
/// </summary>
61120
public bool ResetWhenSubjectivelyDown { get; set; }
121+
122+
/// <summary>
123+
/// Reset client connections when Sentinel reports redis instance is objectively down (default true)
124+
/// </summary>
62125
public bool ResetWhenObjectivelyDown { get; set; }
63-
public bool ResetSentinelsWhenObjectivelyDown { get; set; }
64126

65127
public RedisSentinel(string sentinelHost = null, string masterName = null)
66128
: this(new[] { sentinelHost ?? DefaultAddress }, masterName ?? DefaultMasterName) { }
@@ -81,12 +143,11 @@ public RedisSentinel(IEnumerable<string> sentinelHosts, string masterName = null
81143
RefreshSentinelHostsAfter = TimeSpan.FromMinutes(10);
82144
ResetWhenObjectivelyDown = true;
83145
ResetWhenSubjectivelyDown = true;
84-
ResetSentinelsWhenObjectivelyDown = true;
85146
SentinelWorkerConnectTimeoutMs = 100;
86147
SentinelWorkerReceiveTimeoutMs = 100;
87148
SentinelWorkerSendTimeoutMs = 100;
88-
WaitBetweenSentinelLookups = TimeSpan.FromMilliseconds(250);
89-
MaxWaitBetweenSentinelLookups = TimeSpan.FromSeconds(60);
149+
WaitBetweenFailedHosts = TimeSpan.FromMilliseconds(250);
150+
MaxWaitBetweenFailedHosts = TimeSpan.FromSeconds(60);
90151
WaitBeforeForcingMasterFailover = TimeSpan.FromSeconds(60);
91152
}
92153

@@ -178,8 +239,6 @@ public void RefreshActiveSentinels()
178239
}
179240
}
180241

181-
public Func<string, string> HostFilter { get; set; }
182-
183242
internal string[] ConfigureHosts(IEnumerable<string> hosts)
184243
{
185244
if (hosts == null)
@@ -267,7 +326,7 @@ private RedisSentinelWorker GetValidSentinelWorker()
267326
}
268327

269328
this.failures = 0; //reset
270-
Thread.Sleep(WaitBetweenSentinelLookups);
329+
Thread.Sleep(WaitBetweenFailedHosts);
271330

272331
throw new RedisException("No Redis Sentinels were available", lastEx);
273332
}

src/ServiceStack.Redis/RedisSentinelResolver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
149149
Interlocked.Increment(ref RedisState.TotalForcedMasterFailovers);
150150

151151
sentinel.ForceMasterFailover();
152-
Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
152+
Thread.Sleep(sentinel.WaitBetweenFailedHosts);
153153
role = client.GetServerRole();
154154
}
155155
}
@@ -186,11 +186,11 @@ public virtual RedisClient CreateRedisClient(RedisEndpoint config, bool master)
186186
}
187187
catch { /* Ignore errors until MaxWait */ }
188188

189-
if (stopwatch.Elapsed > sentinel.MaxWaitBetweenSentinelLookups)
189+
if (stopwatch.Elapsed > sentinel.MaxWaitBetweenFailedHosts)
190190
throw new TimeoutException("Max Wait Between Sentinel Lookups Elapsed: {0}"
191-
.Fmt(sentinel.MaxWaitBetweenSentinelLookups.ToString()));
191+
.Fmt(sentinel.MaxWaitBetweenFailedHosts.ToString()));
192192

193-
Thread.Sleep(sentinel.WaitBetweenSentinelLookups);
193+
Thread.Sleep(sentinel.WaitBetweenFailedHosts);
194194
}
195195
}
196196
catch (Exception ex)

0 commit comments

Comments
 (0)