Skip to content

Commit 2d22e29

Browse files
committed
Don't passivate idle for remembering entities
1 parent dbc25db commit 2d22e29

File tree

8 files changed

+15
-92
lines changed

8 files changed

+15
-92
lines changed

docs/articles/clustering/cluster-sharding.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ To reduce memory consumption, you may decide to stop entities after some period
8282

8383
### Automatic Passivation
8484

85-
The entities can be configured to be automatically passivated if they haven't received a message for a while using the `akka.cluster.sharding.passivate-idle-entity-after` setting, or by explicitly setting `ClusterShardingSettings.passivateIdleEntityAfter` to a suitable time to keep the actor alive. Note that only messages sent through sharding are counted, so direct messages to the `ActorRef` of the actor or messages that it sends to itself are not counted as activity. By default automatic passivation is disabled.
85+
The entities can be configured to be automatically passivated if they haven't received a message for a while using the `akka.cluster.sharding.passivate-idle-entity-after` setting, or by explicitly setting `ClusterShardingSettings.PassivateIdleEntityAfter` to a suitable time to keep the actor alive. Note that only messages sent through sharding are counted, so direct messages to the `ActorRef` of the actor or messages that it sends to itself are not counted as activity. Passivation can be disabled by setting `akka.cluster.sharding.passivate-idle-entity-after = off`. It is always disabled if @ref:[Remembering Entities](#remembering-entities) is enabled.
8686

8787
## Remembering entities
8888

src/contrib/cluster/Akka.Cluster.Sharding.Tests/InactiveEntityPassivationSpec.cs

Lines changed: 8 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Akka.Cluster.Tools.Singleton;
1414
using Akka.Configuration;
1515
using Akka.TestKit;
16-
using Akka.TestKit.Xunit2;
1716
using FluentAssertions;
1817
using Xunit;
1918

@@ -48,7 +47,6 @@ protected override void OnReceive(object message)
4847
switch (message)
4948
{
5049
case Passivate _:
51-
Probe.Tell($"{_id} passivating");
5250
Context.Stop(Self);
5351
break;
5452
default:
@@ -112,9 +110,9 @@ public static Config GetConfig()
112110
.WithFallback(ClusterSingletonManager.DefaultConfig());
113111
}
114112

115-
protected IActorRef Start(TestProbe probe, bool rememberEntities)
113+
protected IActorRef Start(TestProbe probe)
116114
{
117-
settings = ClusterShardingSettings.Create(Sys).WithRememberEntities(rememberEntities);
115+
settings = ClusterShardingSettings.Create(Sys);
118116
// single node cluster
119117
Cluster.Get(Sys).Join(Cluster.Get(Sys).SelfAddress);
120118

@@ -134,9 +132,9 @@ protected TimeSpan TimeUntilPassivate(IActorRef region, TestProbe probe)
134132
region.Tell(2);
135133
var responses = new[]
136134
{
137-
probe.ExpectMsg<Entity.GotIt>(),
138-
probe.ExpectMsg<Entity.GotIt>()
139-
};
135+
probe.ExpectMsg<Entity.GotIt>(),
136+
probe.ExpectMsg<Entity.GotIt>()
137+
};
140138
responses.Select(r => r.Id).Should().BeEquivalentTo("1", "2");
141139
var timeOneSawMessage = responses.Single(r => r.Id == "1").When;
142140

@@ -163,97 +161,21 @@ public InactiveEntityPassivationSpec()
163161
public void Passivation_of_inactive_entities_must_passivate_entities_when_they_have_not_seen_messages_for_the_configured_duration()
164162
{
165163
var probe = CreateTestProbe();
166-
var region = Start(probe, false);
164+
var region = Start(probe);
167165

168166
// make sure "1" hasn't seen a message in 3 seconds and passivates
169-
//probe.ExpectNoMsg(TimeUntilPassivate(region, probe));
170-
171-
region.Tell(1);
172-
region.Tell(2);
173-
var responses = new[]
174-
{
175-
probe.ExpectMsg<Entity.GotIt>(),
176-
probe.ExpectMsg<Entity.GotIt>()
177-
};
178-
responses.Select(r => r.Id).Should().BeEquivalentTo("1", "2");
179-
var timeOneSawMessage = responses.Single(r => r.Id == "1").When;
180-
181-
Thread.Sleep(1000);
182-
region.Tell(2);
183-
probe.ExpectMsg<Entity.GotIt>().Id.ShouldBe("2");
184-
Thread.Sleep(1000);
185-
region.Tell(2);
186-
probe.ExpectMsg<Entity.GotIt>().Id.ShouldBe("2");
187-
188-
var timeSinceOneSawAMessage = DateTime.Now.Ticks - timeOneSawMessage;
189-
probe.ExpectNoMsg(settings.PassivateIdleEntityAfter - TimeSpan.FromTicks(timeSinceOneSawAMessage) - smallTolerance);
190-
191-
probe.ExpectMsg("1 passivating");
167+
probe.ExpectNoMsg(TimeUntilPassivate(region, probe));
192168

193169
// but it can be re activated
194170
region.Tell(1);
195171
region.Tell(2);
196-
responses = new[]
197-
{
198-
probe.ExpectMsg<Entity.GotIt>(),
199-
probe.ExpectMsg<Entity.GotIt>()
200-
};
201-
responses.Select(r => r.Id).Should().BeEquivalentTo("1", "2");
202-
}
203-
}
204172

