Permalink
Browse files

Created a LoadBalancerServerManager that does not blacklist failed se…

…rvers. This is useful when using a standalone Load Balancing server (such as an F5) to connect Clients to Nodes.
  • Loading branch information...
1 parent e1691ec commit 739b24d76cf2503f06eea6c3dab0a557385f65d2 @nathannis nathannis committed Jan 24, 2013
@@ -11,7 +11,7 @@ public abstract class ConnectionProvider : IConnectionProvider
protected ConnectionProvider(IConnectionBuilder builder)
{
ConnectionBuilder = builder;
- Servers = new RoundRobinServerManager(builder);
+ Servers = new LoadBalancerServerManager(builder);
}
/// <summary>
@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Diagnostics;
+
+namespace FluentCassandra.Connections
+{
+ public class LoadBalancerServerManager : IServerManager
+ {
+ private readonly object _lock = new object();
+ private Server _server;
+
+ public LoadBalancerServerManager(IConnectionBuilder builder)
+ {
+ _server = builder.Servers[0];
+ }
+
+ #region IServerManager Members
+
+ public bool HasNext
+ {
+ get { return true; }
+ }
+
+ public Server Next()
+ {
+ return _server;
+ }
+
+ public void ErrorOccurred(Server server, Exception exc = null)
+ {
+ Debug.WriteLineIf(exc != null, exc, "connection");
+ }
+
+ public void Add(Server server)
+ {
+ _server = server;
+ }
+ public void Remove(Server server)
+ {
+ throw new NotImplementedException ("LoadBalancedServerManager does not implement Remove(server)");
+ }
+ #endregion
+ #region IEnumerable<Server> Members
+
+ public IEnumerator<Server> GetEnumerator()
+ {
+ throw new NotImplementedException ("LoadBalancedServerManager does not implement Enumerable(server)");
+ }
+
+ #endregion
+
+ #region IEnumerable Members
+
+ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ #endregion
+ }
+}
@@ -114,6 +114,7 @@
<Compile Include="Connections\ConnectionType.cs" />
<Compile Include="Connections\CqlVersion.cs" />
<Compile Include="Connections\IConnectionBuilder.cs" />
+ <Compile Include="Connections\LoadBalancerServerManager.cs" />
<Compile Include="Connections\NormalConnectionProvider.cs" />
<Compile Include="Connections\IConnection.cs" />
<Compile Include="Connections\IConnectionProvider.cs" />
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using Xunit;
+
+namespace FluentCassandra.Connections.Tests
+{
+ public class LoadBalancerServerManagerTests
+ {
+ [Fact]
+ public void CanGetServerAfterError()
+ {
+ LoadBalancerServerManager target = new LoadBalancerServerManager(new ConnectionBuilder("Server=unit-test-1"));
+
+ Server original = target.Next();
+
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.True(target.HasNext, "LoadBalancerServerManager should always have another server available.");
+ Server next = target.Next();
+ Assert.True(original.ToString().Equals(next.ToString(), StringComparison.OrdinalIgnoreCase),
+ "LoadBalancerServerManager always returns the same server.");
+ //mark the server as failing to set up the next test iteration.
+ target.ErrorOccurred(next);
+ }
+ }
+ }
+}
@@ -61,6 +61,7 @@
<Compile Include="CassandraQueryTest.cs" />
<Compile Include="Connections\ConnectionBuilderTests.cs" />
<Compile Include="Connections\ConnectionProviderTests.cs" />
+ <Compile Include="Connections\LoadBalancerServerManagerTests.cs" />
<Compile Include="Connections\NormalConnectionProviderTests.cs" />
<Compile Include="Connections\RoundRobinServerManagerTests.cs" />
<Compile Include="CqlHelperTest.cs" />

0 comments on commit 739b24d

Please sign in to comment.