#312 - make changes corresponding to change in dapr runtime. The run… (#313)

* #312 - make changes corresponding to change in dapr runtime.  The runtime will no longer send an activate message.  The SDK should 'activate' an actor anytime it receives an actor method call

* spaces

* remove comment'

Co-authored-by: LM <lemai>
This commit is contained in:
Leon Mai 2020-05-16 17:06:49 -07:00 committed by GitHub
parent e773243487
commit 6c27126223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 28 deletions

View File

@ -26,8 +26,7 @@ namespace Dapr.Actors.AspNetCore
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/healthz");
endpoints.AddDaprConfigRoute();
endpoints.AddActorActivationRoute();
endpoints.AddDaprConfigRoute();
endpoints.AddActorDeactivationRoute();
endpoints.AddActorMethodRoute();
endpoints.AddReminderRoute();

View File

@ -20,18 +20,7 @@ namespace Dapr.Actors.AspNetCore
context.Response.ContentType = "application/json";
await ActorRuntime.Instance.SerializeSettingsAndRegisteredTypes(context.Response.BodyWriter);
});
}
public static IEndpointConventionBuilder AddActorActivationRoute(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapPost("actors/{actorTypeName}/{actorId}", async context =>
{
var routeValues = context.Request.RouteValues;
var actorTypeName = (string)routeValues["actorTypeName"];
var actorId = (string)routeValues["actorId"];
await ActorRuntime.ActivateAsync(actorTypeName, actorId);
});
}
}
public static IEndpointConventionBuilder AddActorDeactivationRoute(this IEndpointRouteBuilder endpoints)
{

View File

@ -226,9 +226,13 @@ namespace Dapr.Actors.Runtime
private async Task<T> DispatchInternalAsync<T>(ActorId actorId, ActorMethodContext actorMethodContext, Func<Actor, CancellationToken, Task<T>> actorFunc, CancellationToken cancellationToken)
{
if (!this.activeActors.TryGetValue(actorId, out var actor))
if (!this.activeActors.ContainsKey(actorId))
{
// This should never happen, as "Dapr" runtime activates the actor first. if it ever it would mean a bug in "Dapr" runtime.
await this.ActivateActor(actorId);
}
if (!this.activeActors.TryGetValue(actorId, out var actor))
{
var errorMsg = $"Actor {actorId} is not yet activated.";
ActorTrace.Instance.WriteError(TraceType, errorMsg);
throw new InvalidOperationException(errorMsg);

View File

@ -118,17 +118,6 @@ namespace Dapr.Actors.Runtime
return writer.FlushAsync();
}
/// <summary>
/// Activates an actor for an actor type with given actor id.
/// </summary>
/// <param name="actorTypeName">Actor type name to activate the actor for.</param>
/// <param name="actorId">Actor id for the actor to be activated.</param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
internal static async Task ActivateAsync(string actorTypeName, string actorId)
{
await Instance.GetActorManager(actorTypeName).ActivateActor(new ActorId(actorId));
}
/// <summary>
/// Deactivates an actor for an actor type with given actor id.
/// </summary>
@ -201,7 +190,7 @@ namespace Dapr.Actors.Runtime
{
if (!this.actorManagers.TryGetValue(actorTypeName, out var actorManager))
{
var errorMsg = $"Actor type {actorTypeName} is not registerd with Actor runtime.";
var errorMsg = $"Actor type {actorTypeName} is not registered with Actor runtime.";
ActorTrace.Instance.WriteError(TraceType, errorMsg);
throw new InvalidOperationException(errorMsg);
}

View File

@ -7,8 +7,11 @@ namespace Dapr.Actors.Test
{
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Dapr.Actors;
using Dapr.Actors.Runtime;
using Xunit;
@ -49,6 +52,23 @@ namespace Dapr.Actors.Test
Assert.Contains(RenamedActorTypeName, actorRuntime.RegisteredActorTypes, StringComparer.InvariantCulture);
}
// This tests the change that removed the Activate message from Dapr runtime -> app.
[Fact]
public void NoActivateMessageFromRuntime()
{
var actorType = typeof(MyActor);
ActorRuntime.Instance.RegisterActor<MyActor>();
var output = new MemoryStream();
ActorRuntime.DispatchWithoutRemotingAsync("MyActor", "abc", "MyMethod", new MemoryStream(), output).GetAwaiter().GetResult();
string s = Encoding.UTF8.GetString(output.ToArray());
Assert.Equal("\"hi\"", s);
Assert.Contains(actorType.Name, ActorRuntime.Instance.RegisteredActorTypes, StringComparer.InvariantCulture);
Console.WriteLine("done");
}
[Fact]
public void TestActorSettings()
{
@ -117,5 +137,23 @@ namespace Dapr.Actors.Test
{
}
}
private interface IAnotherActor : IActor
{
public Task<string> MyMethod();
}
private sealed class MyActor : Actor, IAnotherActor
{
public MyActor(ActorService actorService, ActorId actorId)
: base(actorService, actorId)
{
}
public Task<string> MyMethod()
{
return Task.FromResult("hi");
}
}
}
}