Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions benchmarks/SkyriseMini/Client/ProtoActorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public static WebApplicationBuilder AddProtoActorClient(this WebApplicationBuild

var remoteConfig = GrpcNetRemoteConfig.BindToLocalhost()
.WithProtoMessages(ProtoActorSut.Contracts.ProtosReflection.Descriptor)
.WithChannelOptions(new GrpcChannelOptions
{
CompressionProviders = new[]
{
new GzipCompressionProvider(CompressionLevel.Fastest)
}
}
)
// .WithChannelOptions(new GrpcChannelOptions
// {
// CompressionProviders = new[]
// {
// new GzipCompressionProvider(CompressionLevel.Fastest)
// }
// }
// )
.WithLogLevelForDeserializationErrors(LogLevel.Critical);

var clusterProvider = new ConsulProvider(new ConsulProviderConfig());
Expand Down
16 changes: 8 additions & 8 deletions benchmarks/SkyriseMini/Server/ProtoActorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public static WebApplicationBuilder AddProtoActorSUT(this WebApplicationBuilder

var remoteConfig = GrpcNetRemoteConfig.BindToLocalhost()
.WithProtoMessages(ProtoActorSut.Contracts.ProtosReflection.Descriptor)
.WithChannelOptions(new GrpcChannelOptions
{
CompressionProviders = new[]
{
new GzipCompressionProvider(CompressionLevel.Fastest)
}
}
)
// .WithChannelOptions(new GrpcChannelOptions
// {
// CompressionProviders = new[]
// {
// new GzipCompressionProvider(CompressionLevel.Fastest)
// }
// }
// )
.WithLogLevelForDeserializationErrors(LogLevel.Critical);

var clusterProvider = new ConsulProvider(new ConsulProviderConfig());
Expand Down
93 changes: 57 additions & 36 deletions src/Proto.Cluster/DefaultClusterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// -----------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -54,12 +55,7 @@ public DefaultClusterContext(Cluster cluster)
i++;

var source = PidSource.Cache;
var pid = clusterIdentity.CachedPid;

if (pid == null)
{
pid = GetCachedPid(clusterIdentity);
}
var pid = clusterIdentity.CachedPid ?? (_pidCache.TryGet(clusterIdentity, out var tmp) ? tmp : null);

if (pid is null)
{
Expand All @@ -75,7 +71,9 @@ public DefaultClusterContext(Cluster cluster)
}

// Ensures that a future is not re-used against another actor.
if (lastPid is not null && !pid.Equals(lastPid)) RefreshFuture();
// avoid equality check for perf
// ReSharper disable once PossibleUnintendedReferenceComparison
if (lastPid is not null && pid != lastPid) RefreshFuture();

Stopwatch t = null!;

Expand All @@ -93,20 +91,40 @@ public DefaultClusterContext(Cluster cluster)

if (task.IsCompleted)
{
var (status, result) = ToResult<T>(source, context, task.Result);
var untypedResult = MessageEnvelope.UnwrapMessage(task.Result);

switch (status)
if (untypedResult is T t1)
{
return t1;
}

if (typeof(T) == typeof(MessageEnvelope))
{
return (T) (object) MessageEnvelope.Wrap(task.Result);
}

if (untypedResult == null)
{
//null = timeout
return default;
}

if (untypedResult is DeadLetterResponse)
{
case ResponseStatus.Ok: return result;
case ResponseStatus.InvalidResponse:
RefreshFuture();
await RemoveFromSource(clusterIdentity, source, pid);
break;
case ResponseStatus.DeadLetter:
RefreshFuture();
await RemoveFromSource(clusterIdentity, PidSource.Lookup, pid);
break;
if (!context.System.Shutdown.IsCancellationRequested && Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug("TryRequestAsync failed, dead PID from {Source}", source);
}

RefreshFuture();
await RemoveFromSource(clusterIdentity, PidSource.Lookup, pid);
break;
}

Logger.LogError("Unexpected message. Was type {Type} but expected {ExpectedType}", untypedResult.GetType(), typeof(T));
RefreshFuture();
await RemoveFromSource(clusterIdentity, source, pid);
break;
}
else
{
Expand Down Expand Up @@ -182,9 +200,6 @@ private async ValueTask RemoveFromSource(ClusterIdentity clusterIdentity, PidSou
_pidCache.RemoveByVal(clusterIdentity, pid);
}


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

private async ValueTask<PID?> GetPidFromLookup(ClusterIdentity clusterIdentity, ISenderContext context, CancellationToken ct)
{
try
Expand Down Expand Up @@ -218,26 +233,32 @@ private async ValueTask RemoveFromSource(ClusterIdentity clusterIdentity, PidSou
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static (ResponseStatus Ok, T?) ToResult<T>(PidSource source, ISenderContext context, object result)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not used anymore and can be removed.

{
var message = MessageEnvelope.UnwrapMessage(result);
switch (message)

if (message is DeadLetterResponse)
{
case DeadLetterResponse:
if (!context.System.Shutdown.IsCancellationRequested)
if (Logger.IsEnabled(LogLevel.Debug)) Logger.LogDebug("TryRequestAsync failed, dead PID from {Source}", source);

return (ResponseStatus.DeadLetter, default);
case null: return (ResponseStatus.Ok, default);
case T t: return (ResponseStatus.Ok, t);
default:
if (typeof(T) == typeof(MessageEnvelope))
{
return (ResponseStatus.Ok, (T) (object) MessageEnvelope.Wrap(result));
}
Logger.LogError("Unexpected message. Was type {Type} but expected {ExpectedType}", message.GetType(), typeof(T));
return (ResponseStatus.InvalidResponse, default);
if (!context.System.Shutdown.IsCancellationRequested && Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug("TryRequestAsync failed, dead PID from {Source}", source);
}

return (ResponseStatus.DeadLetter, default);
}

if (message == null) return (ResponseStatus.Ok, default);

if (message is T t) return (ResponseStatus.Ok, t);

if (typeof(T) == typeof(MessageEnvelope))
{
return (ResponseStatus.Ok, (T) (object) MessageEnvelope.Wrap(result));
}

Logger.LogError("Unexpected message. Was type {Type} but expected {ExpectedType}", message.GetType(), typeof(T));
return (ResponseStatus.InvalidResponse, default);
}

private enum ResponseStatus
Expand Down