/
PostgresqlNodePersistence.cs
335 lines (271 loc) · 12.2 KB
/
PostgresqlNodePersistence.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System.Data.Common;
using JasperFx.Core;
using Npgsql;
using NpgsqlTypes;
using Weasel.Core;
using Weasel.Core.Migrations;
using Weasel.Postgresql;
using Wolverine.RDBMS;
using Wolverine.RDBMS.Durability;
using Wolverine.Runtime.Agents;
using Wolverine.Transports;
using CommandExtensions = Weasel.Core.CommandExtensions;
namespace Wolverine.Postgresql;
internal class PostgresqlNodePersistence : DatabaseConstants, INodeAgentPersistence
{
public static int LeaderLockId = 9999999;
private readonly DbObjectName _assignmentTable;
private readonly DbObjectName _nodeTable;
private readonly DatabaseSettings _settings;
private readonly IMessageDatabase _database;
private readonly NpgsqlDataSource _dataSource;
public PostgresqlNodePersistence(DatabaseSettings settings, PostgresqlMessageStore database,
NpgsqlDataSource dataSource)
{
_settings = settings;
_database = database;
_dataSource = dataSource ?? throw new ArgumentNullException(nameof(dataSource));
_nodeTable = new DbObjectName(settings.SchemaName ?? "public", DatabaseConstants.NodeTableName);
_assignmentTable =
new DbObjectName(settings.SchemaName ?? "public", DatabaseConstants.NodeAssignmentsTableName);
}
public Task ClearAllAsync(CancellationToken cancellationToken)
{
return _dataSource.CreateCommand($"delete from {_nodeTable}")
.ExecuteNonQueryAsync(cancellationToken);
}
public async Task<int> PersistAsync(WolverineNode node, CancellationToken cancellationToken)
{
var cmd = (NpgsqlCommand)_dataSource.CreateCommand(
$"insert into {_nodeTable} (id, uri, capabilities, description) values (:id, :uri, :capabilities, :description) returning node_number")
.With("id", node.Id)
.With("uri", (node.ControlUri ?? TransportConstants.LocalUri).ToString())
.With("description", node.Description);
var strings = node.Capabilities.Select(x => x.ToString()).ToArray();
cmd.With("capabilities", strings);
var raw = await cmd.ExecuteScalarAsync(cancellationToken);
return (int)raw!;
}
public Task DeleteAsync(Guid nodeId)
{
if (_database.HasDisposed) return Task.CompletedTask;
return _dataSource.CreateCommand($"delete from {_nodeTable} where id = :id")
.With("id", nodeId)
.ExecuteNonQueryAsync();
}
public async Task<IReadOnlyList<WolverineNode>> LoadAllNodesAsync(CancellationToken cancellationToken)
{
var nodes = new List<WolverineNode>();
await using var cmd = _dataSource.CreateCommand(
$"select {NodeColumns} from {_nodeTable};select {Id}, {NodeId}, {Started} from {_assignmentTable};");
await using var reader = await cmd.ExecuteReaderAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
var node = await readNodeAsync(reader);
nodes.Add(node);
}
var dict = nodes.ToDictionary(x => x.Id);
await reader.NextResultAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
var agentId = new Uri(await reader.GetFieldValueAsync<string>(0, cancellationToken));
var nodeId = await reader.GetFieldValueAsync<Guid>(1, cancellationToken);
dict[nodeId].ActiveAgents.Add(agentId);
}
await reader.CloseAsync();
return nodes;
}
public async Task<WolverineNode?> LoadNodeAsync(Guid nodeId, CancellationToken cancellationToken)
{
if (_database.HasDisposed) return null;
await using var cmd = _dataSource.CreateCommand(
$"select {NodeColumns} from {_nodeTable} where id = :id;select {Id}, {NodeId}, {Started} from {_assignmentTable} where node_id = :id;")
.With("id", nodeId);
WolverineNode returnValue = default!;
await using var reader = await cmd.ExecuteReaderAsync(cancellationToken);
if (await reader.ReadAsync(cancellationToken))
{
returnValue = await readNodeAsync(reader);
await reader.NextResultAsync(cancellationToken);
while (await reader.ReadAsync(cancellationToken))
{
var agentId = new Uri(await reader.GetFieldValueAsync<string>(0, cancellationToken));
returnValue.ActiveAgents.Add(agentId);
}
}
await reader.CloseAsync();
return returnValue;
}
public async Task AssignAgentsAsync(Guid nodeId, IReadOnlyList<Uri> agents, CancellationToken cancellationToken)
{
await using var conn = await _dataSource.OpenConnectionAsync(cancellationToken);
var builder = new CommandBuilder();
var nodeParameter = builder.AddNamedParameter("node", nodeId, NpgsqlDbType.Uuid);
foreach (var agent in agents)
{
var parameter = builder.AddParameter(agent.ToString());
builder.Append(
$"insert into {_assignmentTable} (id, node_id) values (:{parameter.ParameterName}, :{nodeParameter.ParameterName}) on conflict (id) do update set node_id = :{nodeParameter.ParameterName};");
}
var command = builder.Compile();
command.Connection = conn;
await command.ExecuteNonQueryAsync(cancellationToken);
await conn.CloseAsync();
}
public async Task RemoveAssignmentAsync(Guid nodeId, Uri agentUri, CancellationToken cancellationToken)
{
await _dataSource.CreateCommand($"delete from {_assignmentTable} where id = :id and node_id = :node")
.With("id", agentUri.ToString())
.With("node", nodeId)
.ExecuteNonQueryAsync(cancellationToken);
}
public async Task AddAssignmentAsync(Guid nodeId, Uri agentUri, CancellationToken cancellationToken)
{
await _dataSource.CreateCommand(
$"insert into {_assignmentTable} (id, node_id) values (:id, :node) on conflict (id) do update set node_id = :node;")
.With("id", agentUri.ToString())
.With("node", nodeId)
.ExecuteNonQueryAsync(cancellationToken);
}
public async Task<Guid?> MarkNodeAsLeaderAsync(Guid? originalLeader, Guid id)
{
await using var conn = await _dataSource.OpenConnectionAsync();
var lockResult = await conn.TryGetGlobalLock(LeaderLockId);
if (lockResult == AttainLockResult.Success)
{
var present = await currentLeaderAsync(conn);
if (present != originalLeader)
{
await conn.CloseAsync();
return present;
}
try
{
await conn.CreateCommand(
$"insert into {_assignmentTable} (id, node_id) values (:id, :node) on conflict (id) do update set node_id = :node;")
.With("id", NodeAgentController.LeaderUri.ToString())
.With("node", id)
.ExecuteNonQueryAsync();
}
catch (NpgsqlException e)
{
if (e.Message.Contains("violates foreign key constraint \"fkey_wolverine_node_assignments_node_id\""))
{
return null;
}
throw;
}
finally
{
await conn.ReleaseGlobalLock(LeaderLockId);
await conn.CloseAsync();
}
return id;
}
var leader = await currentLeaderAsync(conn);
await conn.CloseAsync();
return leader;
}
public async Task<Uri?> FindLeaderControlUriAsync(Guid selfId)
{
if (_database.HasDisposed) return null;
var raw = await _dataSource
.CreateCommand(
$"select uri from {_nodeTable} inner join {_assignmentTable} on {_nodeTable}.id = {_assignmentTable}.node_id where {_assignmentTable}.id = :id")
.With("id", NodeAgentController.LeaderUri.ToString())
.ExecuteScalarAsync();
return raw == null ? null : new Uri((string)raw);
}
public async Task<IReadOnlyList<Uri>> LoadAllOtherNodeControlUrisAsync(Guid selfId)
{
var list = await _dataSource.CreateCommand($"select uri from {_nodeTable} where id != :id").With("id", selfId)
.FetchListAsync<string>();
return list.Select(x => x!.ToUri()).ToList();
}
[Obsolete("Will be removed in Wolverine 3.0")]
public async Task<IReadOnlyList<WolverineNode>> LoadAllStaleNodesAsync(DateTimeOffset staleTime,
CancellationToken cancellationToken)
{
var cmd = _dataSource
.CreateCommand($"select id, uri from {_nodeTable} where health_check < :stale")
.With("stale", staleTime);
var nodes = await cmd.FetchListAsync<WolverineNode>(async reader =>
{
var id = await reader.GetFieldValueAsync<Guid>(0, cancellationToken);
var raw = await reader.GetFieldValueAsync<string>(1, cancellationToken);
return new WolverineNode
{
Id = id,
ControlUri = new Uri(raw)
};
}, cancellationToken);
return nodes;
}
public async Task OverwriteHealthCheckTimeAsync(Guid nodeId, DateTimeOffset lastHeartbeatTime)
{
await _dataSource.CreateCommand($"update {_nodeTable} set health_check = :now where id = :id")
.With("id", nodeId)
.With("now", lastHeartbeatTime)
.ExecuteNonQueryAsync();
}
public async Task MarkHealthCheckAsync(Guid nodeId)
{
await _dataSource.CreateCommand($"update {_nodeTable} set health_check = now() where id = :id")
.With("id", nodeId).ExecuteNonQueryAsync();
}
public async Task<IReadOnlyList<int>> LoadAllNodeAssignedIdsAsync()
{
return await _dataSource.CreateCommand($"select node_number from {_nodeTable}")
.FetchListAsync<int>();
}
private async Task<WolverineNode> readNodeAsync(DbDataReader reader)
{
var node = new WolverineNode
{
Id = await reader.GetFieldValueAsync<Guid>(0),
AssignedNodeId = await reader.GetFieldValueAsync<int>(1),
Description = await reader.GetFieldValueAsync<string>(2),
ControlUri = (await reader.GetFieldValueAsync<string>(3)).ToUri(),
Started = await reader.GetFieldValueAsync<DateTimeOffset>(4),
LastHealthCheck = await reader.GetFieldValueAsync<DateTimeOffset>(5),
};
var capabilities = await reader.GetFieldValueAsync<string[]>(6);
node.Capabilities.AddRange(capabilities.Select(x => new Uri(x)));
return node;
}
private async Task<Guid?> currentLeaderAsync(NpgsqlConnection conn)
{
var current = await _dataSource
.CreateCommand(
$"select node_id from {_assignmentTable} where id = '{NodeAgentController.LeaderUri}'")
.ExecuteScalarAsync();
if (current is Guid nodeId)
{
return nodeId;
}
return null;
}
public Task LogRecordsAsync(params NodeRecord[] records)
{
if (records.Length == 0) return Task.CompletedTask;
var op = new PersistNodeRecord(_settings, records);
return _database.EnqueueAsync(op);
}
public async Task<IReadOnlyList<NodeRecord>> FetchRecentRecordsAsync(int count)
{
if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count), "Must be a positive number");
Func<DbDataReader, Task<NodeRecord>> readRecord = async reader =>
{
return new NodeRecord
{
NodeNumber = await reader.GetFieldValueAsync<int>(0),
RecordType = Enum.Parse<NodeRecordType>(await reader.GetFieldValueAsync<string>(1)),
Timestamp = await reader.GetFieldValueAsync<DateTimeOffset>(2),
Description = await reader.GetFieldValueAsync<string>(3)
};
};
return await _dataSource.CreateCommand($"select node_number, event_name, timestamp, description from {_settings.SchemaName}.{DatabaseConstants.NodeRecordTableName} order by id desc LIMIT :limit")
.With("limit", count)
.FetchListAsync(readRecord);
}
}