Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how ClusterShardingSettings were applied #480

Merged
merged 9 commits into from
Jul 29, 2024
46 changes: 26 additions & 20 deletions src/Akka.Cluster.Hosting/AkkaClusterHostingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,17 @@ public sealed class ShardOptions
/// <summary>
/// <para>
/// Settings for the Distributed Data replicator.
/// The <see cref="DistributedData.Role"/> property is not used. The distributed-data
/// The <see cref="ShardingDDataOptions.Role"/> property is not used. The distributed-data
/// role will be the same as <see cref="ShardOptions.Role"/>.
/// Note that there is one Replicator per role and it's not possible
/// to have different distributed-data settings for different sharding entity types.
/// </para>
/// <b>NOTE</b> This setting is only used when <see cref="StateStoreMode"/> is set to
/// <see cref="Akka.Cluster.Sharding.StateStoreMode.DData"/>
/// </summary>
[Obsolete("This property is not being applied to the ActorSystem anymore. " +
"Use manual HOCON configuration to set \"akka.cluster.sharding.distributed-data\" values. " +
"Since v1.5.27")]
public ShardingDDataOptions DistributedData { get; } = new();

/// <summary>
Expand All @@ -302,13 +305,13 @@ public sealed class ShardOptions
/// or <see cref="ShouldPassivateIdleEntities"/> is set to false.
/// </summary>
public TimeSpan? PassivateIdleEntityAfter { get; set; }

internal void Apply(AkkaConfigurationBuilder builder)

public Config ToConfig() => ToString();

public override string ToString()
{
DistributedData.Apply(builder);

var sb = new StringBuilder();

if (Role is not null)
sb.AppendLine($"role = {Role.ToHocon()}");

Expand Down Expand Up @@ -344,12 +347,7 @@ internal void Apply(AkkaConfigurationBuilder builder)
else if(PassivateIdleEntityAfter is not null)
sb.AppendLine($"passivate-idle-entity-after = {PassivateIdleEntityAfter.ToHocon()}");

if (sb.Length > 0)
{
sb.Insert(0, "akka.cluster.sharding {\n");
sb.AppendLine("}");
builder.AddHocon(sb.ToString(), HoconAddMode.Prepend);
}
return sb.ToString();
}
}

Expand Down Expand Up @@ -646,7 +644,7 @@ public static AkkaConfigurationBuilder WithDistributedData(
builder.AddHocon(DistributedData.DistributedData.DefaultConfig(), HoconAddMode.Append);
return builder;
}

/// <summary>
/// Starts a <see cref="ShardRegion"/> actor for the given entity <see cref="typeName"/>
/// and registers the ShardRegion <see cref="IActorRef"/> with <see cref="TKey"/> in the
Expand Down Expand Up @@ -850,7 +848,6 @@ public static AkkaConfigurationBuilder WithShardRegion<TKey>(
IMessageExtractor messageExtractor,
ShardOptions shardOptions)
{
shardOptions.Apply(builder);
builder.AddHocon(
ClusterSharding.DefaultConfig().WithFallback(DistributedData.DistributedData.DefaultConfig()),
HoconAddMode.Append);
Expand All @@ -860,7 +857,12 @@ public static AkkaConfigurationBuilder WithShardRegion<TKey>(
async Task Resolver(ActorSystem system, IActorRegistry registry, IDependencyResolver resolver)
{
var props = entityPropsFactory(system, registry, resolver);
var settings = ClusterShardingSettings.Create(system);
var shardingConfig = shardOptions.ToConfig()
.WithFallback(system.Settings.Config.GetConfig("akka.cluster.sharding"));
var coordinatorConfig = system.Settings.Config.GetConfig(
shardingConfig.GetString("coordinator-singleton"));

var settings = ClusterShardingSettings.Create(shardingConfig, coordinatorConfig);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

var allocationStrategy = ClusterSharding.Get(system).DefaultShardAllocationStrategy(settings);
var shardRegion = await ClusterSharding.Get(system).StartAsync(
typeName, props, settings, messageExtractor, allocationStrategy,
Expand Down Expand Up @@ -911,23 +913,27 @@ public static AkkaConfigurationBuilder WithShardRegion<TKey>(
ExtractShardId extractShardId,
ShardOptions shardOptions)
{
shardOptions.Apply(builder);
builder.AddHocon(
ClusterSharding.DefaultConfig().WithFallback(DistributedData.DistributedData.DefaultConfig()),
HoconAddMode.Append);


return builder.StartActors(Resolver);

async Task Resolver(ActorSystem system, IActorRegistry registry, IDependencyResolver resolver)
{
var props = entityPropsFactory(system, registry, resolver);
var settings = ClusterShardingSettings.Create(system);
var shardingConfig = shardOptions.ToConfig()
.WithFallback(system.Settings.Config.GetConfig("akka.cluster.sharding"));
var coordinatorConfig = system.Settings.Config.GetConfig(
shardingConfig.GetString("coordinator-singleton"));

var settings = ClusterShardingSettings.Create(shardingConfig, coordinatorConfig);
var allocationStrategy = ClusterSharding.Get(system).DefaultShardAllocationStrategy(settings);
var shardRegion = await ClusterSharding.Get(system).StartAsync(
typeName, props, settings, extractEntityId, extractShardId, allocationStrategy,
shardOptions.HandOffStopMessage ?? PoisonPill.Instance).ConfigureAwait(false);
registry.Register<TKey>(shardRegion);
}

return builder.StartActors(Resolver);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ namespace Akka.Cluster.Hosting
public sealed class ShardOptions
{
public ShardOptions() { }
[System.Obsolete("This property is not being applied to the ActorSystem anymore. Use manual HOCON c" +
"onfiguration to set \"akka.cluster.sharding.distributed-data\" values. Since v1.5." +
"27")]
public Akka.Cluster.Hosting.ShardingDDataOptions DistributedData { get; }
public bool? FailOnInvalidEntityStateTransition { get; set; }
public object? HandOffStopMessage { get; set; }
Expand All @@ -104,6 +107,8 @@ namespace Akka.Cluster.Hosting
public Akka.Persistence.Hosting.SnapshotOptions? SnapshotOptions { get; set; }
public string? SnapshotPluginId { get; set; }
public Akka.Cluster.Sharding.StateStoreMode? StateStoreMode { get; set; }
public Akka.Configuration.Config ToConfig() { }
public override string ToString() { }
}
public sealed class ShardingDDataOptions : Akka.Cluster.Hosting.DDataOptions
{
Expand Down
Loading