Merge pull request #494 from Icinga:fix/gc_collection_for_every_rest_connection

Fix: GC collection on every REST call

Fixes GC collection with `Optimize-IcingaForWindowsMemory` for every incoming REST connection call
This commit is contained in:
Lord Hepipud 2022-04-27 08:52:43 +02:00 committed by GitHub
commit 89f4ac7cb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 34 additions and 7 deletions

View file

@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
* [#480](https://github.com/Icinga/icinga-powershell-framework/pull/480) Fixes service locking during Icinga Agent upgrade and ensures errors on service management are caught and printed with internal error handling * [#480](https://github.com/Icinga/icinga-powershell-framework/pull/480) Fixes service locking during Icinga Agent upgrade and ensures errors on service management are caught and printed with internal error handling
* [#483](https://github.com/Icinga/icinga-powershell-framework/issues/483) Fixes REST-Api SSL certificate lookup from the Icinga Agent, in case a custom hostname was used or in certain domain environments were domain is not matching DNS domain * [#483](https://github.com/Icinga/icinga-powershell-framework/issues/483) Fixes REST-Api SSL certificate lookup from the Icinga Agent, in case a custom hostname was used or in certain domain environments were domain is not matching DNS domain
* [#490](https://github.com/Icinga/icinga-powershell-framework/pull/490) Fixes the command `Uninstall-IcingaComponent` for the `service` component which is not doing anything * [#490](https://github.com/Icinga/icinga-powershell-framework/pull/490) Fixes the command `Uninstall-IcingaComponent` for the `service` component which is not doing anything
* [#491](https://github.com/Icinga/icinga-powershell-framework/issues/491) Fixes GC collection with `Optimize-IcingaForWindowsMemory` for every incoming REST connection call
* [#497](https://github.com/Icinga/icinga-powershell-framework/pull/497) Fixes loop sleep for idle REST-Api threads by replacing them with [BlockingCollection](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=net-6.0) [ConcurrentQueue](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-6.0) * [#497](https://github.com/Icinga/icinga-powershell-framework/pull/497) Fixes loop sleep for idle REST-Api threads by replacing them with [BlockingCollection](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=net-6.0) [ConcurrentQueue](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-6.0)
### Enhancements ### Enhancements

View file

@ -62,5 +62,6 @@ function New-IcingaEnvironmentVariable()
$Global:Icinga.Protected.Add('RunAsDaemon', $FALSE); $Global:Icinga.Protected.Add('RunAsDaemon', $FALSE);
$Global:Icinga.Protected.Add('Minimal', $FALSE); $Global:Icinga.Protected.Add('Minimal', $FALSE);
$Global:Icinga.Protected.Add('ThreadName', ''); $Global:Icinga.Protected.Add('ThreadName', '');
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
} }
} }

View file

@ -11,6 +11,10 @@
properly properly
.PARAMETER ClearErrorStack .PARAMETER ClearErrorStack
Also clears the current error stack to free additional memory Also clears the current error stack to free additional memory
.PARAMETER SmartGC
Ensures that memory is not flushed whenever this function is called, but instead
every 30 attempts this function is called to reduce CPU load. Only works for
PowerShell sessions with "$Global:Icinga.Protected.ThreadName" being set
.EXAMPLE .EXAMPLE
Optimize-IcingaForWindowsMemory; Optimize-IcingaForWindowsMemory;
.EXAMPLE .EXAMPLE
@ -21,9 +25,26 @@
function Optimize-IcingaForWindowsMemory() function Optimize-IcingaForWindowsMemory()
{ {
param ( param (
[switch]$ClearErrorStack = $FALSE [switch]$ClearErrorStack = $FALSE,
[switch]$SmartGC = $FALSE
); );
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.ThreadName) -eq $FALSE -And $SmartGC) {
if ($Global:Icinga.Protected.GarbageCollector.ContainsKey($Global:Icinga.Protected.ThreadName) -eq $FALSE) {
$Global:Icinga.Protected.GarbageCollector.Add($Global:Icinga.Protected.ThreadName, 0);
return;
} else {
$Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] += 1;
}
if ($Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] -le 30) {
return;
}
$Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] = 0;
}
# Clear all errors within our error stack # Clear all errors within our error stack
if ($ClearErrorStack) { if ($ClearErrorStack) {
$Error.Clear(); $Error.Clear();

View file

@ -20,9 +20,12 @@ function Add-IcingaForWindowsDaemon()
} }
while ($TRUE) { while ($TRUE) {
Start-Sleep -Seconds 1; Start-Sleep -Seconds 10;
# Handle possible threads being frozen # Handle possible threads being frozen
Suspend-IcingaForWindowsFrozenThreads; Suspend-IcingaForWindowsFrozenThreads;
# Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
} }
} }

View file

@ -78,7 +78,7 @@ function New-IcingaForWindowsRESTApi()
while ($TRUE) { while ($TRUE) {
# Force Icinga for Windows Garbage Collection # Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -ClearErrorStack; Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
$Connection = Open-IcingaTCPClientConnection ` $Connection = Open-IcingaTCPClientConnection `
-Client (New-IcingaTCPClient -Socket $Socket) ` -Client (New-IcingaTCPClient -Socket $Socket) `

View file

@ -101,6 +101,6 @@ function New-IcingaForWindowsRESTThread()
Close-IcingaTCPConnection -Client $Connection.Client; Close-IcingaTCPConnection -Client $Connection.Client;
# Force Icinga for Windows Garbage Collection # Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -ClearErrorStack; Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
} }
} }

View file

@ -78,7 +78,8 @@ function Add-IcingaServiceCheckDaemon()
} }
} }
Optimize-IcingaForWindowsMemory; # Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -SmartGC;
Start-Sleep -Seconds 10; Start-Sleep -Seconds 10;
} }

View file

@ -36,7 +36,7 @@ function Add-IcingaServiceCheckTask()
Clear-IcingaCheckSchedulerEnvironment; Clear-IcingaCheckSchedulerEnvironment;
# Force Icinga for Windows Garbage Collection # Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -ClearErrorStack; Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
continue; continue;
} }
@ -133,6 +133,6 @@ function Add-IcingaServiceCheckTask()
# Reset certain values from the scheduler environment # Reset certain values from the scheduler environment
Clear-IcingaServiceCheckDaemonEnvironment; Clear-IcingaServiceCheckDaemonEnvironment;
# Force Icinga for Windows Garbage Collection # Force Icinga for Windows Garbage Collection
Optimize-IcingaForWindowsMemory -ClearErrorStack; Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
} }
} }