Skip to content

Commit 8eee12a

Browse files
berndverstrynowak
authored andcommitted
Configurable actor reminder storage patitions
1 parent 1f7f200 commit 8eee12a

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

src/Dapr.Actors/Runtime/ActorRuntime.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ internal Task SerializeSettingsAndRegisteredTypes(IBufferWriter<byte> output)
9393
writer.WriteBoolean("drainRebalancedActors", (this.options.DrainRebalancedActors));
9494
}
9595

96+
// default is null, don't write it if default
97+
if (this.options.RemindersStoragePartitions != null)
98+
{
99+
writer.WriteNumber("remindersStoragePartitions", this.options.RemindersStoragePartitions.Value);
100+
}
101+
96102
writer.WriteEndObject();
97103
return writer.FlushAsync();
98104
}

src/Dapr.Actors/Runtime/ActorRuntimeOptions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public sealed class ActorRuntimeOptions
2222
private bool drainRebalancedActors;
2323
private JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web;
2424
private string daprApiToken = null;
25+
private int? remindersStoragePartitions = null;
2526

2627
/// <summary>
2728
/// Gets the collection of <see cref="ActorRegistration" /> instances.
@@ -162,6 +163,27 @@ public string DaprApiToken
162163
}
163164
}
164165

166+
/// <summary>
167+
/// An int used to determine how many partitions to use for reminders storage.
168+
/// </summary>
169+
public int? RemindersStoragePartitions
170+
{
171+
get
172+
{
173+
return this.remindersStoragePartitions;
174+
}
175+
176+
set
177+
{
178+
if (value < 0)
179+
{
180+
throw new ArgumentOutOfRangeException(nameof(remindersStoragePartitions), remindersStoragePartitions, "must be positive");
181+
}
182+
183+
this.remindersStoragePartitions = value;
184+
}
185+
}
186+
165187
/// <summary>
166188
/// Gets or sets the HTTP endpoint URI used to communicate with the Dapr sidecar.
167189
/// </summary>

test/Dapr.Actors.Test/Runtime/ActorRuntimeOptionsTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,14 @@ public void SettingJsonSerializerOptionsToNull_Fails()
113113

114114
action.Should().Throw<ArgumentNullException>();
115115
}
116+
117+
[Fact]
118+
public void SettingRemindersStoragePartitionsToLessThanZero_Fails()
119+
{
120+
var options = new ActorRuntimeOptions();
121+
Action action = () => options.RemindersStoragePartitions = -1;
122+
123+
action.Should().Throw<ArgumentOutOfRangeException>();
124+
}
116125
}
117126
}

test/Dapr.Actors.Test/Runtime/ActorRuntimeTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Dapr.Actors.Test
77
{
88
using System;
99
using System.Buffers;
10+
using System.Collections.Generic;
1011
using System.Linq;
1112
using System.IO;
1213
using System.Text;
@@ -144,6 +145,51 @@ public async Task TestActorSettings()
144145

145146
element = root.GetProperty("drainRebalancedActors");
146147
Assert.True(element.GetBoolean());
148+
149+
try {
150+
element = root.GetProperty("remindersStoragePartitions");
151+
Assert.False(true, "remindersStoragePartitions should not be serialized");
152+
}
153+
catch (Exception ex) {
154+
Assert.IsType<KeyNotFoundException>(ex);
155+
}
156+
157+
}
158+
159+
[Fact]
160+
public async Task TestActorSettingsWithRemindersStoragePartitions()
161+
{
162+
var actorType = typeof(TestActor);
163+
164+
var options = new ActorRuntimeOptions();
165+
options.Actors.RegisterActor<TestActor>();
166+
options.RemindersStoragePartitions = 12;
167+
168+
var runtime = new ActorRuntime(options, loggerFactory, activatorFactory, proxyFactory);
169+
170+
Assert.Contains(actorType.Name, runtime.RegisteredActors.Select(a => a.Type.ActorTypeName), StringComparer.InvariantCulture);
171+
172+
ArrayBufferWriter<byte> writer = new ArrayBufferWriter<byte>();
173+
await runtime.SerializeSettingsAndRegisteredTypes(writer);
174+
175+
// read back the serialized json
176+
var array = writer.WrittenSpan.ToArray();
177+
string s = Encoding.UTF8.GetString(array, 0, array.Length);
178+
179+
JsonDocument document = JsonDocument.Parse(s);
180+
JsonElement root = document.RootElement;
181+
182+
// parse out the entities array
183+
JsonElement element = root.GetProperty("entities");
184+
Assert.Equal(1, element.GetArrayLength());
185+
186+
JsonElement arrayElement = element[0];
187+
string actor = arrayElement.GetString();
188+
Assert.Equal("TestActor", actor);
189+
190+
element = root.GetProperty("remindersStoragePartitions");
191+
Assert.Equal(12, element.GetInt64());
192+
147193
}
148194

149195
private sealed class TestActor : Actor, ITestActor

0 commit comments

Comments
 (0)