-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathQueueLengthProvider.cs
More file actions
139 lines (114 loc) · 4.38 KB
/
QueueLengthProvider.cs
File metadata and controls
139 lines (114 loc) · 4.38 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
namespace ServiceControl.Transports.PostgreSql;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Npgsql;
class QueueLengthProvider : AbstractQueueLengthProvider
{
public QueueLengthProvider(TransportSettings settings, Action<QueueLengthEntry[], EndpointToQueueMapping> store, ILogger<QueueLengthProvider> logger) : base(settings, store)
{
connectionString = ConnectionString
.RemoveCustomConnectionStringParts(out var customSchema, out _);
defaultSchema = customSchema ?? "public";
this.logger = logger;
}
public override void TrackEndpointInputQueue(EndpointToQueueMapping queueToTrack)
{
var parsedAddress = QueueAddress.Parse(queueToTrack.InputQueue);
var sqlTable = new PostgreSqlTable(parsedAddress.Table, parsedAddress.Schema ?? defaultSchema);
tableNames.AddOrUpdate(queueToTrack, _ => sqlTable, (_, currentSqlTable) =>
{
if (!currentSqlTable.Equals(sqlTable))
{
tableSizes.TryRemove(currentSqlTable, out var _);
}
return sqlTable;
});
tableSizes.TryAdd(sqlTable, 0);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(QueryDelayInterval, stoppingToken);
await QueryTableSizes(stoppingToken);
UpdateQueueLengthStore();
}
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
{
// no-op
}
catch (Exception e)
{
logger.LogError(e, "Error querying SQL queue sizes.");
}
}
}
void UpdateQueueLengthStore()
{
var nowTicks = DateTime.UtcNow.Ticks;
foreach (var tableNamePair in tableNames)
{
Store(
[
new QueueLengthEntry
{
DateTicks = nowTicks,
Value = tableSizes.GetValueOrDefault(tableNamePair.Value, 0)
}
],
tableNamePair.Key);
}
}
async Task QueryTableSizes(CancellationToken cancellationToken)
{
var chunks = tableSizes
.Select((i, index) => new
{
i,
index
})
.GroupBy(p => p.index / QueryChunkSize)
.Select(grp => grp.Select(g => g.i).ToArray())
.ToList();
await using var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync(cancellationToken);
foreach (var chunk in chunks)
{
await UpdateChunk(connection, chunk, cancellationToken);
}
}
async Task UpdateChunk(NpgsqlConnection connection, KeyValuePair<PostgreSqlTable, int>[] chunk, CancellationToken cancellationToken)
{
var query = string.Join(Environment.NewLine, chunk.Select(c => c.Key.LengthQuery));
await using var command = new NpgsqlCommand(query, connection);
await using var reader = await command.ExecuteReaderAsync(cancellationToken);
foreach (var chunkPair in chunk)
{
await reader.ReadAsync(cancellationToken);
var queueLength = reader.GetInt32(0);
if (queueLength == -1)
{
logger.LogWarning("Table {TableName} does not exist.", chunkPair.Key);
}
else
{
tableSizes.TryUpdate(chunkPair.Key, queueLength, chunkPair.Value);
}
await reader.NextResultAsync(cancellationToken);
}
}
readonly ConcurrentDictionary<EndpointToQueueMapping, PostgreSqlTable> tableNames = new ConcurrentDictionary<EndpointToQueueMapping, PostgreSqlTable>();
readonly ConcurrentDictionary<PostgreSqlTable, int> tableSizes = new ConcurrentDictionary<PostgreSqlTable, int>();
readonly string connectionString;
readonly string defaultSchema;
readonly ILogger<QueueLengthProvider> logger;
static readonly TimeSpan QueryDelayInterval = TimeSpan.FromMilliseconds(200);
const int QueryChunkSize = 10;
}