/
OutOfMemoryNotifications.cs
92 lines (79 loc) · 3.36 KB
/
OutOfMemoryNotifications.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Threading;
using Raven.Server.NotificationCenter.Notifications;
using Raven.Server.NotificationCenter.Notifications.Details;
using Sparrow;
using Sparrow.LowMemory;
using Sparrow.Utils;
using Voron;
namespace Raven.Server.NotificationCenter
{
public class OutOfMemoryNotifications
{
private readonly TimeSpan _updateFrequency = TimeSpan.FromSeconds(15);
private readonly ConditionalWeakTable<StorageEnvironment, ConcurrentDictionary<Type, NotificationTime>> _notificationsMetadataTable =
new ConditionalWeakTable<StorageEnvironment, ConcurrentDictionary<Type, NotificationTime>>();
private readonly NotificationCenter _notificationsCenter;
public OutOfMemoryNotifications(NotificationCenter notificationsCenter)
{
_notificationsCenter = notificationsCenter;
}
public void Add(StorageEnvironment environment, Exception exception)
{
var notificationsMetadata = _notificationsMetadataTable.GetOrCreateValue(environment);
if (notificationsMetadata.TryGetValue(exception.GetType(), out var notificationMetadata))
{
if (DateTime.Now - notificationMetadata.Time < _updateFrequency)
return;
if (Interlocked.CompareExchange(ref notificationMetadata.IsInProgress, 1, 0) == 1)
return;
notificationMetadata.Time = DateTime.Now;
}
else
{
notificationMetadata = new NotificationTime
{
Time = DateTime.Now,
IsInProgress = 1
};
if (notificationsMetadata.TryAdd(exception.GetType(), notificationMetadata) == false)
return;
//We are in low of memory so we want to minimize allocations
notificationMetadata.Key = $"{environment}:{exception.GetType()}";
notificationMetadata.Title = $"Out of memory occurred for '{environment}'";
}
var alert = AlertRaised.Create(
null,
notificationMetadata.Title,
exception.Message,
AlertType.OutOfMemoryException,
NotificationSeverity.Error,
notificationMetadata.Key,
OutOfMemoryDetails(exception));
_notificationsCenter.Add(alert);
Volatile.Write(ref notificationMetadata.IsInProgress, 0);
}
private static MessageDetails OutOfMemoryDetails(Exception exception)
{
MemoryInfoResult memoryInfo;
if (exception is EarlyOutOfMemoryException eoome && eoome.MemoryInfo != null)
memoryInfo = eoome.MemoryInfo.Value;
else
memoryInfo = MemoryInformation.GetMemoryInformationUsingOneTimeSmapsReader();
return new MessageDetails
{
Message = $"{MemoryUtils.GetExtendedMemoryInfo(memoryInfo)} {Environment.NewLine}" +
$"Error: {exception}"
};
}
private class NotificationTime
{
public int IsInProgress;
public DateTime Time;
public string Key;
public string Title;
}
}
}