Skip to content

Commit 2507d53

Browse files
committed
Add IsLeader property to ClusterInfo
1 parent e4f46bd commit 2507d53

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

Core/Cleipnir.ResilientFunctions.Tests/TestTemplates/WatchDogsTests/ReplicaWatchdogTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,21 @@ public async Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted(Task<IFuncti
191191
var watchdog2 = new ReplicaWatchdog(cluster2, functionStore, heartbeatFrequency: TimeSpan.FromHours(1), utcNow: () => DateTime.UtcNow, unhandledExceptionHandler: default(UnhandledExceptionHandler)!);
192192
var watchdog3 = new ReplicaWatchdog(cluster3, functionStore, heartbeatFrequency: TimeSpan.FromHours(1), utcNow: () => DateTime.UtcNow, unhandledExceptionHandler: default(UnhandledExceptionHandler)!);
193193

194+
cluster3.IsLeader.ShouldBeFalse();
194195
await watchdog3.Initialize();
195196
cluster3.Offset.ShouldBe((ulong) 0);
196197
cluster3.ReplicaCount.ShouldBe((ulong) 1);
198+
cluster3.IsLeader.ShouldBeTrue();
197199

198200
await watchdog2.Initialize();
199201
await watchdog3.PerformIteration(utcNowTicks: 0);
200202
cluster3.Offset.ShouldBe((ulong) 1);
201203
cluster3.ReplicaCount.ShouldBe((ulong) 2);
202204
cluster2.Offset.ShouldBe((ulong) 0);
203205
cluster2.ReplicaCount.ShouldBe((ulong) 2);
204-
206+
cluster3.IsLeader.ShouldBeFalse();
207+
cluster2.IsLeader.ShouldBeTrue();
208+
205209
await watchdog1.Initialize();
206210
await watchdog2.PerformIteration(utcNowTicks: 0);
207211
await watchdog3.PerformIteration(utcNowTicks: 1);
@@ -211,6 +215,9 @@ public async Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted(Task<IFuncti
211215
cluster2.ReplicaCount.ShouldBe((ulong) 3);
212216
cluster1.Offset.ShouldBe((ulong) 0);
213217
cluster1.ReplicaCount.ShouldBe((ulong) 3);
218+
cluster3.IsLeader.ShouldBeFalse();
219+
cluster2.IsLeader.ShouldBeFalse();
220+
cluster1.IsLeader.ShouldBeTrue();
214221

215222
await store.Delete(cluster1.ReplicaId);
216223
await watchdog3.PerformIteration(utcNowTicks: 1);
@@ -219,11 +226,14 @@ public async Task ReplicaIdOffsetIsUpdatedWhenNodeIsAddedAndDeleted(Task<IFuncti
219226
cluster3.ReplicaCount.ShouldBe((ulong) 2);
220227
cluster2.Offset.ShouldBe((ulong) 0);
221228
cluster2.ReplicaCount.ShouldBe((ulong) 2);
222-
229+
cluster3.IsLeader.ShouldBeFalse();
230+
cluster2.IsLeader.ShouldBeTrue();
231+
223232
await store.Delete(cluster2.ReplicaId);
224233
await watchdog3.PerformIteration(utcNowTicks: 2);
225234
cluster3.Offset.ShouldBe((ulong) 0);
226235
cluster3.ReplicaCount.ShouldBe((ulong) 1);
236+
cluster3.IsLeader.ShouldBeTrue();
227237
}
228238

229239
public abstract Task ActiveReplicasDoNotDeleteEachOther();

Core/Cleipnir.ResilientFunctions/Domain/ClusterInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ internal set
4040
}
4141
}
4242

43+
// The leader is the replica with the lowest id - i.e. the replica with offset 0 in the ascendingly ordered replica ids
44+
public bool IsLeader
45+
{
46+
get
47+
{
48+
lock (_sync)
49+
return _replicaCount > 0 && _offset == 0;
50+
}
51+
}
52+
4353
private readonly Lock _sync = new();
4454

4555
public bool OwnedByThisReplica(StoredId storedId)

0 commit comments

Comments
 (0)