Skip to content

Commit

Permalink
enable gossip debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing committed Sep 22, 2023
1 parent 87480ff commit aee5548
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/Proto.Cluster/ClusterConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ private ClusterConfig(string clusterName, IClusterProvider clusterProvider, IIde
/// </summary>
public string ClusterName { get; }

/// <summary>
/// Enables debug logging for the gossip protocol.
/// </summary>
public bool GossipDebugLogging { get; set; } = false;

/// <summary>
/// Cluster kinds define types of the virtual actors supported by this member.
/// </summary>
Expand Down
23 changes: 21 additions & 2 deletions src/Proto.Cluster/Gossip/Gossip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ internal class Gossip
private static readonly ILogger Logger = Log.CreateLogger<Gossip>();
private readonly ConsensusChecks _consensusChecks = new();
private readonly Func<ImmutableHashSet<string>> _getMembers;
private readonly bool _gossipDebugLogging;
private readonly int _gossipFanout;
private readonly int _gossipMaxSend;
private readonly InstanceLogger? _logger;
Expand All @@ -34,11 +35,12 @@ internal class Gossip
private GossipState _state = new();

public Gossip(string myId, int gossipFanout, int gossipMaxSend, InstanceLogger? logger,
Func<ImmutableHashSet<string>> getMembers)
Func<ImmutableHashSet<string>> getMembers, bool gossipDebugLogging)
{
_myId = myId;
_logger = logger;
_getMembers = getMembers;
_gossipDebugLogging = gossipDebugLogging;
_gossipFanout = gossipFanout;
_gossipMaxSend = gossipMaxSend;
}
Expand Down Expand Up @@ -89,6 +91,11 @@ public ImmutableList<GossipUpdate> ReceiveState(GossipState remoteState)
return ImmutableList<GossipUpdate>.Empty;
}

if (_gossipDebugLogging)
{
Logger.LogInformation("ReceiveState: Gossip updates {Updates}", updates);
}

_state = newState;

CheckConsensus(updatedKeys);
Expand All @@ -102,9 +109,15 @@ public void SetState(string key, IMessage message)
_localSequenceNo = GossipStateManagement.SetKey(_state, key, message, _myId, _localSequenceNo);
logger?.LogDebug("Setting state key {Key} - {Value} - {State}", key, message, _state);
Logger.LogDebug("Setting state key {Key} - {Value} - {State}", key, message, _state);

if (_gossipDebugLogging)
{
Logger.LogInformation("SetState: Gossip key {Key} - {Value} - {State}", key, message, _state);
}

if (!_state.Members.ContainsKey(_myId))
{
Logger?.LogCritical("State corrupt");
logger?.LogCritical("State corrupt");
}

Expand All @@ -123,9 +136,15 @@ public void SendState(SendStateAction sendStateToMember)
GossipStateManagement.EnsureMemberStateExists(_state, member.Id);
}

var randomMembers = _otherMembers.OrderByRandom(_rnd);
var randomMembers = _otherMembers.OrderByRandom(_rnd).ToArray();

var fanoutCount = 0;

if (_gossipDebugLogging)
{
var ids = randomMembers.Select(m => m.Id).ToArray();
Logger.LogInformation("SendState: Gossip to members {Members}", ids);
}

foreach (var member in randomMembers)
{
Expand Down
17 changes: 11 additions & 6 deletions src/Proto.Cluster/Gossip/GossipActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int gossipMaxSend
{
_gossipRequestTimeout = gossipRequestTimeout;
_internal = new Gossip(system.Id, gossipFanout, gossipMaxSend, instanceLogger,
() => system.Cluster().MemberList.GetMembers());
() => system.Cluster().MemberList.GetMembers(), system.Cluster().Config.GossipDebugLogging);
}

public async Task ReceiveAsync(IContext context)
Expand Down Expand Up @@ -163,11 +163,16 @@ private void SendGossipForMember(IContext context, Member targetMember,
}

var start = DateTime.UtcNow;
context.RequestReenter<GossipResponse>(pid, new GossipRequest
{
MemberId = context.System.Id,
State = memberStateDelta.State.Clone() //ensure we have a copy and not send state that might mutate
},
var gossipRequest = new GossipRequest
{
MemberId = context.System.Id,
State = memberStateDelta.State.Clone() //ensure we have a copy and not send state that might mutate
};
if (context.Cluster().Config.GossipDebugLogging)
{
Logger.LogInformation("Sending GossipRequest {Request} to {MemberId}", gossipRequest, targetMember.Id);
}
context.RequestReenter<GossipResponse>(pid, gossipRequest,
async task =>
{
var delta = DateTime.UtcNow - start;
Expand Down
2 changes: 1 addition & 1 deletion tests/Proto.Cluster.Tests/GossipCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task Large_cluster_should_get_topology_consensus()
m => m.Id,
m => (
Gossip: new Gossip.Gossip(m.Id, fanout, memberCount, null,
() => members.Select(m => m.Id).ToImmutableHashSet()),
() => members.Select(m => m.Id).ToImmutableHashSet(), false),
Member: m));

var sends = 0L;
Expand Down

0 comments on commit aee5548

Please sign in to comment.