Skip to content

Commit

Permalink
Merge pull request #115 from Drawaes/FixingLeadership
Browse files Browse the repository at this point in the history
Fixing Leadership
  • Loading branch information
Drawaes authored Mar 21, 2019
2 parents 1b153b4 + 7fed437 commit 23b279f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
os: Visual Studio 2017
os: Visual Studio 2019 Preview
build: off

environment:
Expand All @@ -8,7 +8,7 @@ environment:
secure: x41DSerLXKgGVbKIokF+zuR3eNRVJXsgJA6j5yggnCB8/TTyYfa/2euNflfGzCot

install:
- cmd: curl -fsS -o consul.zip https://releases.hashicorp.com/consul/1.0.2/consul_1.0.2_windows_amd64.zip
- cmd: curl -fsS -o consul.zip https://releases.hashicorp.com/consul/1.4.4/consul_1.4.4_windows_amd64.zip
- cmd: 7z x consul.zip -o"C:\Consul" -y > nul
- ps: $MyProcess = Start-Process C:\Consul\consul.exe -ArgumentList "agent -server -log-level err -bootstrap-expect 1 -data-dir C:\Consul\Data -advertise=127.0.0.1" -PassThru

Expand Down
4 changes: 2 additions & 2 deletions src/CondenserDotNet.Client/Leadership/LeaderRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CondenserDotNet.Client.Leadership
public class LeaderRegistry : ILeaderRegistry
{
private readonly IServiceManager _serviceManager;
private readonly Dictionary<string, LeaderWatcherNew> _leaderWatchers = new Dictionary<string, LeaderWatcherNew>(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, LeaderWatcher> _leaderWatchers = new Dictionary<string, LeaderWatcher>(StringComparer.OrdinalIgnoreCase);

public LeaderRegistry(IServiceManager serviceManager) => _serviceManager = serviceManager;

Expand All @@ -23,7 +23,7 @@ public async Task<ILeaderWatcher> GetLeaderWatcherAsync(string keyForLeadership)
{
if (!_leaderWatchers.TryGetValue(keyForLeadership, out var returnValue))
{
returnValue = new LeaderWatcherNew(_serviceManager, keyForLeadership);
returnValue = new LeaderWatcher(_serviceManager, keyForLeadership);
_leaderWatchers[keyForLeadership] = returnValue;
}
return returnValue;
Expand Down
15 changes: 12 additions & 3 deletions src/CondenserDotNet.Client/Leadership/LeaderWatcherNew.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace CondenserDotNet.Client.Leadership
{
public class LeaderWatcherNew : ILeaderWatcher
public class LeaderWatcher : ILeaderWatcher
{
private readonly AsyncManualResetEvent<bool> _electedLeaderEvent = new AsyncManualResetEvent<bool>();
private Action<InformationService> _callback;
Expand All @@ -23,8 +23,10 @@ public class LeaderWatcherNew : ILeaderWatcher
private string _keyToWatch;
private AsyncManualResetEvent<InformationService> _currentInfoService = new AsyncManualResetEvent<InformationService>();

public LeaderWatcherNew(IServiceManager serviceManager, string keyToWatch)
public LeaderWatcher(IServiceManager serviceManager, string keyToWatch)
{
_currentInfoService.Reset();
_electedLeaderEvent.Reset();
_keyToWatch = keyToWatch;
_serviceManager = serviceManager;
_sessionIdTask = GetSession();
Expand Down Expand Up @@ -92,7 +94,7 @@ private async Task KeepLeadershipLoop()
}
else
{
_electedLeaderEvent.Set(false);
_electedLeaderEvent.Reset();
}
await WaitForLeadershipChange();
}
Expand All @@ -105,6 +107,13 @@ private async Task WaitForLeadershipChange()
var leaderResult = await _serviceManager.Client.GetAsync($"{KeyPath}{_keyToWatch}?index={_consulIndex}");
if(!leaderResult.IsSuccessStatusCode)
{
//Lock deleted
if(leaderResult.StatusCode == System.Net.HttpStatusCode.NotFound)
{
_electedLeaderEvent.Reset();
_currentInfoService.Reset();
return;
}
await Task.Delay(500);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AssemblyName>Condenser.Tests.Integration</AssemblyName>
<PackageId>Condenser.Tests.Integration</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down
18 changes: 17 additions & 1 deletion test/Condenser.Tests.Integration/LeadershipFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ public async Task TestGetLeadership()
}
}

//[Fact]
//public async Task TestHoldingLeadershipForAWhile()
//{
// using (var manager = GetConfig("Service1"))
// {
// manager.AddTtlHealthCheck(100);
// var registerResult = await manager.RegisterServiceAsync();
// var ttlResult = await manager.TtlCheck.ReportPassingAsync();
// var leaderRegistry = new LeaderRegistry(manager);
// var watcher = await leaderRegistry.GetLeaderWatcherAsync(leadershipKey);
// await watcher.GetLeadershipAsync();
// await Task.Delay(50000);
// }
//}

[Fact]
public async Task TestLeadershipFailOver()
{
Expand Down Expand Up @@ -116,7 +131,8 @@ public async Task TestLeadershipFailOver()
await manager.TtlCheck.ReportFailAsync();

//Now we wait, the leadership should fall over
Assert.True(resetEvent.WaitOne(5000));
var waitResult = resetEvent.WaitOne(5000);
Assert.True(waitResult);

//Now check that service 2 is the leader
var leader = await watcher2.GetCurrentLeaderAsync();
Expand Down
4 changes: 2 additions & 2 deletions test/CondenserTests/CondenserTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\common.props" />
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AssemblyName>CondenserTests</AssemblyName>
<PackageId>CondenserTests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand Down

0 comments on commit 23b279f

Please sign in to comment.