Skip to content

Commit

Permalink
Made akka.cluster.sharding.verbose-debug-logging accept a LogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaronontheweb committed Mar 12, 2024
1 parent 251622d commit 42a9118
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 63 deletions.
28 changes: 14 additions & 14 deletions src/contrib/cluster/Akka.Cluster.Sharding/DDataShardCoordinator.cs
Expand Up @@ -15,6 +15,7 @@
using Akka.DistributedData;
using Akka.Event;
using Akka.Pattern;
using static Akka.Cluster.Sharding.InternalConfigUtilities;

namespace Akka.Cluster.Sharding
{
Expand All @@ -29,7 +30,7 @@ internal sealed class DDataShardCoordinator : ActorBase, IWithTimers, IWithUnbou

private sealed class RememberEntitiesStoreStopped
{
public static RememberEntitiesStoreStopped Instance = new();
public static readonly RememberEntitiesStoreStopped Instance = new();

private RememberEntitiesStoreStopped()
{
Expand Down Expand Up @@ -77,7 +78,7 @@ private RememberEntitiesLoadTimeout()

private readonly IActorRef _replicator;
private readonly ShardCoordinator _baseImpl;
private bool VerboseDebug => _baseImpl.VerboseDebug;
private LogLevel? VerboseDebug => _baseImpl.VerboseDebug;

private readonly IReadConsistency _stateReadConsistency;
private readonly IWriteConsistency _stateWriteConsistency;
Expand Down Expand Up @@ -108,7 +109,7 @@ private RememberEntitiesLoadTimeout()
{
_replicator = replicator;
var log = Context.GetLogger();
var verboseDebug = Context.System.Settings.Config.GetBoolean("akka.cluster.sharding.verbose-debug-logging");
var verboseDebug = ParseVerboseLogSettings(Context.System.Settings.Config);

_baseImpl = new ShardCoordinator(typeName, settings, allocationStrategy,
Context, log, verboseDebug, Update, UnstashOneGetShardHomeRequest);
Expand Down Expand Up @@ -171,9 +172,9 @@ bool ReceiveDelegate(object message)
{
case GetSuccess g when g.Key.Equals(_coordinatorStateKey):
var existingState = g.Get(_coordinatorStateKey).Value.WithRememberEntities(Settings.RememberEntities);
if (VerboseDebug)
Log.Debug("{0}: Received initial coordinator state [{1}]", TypeName, existingState);
else
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: Received initial coordinator state [{1}]", TypeName, existingState);
else if(Log.IsDebugEnabled)
Log.Debug(
"{0}: Received initial coordinator state with [{1}] shards",
TypeName,
Expand All @@ -183,8 +184,7 @@ bool ReceiveDelegate(object message)

case GetFailure m when m.Key.Equals(_coordinatorStateKey):
_initialStateRetries++;
var template =
"{0}: The ShardCoordinator was unable to get an initial state within 'waiting-for-state-timeout': {1} millis (retrying). Has ClusterSharding been started on all nodes?";
const string template = "{0}: The ShardCoordinator was unable to get an initial state within 'waiting-for-state-timeout': {1} millis (retrying). Has ClusterSharding been started on all nodes?";
if (_initialStateRetries == 1)
Log.Info(template, TypeName, _stateReadConsistency.Timeout.TotalMilliseconds);
else if (_initialStateRetries < 5)
Expand Down Expand Up @@ -488,8 +488,8 @@ bool ReceiveDelegate(object message)
{
Context.UnbecomeStacked();
afterUpdateCallback(evt);
if (VerboseDebug)
Log.Debug("{0}: New coordinator state after [{1}]: [{2}]", TypeName, evt, State);
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: New coordinator state after [{1}]: [{2}]", TypeName, evt, State);
UnstashOneGetShardHomeRequest();
Stash.UnstashAll();
}
Expand Down Expand Up @@ -521,8 +521,8 @@ private void Activate()
{
Context.Become(msg => _baseImpl.Active(msg) || ReceiveLateRememberedEntities(msg));
Log.Info("{0}: ShardCoordinator was moved to the active state with [{1}] shards", TypeName, State.Shards.Count);
if (VerboseDebug)
Log.Debug("{0}: Full ShardCoordinator initial state {1}", TypeName, State);
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: Full ShardCoordinator initial state {1}", TypeName, State);
}

/// <summary>
Expand Down Expand Up @@ -599,8 +599,8 @@ private void GetAllRememberedShards()
private void SendCoordinatorStateUpdate(IDomainEvent evt)
{
var s = State.Updated(evt);
if (VerboseDebug)
Log.Debug("{0}: Storing new coordinator state [{1}]", TypeName, State);
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: Storing new coordinator state [{1}]", TypeName, State);
_replicator.Tell(Dsl.Update(
_coordinatorStateKey,
new LWWRegister<CoordinatorState>(_selfUniqueAddress, _initEmptyState),
Expand Down
@@ -0,0 +1,39 @@
// -----------------------------------------------------------------------
// <copyright file="InternalConfigUtilities.cs" company="Akka.NET Project">
// Copyright (C) 2009-2024 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2024 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using Akka.Configuration;
using Akka.Event;

namespace Akka.Cluster.Sharding;

/// <summary>
/// INTERNAL API
/// </summary>
internal static class InternalConfigUtilities
{
/// <summary>
/// Null in this case means "off"
/// </summary>
/// <param name="coordinatorHocon">The HOCON config used by a shard coordinator</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static LogLevel? ParseVerboseLogSettings(Config coordinatorHocon)
{
var logLevel = coordinatorHocon.GetString("akka.cluster.sharding.verbose-debug-logging");
return logLevel switch
{
"off" => null,
"on" => LogLevel.DebugLevel,
"error" => LogLevel.ErrorLevel,
"warning" => LogLevel.WarningLevel,
"info" => LogLevel.InfoLevel,
"debug" => LogLevel.DebugLevel,
_ => throw new ArgumentException($"Unknown log level: {logLevel}")
};
}
}
Expand Up @@ -42,7 +42,7 @@ internal static Props Props(string typeName, ClusterShardingSettings settings, I

private readonly ShardCoordinator _baseImpl;

private bool VerboseDebug => _baseImpl.VerboseDebug;
private LogLevel? VerboseDebug => _baseImpl.VerboseDebug;
private string TypeName => _baseImpl.TypeName;
private ClusterShardingSettings Settings => _baseImpl.Settings;
private CoordinatorState State { get => _baseImpl.State; set => _baseImpl.State = value; }
Expand All @@ -54,7 +54,7 @@ IShardAllocationStrategy allocationStrategy
)
{
var log = Context.GetLogger();
var verboseDebug = Context.System.Settings.Config.GetBoolean("akka.cluster.sharding.verbose-debug-logging");
var verboseDebug = InternalConfigUtilities.ParseVerboseLogSettings(Context.System.Settings.Config);

_baseImpl = new ShardCoordinator(typeName, settings, allocationStrategy,
Context, log, verboseDebug, Update, UnstashOneGetShardHomeRequest);
Expand Down Expand Up @@ -83,8 +83,8 @@ protected override bool ReceiveRecover(object message)
"state-store is set to persistence but a migration has taken place to remember-entities-store=eventsourced. You can not downgrade.");

case IDomainEvent evt:
if (VerboseDebug)
Log.Debug("{0}: receiveRecover {1}", TypeName, evt);
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: receiveRecover {1}", TypeName, evt);

switch (evt)
{
Expand Down Expand Up @@ -127,8 +127,8 @@ protected override bool ReceiveRecover(object message)
}
return false;
case SnapshotOffer { Snapshot: CoordinatorState state }:
if (VerboseDebug)
Log.Debug("{0}: receiveRecover SnapshotOffer {1}", TypeName, state);
if (VerboseDebug.HasValue)
Log.Log(VerboseDebug.Value, null, "{0}: receiveRecover SnapshotOffer {1}", TypeName, state);
State = state.WithRememberEntities(Settings.RememberEntities);
// Old versions of the state object may not have unallocatedShard set,
// thus it will be null.
Expand Down

0 comments on commit 42a9118

Please sign in to comment.