-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ZooKeeperGatewayListProvider.cs
71 lines (63 loc) · 2.99 KB
/
ZooKeeperGatewayListProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Orleans.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
namespace Orleans.Runtime.Membership
{
public class ZooKeeperGatewayListProvider : IGatewayListProvider
{
private readonly ZooKeeperWatcher _watcher;
/// <summary>
/// the node name for this deployment. for eg. /ClusterId
/// </summary>
private readonly string _deploymentPath;
/// <summary>
/// The deployment connection string. for eg. "192.168.1.1,192.168.1.2/ClusterId"
/// </summary>
private readonly string _deploymentConnectionString;
private readonly TimeSpan _maxStaleness;
public ZooKeeperGatewayListProvider(
ILogger<ZooKeeperGatewayListProvider> logger,
IOptions<ZooKeeperGatewayListProviderOptions> options,
IOptions<GatewayOptions> gatewayOptions,
IOptions<ClusterOptions> clusterOptions)
{
_watcher = new ZooKeeperWatcher(logger);
_deploymentPath = "/" + clusterOptions.Value.ClusterId;
_deploymentConnectionString = options.Value.ConnectionString + _deploymentPath;
_maxStaleness = gatewayOptions.Value.GatewayListRefreshPeriod;
}
/// <summary>
/// Initializes the ZooKeeper based gateway provider
/// </summary>
public Task InitializeGatewayListProvider() => Task.CompletedTask;
/// <summary>
/// Returns the list of gateways (silos) that can be used by a client to connect to Orleans cluster.
/// The Uri is in the form of: "gwy.tcp://IP:port/Generation". See Utils.ToGatewayUri and Utils.ToSiloAddress for more details about Uri format.
/// </summary>
public async Task<IList<Uri>> GetGateways()
{
var membershipTableData = await ZooKeeperBasedMembershipTable.ReadAll(this._deploymentConnectionString, this._watcher);
return membershipTableData.Members.Select(e => e.Item1).
Where(m => m.Status == SiloStatus.Active && m.ProxyPort != 0).
Select(m =>
{
var gatewayAddress = SiloAddress.New(m.SiloAddress.Endpoint.Address, m.ProxyPort, m.SiloAddress.Generation);
return gatewayAddress.ToGatewayUri();
}).ToList();
}
/// <summary>
/// Specifies how often this IGatewayListProvider is refreshed, to have a bound on max staleness of its returned information.
/// </summary>
public TimeSpan MaxStaleness => _maxStaleness;
/// <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>
public bool IsUpdatable => true;
}
}