-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Persistent stream provider to lifecycle #4042
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| | ||
| using System; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Orleans.Configuration; | ||
| using Orleans.Hosting; | ||
| using OrleansAWSUtils.Streams; | ||
|
|
||
| namespace Orleans.Hosting | ||
| { | ||
| public static class ClientBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configure cluster client to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IClientBuilder AddSqsStreams(this IClientBuilder builder, string name, Action<SqsStreamOptions> configureOptions) | ||
| { | ||
| return builder.ConfigureServices(services => services.AddClusterClientSqsStreams(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IClientBuilder AddSqsStreams(this IClientBuilder builder, string name, Action<OptionsBuilder<SqsStreamOptions>> configureOptions = null) | ||
| { | ||
| return builder.ConfigureServices(services => services.AddClusterClientSqsStreams(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddClusterClientSqsStreams(this IServiceCollection services, string name, Action<SqsStreamOptions> configureOptions) | ||
| { | ||
| return services.AddClusterClientSqsStreams(name, ob => ob.Configure(configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddClusterClientSqsStreams(this IServiceCollection services, string name, | ||
| Action<OptionsBuilder<SqsStreamOptions>> configureOptions = null) | ||
| { | ||
| return services.ConfigureNamedOptionForLogging<SqsStreamOptions>(name) | ||
| .AddClusterClientPersistentStreams<SqsStreamOptions>(name, SQSAdapterFactory.Create, configureOptions); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Orleans.Configuration; | ||
| using Orleans.Hosting; | ||
| using OrleansAWSUtils.Streams; | ||
|
|
||
| namespace Orleans.Hosting | ||
| { | ||
| public static class SiloBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configure silo to use SQS persistent streams. | ||
| /// </summary> | ||
| public static ISiloHostBuilder AddSqsStreams(this ISiloHostBuilder builder, string name, Action<SqsStreamOptions> configureOptions) | ||
| { | ||
| return builder.ConfigureServices(services => services.AddSiloSqsStreams(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure silo to use SQS persistent streams. | ||
| /// </summary> | ||
| public static ISiloHostBuilder AddSqsStreams(this ISiloHostBuilder builder, string name, Action<OptionsBuilder<SqsStreamOptions>> configureOptions = null) | ||
| { | ||
| return builder.ConfigureServices(services => services.AddSiloSqsStreams(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure silo to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddSiloSqsStreams(this IServiceCollection services, string name, Action<SqsStreamOptions> configureOptions) | ||
| { | ||
| return services.AddSiloSqsStreams(name, ob => ob.Configure(configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure silo to use SQS persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddSiloSqsStreams(this IServiceCollection services, string name, | ||
| Action<OptionsBuilder<SqsStreamOptions>> configureOptions = null) | ||
| { | ||
| return services.ConfigureNamedOptionForLogging<SqsStreamOptions>(name) | ||
| .AddSiloPersistentStreams<SqsStreamOptions>(name, SQSAdapterFactory.Create, configureOptions); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,60 +1,46 @@ | ||
| using Orleans.Providers; | ||
| using Orleans.Providers.Streams.Common; | ||
| using Orleans.Runtime; | ||
| using Orleans.Streams; | ||
| using System; | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
| using Orleans.Providers.Streams.Common; | ||
| using Orleans.Streams; | ||
| using Orleans.Serialization; | ||
| using Orleans.Configuration; | ||
|
|
||
| namespace OrleansAWSUtils.Streams | ||
| { | ||
| /// <summary> Factory class for Azure Queue based stream provider.</summary> | ||
| public class SQSAdapterFactory : IQueueAdapterFactory | ||
| { | ||
| internal const int CacheSizeDefaultValue = 4096; | ||
| internal const string NumQueuesPropertyName = "NumQueues"; | ||
|
|
||
| /// <summary> Default number of Azure Queue used in this stream provider.</summary> | ||
| public const int NumQueuesDefaultValue = 8; // keep as power of 2. | ||
|
|
||
| private string deploymentId; | ||
| private string dataConnectionString; | ||
| private string providerName; | ||
| private int cacheSize; | ||
| private int numQueues; | ||
| private readonly string providerName; | ||
| private readonly SqsStreamOptions options; | ||
| private readonly SiloOptions siloOptions; | ||
| private readonly SerializationManager serializationManager; | ||
| private readonly ILoggerFactory loggerFactory; | ||
| private HashRingBasedStreamQueueMapper streamQueueMapper; | ||
| private IQueueAdapterCache adapterCache; | ||
| private SerializationManager serializationManager; | ||
| private ILoggerFactory loggerFactory; | ||
| /// <summary>"DataConnectionString".</summary> | ||
| public const string DataConnectionStringPropertyName = "DataConnectionString"; | ||
| /// <summary>"DeploymentId".</summary> | ||
| public const string DeploymentIdPropertyName = "DeploymentId"; | ||
|
|
||
| /// <summary> | ||
| /// Application level failure handler override. | ||
| /// </summary> | ||
| protected Func<QueueId, Task<IStreamFailureHandler>> StreamFailureHandlerFactory { private get; set; } | ||
|
|
||
| /// <summary> Init the factory.</summary> | ||
| public virtual void Init(IProviderConfiguration config, string providerName, IServiceProvider serviceProvider) | ||
| public SQSAdapterFactory(string name, SqsStreamOptions options, IServiceProvider serviceProvider, IOptions<SiloOptions> siloOptions, SerializationManager serializationManager, ILoggerFactory loggerFactory) | ||
| { | ||
| if (config == null) throw new ArgumentNullException("config"); | ||
| if (!config.Properties.TryGetValue(DataConnectionStringPropertyName, out dataConnectionString)) | ||
| throw new ArgumentException(string.Format("{0} property not set", DataConnectionStringPropertyName)); | ||
| if (!config.Properties.TryGetValue(DeploymentIdPropertyName, out deploymentId)) | ||
| throw new ArgumentException(string.Format("{0} property not set", DeploymentIdPropertyName)); | ||
| this.providerName = name; | ||
| this.options = options; | ||
| this.siloOptions = siloOptions.Value; | ||
| this.serializationManager = serializationManager; | ||
| this.loggerFactory = loggerFactory; | ||
| } | ||
|
|
||
| cacheSize = SimpleQueueAdapterCache.ParseSize(config, CacheSizeDefaultValue); | ||
| this.loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(); | ||
| numQueues = config.GetIntProperty(NumQueuesPropertyName, NumQueuesDefaultValue); | ||
|
|
||
| this.providerName = providerName; | ||
| streamQueueMapper = new HashRingBasedStreamQueueMapper(numQueues, providerName); | ||
| adapterCache = new SimpleQueueAdapterCache(cacheSize, providerName, serviceProvider.GetRequiredService<ILoggerFactory>()); | ||
| this.serializationManager = serviceProvider.GetRequiredService<SerializationManager>(); | ||
| /// <summary> Init the factory.</summary> | ||
| public virtual void Init() | ||
| { | ||
| streamQueueMapper = new HashRingBasedStreamQueueMapper(this.options.NumQueues, this.providerName); | ||
| adapterCache = new SimpleQueueAdapterCache(this.options.CacheSize, this.providerName, this.loggerFactory); | ||
| if (StreamFailureHandlerFactory == null) | ||
| { | ||
| StreamFailureHandlerFactory = | ||
|
|
@@ -65,7 +51,7 @@ public virtual void Init(IProviderConfiguration config, string providerName, ISe | |
| /// <summary>Creates the Azure Queue based adapter.</summary> | ||
| public virtual Task<IQueueAdapter> CreateAdapter() | ||
| { | ||
| var adapter = new SQSAdapter(this.serializationManager, streamQueueMapper, this.loggerFactory, dataConnectionString, deploymentId, providerName); | ||
| var adapter = new SQSAdapter(this.serializationManager, this.streamQueueMapper, this.loggerFactory, this.options.ConnectionString, this.options.ClusterId ?? this.siloOptions.ClusterId, this.providerName); | ||
| return Task.FromResult<IQueueAdapter>(adapter); | ||
| } | ||
|
|
||
|
|
@@ -90,5 +76,13 @@ public Task<IStreamFailureHandler> GetDeliveryFailureHandler(QueueId queueId) | |
| { | ||
| return StreamFailureHandlerFactory(queueId); | ||
| } | ||
|
|
||
| public static SQSAdapterFactory Create(IServiceProvider services, string name) | ||
| { | ||
| IOptionsSnapshot<SqsStreamOptions> streamOptionsSnapshot = services.GetRequiredService<IOptionsSnapshot<SqsStreamOptions>>(); | ||
| var factory = ActivatorUtilities.CreateInstance<SQSAdapterFactory>(services, name, streamOptionsSnapshot.Get(name)); | ||
| factory.Init(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should factory handle its init or lifecycle?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Init is not async, so no reason I can see to link adapters to lifecycle, they are just pluggable components. |
||
| return factory; | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| | ||
| namespace Orleans.Configuration | ||
| { | ||
| public class SqsStreamOptions : PersistentStreamOptions | ||
| { | ||
| public string ClusterId { get; set; } | ||
|
|
||
| [Redact] | ||
| public string ConnectionString { get; set; } | ||
|
||
|
|
||
| public int CacheSize { get; set; } = CacheSizeDefaultValue; | ||
| public const int CacheSizeDefaultValue = 4096; | ||
|
|
||
| public int NumQueues { get; set; } = NumQueuesDefaultValue; | ||
| public const int NumQueuesDefaultValue = 8; // keep as power of 2. | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| | ||
| using System; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Orleans.Configuration; | ||
| using Orleans.Providers.Streams.AzureQueue; | ||
|
|
||
| namespace Orleans.Hosting | ||
| { | ||
| public static class ClientBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configure cluster client to use azure queue persistent streams. | ||
| /// </summary> | ||
| public static IClientBuilder AddAzureQueueStreams<TDataAdapter>(this IClientBuilder builder, string name, Action<AzureQueueStreamOptions> configureOptions) | ||
| where TDataAdapter : IAzureQueueDataAdapter | ||
| { | ||
| return builder.ConfigureServices(services => services.AddClusterClientAzureQueueStreams<TDataAdapter>(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use azure queue persistent streams. | ||
| /// </summary> | ||
| public static IClientBuilder AddAzureQueueStreams<TDataAdapter>(this IClientBuilder builder, string name, Action<OptionsBuilder<AzureQueueStreamOptions>> configureOptions = null) | ||
| where TDataAdapter : IAzureQueueDataAdapter | ||
| { | ||
| return builder.ConfigureServices(services => services.AddClusterClientAzureQueueStreams<TDataAdapter>(name, configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use azure queue persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddClusterClientAzureQueueStreams<TDataAdapter>(this IServiceCollection services, string name, Action<AzureQueueStreamOptions> configureOptions) | ||
| where TDataAdapter : IAzureQueueDataAdapter | ||
| { | ||
| return services.AddClusterClientAzureQueueStreams<TDataAdapter>(name, ob => ob.Configure(configureOptions)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configure cluster client to use azure queue persistent streams. | ||
| /// </summary> | ||
| public static IServiceCollection AddClusterClientAzureQueueStreams<TDataAdapter>(this IServiceCollection services, string name, | ||
| Action<OptionsBuilder<AzureQueueStreamOptions>> configureOptions = null) | ||
| where TDataAdapter : IAzureQueueDataAdapter | ||
| { | ||
| return services.ConfigureNamedOptionForLogging<AzureQueueStreamOptions>(name) | ||
| .AddClusterClientPersistentStreams<AzureQueueStreamOptions>(name, AzureQueueAdapterFactory<TDataAdapter>.Create, configureOptions); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
I think we should not add any extension methods which hang from
IServiceCollectionand we should endeavor to remove the existing ones.(I'd be happy if they were private or internal)
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.
Sorry, not following... Why?
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.
Because otherwise we will have methods which are ambiguous between client & silo & SignalR & ASP.NET. I'm thinking about the future, but the issue is present here today: we need to name the methods differently for client and silo instead of having a nice simple "AddSqsStreams" method.
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.
I see... Well, other framework (including asp.net) does that IIRC. However, I think we should have diff assemblies for client and silo for the providers. For example, in Membership, makes no sense to bring the Membership table to the client, but instead it should be only have the gateway. So
Orleans.Clustering.XXXXandOrleans.Clustering.XXXX.Client(orGateway). I know, extra nuget package to maintain, but the benefits are: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.
Will discuss then address in separate PR based on consensus.
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.
Maybe the extension methods should at least live in their own namespace? One for the clientbuilder and one for the silo? Better for discoverability.