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

Commit a968f1c

Browse files
committed
Add more debug logging messages
1 parent 9b8e5f1 commit a968f1c

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ private void Connect()
9292
};
9393
try
9494
{
95+
if (log.IsDebugEnabled)
96+
{
97+
var type = ConnectTimeout <= 0 ? "sync" : "async";
98+
logDebug($"Attempting {type} connection to '{Host}:{Port}' (SEND {SendTimeout}, RECV {ReceiveTimeout} Timeouts)...");
99+
}
100+
95101
if (ConnectTimeout <= 0)
96102
{
97103
socket.Connect(Host, Port);
@@ -107,7 +113,7 @@ private void Connect()
107113
if (!socket.Connected)
108114
{
109115
if (log.IsDebugEnabled)
110-
logDebug("Socket Connect failed");
116+
logDebug($"Socket failed connect to '{Host}:{Port}' (ConnectTimeout {ConnectTimeout})");
111117

112118
socket.Close();
113119
socket = null;
@@ -116,7 +122,7 @@ private void Connect()
116122
}
117123

118124
if (log.IsDebugEnabled)
119-
logDebug("Socket Connected");
125+
logDebug($"Socket connected to '{Host}:{Port}'");
120126

121127
Stream networkStream = new NetworkStream(socket);
122128

@@ -169,7 +175,7 @@ private void Connect()
169175
#endif
170176

171177
if (!sslStream.IsEncrypted)
172-
throw new Exception("Could not establish an encrypted connection to " + Host);
178+
throw new Exception($"Could not establish an encrypted connection to '{Host}:{Port}'");
173179

174180
networkStream = sslStream;
175181
}

src/ServiceStack.Redis/RedisSentinel.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ public class RedisSentinel : IRedisSentinel
123123
/// </summary>
124124
public bool ResetWhenObjectivelyDown { get; set; }
125125

126+
internal string DebugId => $"";
127+
126128
public RedisSentinel(string sentinelHost = null, string masterName = null)
127129
: this(new[] { sentinelHost ?? DefaultAddress }, masterName ?? DefaultMasterName) { }
128130

@@ -287,10 +289,8 @@ private IRedisClientsManager CreateRedisManager(SentinelInfo sentinelInfo)
287289
return redisManager;
288290
}
289291

290-
public IRedisClientsManager GetRedisManager()
291-
{
292-
return RedisManager ?? (RedisManager = CreateRedisManager(GetSentinelInfo()));
293-
}
292+
public IRedisClientsManager GetRedisManager() =>
293+
RedisManager ??= CreateRedisManager(GetSentinelInfo());
294294

295295
private RedisSentinelWorker GetValidSentinelWorker()
296296
{
@@ -304,17 +304,30 @@ private RedisSentinelWorker GetValidSentinelWorker()
304304

305305
while (this.worker == null && ShouldRetry())
306306
{
307+
var step = 0;
307308
try
308309
{
309310
this.worker = GetNextSentinel();
311+
step = 1;
310312
GetRedisManager();
311313

314+
step = 2;
312315
this.worker.BeginListeningForConfigurationChanges();
313316
this.failures = 0; //reset
314317
return this.worker;
315318
}
316319
catch (RedisException ex)
317320
{
321+
if (Log.IsDebugEnabled)
322+
{
323+
var name = step switch {
324+
0 => "GetNextSentinel()",
325+
1 => "GetRedisManager()",
326+
2 => "BeginListeningForConfigurationChanges()",
327+
};
328+
Log.Debug($"Failed to {name}: {ex.Message}");
329+
}
330+
318331
if (OnWorkerError != null)
319332
OnWorkerError(ex);
320333

@@ -378,6 +391,9 @@ private RedisSentinelWorker GetNextSentinel()
378391

379392
if (++sentinelIndex >= SentinelEndpoints.Length)
380393
sentinelIndex = 0;
394+
395+
if (Log.IsDebugEnabled)
396+
Log.Debug($"Attempt to connect to next sentinel '{SentinelEndpoints[sentinelIndex]}'...");
381397

382398
var sentinelWorker = new RedisSentinelWorker(this, SentinelEndpoints[sentinelIndex])
383399
{
@@ -443,6 +459,6 @@ public SentinelInfo(string masterName, IEnumerable<string> redisMasters, IEnumer
443459

444460
public override string ToString()
445461
{
446-
return $"{MasterName} masters: {string.Join(", ", RedisMasters)}, slaves: {string.Join(", ", RedisSlaves)}";
462+
return $"{MasterName} primary: {string.Join(", ", RedisMasters)}, replicas: {string.Join(", ", RedisSlaves)}";
447463
}
448464
}

src/ServiceStack.Redis/RedisSentinelWorker.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ public void BeginListeningForConfigurationChanges()
173173
//Use BasicRedisResolver which doesn't validate non-Master Sentinel instances
174174
RedisResolver = new BasicRedisResolver(sentinel.SentinelEndpoints, sentinel.SentinelEndpoints)
175175
};
176+
177+
if (Log.IsDebugEnabled)
178+
Log.Debug($"Starting subscription to {sentinel.SentinelHosts.ToArray()}, replicas: {sentinel.SentinelHosts.ToArray()}...");
179+
176180
this.sentinePubSub = new RedisPubSubServer(sentinelManager)
177181
{
178182
HeartbeatInterval = null,
@@ -187,7 +191,7 @@ public void BeginListeningForConfigurationChanges()
187191
}
188192
catch (Exception ex)
189193
{
190-
Log.Error($"Error Subscribing to Redis Channel on {this.sentinelClient.Host}:{this.sentinelClient.Port}", ex);
194+
Log.Error($"Error Subscribing to Redis Channel on {sentinelClient.Host}:{sentinelClient.Port}", ex);
191195

192196
if (OnSentinelError != null)
193197
OnSentinelError(ex);

0 commit comments

Comments
 (0)