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

Commit 73e8ab8

Browse files
committed
Add tweaks to RedisSentinel API
1 parent 64bab98 commit 73e8ab8

2 files changed

Lines changed: 49 additions & 46 deletions

File tree

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,43 @@ public class RedisSentinel : IRedisSentinel
2424
private int sentinelIndex = -1;
2525
private List<string> sentinels;
2626
private RedisSentinelWorker worker;
27-
internal IRedisClientsManager redisManager;
2827
private static int MaxFailures = 5;
28+
29+
public IRedisClientsManager RedisManager { get; set; }
2930
public Action<IRedisClientsManager> OnFailover { get; set; }
30-
public Action OnWorkerError { get; set; }
31-
public Action<string,string> OnSentinelMessageReceived { get; set; }
31+
public Action<Exception> OnWorkerError { get; set; }
32+
public Action<string, string> OnSentinelMessageReceived { get; set; }
33+
34+
public RedisSentinel(string sentinelHost, string sentinelName)
35+
: this(new[] { sentinelHost }, sentinelName) { }
3236

3337
public RedisSentinel(IEnumerable<string> sentinelHosts, string sentinelName)
3438
{
3539
this.sentinels = sentinelHosts != null ? sentinelHosts.ToList() : null;
36-
if (sentinelHosts == null || sentinels.Count == 0)
40+
if (sentinelHosts == null || sentinels.Count == 0)
3741
throw new ArgumentException("sentinels must have at least one entry");
3842

3943
this.sentinelName = sentinelName;
4044
this.RedisManagerFactory = new RedisManagerFactory();
4145
}
4246

47+
[Obsolete("Use Start()")]
48+
public IRedisClientsManager Setup()
49+
{
50+
return Start();
51+
}
52+
4353
/// <summary>
44-
/// Initialize channel and register client manager
54+
/// Initialize Sentinel Subscription and Configure Redis ClientsManager
4555
/// </summary>
46-
/// <param name="container"></param>
47-
public IRedisClientsManager Setup()
56+
public IRedisClientsManager Start()
4857
{
4958
GetValidSentinel();
5059

51-
if (this.redisManager == null)
52-
{
60+
if (this.RedisManager == null)
5361
throw new ApplicationException("Unable to resolve sentinels!");
54-
}
5562

56-
return this.redisManager;
63+
return this.RedisManager;
5764
}
5865

5966
public Func<string, string> HostFilter { get; set; }
@@ -75,23 +82,23 @@ private RedisSentinelWorker GetValidSentinel()
7582

7683
RedisException lastEx = null;
7784

78-
while (this.redisManager == null && ShouldRetry())
85+
while (this.RedisManager == null && ShouldRetry())
7986
{
8087
try
8188
{
8289
this.worker = GetNextSentinel();
83-
this.redisManager = worker.GetClientManager();
90+
this.RedisManager = worker.GetClientManager();
8491
this.worker.BeginListeningForConfigurationChanges();
8592
return this.worker;
8693
}
8794
catch (RedisException ex)
8895
{
96+
if (OnWorkerError != null)
97+
OnWorkerError(ex);
98+
8999
lastEx = ex;
90100
if (this.worker != null)
91-
{
92-
this.worker.SentinelError -= Worker_SentinelError;
93101
this.worker.Dispose();
94-
}
95102

96103
this.failures++;
97104
}
@@ -115,13 +122,13 @@ private RedisSentinelWorker GetNextSentinel()
115122
sentinelIndex++;
116123

117124
if (sentinelIndex >= sentinels.Count)
118-
{
119125
sentinelIndex = 0;
120-
}
121126

122-
var sentinelWorker = new RedisSentinelWorker(this, sentinels[sentinelIndex], this.sentinelName);
127+
var sentinelWorker = new RedisSentinelWorker(this, sentinels[sentinelIndex], this.sentinelName)
128+
{
129+
OnSentinelError = OnSentinelError
130+
};
123131

124-
sentinelWorker.SentinelError += Worker_SentinelError;
125132
return sentinelWorker;
126133
}
127134

