Skip to content

Commit 9017e6e

Browse files
authored
Valuetask clustercontext (#1698)
* ValueTask cluster context * add tps to skyrise benchmark * short pids
1 parent de6c2d8 commit 9017e6e

File tree

8 files changed

+58
-18
lines changed

8 files changed

+58
-18
lines changed

benchmarks/SkyriseMini/Client/Tests/MessagingTest.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace SkyriseMini.Tests;
66

77
public class MessagingTest
88
{
9-
readonly Activate _activate;
10-
readonly Ping _ping;
11-
readonly ILogger<MessagingTest> _logger;
9+
private readonly Activate _activate;
10+
private readonly Ping _ping;
11+
private readonly ILogger<MessagingTest> _logger;
1212

1313
public MessagingTest(Activate activate, Ping ping, ILogger<MessagingTest> logger)
1414
{
@@ -60,26 +60,30 @@ async Task<object[]> ActivateActors(string[] actorIds)
6060
var overallStopwatch = new Stopwatch();
6161
overallStopwatch.Start();
6262

63-
var tasks = handles.Select(async handle =>
64-
{
65-
var messageStopwatch = new Stopwatch();
66-
while (!cancel.IsCancellationRequested)
63+
64+
bool error = false;
65+
var sw = Stopwatch.StartNew();
66+
var tasks = handles.Select(async handle => {
67+
while (!cancel.IsCancellationRequested && !error)
6768
{
6869
try
6970
{
70-
messageStopwatch.Restart();
7171
await _ping(handle, Guid.NewGuid().ToString("N"));
7272

73-
Interlocked.Increment(ref totalMessages);
73+
var res = Interlocked.Increment(ref totalMessages);
74+
75+
if (res % 100000 == 0)
76+
{
77+
var tps = (int)(totalMessages / (double) sw.ElapsedMilliseconds * 1000.0);
78+
Console.WriteLine(tps);
79+
}
7480
}
7581
catch (Exception e)
7682
{
83+
error = true;
7784
_logger.LogError(e, "Error during test");
78-
7985
}
8086
}
81-
82-
messageStopwatch.Stop();
8387
});
8488

8589
await Task.WhenAll(tasks);

src/Proto.Actor/Context/ActorContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ public PID SpawnNamed(Props props, string name, Action<IContext>? callback = nul
9595

9696
try
9797
{
98-
var pid = props.Spawn(System, $"{Self.Id}/{name}", Self, callback);
98+
var id = name switch
99+
{
100+
"" => System.ProcessRegistry.NextId(),
101+
_ => $"{Self.Id}/{name}",
102+
};
103+
104+
var pid = props.Spawn(System, id, Self, callback);
99105
EnsureExtras().AddChild(pid);
100106

101107
return pid;

src/Proto.Actor/Context/ISpawnerContext.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,18 @@ public static class SpawnerContextExtensions
3030
/// <returns>The PID of the child actor</returns>
3131
public static PID Spawn(this ISpawnerContext self, Props props)
3232
{
33-
var id = self.System.ProcessRegistry.NextId();
34-
return self.SpawnNamed(props, id);
33+
return self.SpawnNamed(props, "");
34+
}
35+
36+
/// <summary>
37+
/// Spawns a new child actor based on props and named with a unique ID.
38+
/// </summary>
39+
/// <param name="props">The Props used to spawn the actor</param>
40+
/// <param name="callback"></param>
41+
/// <returns>The PID of the child actor</returns>
42+
public static PID Spawn(this ISpawnerContext self, Props props, Action<IContext> callback)
43+
{
44+
return self.SpawnNamed(props, "", callback);
3545
}
3646

3747
/// <summary>

src/Proto.Actor/Context/RootContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public PID SpawnNamed(Props props, string name, Action<IContext>? callback=null)
6767
{
6868
try
6969
{
70+
if (string.IsNullOrEmpty(name))
71+
{
72+
name = System.ProcessRegistry.NextId();
73+
}
74+
7075
var parent = props.GuardianStrategy is not null
7176
? System.Guardians.GetGuardianPid(props.GuardianStrategy)
7277
: null;

src/Proto.Cluster/DefaultClusterContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private async ValueTask RemoveFromSource(ClusterIdentity clusterIdentity, PidSou
185185

186186
private PID? GetCachedPid(ClusterIdentity clusterIdentity) => _pidCache.TryGet(clusterIdentity, out var pid) ? pid : null;
187187

188-
private async Task<PID?> GetPidFromLookup(ClusterIdentity clusterIdentity, ISenderContext context, CancellationToken ct)
188+
private async ValueTask<PID?> GetPidFromLookup(ClusterIdentity clusterIdentity, ISenderContext context, CancellationToken ct)
189189
{
190190
try
191191
{

src/Proto.Cluster/Partition/PartitionPlacementActor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ private void Spawn(ActivationRequest msg, IContext context, ActivatedClusterKind
394394
{
395395
try
396396
{
397-
var pid = context.SpawnPrefix(clusterKind.Props, msg.ClusterIdentity.Identity, ctx => ctx.Set(msg.ClusterIdentity));
397+
var pid = context.Spawn(clusterKind.Props, ctx => ctx.Set(msg.ClusterIdentity));
398398
_actors.Add(msg.ClusterIdentity, pid);
399399
context.Respond(new ActivationResponse
400400
{

src/Proto.Cluster/PartitionActivator/PartitionActivatorActor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private void Spawn(ActivationRequest msg, IContext context, ActivatedClusterKind
225225
{
226226
try
227227
{
228-
var pid = context.SpawnPrefix(clusterKind.Props, msg.ClusterIdentity.Identity, ctx => ctx.Set(msg.ClusterIdentity));
228+
var pid = context.Spawn(clusterKind.Props, ctx => ctx.Set(msg.ClusterIdentity));
229229
_actors.Add(msg.ClusterIdentity, pid);
230230
context.Respond(new ActivationResponse
231231
{

tests/Proto.Remote.Tests/SerializationTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public class MockSerializer2 : ISerializer
2727
public ByteString Serialize(object obj) => ByteString.CopyFrom(new byte[0]);
2828
}
2929

30+
[Fact]
31+
public void ProtobufDefaultValuesAreSameAsEmpty()
32+
{
33+
var p1 = new PID();
34+
var p2 = new PID()
35+
{
36+
Address = "",
37+
Id = "",
38+
};
39+
40+
var b1 = p1.ToByteArray();
41+
var b2 = p2.ToByteArray();
42+
b1.Length.Should().Be(b2.Length);
43+
}
44+
3045
[Fact]
3146
public void CanUtilizeMultipleSerializers()
3247
{

0 commit comments

Comments
 (0)