Skip to content

Commit

Permalink
Service Fabric: support injecting FabricClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Apr 19, 2017
1 parent 6939692 commit 6ea5969
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 59 deletions.
51 changes: 12 additions & 39 deletions src/OrleansServiceFabricUtils/FabricGatewayProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Microsoft.Orleans.ServiceFabric
{
using System;
using System.Collections.Generic;
using System.Fabric;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,7 +15,6 @@ namespace Microsoft.Orleans.ServiceFabric

using Microsoft.Orleans.ServiceFabric.Models;
using Microsoft.Orleans.ServiceFabric.Utilities;
using Microsoft.ServiceFabric.Services.Client;

/// <summary>
/// Gateway provider which reads gateway information from Service Fabric's naming service.
Expand All @@ -28,7 +26,7 @@ internal class FabricGatewayProvider : IGatewayListProvider, IGatewayListObserva

private readonly TimeSpan refreshPeriod;

private FabricServiceSiloResolver fabricServiceSiloResolver;
private readonly IFabricServiceSiloResolver fabricServiceSiloResolver;

private List<Uri> gateways = new List<Uri>();

Expand All @@ -39,73 +37,48 @@ internal class FabricGatewayProvider : IGatewayListProvider, IGatewayListObserva
/// <summary>
/// Initializes a new instance of the <see cref="FabricGatewayProvider"/> class.
/// </summary>
public FabricGatewayProvider()
/// <param name="siloResolver">The silo resolver.</param>
public FabricGatewayProvider(IFabricServiceSiloResolver siloResolver)
{
this.fabricServiceSiloResolver = siloResolver;
this.refreshPeriod = TimeSpan.FromSeconds(30);
this.MaxStaleness = TimeSpan.FromSeconds(this.refreshPeriod.TotalSeconds * 2);
}

/// <summary>
/// Initializes the provider, will be called before all other methods
/// </summary>
/// <param name="clientConfiguration">The client configuration.</param>
/// <param name="logger">The logger to be used by the provider.</param>
/// <inheritdoc />
public async Task InitializeGatewayListProvider(ClientConfiguration clientConfiguration, Logger logger)
{
// TODO: inject these
var serviceName = new Uri(clientConfiguration.DataConnectionString);
var fabricClient = new FabricClient();
var queryManager = new FabricQueryManager(fabricClient, new ServicePartitionResolver(() => fabricClient));
this.fabricServiceSiloResolver = new FabricServiceSiloResolver(serviceName, queryManager, logger.GetLogger);
this.fabricServiceSiloResolver.Subscribe(this);

this.log = logger.GetLogger(nameof(FabricGatewayProvider));
await this.RefreshAsync();
this.timer = new Timer(this.Refresh, null, this.refreshPeriod, this.refreshPeriod);
}

/// <summary>
/// Returns the list of gateways (silos) that can be used by a client to connect to Orleans cluster.
/// </summary>
/// <inheritdoc />
public Task<IList<Uri>> GetGateways() => Task.FromResult<IList<Uri>>(this.gateways);

/// <summary>
/// Specifies how often this IGatewayListProvider is refreshed, to have a bound on max staleness of its returned information.
/// </summary>
/// <inheritdoc />
public TimeSpan MaxStaleness { get; }

/// <summary>
/// Specifies whether this IGatewayListProvider ever refreshes its returned information, or always returns the same gw list.
/// (currently only the static config based StaticGatewayListProvider is not updatable. All others are.)
/// </summary>
/// <inheritdoc />
public bool IsUpdatable => true;

/// <summary>
/// Subscribes the provided <paramref name="subscriber"/> from notification events.
/// </summary>
/// <param name="subscriber">The listener.</param>
/// <returns>A value indicating whether the listener was subscribed.</returns>
/// <inheritdoc />
public bool SubscribeToGatewayNotificationEvents(IGatewayListListener subscriber)
{
this.subscribers.TryAdd(subscriber, subscriber);
return true;
}

/// <summary>
/// Unsubscribes the provided <paramref name="listener"/> from notification events.
/// </summary>
/// <param name="listener">The listener.</param>
/// <returns>A value indicating whether the listener was unsubscribed.</returns>
/// <inheritdoc />
public bool UnSubscribeFromGatewayNotificationEvents(IGatewayListListener listener)
{
this.subscribers.TryRemove(listener, out listener);
return true;
}

/// <summary>
/// Notifies this instance of an update to one or more partitions.
/// </summary>
/// <param name="silos">The updated set of partitions.</param>
/// <inheritdoc />
public void OnUpdate(FabricSiloInfo[] silos)
{
this.gateways = silos.Select(silo => silo.GatewayAddress.ToGatewayUri()).ToList();
Expand All @@ -130,7 +103,7 @@ public void OnUpdate(FabricSiloInfo[] silos)
}
}

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
/// <inheritdoc />
public void Dispose()
{
this.timer?.Dispose();
Expand Down
57 changes: 37 additions & 20 deletions src/OrleansServiceFabricUtils/OrleansServiceFabricExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Fabric;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.ServiceFabric.Services.Runtime;
using Orleans;
using Orleans.Messaging;
Expand Down Expand Up @@ -74,34 +73,52 @@ public static IServiceCollection AddServiceFabricSupport(
}

/// <summary>
/// Adds support for connecting to a cluster hosted in Service Fabric to the provided service collection.
/// Adds support for connecting to a cluster hosted in Service Fabric.
/// </summary>
/// <param name="serviceCollection">The service collection.</param>
/// <param name="clientBuilder">The client builder.</param>
/// <param name="serviceName">The Service Fabric service name.</param>
/// <returns>The provided service collection.</returns>
public static IServiceCollection AddServiceFabricClientSupport(
this IServiceCollection serviceCollection,
/// <returns>The provided client builder.</returns>
public static IClientBuilder AddServiceFabric(
this IClientBuilder clientBuilder,
string serviceName)
{
return clientBuilder.AddServiceFabric(new Uri(serviceName));
}

/// <summary>
/// Adds support for connecting to a cluster hosted in Service Fabric.
/// </summary>
/// <param name="clientBuilder">The client builder.</param>
/// <param name="serviceName">The Service Fabric service name.</param>
/// <returns>The provided client builder.</returns>
public static IClientBuilder AddServiceFabric(
this IClientBuilder clientBuilder,
Uri serviceName)
{
AddStandardServices(serviceCollection);
clientBuilder.ConfigureServices(
serviceCollection =>
{
AddStandardServices(serviceCollection);

// Use Service Fabric for cluster membership.
serviceCollection.AddSingleton<IFabricServiceSiloResolver>(
sp =>
new FabricServiceSiloResolver(
serviceName,
sp.GetService<IFabricQueryManager>(),
sp.GetService<Factory<string, Logger>>()));
serviceCollection.AddSingleton<IMembershipOracle, FabricMembershipOracle>();
return serviceCollection;
// Use Service Fabric for cluster membership.
serviceCollection.AddSingleton<IFabricServiceSiloResolver>(
sp =>
new FabricServiceSiloResolver(
serviceName,
sp.GetService<IFabricQueryManager>(),
sp.GetService<Factory<string, Logger>>()));
serviceCollection.AddSingleton<IGatewayListProvider, FabricGatewayProvider>();
});

return clientBuilder;
}

private static void AddStandardServices(IServiceCollection serviceCollection)
{
serviceCollection.TryAddSingleton<FabricClient>();
serviceCollection.TryAddSingleton<CreateFabricClientDelegate>(sp => () => sp.GetService<FabricClient>());
serviceCollection.TryAddSingleton<IServicePartitionResolver, ServicePartitionResolver>();
serviceCollection.TryAddSingleton<IFabricQueryManager, FabricQueryManager>();
serviceCollection.AddSingleton<FabricClient>();
serviceCollection.AddSingleton<CreateFabricClientDelegate>(sp => () => sp.GetRequiredService<FabricClient>());
serviceCollection.AddSingleton<IServicePartitionResolver, ServicePartitionResolver>();
serviceCollection.AddSingleton<IFabricQueryManager, FabricQueryManager>();
}
}
}

0 comments on commit 6ea5969

Please sign in to comment.