@@ -130,21 +137,17 @@ private RedisSentinelWorker GetNextSentinel()
130137
/// </summary>
131138
/// <param name="sender"></param>
132139
/// <param name="e"></param>
133-
private void Worker_SentinelError(object sender, EventArgs e)
140+
private void OnSentinelError(Exception ex)
134141
{
135-
var worker = sender as RedisSentinelWorker;
136-
137-
if (worker != null)
142+
if (this.worker != null)
138143
{
139-
Log.Info("Error on existing SentinelWorker, reconnecting...");
144+
Log.Error("Error on existing SentinelWorker, reconnecting...");
145+
140146
if (OnWorkerError != null)
141-
{
142-
OnWorkerError();
143-
}
147+
OnWorkerError(ex);
144148

145149
// dispose the worker
146-
worker.SentinelError -= Worker_SentinelError;
147-
worker.Dispose();
150+
this.worker.Dispose();
148151

149152
// get a new worker and start looking for more changes
150153
this.worker = GetNextSentinel();
@@ -154,15 +157,13 @@ private void Worker_SentinelError(object sender, EventArgs e)
154157

155158
public SentinelInfo FailoverToSentinelHosts()
156159
{
157-
var worker = GetValidSentinel();
158-
return worker.ConfigureRedisFromSentinel();
160+
return GetValidSentinel().ConfigureRedisFromSentinel();
159161
}
160162

161163
public void Dispose()
162164
{
163165
if (worker != null)
164166
{
165-
worker.SentinelError -= Worker_SentinelError;
166167
worker.Dispose();
167168
worker = null;
168169
}
@@ -178,7 +179,7 @@ public class SentinelInfo
178179
public SentinelInfo(List<string> redisMasters, List<string> redisSlaves)
179180
{
180181
RedisMasters = redisMasters != null ? redisMasters.ToArray() : new string[0];
181-
RedisSlaves = redisSlaves != null ? RedisSlaves.ToArray() : new string[0];
182+
RedisSlaves = redisSlaves != null ? redisSlaves.ToArray() : new string[0];
182183
}
183184

184185
public override string ToString()

src/ServiceStack.Redis/RedisSentinelWorker.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ internal class RedisSentinelWorker : IDisposable
1919
private string host;
2020
private IRedisClientsManager redisManager;
2121

22-
public event EventHandler SentinelError;
22+
public Action<Exception> OnSentinelError;
2323

2424
public RedisSentinelWorker(RedisSentinel redisSentinel, string host, string sentinelName)
2525
{
26+
2627
this.redisSentinel = redisSentinel;
27-
this.redisManager = redisSentinel.redisManager;
28+
this.redisManager = redisSentinel.RedisManager;
2829
this.sentinelName = sentinelName;
2930

3031
//Sentinel Servers doesn't support DB, reset to 0
@@ -33,7 +34,8 @@ public RedisSentinelWorker(RedisSentinel redisSentinel, string host, string sent
3334
this.sentinelSubscription = this.sentinelPubSubClient.CreateSubscription();
3435
this.sentinelSubscription.OnMessage = SentinelMessageReceived;
3536

36-
Log.Info("Set up Redis Sentinel on {0}".Fmt(host));
37+
if (Log.IsDebugEnabled)
38+
Log.Debug("Set up Redis Sentinel on {0}".Fmt(host));
3739
}
3840

3941
private void SubscribeForChanges(object arg)
@@ -43,14 +45,13 @@ private void SubscribeForChanges(object arg)
4345
// subscribe to all messages
4446
this.sentinelSubscription.SubscribeToChannelsMatching("*");
4547
}
46-
catch (Exception)
48+
catch (Exception ex)
4749
{
48-
Log.Error("Problem Subscribing to Redis Channel on {0}:{1}".Fmt(this.sentinelClient.Host, this.sentinelClient.Port));
49-
// problem communicating to sentinel
50-
if (SentinelError != null)
51-
{
52-
SentinelError(this, EventArgs.Empty);
53-
}
50+
Log.Error("Error Subscribing to Redis Channel on {0}:{1}"
51+
.Fmt(this.sentinelClient.Host, this.sentinelClient.Port), ex);
52+
53+
if (OnSentinelError != null)
54+
OnSentinelError(ex);
5455
}
5556
}
5657

@@ -61,6 +62,9 @@ private void SubscribeForChanges(object arg)
6162
/// <param name="message"></param>
6263
private void SentinelMessageReceived(string channel, string message)
6364
{
65+
if (Log.IsDebugEnabled)
66+
Log.Debug("Received '{0}' on channel '{1}' from Sentinel".Fmt(channel, message));
67+
6468
// {+|-}sdown is the event for server coming up or down
6569
if (channel.ToLower().Contains("sdown"))
6670
{
@@ -70,9 +74,7 @@ private void SentinelMessageReceived(string channel, string message)
7074
}
7175

7276
if (redisSentinel.OnSentinelMessageReceived != null)
73-
{
7477
redisSentinel.OnSentinelMessageReceived(channel, message);
75-
}
7678
}
7779

7880
/// <summary>

0 commit comments

Comments
 (0)