From de64824b3af5a1939beaf90f95e3e1d8e6404014 Mon Sep 17 00:00:00 2001 From: sakno Date: Tue, 20 Feb 2024 21:48:38 +0200 Subject: [PATCH] Fixed GC latency mode when replication forced programmatically --- .../Net/Cluster/Consensus/Raft/LeaderState.cs | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs index 9127282ff..9b567b918 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/LeaderState.cs @@ -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 : RaftState where TMember : class, IRaftClusterMember @@ -136,21 +136,11 @@ private async Task DoHeartbeats(TimeSpan period, IAuditTrail 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 @@ -205,10 +195,7 @@ private async Task DoHeartbeats(TimeSpan period, IAuditTrail audi finally { var broadcastTime = startTime.ElapsedMilliseconds; - - if (forced) - GCSettings.LatencyMode = latencyMode; - + latencyScope.Dispose(); LeaderState.BroadcastTimeMeter.Record(broadcastTime, MeasurementTags); }