From 0c81f453ed156779f145c255bfd1509fab8495c0 Mon Sep 17 00:00:00 2001 From: "Jordan S. Jones" Date: Sun, 26 Apr 2015 21:35:22 -0600 Subject: [PATCH] Removed RoutingStrategyConsistentKeyHashing Didn't have the time to get it working correctly. --- source/Draft/Draft-Net45.csproj | 1 - .../Endpoints/EndpointRoutingStrategy.cs | 10 -- .../RoutingStrategy.ConsistentKeyHashing.cs | 22 ----- tests/Draft.Tests/Draft-Net45.Tests.csproj | 1 - ...outingStrategyConsistentKeyHashingTests.cs | 91 ------------------- 5 files changed, 125 deletions(-) delete mode 100644 source/Draft/Endpoints/RoutingStrategy.ConsistentKeyHashing.cs delete mode 100644 tests/Draft.Tests/Tests/RoutingStrategies/RoutingStrategyConsistentKeyHashingTests.cs diff --git a/source/Draft/Draft-Net45.csproj b/source/Draft/Draft-Net45.csproj index d850987..cd5ee2d 100644 --- a/source/Draft/Draft-Net45.csproj +++ b/source/Draft/Draft-Net45.csproj @@ -72,7 +72,6 @@ - diff --git a/source/Draft/Endpoints/EndpointRoutingStrategy.cs b/source/Draft/Endpoints/EndpointRoutingStrategy.cs index 478f150..bee018a 100644 --- a/source/Draft/Endpoints/EndpointRoutingStrategy.cs +++ b/source/Draft/Endpoints/EndpointRoutingStrategy.cs @@ -24,22 +24,12 @@ internal static EndpointRoutingStrategy Default #region Built-in strategies - private static readonly Lazy LazyConsistenKeyHashing = new Lazy(() => new RoutingStrategyConsistentKeyHashing()); - private static readonly Lazy LazyFirst = new Lazy(() => new RoutingStrategyFirst()); private static readonly Lazy LazyRandom = new Lazy(() => new RoutingStrategyRandom()); private static readonly Lazy LazyRoundRobin = new Lazy(() => new RoutingStrategyRoundRobin()); -// /// -// /// Uses a consistent hashing algorithm on the etcd key to select the . -// /// -// public static EndpointRoutingStrategy ConsistentKeyHashing -// { -// get { return LazyConsistenKeyHashing.Value; } -// } - /// /// Uses the first . /// diff --git a/source/Draft/Endpoints/RoutingStrategy.ConsistentKeyHashing.cs b/source/Draft/Endpoints/RoutingStrategy.ConsistentKeyHashing.cs deleted file mode 100644 index 549dc09..0000000 --- a/source/Draft/Endpoints/RoutingStrategy.ConsistentKeyHashing.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Linq; - -namespace Draft.Endpoints -{ - internal sealed class RoutingStrategyConsistentKeyHashing : EndpointRoutingStrategy - { - - public override Endpoint Select(string key, Endpoint[] endpoints) - { - - return endpoints[GetOffset(key, endpoints.Length)]; - } - - internal static int GetOffset(string key, int totalItems) - { - var value = key.GetHashCode(); - return (value & int.MaxValue) % totalItems; - } - - } -} diff --git a/tests/Draft.Tests/Draft-Net45.Tests.csproj b/tests/Draft.Tests/Draft-Net45.Tests.csproj index 0aed7a8..9e54b12 100644 --- a/tests/Draft.Tests/Draft-Net45.Tests.csproj +++ b/tests/Draft.Tests/Draft-Net45.Tests.csproj @@ -121,7 +121,6 @@ - diff --git a/tests/Draft.Tests/Tests/RoutingStrategies/RoutingStrategyConsistentKeyHashingTests.cs b/tests/Draft.Tests/Tests/RoutingStrategies/RoutingStrategyConsistentKeyHashingTests.cs deleted file mode 100644 index e4955c4..0000000 --- a/tests/Draft.Tests/Tests/RoutingStrategies/RoutingStrategyConsistentKeyHashingTests.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; - -using FluentAssertions; - -using System.Linq; - -using Draft.Endpoints; - -using Xunit; - -namespace Draft.Tests.RoutingStrategies -{ - public class RoutingStrategyConsistentKeyHashingTests : BaseRoutingStrategyTests - { - - private Endpoint[] Endpoints - { - get { return new[] {Endpoint1, Endpoint2, Endpoint3, Endpoint4, Endpoint5}; } - } - - protected override EndpointRoutingStrategy RoutingStrategy - { - get { return new RoutingStrategyConsistentKeyHashing(); } - } - - [Fact] - public void ShouldAlwaysSelectTheSameEndpointWithHardcodedKey() - { - var sut = CreateSut(RoutingStrategy, Endpoints); - - for (var i = 0; i < 100; i++) - { - var result = sut.GetEndpointUrl("35b0c8db-f305-4e5c-95f7-cf1d77cfcc18"); - - result.Should().NotBeNull(); - result.ToString() - .Should() - .StartWith(Endpoint1.Uri.ToString()); - } - } - - [Fact] - public void ShouldAlwaysSelectTheSameEndpointWithGeneratedKey() - { - var bytes = new byte[StaticRandom.Instance.Next(100, 1000)]; - StaticRandom.Instance.NextBytes(bytes); - - var key1 = Guid.NewGuid().ToString(); - var key2 = StaticRandom.Instance.Next().ToString(); - var key3 = Convert.ToBase64String(bytes); - var key4 = StaticRandom.Instance.NextDouble().ToString("G"); - - var key1Offset = RoutingStrategyConsistentKeyHashing.GetOffset(key1, Endpoints.Length); - var key2Offset = RoutingStrategyConsistentKeyHashing.GetOffset(key2, Endpoints.Length); - var key3Offset = RoutingStrategyConsistentKeyHashing.GetOffset(key3, Endpoints.Length); - var key4Offset = RoutingStrategyConsistentKeyHashing.GetOffset(key4, Endpoints.Length); - - var sut = CreateSut(RoutingStrategy, Endpoints); - - for (var i = 0; i < 500; i++) - { - var result1 = sut.GetEndpointUrl(key1); - var result2 = sut.GetEndpointUrl(key2); - var result3 = sut.GetEndpointUrl(key3); - var result4 = sut.GetEndpointUrl(key4); - - result1.Should().NotBeNull(); - result2.Should().NotBeNull(); - result3.Should().NotBeNull(); - result4.Should().NotBeNull(); - - result1.ToString() - .Should() - .StartWith(Endpoints[key1Offset].Uri.ToString()); - - result2.ToString() - .Should() - .StartWith(Endpoints[key2Offset].Uri.ToString()); - - result3.ToString() - .Should() - .StartWith(Endpoints[key3Offset].Uri.ToString()); - - result4.ToString() - .Should() - .StartWith(Endpoints[key4Offset].Uri.ToString()); - } - } - - } -}