Skip to content
Browse files

added new ServerManagerFactory and pull #109 fixes the issue where ag…

…gressive blacklisting doesn't make sense for single server senarios
  • Loading branch information...
1 parent f83865b commit 6203be3b417f863cfe17d04c538d74e826670dbb @nberardi nberardi committed
View
2 FluentCassandra.sln
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 2010
+# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra", "src\FluentCassandra.csproj", "{EAA32600-3C2A-4B34-B9B2-5764F280FCE3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentCassandra.Sandbox", "test\FluentCassandra.Sandbox\FluentCassandra.Sandbox.csproj", "{DE6B46DE-C37A-49AF-8B9A-B9B6D4F03A55}"
View
2 src/Connections/ConnectionBuilder.cs
@@ -37,7 +37,7 @@ public ConnectionBuilder(string keyspace, string host, int port = Server.Default
ConnectionString = GetConnectionString();
}
- public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int maxRetries = 0, int serverPollingInterval = 30, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.Edge, bool compressCqlQueries = true, string username = null, string password = null)
+ public ConnectionBuilder(string keyspace, Server server, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int maxRetries = 0, int serverPollingInterval = 30, int connectionLifetime = 0, ConnectionType connectionType = ConnectionType.Framed, int bufferSize = 1024, ConsistencyLevel read = ConsistencyLevel.QUORUM, ConsistencyLevel write = ConsistencyLevel.QUORUM, string cqlVersion = FluentCassandra.Connections.CqlVersion.ConnectionDefault, bool compressCqlQueries = true, string username = null, string password = null)
{
Keyspace = keyspace;
Servers = new List<Server>() { server };
View
2 src/Connections/ConnectionProvider.cs
@@ -11,7 +11,7 @@ public abstract class ConnectionProvider : IConnectionProvider
protected ConnectionProvider(IConnectionBuilder builder)
{
ConnectionBuilder = builder;
- Servers = new SingleServerManager(builder);
+ Servers = ServerManagerFactory.Get(builder);
}
/// <summary>
View
13 src/Connections/ConnectionProviderFactory.cs
@@ -10,12 +10,10 @@ public static class ConnectionProviderFactory
public static IConnectionProvider Get(IConnectionBuilder connectionBuilder)
{
- lock(Lock)
- {
+ lock (Lock) {
IConnectionProvider provider;
- if (!Providers.TryGetValue(connectionBuilder.Uuid, out provider))
- {
+ if (!Providers.TryGetValue(connectionBuilder.Uuid, out provider)) {
provider = CreateProvider(connectionBuilder);
Providers.Add(connectionBuilder.Uuid, provider);
}
@@ -26,10 +24,11 @@ public static IConnectionProvider Get(IConnectionBuilder connectionBuilder)
private static IConnectionProvider CreateProvider(IConnectionBuilder builder)
{
- if (builder.Pooling)
+ if (builder.Pooling) {
return new PooledConnectionProvider(builder);
- else
+ } else {
return new NormalConnectionProvider(builder);
+ }
}
}
-}
+}
View
33 src/Connections/ServerManagerFactory.cs
@@ -0,0 +1,33 @@
+using System.Collections.Generic;
+
+namespace FluentCassandra.Connections
+{
+ public static class ServerManagerFactory
+ {
+ private static readonly object Lock = new object();
+ private static volatile IDictionary<string, IServerManager> Managers = new Dictionary<string, IServerManager>();
+
+ public static IServerManager Get(IConnectionBuilder connectionBuilder)
+ {
+ lock (Lock) {
+ IServerManager manager;
+
+ if (!Managers.TryGetValue(connectionBuilder.Uuid, out manager)) {
+ manager = CreateManager(connectionBuilder);
+ Managers.Add(connectionBuilder.Uuid, manager);
+ }
+
+ return manager;
+ }
+ }
+
+ private static IServerManager CreateManager(IConnectionBuilder builder)
+ {
+ if (builder.Servers.Count == 1) {
+ return new SingleServerManager(builder);
+ } else {
+ return new RoundRobinServerManager(builder);
+ }
+ }
+ }
+}
View
46 src/Connections/SingleServerManager.cs
@@ -6,12 +6,12 @@
namespace FluentCassandra.Connections
{
- public class SingleServerManager : IServerManager
- {
+ public class SingleServerManager : IServerManager
+ {
private readonly object _lock = new object();
private Server _server;
- public SingleServerManager(IConnectionBuilder builder)
+ public SingleServerManager(IConnectionBuilder builder)
{
_server = builder.Servers[0];
}
@@ -20,44 +20,48 @@ public SingleServerManager(IConnectionBuilder builder)
public bool HasNext
{
- get { return true; }
+ get { return true; }
}
public Server Next()
{
- return _server;
- }
+ 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 ("SingleServerManager does not implement Remove(server)");
- }
- #endregion
- #region IEnumerable<Server> Members
+ public void Add(Server server)
+ {
+ _server = server;
+ }
- public IEnumerator<Server> GetEnumerator()
+ public void Remove(Server server)
{
- throw new NotImplementedException("SingleServerManager does not implement Enumerable(server)");
+ throw new NotSupportedException("You cannot remove a server since SingleServerManager supports one server. Call the Add method to change the server.");
}
#endregion
-
+
+ #region IEnumerable<Server> Members
+
+ public IEnumerator<Server> GetEnumerator()
+ {
+ throw new NotImplementedException("SingleServerManager does not implement Enumerable(server)");
+ }
+
+ #endregion
+
#region IEnumerable Members
-
+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
-
+
#endregion
+
}
}
View
6 src/FluentCassandra.csproj
@@ -39,10 +39,7 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
- <SignAssembly>true</SignAssembly>
- </PropertyGroup>
- <PropertyGroup>
- <AssemblyOriginatorKeyFile>fluentCassandra-me.snk</AssemblyOriginatorKeyFile>
+ <SignAssembly>false</SignAssembly>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@@ -114,6 +111,7 @@
<Compile Include="Connections\ConnectionType.cs" />
<Compile Include="Connections\CqlVersion.cs" />
<Compile Include="Connections\IConnectionBuilder.cs" />
+ <Compile Include="Connections\ServerManagerFactory.cs" />
<Compile Include="Connections\SingleServerManager.cs" />
<Compile Include="Connections\NormalConnectionProvider.cs" />
<Compile Include="Connections\IConnection.cs" />
View
38 test/FluentCassandra.Tests/Connections/SingleServerManagerTests.cs
@@ -1,27 +1,27 @@
using System;
-using System.Collections.Generic;
using Xunit;
namespace FluentCassandra.Connections.Tests
{
- public class SingleServerManagerTests
- {
- [Fact]
- public void CanGetServerAfterError()
- {
- SingleServerManager target = new SingleServerManager(new ConnectionBuilder("Server=unit-test-1"));
+ public class SingleServerManagerTests
+ {
+ [Fact]
+ public void CanGetServerAfterError()
+ {
+ SingleServerManager target = new SingleServerManager(new ConnectionBuilder("Server=unit-test-1"));
- Server original = target.Next();
+ Server original = target.Next();
- for (int i = 0; i < 10; i++)
- {
- Assert.True(target.HasNext, "SingleServerManager should always have another server available.");
- Server next = target.Next();
- Assert.True(original.ToString().Equals(next.ToString(), StringComparison.OrdinalIgnoreCase),
- "SingleServerManager always returns the same server.");
- //mark the server as failing to set up the next test iteration.
- target.ErrorOccurred(next);
- }
- }
- }
+ for (int i = 0; i < 10; i++)
+ {
+ Assert.True(target.HasNext, "SingleServerManager should always have another server available.");
+
+ Server next = target.Next();
+ Assert.True(original.ToString().Equals(next.ToString(), StringComparison.OrdinalIgnoreCase), "SingleServerManager always returns the same server.");
+
+ //mark the server as failing to set up the next test iteration.
+ target.ErrorOccurred(next);
+ }
+ }
+ }
}
View
2 test/FluentCassandra.Tests/app.config
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
- <add key="TestServer" value="dev-Cassandra"></add>
+ <add key="TestServer" value="localhost"></add>
<add key="TestPort" value="9160"></add>
<add key="TestKeySpace" value="Testing"></add>
</appSettings>

0 comments on commit 6203be3

Please sign in to comment.
Something went wrong with that request. Please try again.