Skip to content

Commit

Permalink
Fixed GC latency mode when replication forced programmatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Feb 20, 2024
1 parent c7b7f57 commit de64824
Showing 1 changed file with 7 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace DotNext.Net.Cluster.Consensus.Raft;
using Membership;
using Runtime.CompilerServices;
using Threading.Tasks;
using static Threading.LinkedTokenSourceFactory;
using GCLatencyModeScope = Runtime.GCLatencyModeScope;

internal sealed partial class LeaderState<TMember> : RaftState<TMember>
where TMember : class, IRaftClusterMember
Expand Down Expand Up @@ -136,21 +136,11 @@ private async Task DoHeartbeats(TimeSpan period, IAuditTrail<IRaftLogEntry> audi
// do not resume suspended callers that came after the barrier, resume them in the next iteration
replicationQueue.SwitchValve();

GCLatencyMode latencyMode;
if (forced)
{
// in case of forced (initiated programmatically, not by timeout) replication
// do not change GC latency. Otherwise, in case of high load GC is not able to collect garbage
Unsafe.SkipInit(out latencyMode);
}
else
{
// we want to minimize GC intrusion during replication process
// (however, it is still allowed in case of system-wide memory pressure, e.g. due to container limits)
latencyMode = GCSettings.LatencyMode;
GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
}

// in case of forced (initiated programmatically, not by timeout) replication
// do not change GC latency. Otherwise, in case of high load GC is not able to collect garbage
var latencyScope = forced
? default
: GCLatencyModeScope.SustainedLowLatency;
try
{
// Perf: the code in this block is inlined instead of moved to separated method because
Expand Down Expand Up @@ -205,10 +195,7 @@ private async Task DoHeartbeats(TimeSpan period, IAuditTrail<IRaftLogEntry> audi
finally
{
var broadcastTime = startTime.ElapsedMilliseconds;

if (forced)
GCSettings.LatencyMode = latencyMode;

latencyScope.Dispose();
LeaderState.BroadcastTimeMeter.Record(broadcastTime, MeasurementTags);
}

Expand Down

0 comments on commit de64824

Please sign in to comment.