205-
public class InactivePersistentEntityPassivationSpec : AbstractInactiveEntityPassivationSpec
206-
{
207-
public InactivePersistentEntityPassivationSpec()
208-
: base(ConfigurationFactory.ParseString(@"akka.cluster.sharding.passivate-idle-entity-after = 3s"))
209-
{
210-
}
211-
212-
[Fact]
213-
public void Passivation_of_inactive_persistent_entities_must_passivate_entities_when_they_have_not_seen_messages_for_the_configured_duration()
214-
{
215-
var probe = CreateTestProbe();
216-
var region = Start(probe, true);
217-
218-
region.Tell(1);
219-
region.Tell(2);
220173
var responses = new[]
221174
{
222175
probe.ExpectMsg<Entity.GotIt>(),
223176
probe.ExpectMsg<Entity.GotIt>()
224177
};
225178
responses.Select(r => r.Id).Should().BeEquivalentTo("1", "2");
226-
227-
Thread.Sleep(1500);
228-
229-
region.Tell(1);
230-
probe.ExpectMsg<Entity.GotIt>(m => m.Id == "1");
231-
232-
Thread.Sleep(1500);
233-
234-
region.Tell(1);
235-
236-
probe.ExpectMsgAllOf<object>(
237-
new Entity.GotIt("1", null, 0),
238-
"2 passivating"
239-
);
240-
241-
Thread.Sleep(1300);
242-
243-
region.Tell(1);
244-
probe.ExpectMsg<Entity.GotIt>(m => m.Id == "1");
245-
246-
probe.ExpectMsg("1 passivating", TimeSpan.FromSeconds(5));
247-
248-
// but it can be re activated
249-
region.Tell(1);
250-
region.Tell(2);
251-
responses = new[]
252-
{
253-
probe.ExpectMsg<Entity.GotIt>(),
254-
probe.ExpectMsg<Entity.GotIt>()
255-
};
256-
responses.Select(r => r.Id).Should().BeEquivalentTo("1", "2");
257179
}
258180
}
259181

@@ -268,7 +190,7 @@ public DisabledInactiveEntityPassivationSpec()
268190
public void Passivation_of_inactive_entities_must_not_passivate_when_passivation_is_disabled()
269191
{
270192
var probe = CreateTestProbe();
271-
var region = Start(probe, false);
193+
var region = Start(probe);
272194
probe.ExpectNoMsg(TimeUntilPassivate(region, probe));
273195
}
274196
}

src/contrib/cluster/Akka.Cluster.Sharding/ClusterShardingSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public sealed class ClusterShardingSettings : INoSerializationVerificationNeeded
187187
/// Passivate entities that have not received any message in this interval.
188188
/// Note that only messages sent through sharding are counted, so direct messages
189189
/// to the <see cref="IActorRef"/> of the actor or messages that it sends to itself are not counted as activity.
190-
/// Use 0 to disable automatic passivation.
190+
/// Use 0 to disable automatic passivation. It is always disabled if `RememberEntities` is enabled.
191191
/// </summary>
192192
public readonly TimeSpan PassivateIdleEntityAfter;
193193

src/contrib/cluster/Akka.Cluster.Sharding/DDataShard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public DDataShard(
100100
: EntityRecoveryStrategy.AllStrategy;
101101

102102
var idleInterval = TimeSpan.FromTicks(Settings.PassivateIdleEntityAfter.Ticks / 2);
103-
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero
103+
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero && !Settings.RememberEntities
104104
? Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(idleInterval, idleInterval, Self, Shard.PassivateIdleTick.Instance, Self)
105105
: null;
106106

src/contrib/cluster/Akka.Cluster.Sharding/PersistentShard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public PersistentShard(
7777
: EntityRecoveryStrategy.AllStrategy;
7878

7979
var idleInterval = TimeSpan.FromTicks(Settings.PassivateIdleEntityAfter.Ticks / 2);
80-
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero
80+
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero && !Settings.RememberEntities
8181
? Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(idleInterval, idleInterval, Self, Shard.PassivateIdleTick.Instance, Self)
8282
: null;
8383
}

src/contrib/cluster/Akka.Cluster.Sharding/Shard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public Shard(
407407
: EntityRecoveryStrategy.AllStrategy;
408408

409409
var idleInterval = TimeSpan.FromTicks(Settings.PassivateIdleEntityAfter.Ticks / 2);
410-
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero
410+
PassivateIdleTask = Settings.PassivateIdleEntityAfter > TimeSpan.Zero && !Settings.RememberEntities
411411
? Context.System.Scheduler.ScheduleTellRepeatedlyCancelable(idleInterval, idleInterval, Self, PassivateIdleTick.Instance, Self)
412412
: null;
413413

src/contrib/cluster/Akka.Cluster.Sharding/ShardRegion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ protected object RegistrationMessage
454454
protected override void PreStart()
455455
{
456456
Cluster.Subscribe(Self, typeof(ClusterEvent.IMemberEvent));
457-
if (Settings.PassivateIdleEntityAfter > TimeSpan.Zero)
457+
if (Settings.PassivateIdleEntityAfter > TimeSpan.Zero && !Settings.RememberEntities)
458458
{
459459
Log.Info($"Idle entities will be passivated after [{Settings.PassivateIdleEntityAfter}]");
460460
}

src/contrib/cluster/Akka.Cluster.Sharding/reference.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ akka.cluster.sharding {
2525

2626
# Set this to a time duration to have sharding passivate entities when they have not
2727
# gotten any message in this long time. Set to 'off' to disable.
28+
# It is always disabled if `remember-entities` is enabled.
2829
passivate-idle-entity-after = 120s
2930

3031
# If the coordinator can't store state changes it will be stopped

0 commit comments

Comments
 (0)