-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from mizrael/memory-consumption
added memory watcher
- Loading branch information
Showing
15 changed files
with
301 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace EvenireDB.Server | ||
{ | ||
public static partial class LogMessages | ||
{ | ||
[LoggerMessage( | ||
EventId = 1, | ||
Level = LogLevel.Warning, | ||
Message = "Memory usage is {MemoryUsage} bytes, more than the allowed value: {MaxAllowedAllocatedBytes}. Dropping some cached streams")] | ||
public static partial void HighMemoryUsageDetected(this ILogger logger, long memoryUsage, long maxAllowedAllocatedBytes); | ||
|
||
[LoggerMessage( | ||
EventId = 2, | ||
Level = LogLevel.Debug, | ||
Message = "Memory usage is {MemoryUsage} / {MaxAllowedAllocatedBytes} bytes")] | ||
public static partial void MemoryUsageBelowTreshold(this ILogger logger, long memoryUsage, long maxAllowedAllocatedBytes); | ||
|
||
[LoggerMessage( | ||
EventId = 3, | ||
Level = LogLevel.Error, | ||
Message = "an error has occurred while persisting events group for stream {StreamId}: {Error}")] | ||
public static partial void EventsGroupPersistenceError(this ILogger logger, Guid streamId, string error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using EvenireDB.Utils; | ||
using System.Diagnostics; | ||
|
||
namespace EvenireDB.Server | ||
{ | ||
public record MemoryWatcherSettings( | ||
TimeSpan Interval, | ||
long MaxAllowedAllocatedBytes); | ||
|
||
public class MemoryWatcher : BackgroundService | ||
{ | ||
private readonly MemoryWatcherSettings _settings; | ||
private readonly ILogger<MemoryWatcher> _logger; | ||
private readonly IServiceProvider _sp; | ||
|
||
public MemoryWatcher(MemoryWatcherSettings settings, ILogger<MemoryWatcher> logger, IServiceProvider sp) | ||
{ | ||
_settings = settings; | ||
_logger = logger; | ||
_sp = sp; | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
using var process = Process.GetCurrentProcess(); | ||
|
||
bool needDrop = process.PrivateMemorySize64 > _settings.MaxAllowedAllocatedBytes; | ||
if (needDrop) | ||
{ | ||
_logger.HighMemoryUsageDetected(process.PrivateMemorySize64, _settings.MaxAllowedAllocatedBytes); | ||
|
||
using var scope = _sp.CreateScope(); | ||
var cache = scope.ServiceProvider.GetRequiredService<ICache<Guid, CachedEvents>>(); | ||
|
||
var dropCount = cache.Count / 3; | ||
cache.DropOldest(dropCount); | ||
|
||
GC.Collect(); | ||
} | ||
else | ||
{ | ||
_logger.MemoryUsageBelowTreshold(process.PrivateMemorySize64, _settings.MaxAllowedAllocatedBytes); | ||
} | ||
|
||
await Task.Delay(_settings.Interval, stoppingToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
{ | ||
"Logging": { | ||
"Console": { | ||
"IncludeScopes": true | ||
}, | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
"Default": "Warning", | ||
"EvenireDB": "Trace" | ||
} | ||
}, | ||
"Server": { | ||
"HttpPort": 5001, | ||
"GrpcPort": 5002 | ||
"GrpcPort": 5002, | ||
"MemoryWatcherInterval": "0:00:10", | ||
"MaxAllowedAllocatedBytes": 50000000 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EvenireDB | ||
{ | ||
public static partial class LogMessages | ||
{ | ||
[LoggerMessage( | ||
EventId = 0, | ||
Level = LogLevel.Warning, | ||
Message = "Reading stream '{StreamId}' from repository")] | ||
public static partial void ReadingStreamFromRepository(this ILogger logger, Guid streamId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.