-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ClusterSingletonProxySettings.cs
202 lines (184 loc) · 10 KB
/
ClusterSingletonProxySettings.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//-----------------------------------------------------------------------
// <copyright file="ClusterSingletonProxySettings.cs" company="Akka.NET Project">
// Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using Akka.Actor;
using Akka.Configuration;
using Akka.Util;
using Google.Protobuf.WellKnownTypes;
namespace Akka.Cluster.Tools.Singleton
{
/// <summary>
/// Create settings from the default configuration
/// `akka.cluster.singleton-proxy`.
/// </summary>
public sealed class ClusterSingletonProxySettings : INoSerializationVerificationNeeded
{
/// <summary>
/// TBD
/// </summary>
/// <param name="system">TBD</param>
/// <exception cref="ConfigurationException">TBD</exception>
/// <returns>TBD</returns>
public static ClusterSingletonProxySettings Create(ActorSystem system)
{
system.Settings.InjectTopLevelFallback(ClusterSingletonManager.DefaultConfig());
var config = system.Settings.Config.GetConfig("akka.cluster.singleton-proxy");
if (config.IsNullOrEmpty())
throw ConfigurationException.NullOrEmptyConfig<ClusterSingletonProxySettings>("akka.cluster.singleton-proxy");
var considerAppVersion =
system.Settings.Config.GetBoolean("akka.cluster.singleton.consider-app-version", false);
return Create(config, considerAppVersion);
}
/// <summary>
/// Create settings from a configuration with the same layout as
/// the default configuration `akka.cluster.singleton-proxy`.
/// </summary>
/// <param name="config">TBD</param>
/// <param name="considerAppVersion">TBD</param>
/// <returns>TBD</returns>
public static ClusterSingletonProxySettings Create(Config config, bool considerAppVersion)
{
if (config.IsNullOrEmpty())
throw ConfigurationException.NullOrEmptyConfig<ClusterSingletonProxySettings>();
var role = config.GetString("role", null);
if (role == string.Empty) role = null;
return new ClusterSingletonProxySettings(
singletonName: config.GetString("singleton-name"),
role: role,
singletonIdentificationInterval: config.GetTimeSpan("singleton-identification-interval"),
bufferSize: config.GetInt("buffer-size", 0),
considerAppVersion: considerAppVersion);
}
/// <summary>
/// The actor name of the singleton actor that is started by the <see cref="ClusterSingletonManager"/>.
/// </summary>
public string SingletonName { get; }
/// <summary>
/// The role of the cluster nodes where the singleton can be deployed.
/// </summary>
public string Role { get; }
/// <summary>
/// Interval at which the proxy will try to resolve the singleton instance.
/// </summary>
public TimeSpan SingletonIdentificationInterval { get; }
/// <summary>
/// If the location of the singleton is unknown the proxy will buffer this number of messages and deliver them when the singleton is identified.
/// </summary>
public int BufferSize { get; }
/// <summary>
/// Should <see cref="Member.AppVersion"/> be considered when the cluster singleton instance is being moved to another node.
/// When set to false, singleton instance will always be created on oldest member.
/// When set to true, singleton instance will be created on the oldest member with the highest <see cref="Member.AppVersion"/> number.
/// </summary>
public bool ConsiderAppVersion { get; }
/// <summary>
/// Creates new instance of the <see cref="ClusterSingletonProxySettings"/>.
/// </summary>
/// <param name="singletonName">The actor name of the singleton actor that is started by the <see cref="ClusterSingletonManager"/>.</param>
/// <param name="role">The role of the cluster nodes where the singleton can be deployed. If None, then any node will do.</param>
/// <param name="singletonIdentificationInterval">Interval at which the proxy will try to resolve the singleton instance.</param>
/// <param name="bufferSize">
/// If the location of the singleton is unknown the proxy will buffer this number of messages and deliver them
/// when the singleton is identified.When the buffer is full old messages will be dropped when new messages
/// are sent via the proxy. Use 0 to disable buffering, i.e.messages will be dropped immediately if the location
/// of the singleton is unknown.
/// </param>
/// <param name="considerAppVersion">
/// Should <see cref="Member.AppVersion"/> be considered when the cluster singleton instance is being moved to another node.
/// When set to false, singleton instance will always be created on oldest member.
/// When set to true, singleton instance will be created on the oldest member with the highest <see cref="Member.AppVersion"/> number.
/// </param>
/// <exception cref="ArgumentException">
/// This exception is thrown when either the specified <paramref name="singletonIdentificationInterval"/>
/// or <paramref name="bufferSize"/> is less than or equal to zero.
/// </exception>
/// <exception cref="ArgumentNullException"></exception>
public ClusterSingletonProxySettings(
string singletonName,
string role,
TimeSpan singletonIdentificationInterval,
int bufferSize,
bool considerAppVersion)
{
if (string.IsNullOrEmpty(singletonName))
throw new ArgumentNullException(nameof(singletonName));
if (singletonIdentificationInterval == TimeSpan.Zero)
throw new ArgumentException("singletonIdentificationInterval must be positive", nameof(singletonIdentificationInterval));
if (bufferSize < 0)
throw new ArgumentException("bufferSize must not be negative", nameof(bufferSize));
SingletonName = singletonName;
Role = role;
SingletonIdentificationInterval = singletonIdentificationInterval;
BufferSize = bufferSize;
ConsiderAppVersion = considerAppVersion;
}
/// <summary>
/// Creates a new <see cref="ClusterSingletonProxySettings" /> setting with the specified <paramref name="singletonName"/>.
/// <note>
/// This method is immutable and returns a new instance of the setting.
/// </note>
/// </summary>
/// <param name="singletonName">The name of the singleton actor used by the <see cref="ClusterSingletonManager"/>.</param>
/// <returns>A new setting with the provided <paramref name="singletonName" />.</returns>
public ClusterSingletonProxySettings WithSingletonName(string singletonName)
{
return Copy(singletonName: singletonName);
}
/// <summary>
/// Creates a new <see cref="ClusterSingletonProxySettings" /> setting with the specified <paramref name="role"/>
/// from the <c>akka.cluster.role</c> in the configuration used. The <paramref name="role"/> specifies the nodes
/// on which the targeted singleton can run.
/// <note>
/// This method is immutable and returns a new instance of the setting.
/// </note>
/// </summary>
/// <param name="role">The role of the singleton proxy.</param>
/// <returns>A new setting with the provided <paramref name="role" />.</returns>
public ClusterSingletonProxySettings WithRole(string role)
{
return Copy(role: role);
}
/// <summary>
/// Creates a new <see cref="ClusterSingletonProxySettings" /> setting with the specified <paramref name="singletonIdentificationInterval"/>.
/// <note>
/// This method is immutable and returns a new instance of the setting.
/// </note>
/// </summary>
/// <param name="singletonIdentificationInterval">The identification level of the singleton proxy.</param>
/// <returns>A new setting with the provided <paramref name="singletonIdentificationInterval" />.</returns>
public ClusterSingletonProxySettings WithSingletonIdentificationInterval(TimeSpan singletonIdentificationInterval)
{
return Copy(singletonIdentificationInterval: singletonIdentificationInterval);
}
/// <summary>
/// Creates a new <see cref="ClusterSingletonProxySettings" /> setting with the specified <paramref name="bufferSize"/>.
/// <note>
/// This method is immutable and returns a new instance of the setting.
/// </note>
/// </summary>
/// <param name="bufferSize">The buffer size of the singleton proxy.</param>
/// <returns>A new setting with the provided <paramref name="bufferSize" />.</returns>
public ClusterSingletonProxySettings WithBufferSize(int bufferSize)
{
return Copy(bufferSize: bufferSize);
}
private ClusterSingletonProxySettings Copy(
string singletonName = null,
Option<string> role = default,
TimeSpan? singletonIdentificationInterval = null,
int? bufferSize = null,
bool? considerAppVersion = null)
{
return new ClusterSingletonProxySettings(
singletonName: singletonName ?? SingletonName,
role: role.HasValue ? role.Value : Role,
singletonIdentificationInterval: singletonIdentificationInterval ?? SingletonIdentificationInterval,
bufferSize: bufferSize ?? BufferSize,
considerAppVersion: considerAppVersion ?? ConsiderAppVersion);
}
}
}