Skip to content

Commit 65ce9a8

Browse files
committed
🚀 feat(Actor): 优化激活和停用方法,新增取消令牌参数以支持异步操作
1 parent 081da26 commit 65ce9a8

6 files changed

Lines changed: 27 additions & 14 deletions

File tree

Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<PackageReference Update="Microsoft.Orleans.OrleansProviders" Version="8.2.0"/>
1717
<PackageReference Update="Microsoft.Orleans.Core" Version="8.2.0" />
18+
<PackageReference Update="Microsoft.Orleans.Sdk" Version="8.2.0" />
1819
<PackageReference Update="Microsoft.Orleans.Core.Abstractions" Version="8.2.0" />
1920
<PackageReference Update="Microsoft.Orleans.Runtime.Abstractions" Version="8.2.0" />
2021
<PackageReference Update="Microsoft.Orleans.OrleansRuntime" Version="8.2.0" />
@@ -28,7 +29,7 @@
2829
<PackageReference Update="RabbitMQ.Client" Version="6.8.1" />
2930
<PackageReference Update="System.Reactive" Version="6.0.1" />
3031
<PackageReference Update="linq2db" Version="4.1.1" />
31-
<PackageReference Update="IdGen" Version="3.0.1" />
32+
<PackageReference Update="IdGen" Version="3.0.7" />
3233

3334
<PackageReference Update="Lindhart.Analyser.MissingAwaitWarning" Version="2.0.0" PrivateAssets="All" />
3435
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="All" />

src/Vertex.Runtime/Actor/ActorBase.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using Orleans;
45
using Vertex.Abstractions.Actor;
@@ -18,12 +19,17 @@ public ActorBase()
1819
/// </summary>
1920
public TPrimaryKey ActorId { get; private set; }
2021

22+
public Task OnActivateAsync()
23+
{
24+
return this.OnActivateAsync(CancellationToken.None);
25+
}
26+
2127
/// <summary>
2228
/// Gets the real Type of the current Grain.
2329
/// </summary>
2430
protected Type ActorType { get; }
2531

26-
public override Task OnActivateAsync()
32+
public override Task OnActivateAsync(CancellationToken cancellationToken)
2733
{
2834
var type = typeof(TPrimaryKey);
2935
if (type == typeof(long) && this.GetPrimaryKeyLong() is TPrimaryKey longKey)
@@ -43,7 +49,7 @@ public override Task OnActivateAsync()
4349
throw new ArgumentOutOfRangeException(typeof(TPrimaryKey).FullName);
4450
}
4551

46-
return base.OnActivateAsync();
52+
return base.OnActivateAsync(cancellationToken);
4753
}
4854
}
4955
}

src/Vertex.Runtime/Actor/FlowActor.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
using System.Reflection;
88
using System.Reflection.Emit;
99
using System.Runtime.CompilerServices;
10+
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Microsoft.Extensions.Caching.Distributed;
1213
using Microsoft.Extensions.DependencyInjection;
1314
using Microsoft.Extensions.Logging;
1415
using Microsoft.Extensions.Options;
16+
using Orleans;
1517
using Orleans.Concurrency;
1618
using Orleans.Runtime;
1719
using Vertex.Abstractions.Actor;
@@ -364,10 +366,10 @@ protected virtual async ValueTask DependencyInjection()
364366
var snapshotStorageFactory = this.ServiceProvider.GetService<ISubSnapshotStorageFactory>();
365367
this.SnapshotStorage = await snapshotStorageFactory.Create(this);
366368
}
367-
368-
public override async Task OnActivateAsync()
369+
370+
public override async Task OnActivateAsync(CancellationToken cancellationToken)
369371
{
370-
await base.OnActivateAsync();
372+
await base.OnActivateAsync(cancellationToken);
371373
await this.DependencyInjection();
372374

373375
if (this.ConcurrentHandle)
@@ -395,13 +397,14 @@ public override async Task OnActivateAsync()
395397
}
396398
}
397399

398-
public override async Task OnDeactivateAsync()
400+
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
399401
{
400402
await this.SaveSnapshotAsync(isDeactivate: true);
401403

402404
if (this.Logger.IsEnabled(LogLevel.Trace))
403405
{
404406
this.Logger.LogTrace("Deactivate completed: {0}->{1}", this.ActorType.Name, this.Serializer.Serialize(this.Snapshot));
407+
this.Logger.LogTrace("Deactivate reason: {0}->{1}", this.ActorType.Name, reason.Description);
405408
}
406409
}
407410

src/Vertex.Runtime/Actor/ShadowActor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Data;
44
using System.Linq;
55
using System.Runtime.CompilerServices;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.Extensions.DependencyInjection;
89
using Microsoft.Extensions.Logging;
@@ -65,9 +66,9 @@ protected virtual async ValueTask DependencyInjection()
6566
this.SnapshotStorage = await snapshotStorageFactory.Create(this);
6667
}
6768

68-
public override async Task OnActivateAsync()
69+
public override async Task OnActivateAsync(CancellationToken cancellationToken)
6970
{
70-
await base.OnActivateAsync();
71+
await base.OnActivateAsync(cancellationToken);
7172
await this.DependencyInjection();
7273

7374
try

src/Vertex.Runtime/Actor/VertexActor.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
using System.Reflection;
66
using System.Runtime.CompilerServices;
77
using System.Text;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Logging;
1112
using Microsoft.Extensions.Options;
13+
using Orleans;
1214
using Orleans.Runtime;
1315
using Vertex.Abstractions.Actor;
1416
using Vertex.Abstractions.Event;
@@ -103,9 +105,9 @@ protected virtual async ValueTask DependencyInjection()
103105
/// The method used to initialize is called when Grain is activated (overriding in subclasses is prohibited)
104106
/// </summary>
105107
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
106-
public override async Task OnActivateAsync()
108+
public override async Task OnActivateAsync(CancellationToken cancellationToken)
107109
{
108-
await base.OnActivateAsync();
110+
await base.OnActivateAsync(cancellationToken);
109111
await this.DependencyInjection();
110112

111113
// Load snapshot
@@ -250,7 +252,7 @@ protected virtual ValueTask CreateSnapshot()
250252
[MethodImpl(MethodImplOptions.AggressiveInlining)]
251253
protected virtual ValueTask OnStartSaveSnapshot() => ValueTask.CompletedTask;
252254

253-
public override async Task OnDeactivateAsync()
255+
public override async Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
254256
{
255257
try
256258
{
@@ -263,6 +265,7 @@ public override async Task OnDeactivateAsync()
263265
if (this.Logger.IsEnabled(LogLevel.Trace))
264266
{
265267
this.Logger.LogTrace("Deactivate completed: {0}->{1}", this.ActorType.FullName, this.Serializer.Serialize(this.Snapshot));
268+
this.Logger.LogTrace("Deactivate reason: {0}->{1}", this.ActorType.Name, reason.Description);
266269
}
267270
}
268271
catch (Exception ex)

src/Vertex.Runtime/Vertex.Runtime.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<PackageReference Include="IdGen" />
55
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" />
66
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
7-
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" />
8-
<PackageReference Include="Microsoft.Orleans.Core" />
7+
<PackageReference Include="Microsoft.Orleans.Sdk" />
98
</ItemGroup>
109

1110
<ItemGroup>

0 commit comments

Comments
 (0)