Skip to content

Commit 554f4b0

Browse files
author
Luke Bakken
committed
Move externalLoadBalancer setting to RiakNode
1 parent eed94b1 commit 554f4b0

File tree

8 files changed

+36
-38
lines changed

8 files changed

+36
-38
lines changed

src/RiakClient/Comms/IRiakNode.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ namespace RiakClient.Comms
2828
/// </summary>
2929
public interface IRiakNode : IDisposable
3030
{
31+
/// <summary>
32+
/// Indicates if this node can be marked offline. <b>false</b> if the node points to a load balancer.
33+
/// </summary>
34+
/// <returns>
35+
/// <b>true</b> or <b>false</b> depending on the node's configuration.
36+
/// </returns>
37+
bool CanMarkOffline { get; }
38+
3139
/// <summary>
3240
/// Execute the <paramref name="useFun"/> delegate using this one of this <see cref="IRiakNode"/>'s connections.
3341
/// </summary>

src/RiakClient/Comms/RiakNode.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ namespace RiakClient.Comms
2626
internal class RiakNode : IRiakNode
2727
{
2828
private readonly IRiakConnectionManager connections;
29+
private readonly bool externalLoadBalancer = false;
2930
private bool disposing;
3031

3132
public RiakNode(
3233
IRiakNodeConfiguration nodeConfig,
3334
IRiakAuthenticationConfiguration authConfig,
3435
IRiakConnectionFactory connectionFactory)
3536
{
37+
externalLoadBalancer = nodeConfig.ExternalLoadBalancer;
38+
3639
// assume that if the node has a pool size of 0 then the intent is to have the connections
3740
// made on the fly
3841
if (nodeConfig.PoolSize == 0)
@@ -45,6 +48,11 @@ public RiakNode(
4548
}
4649
}
4750

51+
public bool CanMarkOffline
52+
{
53+
get { return externalLoadBalancer == false; }
54+
}
55+
4856
public RiakResult UseConnection(Func<IRiakConnection, RiakResult> useFun)
4957
{
5058
return UseConnection(useFun, RiakResult.FromError);

src/RiakClient/Config/IRiakClusterConfiguration.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ public interface IRiakClusterConfiguration
4141
/// </summary>
4242
Timeout DefaultRetryWaitTime { get; set; }
4343

44-
/// <summary>
45-
/// If True and only one node is configured, the node (pointing to the load balancer) will not be marked offline and requests will always be routed to that node.
46-
/// If False (default) offline nodes will be removed from the pool until health check determines they are healthy again.
47-
/// </summary>
48-
bool ExternalLoadBalancer { get; set; }
49-
5044
/// <summary>
5145
/// The max number of retry attempts to make when the client encounters
5246
/// <see cref="ResultCode"/>.NoConnections or <see cref="ResultCode"/>.CommunicationError errors.

src/RiakClient/Config/IRiakNodeConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public interface IRiakNodeConfiguration
4646
/// </summary>
4747
int PoolSize { get; set; }
4848

49+
/// <summary>
50+
/// If True, this node (pointing to a load balancer) will not be marked offline and requests will always be routed to it.
51+
/// If False (default) and this node is offline, it will be removed from the pool until health check determines it is healthy again.
52+
/// </summary>
53+
bool ExternalLoadBalancer { get; set; }
54+
4955
/// <summary>
5056
/// The network timeout to use when attempting to read data from Riak.
5157
/// </summary>

src/RiakClient/Config/RiakClusterConfiguration.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,6 @@ public int DefaultRetryCount
104104
set { this["defaultRetryCount"] = value; }
105105
}
106106

107-
/// <inheritdoc/>
108-
/// <remarks>Defaults to False if omitted from the configuration file.</remarks>
109-
[ConfigurationProperty("externalLoadBalancer", DefaultValue = "False", IsRequired = false)]
110-
public bool ExternalLoadBalancer
111-
{
112-
get { return (bool)this["externalLoadBalancer"]; }
113-
set { this["externalLoadBalancer"] = value; }
114-
}
115-
116107
/// <summary>
117108
/// A <see cref="IRiakAuthenticationConfiguration"/> configuration that details any authentication information.
118109
/// </summary>

src/RiakClient/Config/RiakNodeConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public int PoolSize
7272
* }
7373
*/
7474

75+
/// <inheritdoc/>
76+
/// <remarks>Defaults to False if omitted from the configuration file.</remarks>
77+
[ConfigurationProperty("externalLoadBalancer", DefaultValue = "False", IsRequired = false)]
78+
public bool ExternalLoadBalancer
79+
{
80+
get { return (bool)this["externalLoadBalancer"]; }
81+
set { this["externalLoadBalancer"] = value; }
82+
}
83+
7584
/// <inheritdoc/>
7685
/// <remarks>Defaults to 4000ms if omitted from the configuration file.</remarks>
7786
public Timeout NetworkReadTimeout

src/RiakClient/RiakCluster.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class RiakCluster : RiakEndPoint
4242
private readonly ConcurrentQueue<IRiakNode> offlineNodes;
4343
private readonly TimeSpan nodePollTime;
4444
private readonly int defaultRetryCount;
45-
private readonly bool externalLoadBalancer = false;
4645

4746
private readonly CancellationTokenSource cts = new CancellationTokenSource();
4847
private readonly CancellationToken ct;
@@ -59,7 +58,6 @@ public class RiakCluster : RiakEndPoint
5958
public RiakCluster(IRiakClusterConfiguration clusterConfig, IRiakConnectionFactory connectionFactory)
6059
{
6160
nodePollTime = clusterConfig.NodePollTime;
62-
externalLoadBalancer = clusterConfig.ExternalLoadBalancer;
6361
nodes = clusterConfig.RiakNodes.Select(rn =>
6462
new RiakNode(rn, clusterConfig.Authentication, connectionFactory)).Cast<IRiakNode>().ToList();
6563
loadBalancer = new RoundRobinStrategy();
@@ -88,22 +86,6 @@ protected override int DefaultRetryCount
8886
get { return defaultRetryCount; }
8987
}
9088

91-
private bool CanMarkNodesOffline
92-
{
93-
get
94-
{
95-
// Assume OK to mark nodes offline.
96-
bool rv = true;
97-
98-
if (externalLoadBalancer && nodes.Count == 1)
99-
{
100-
rv = false; // Only one node configured and LB present, never mark it offline.
101-
}
102-
103-
return rv;
104-
}
105-
}
106-
10789
/// <summary>
10890
/// Creates an instance of <see cref="IRiakClient"/> populated from from the configuration section
10991
/// specified by <paramref name="configSectionName"/>.
@@ -260,7 +242,7 @@ protected override TRiakResult UseConnection<TRiakResult>(
260242

261243
private void MaybeDeactivateNode(bool nodeOffline, IRiakNode node)
262244
{
263-
if (nodeOffline && CanMarkNodesOffline)
245+
if (nodeOffline && node.CanMarkOffline)
264246
{
265247
lock (node)
266248
{

src/RiakClientTests.Live/RiakConfigurationTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public class WhenLoadingFromExternalConfiguration
3333
<configSections>
3434
<section name=""riakConfig"" type=""RiakClient.Config.RiakClusterConfiguration, RiakClient"" />
3535
</configSections>
36-
<riakConfig nodePollTime=""5000"" defaultRetryWaitTime=""200"" defaultRetryCount=""3"" externalLoadBalancer=""true"">
36+
<riakConfig nodePollTime=""5000"" defaultRetryWaitTime=""200"" defaultRetryCount=""3"">
3737
<nodes>
38-
<node name=""node1"" hostAddress=""host1"" pbcPort=""8081"" poolSize=""5"" />
38+
<node name=""node1"" hostAddress=""host1"" pbcPort=""8081"" poolSize=""5"" externalLoadBalancer=""true"" />
3939
<node name=""node2"" hostAddress=""host2"" pbcPort=""8081"" poolSize=""6""
4040
networkReadTimeout=""5000"" networkWriteTimeout=""5000"" networkConnectTimeout=""5000"" />
4141
</nodes>
@@ -73,14 +73,14 @@ public void ConfigurationLoadsProperly()
7373
config.DefaultRetryWaitTime.ShouldEqual((Timeout)twoHundredMillis);
7474
config.NodePollTime.ShouldEqual((Timeout)fiveSecsAsMillis);
7575
config.RiakNodes.Count().ShouldEqual(2);
76-
Assert.IsTrue(config.ExternalLoadBalancer);
7776

7877
var nodes = config.RiakNodes.ToArray();
7978
IRiakNodeConfiguration node1 = nodes[0];
8079
node1.Name.ShouldEqual("node1");
8180
node1.HostAddress.ShouldEqual("host1");
8281
node1.PbcPort.ShouldEqual(8081);
8382
node1.PoolSize.ShouldEqual(5);
83+
Assert.IsTrue(node1.ExternalLoadBalancer);
8484
node1.NetworkConnectTimeout.ShouldEqual((Timeout)fourSecsAsMillis);
8585
node1.NetworkReadTimeout.ShouldEqual((Timeout)fourSecsAsMillis);
8686
node1.NetworkWriteTimeout.ShouldEqual((Timeout)fourSecsAsMillis);
@@ -90,6 +90,7 @@ public void ConfigurationLoadsProperly()
9090
node2.HostAddress.ShouldEqual("host2");
9191
node2.PbcPort.ShouldEqual(8081);
9292
node2.PoolSize.ShouldEqual(6);
93+
Assert.IsFalse(node2.ExternalLoadBalancer);
9394
node2.NetworkConnectTimeout.ShouldEqual((Timeout)fiveSecsAsMillis);
9495
node2.NetworkReadTimeout.ShouldEqual((Timeout)fiveSecsAsMillis);
9596
node2.NetworkWriteTimeout.ShouldEqual((Timeout)fiveSecsAsMillis);
@@ -113,7 +114,6 @@ public void ConfigurationLoadsDefaults()
113114
Assert.AreEqual(200, (int)config.DefaultRetryWaitTime);
114115
Assert.AreEqual(5000, (int)config.NodePollTime);
115116
Assert.AreEqual(1, config.RiakNodes.Count());
116-
Assert.IsFalse(config.ExternalLoadBalancer);
117117

118118
var nodes = config.RiakNodes.ToArray();
119119
IRiakNodeConfiguration node = nodes[0];

0 commit comments

Comments
 (0)