Skip to content

Commit 261910e

Browse files
authored
Fix ChannelExecutor configuration backward compatibility problem (#5568)
* Fix ChannelExecutor configuration backward compatibility problem * Move channel scheduler configuration from `akka.actor.channel-scheduler` to `akka.channel-scheduler`
1 parent d4cb079 commit 261910e

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// //-----------------------------------------------------------------------
2+
// // <copyright file="ChannelExecutorConfigurationSpec.cs" company="Akka.NET Project">
3+
// // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
4+
// // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
5+
// // </copyright>
6+
// //-----------------------------------------------------------------------
7+
8+
using System.Threading.Tasks;
9+
using Akka.Actor;
10+
using Akka.Configuration;
11+
using Akka.Dispatch;
12+
using Akka.TestKit;
13+
using Xunit;
14+
using FluentAssertions;
15+
16+
namespace Akka.Tests.Dispatch
17+
{
18+
public class ChannelExecutorConfigurationSpec : AkkaSpec
19+
{
20+
[Fact]
21+
public void ChannelExecutor_config_should_be_injected_when_it_doesnt_exist()
22+
{
23+
var config = ConfigurationFactory.ParseString(@"executor = channel-executor");
24+
var configurator = new ChannelExecutorConfigurator(config, Sys.Dispatchers.Prerequisites);
25+
configurator.Priority.Should().Be(TaskSchedulerPriority.Normal);
26+
}
27+
28+
[Fact]
29+
public void ChannelExecutor_default_should_be_overriden_by_config()
30+
{
31+
var config = ConfigurationFactory.ParseString(@"
32+
executor = channel-executor
33+
channel-executor.priority = high");
34+
var configurator = new ChannelExecutorConfigurator(config, Sys.Dispatchers.Prerequisites);
35+
configurator.Priority.Should().Be(TaskSchedulerPriority.High);
36+
}
37+
38+
[Fact]
39+
public void ChannelExecutorConfigurator_should_use_default_when_config_is_null()
40+
{
41+
var configurator = new ChannelExecutorConfigurator(null, Sys.Dispatchers.Prerequisites);
42+
configurator.Priority.Should().Be(TaskSchedulerPriority.Normal);
43+
}
44+
45+
// backward compatibility test
46+
[Fact]
47+
public async Task ChannelExecutor_instantiation_should_not_throw_when_config_doesnt_exist()
48+
{
49+
var config = ConfigurationFactory.ParseString(@"
50+
akka.actor.default-dispatcher = {
51+
executor = channel-executor
52+
}");
53+
54+
// Throws NRE in 1.4.29-32
55+
var sys = ActorSystem.Create("test", config);
56+
57+
// Check that all settings are correct
58+
var dispatcher = sys.Dispatchers.Lookup("akka.actor.default-dispatcher");
59+
dispatcher.Configurator.Config.GetString("executor").Should().Be("channel-executor");
60+
61+
var configurator = new ChannelExecutorConfigurator(dispatcher.Configurator.Config, Sys.Dispatchers.Prerequisites);
62+
configurator.Priority.Should().Be(TaskSchedulerPriority.Normal);
63+
64+
await sys.Terminate();
65+
}
66+
}
67+
}

src/core/Akka/Configuration/Pigeon.conf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,6 @@ akka {
269269
}
270270
}
271271

272-
channel-scheduler {
273-
parallelism-min = 4 #same as for ForkJoinDispatcher
274-
parallelism-factor = 1 #same as for ForkJoinDispatcher
275-
parallelism-max = 64 #same as for ForkJoinDispatcher
276-
work-max = 10 #max executed work items in sequence until priority loop
277-
work-interval = 500 #time target of executed work items in ms
278-
work-step = 2 #target work item count in interval / burst
279-
}
280-
281272
#used for GUI applications
282273
synchronized-dispatcher {
283274
type = "SynchronizedDispatcher"
@@ -561,6 +552,15 @@ akka {
561552
}
562553
}
563554

555+
channel-scheduler {
556+
parallelism-min = 4 #same as for ForkJoinDispatcher
557+
parallelism-factor = 1 #same as for ForkJoinDispatcher
558+
parallelism-max = 64 #same as for ForkJoinDispatcher
559+
work-max = 10 #max executed work items in sequence until priority loop
560+
work-interval = 500 #time target of executed work items in ms
561+
work-step = 2 #target work item count in interval / burst
562+
}
563+
564564
# Used to set the behavior of the scheduler.
565565
# Changing the default values may change the system behavior drastically so make
566566
# sure you know what you're doing! See the Scheduler section of the Akka

src/core/Akka/Dispatch/AbstractDispatcher.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,16 @@ protected ExecutorServiceConfigurator(Config config, IDispatcherPrerequisites pr
118118

119119
internal sealed class ChannelExecutorConfigurator : ExecutorServiceConfigurator
120120
{
121+
private static readonly Config PriorityDefault = ConfigurationFactory.ParseString(@"
122+
executor = channel-executor
123+
channel-executor.priority = normal");
124+
121125
public ChannelExecutorConfigurator(Config config, IDispatcherPrerequisites prerequisites) : base(config, prerequisites)
122126
{
123-
var cfg = config.GetConfig("channel-executor");
124-
Priority = (TaskSchedulerPriority)Enum.Parse(typeof(TaskSchedulerPriority), cfg.GetString("priority", "normal"), true);
127+
config = config == null ? PriorityDefault : config.WithFallback(PriorityDefault);
128+
129+
var priority = config.GetString("channel-executor.priority", "normal");
130+
Priority = (TaskSchedulerPriority)Enum.Parse(typeof(TaskSchedulerPriority), priority, true);
125131
}
126132

127133
public TaskSchedulerPriority Priority { get; }

src/core/Akka/Dispatch/ChannelSchedulerExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public void Dispose()
349349
/// and to help execute queued works internaly.
350350
/// It supports task-inlining only for task equal or above the own priority
351351
/// </summary>
352-
sealed class PriorityTaskScheduler : TaskScheduler, IDisposable
352+
internal sealed class PriorityTaskScheduler : TaskScheduler, IDisposable
353353
{
354354
readonly Channel<Task> _channel;
355355

0 commit comments

Comments
 (0)