Repository navigation
Add startup tasks to replace deprecated bootstrap providers #4026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Orleans.Runtime | ||
| { | ||
| /// <summary> | ||
| /// Defines an action to be taken during silo startup. | ||
| /// </summary> | ||
| public interface IStartupTask | ||
| { | ||
| /// <summary> | ||
| /// Called after the silo has started. | ||
| /// </summary> | ||
| /// <param name="cancellationToken">The cancellation token which is canceled when the method must abort.</param> | ||
| /// <returns>A <see cref="Task"/> representing the work performed.</returns> | ||
| Task Execute(CancellationToken cancellationToken); | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
src/Orleans.Runtime/Lifecycle/SiloBuilderStartupExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| using Orleans.Runtime; | ||
| using Orleans.Runtime.Scheduler; | ||
|
|
||
| namespace Orleans.Hosting | ||
| { | ||
| /// <summary> | ||
| /// The silo builder startup extensions. | ||
| /// </summary> | ||
| public static class SiloBuilderStartupExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adds a startup task to be executed when the silo has started. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The builder. | ||
| /// </param> | ||
| /// <param name="stage"> | ||
| /// The stage to execute the startup task, see values in <see cref="SiloLifecycleStage"/>. | ||
| /// </param> | ||
| /// <typeparam name="TStartup"> | ||
| /// The startup task type. | ||
| /// </typeparam> | ||
| /// <returns> | ||
| /// The provided <see cref="ISiloHostBuilder"/>. | ||
| /// </returns> | ||
| public static ISiloHostBuilder AddStartupTask<TStartup>( | ||
| this ISiloHostBuilder builder, | ||
| int stage = SiloLifecycleStage.SiloActive) | ||
| where TStartup : class, IStartupTask | ||
| { | ||
| return builder.AddStartupTask((sp, ct) => ActivatorUtilities.GetServiceOrCreateInstance<TStartup>(sp).Execute(ct), stage); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a startup task to be executed when the silo has started. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The builder. | ||
| /// </param> | ||
| /// <param name="startupTask"> | ||
| /// The startup task. | ||
| /// </param> | ||
| /// <param name="stage"> | ||
| /// The stage to execute the startup task, see values in <see cref="SiloLifecycleStage"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The provided <see cref="ISiloHostBuilder"/>. | ||
| /// </returns> | ||
| public static ISiloHostBuilder AddStartupTask( | ||
| this ISiloHostBuilder builder, | ||
| IStartupTask startupTask, | ||
| int stage = SiloLifecycleStage.SiloActive) | ||
| { | ||
| return builder.AddStartupTask((sp, ct) => startupTask.Execute(ct), stage); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adds a startup task to be executed when the silo has started. | ||
| /// </summary> | ||
| /// <param name="builder"> | ||
| /// The builder. | ||
| /// </param> | ||
| /// <param name="startupTask"> | ||
| /// The startup task. | ||
| /// </param> | ||
| /// <param name="stage"> | ||
| /// The stage to execute the startup task, see values in <see cref="SiloLifecycleStage"/>. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The provided <see cref="ISiloHostBuilder"/>. | ||
| /// </returns> | ||
| public static ISiloHostBuilder AddStartupTask( | ||
| this ISiloHostBuilder builder, | ||
| Func<IServiceProvider, CancellationToken, Task> startupTask, | ||
| int stage = SiloLifecycleStage.SiloActive) | ||
| { | ||
| builder.ConfigureServices(services => | ||
| services.AddTransient<ILifecycleParticipant<ISiloLifecycle>>(sp => | ||
| new StartupTask( | ||
| sp, | ||
| sp.GetRequiredService<OrleansTaskScheduler>(), | ||
| sp.GetRequiredService<StartupTaskSystemTarget>(), | ||
| startupTask, | ||
| stage))); | ||
| return builder; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| private class StartupTask : ILifecycleParticipant<ISiloLifecycle> | ||
| { | ||
| private readonly IServiceProvider serviceProvider; | ||
| private readonly OrleansTaskScheduler scheduler; | ||
| private readonly StartupTaskSystemTarget schedulingTarget; | ||
| private readonly Func<IServiceProvider, CancellationToken, Task> startupTask; | ||
|
|
||
| private readonly int stage; | ||
|
|
||
| public StartupTask( | ||
| IServiceProvider serviceProvider, | ||
| OrleansTaskScheduler scheduler, | ||
| StartupTaskSystemTarget schedulingTarget, | ||
| Func<IServiceProvider, CancellationToken, Task> startupTask, | ||
| int stage) | ||
| { | ||
| this.serviceProvider = serviceProvider; | ||
| this.scheduler = scheduler; | ||
| this.schedulingTarget = schedulingTarget; | ||
| this.startupTask = startupTask; | ||
| this.stage = stage; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Participate(ISiloLifecycle lifecycle) | ||
| { | ||
| lifecycle.Subscribe( | ||
| this.stage, | ||
| cancellation => this.scheduler.QueueTask( | ||
| () => this.startupTask(this.serviceProvider, cancellation), | ||
| this.schedulingTarget.SchedulingContext)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| using Orleans; | ||
| using Orleans.Hosting; | ||
| using Orleans.Runtime; | ||
| using Orleans.TestingHost; | ||
|
|
||
| using TestExtensions; | ||
|
|
||
| using UnitTests.GrainInterfaces; | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace DefaultCluster.Tests | ||
| { | ||
| [TestCategory("BVT"), TestCategory("Lifecycle")] | ||
| public class StartupTaskTests : IClassFixture<StartupTaskTests.Fixture> | ||
| { | ||
| public class Fixture : BaseTestClusterFixture | ||
| { | ||
| protected override void ConfigureTestCluster(TestClusterBuilder builder) | ||
| { | ||
| builder.ConfigureHostConfiguration(TestDefaultConfiguration.ConfigureHostConfiguration); | ||
| builder.AddSiloBuilderConfigurator<StartupTaskSiloConfigurator>(); | ||
| } | ||
|
|
||
| private class StartupTaskSiloConfigurator : ISiloBuilderConfigurator | ||
| { | ||
| public void Configure(ISiloHostBuilder hostBuilder) | ||
| { | ||
| hostBuilder.AddStartupTask<CallGrainStartupTask>(); | ||
| hostBuilder.AddStartupTask( | ||
| async (services, cancellation) => | ||
| { | ||
| var grainFactory = services.GetRequiredService<IGrainFactory>(); | ||
| var grain = grainFactory.GetGrain<ISimpleGrain>(98052); | ||
|
|
||
| // Modify "A" to validate ordering of events. | ||
| await grain.SetA(await grain.GetA() * 2); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class CallGrainStartupTask : IStartupTask | ||
| { | ||
| private readonly IGrainFactory grainFactory; | ||
|
|
||
| public CallGrainStartupTask(IGrainFactory grainFactory) | ||
| { | ||
| this.grainFactory = grainFactory; | ||
| } | ||
|
|
||
| public async Task Execute(CancellationToken cancellationToken) | ||
| { | ||
| var grain = this.grainFactory.GetGrain<ISimpleGrain>(98052); | ||
| await grain.SetA(1331); | ||
| } | ||
| } | ||
|
|
||
| private readonly Fixture fixture; | ||
|
|
||
| public StartupTaskTests(Fixture fixture) | ||
| { | ||
| this.fixture = fixture; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Ensures that startup tasks can call grains and are executed in the registered order. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task StartupTaskCanCallGrains() | ||
| { | ||
| var grain = this.fixture.GrainFactory.GetGrain<ISimpleGrain>(98052); | ||
| var value = await grain.GetA(); | ||
| Assert.Equal(2662, value); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what we need to setup another task scheduler, as we already have and use fallback for this. It's not a bad thing, just not convinced it's necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary, but during our meeting it was mentioned, so I added a new special-purpose one. We can always axe it, especially when we fix scheduling of lifecycle participants more broadly