-
-
Notifications
You must be signed in to change notification settings - Fork 278
/
ResourceReaper.cs
339 lines (287 loc) · 12 KB
/
ResourceReaper.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
336
337
338
339
namespace DotNet.Testcontainers.Containers
{
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;
/// <summary>
/// https://hub.docker.com/r/testcontainers/ryuk.
/// </summary>
public sealed class ResourceReaper : IAsyncDisposable
{
public const string ResourceReaperSessionLabel = TestcontainersClient.TestcontainersLabel + ".resource-reaper-session";
private const string RyukImage = "ghcr.io/psanetra/ryuk:2021.12.20";
private const int RyukPort = 8080;
private static readonly SemaphoreSlim DefaultLock = new SemaphoreSlim(1, 1);
private static ResourceReaper defaultInstance;
private readonly CancellationTokenSource maintainConnectionCts = new CancellationTokenSource();
private readonly TestcontainersContainer resourceReaperContainer;
private Task maintainConnectionTask = Task.CompletedTask;
private bool disposed;
private ResourceReaper(Guid sessionId, string ryukImage)
{
this.resourceReaperContainer = new TestcontainersBuilder<TestcontainersContainer>()
.WithName($"testcontainers-ryuk-{sessionId:D}")
.WithImage(ryukImage)
.WithAutoRemove(true)
.WithCleanUp(false)
.WithExposedPort(RyukPort)
.WithPortBinding(RyukPort, true)
.WithBindMount("/var/run/docker.sock", "/var/run/docker.sock", AccessMode.ReadOnly)
.Build();
this.SessionId = sessionId;
}
/// <summary>
/// Occurs when a ResourceReaper state has changed.
/// </summary>
/// <remarks>
/// It emits state changes to uninitialized instances too.
/// </remarks>
[PublicAPI]
public static event EventHandler<ResourceReaperStateEventArgs> StateChanged;
/// <summary>
/// Gets the default <see cref="ResourceReaper" /> session id.
/// </summary>
/// <remarks>
/// The default <see cref="ResourceReaper" /> will start either on <see cref="GetAndStartDefaultAsync(CancellationToken)" />
/// or if a <see cref="ITestcontainersContainer" /> is configured with <see cref="ITestcontainersBuilder{TDockerContainer}.WithCleanUp" />.
/// </remarks>
[PublicAPI]
public static Guid DefaultSessionId { get; }
= Guid.NewGuid();
/// <summary>
/// Gets the <see cref="ResourceReaper" /> session id.
/// </summary>
[PublicAPI]
public Guid SessionId { get; }
/// <summary>
/// Starts and returns the default <see cref="ResourceReaper" /> instance.
/// </summary>
/// <param name="ct">The cancellation token to cancel the <see cref="ResourceReaper" /> initialization.</param>
/// <returns>Task that completes when the <see cref="ResourceReaper" /> has been started.</returns>
[PublicAPI]
public static async Task<ResourceReaper> GetAndStartDefaultAsync(CancellationToken ct = default)
{
if (defaultInstance != null && !defaultInstance.disposed)
{
return defaultInstance;
}
await DefaultLock.WaitAsync(ct)
.ConfigureAwait(false);
if (defaultInstance != null && !defaultInstance.disposed)
{
return defaultInstance;
}
try
{
defaultInstance = await GetAndStartNewAsync(DefaultSessionId, RyukImage, default, ct)
.ConfigureAwait(false);
return defaultInstance;
}
finally
{
DefaultLock.Release();
}
}
/// <summary>
/// Starts and returns a new <see cref="ResourceReaper" /> instance.
/// </summary>
/// <param name="ryukImage">The Ryuk image.</param>
/// <param name="initTimeout">The timeout to initialize the Ryuk connection (Default: 10 seconds).</param>
/// <param name="ct">The cancellation token to cancel the <see cref="ResourceReaper" /> initialization.</param>
/// <returns>Task that completes when the <see cref="ResourceReaper" /> has been started.</returns>
[PublicAPI]
public static Task<ResourceReaper> GetAndStartNewAsync(string ryukImage = RyukImage, TimeSpan initTimeout = default, CancellationToken ct = default)
{
return GetAndStartNewAsync(Guid.NewGuid(), ryukImage, initTimeout, ct);
}
/// <summary>
/// Starts and returns a new <see cref="ResourceReaper" /> instance.
/// </summary>
/// <param name="sessionId">The session id.</param>
/// <param name="ryukImage">The Ryuk image.</param>
/// <param name="initTimeout">The timeout to initialize the Ryuk connection (Default: 10 seconds).</param>
/// <param name="ct">The cancellation token to cancel the <see cref="ResourceReaper" /> initialization.</param>
/// <returns>Task that completes when the <see cref="ResourceReaper" /> has been started.</returns>
[PublicAPI]
public static async Task<ResourceReaper> GetAndStartNewAsync(Guid sessionId, string ryukImage = RyukImage, TimeSpan initTimeout = default, CancellationToken ct = default)
{
var ryukInitializedTaskSource = new TaskCompletionSource<bool>();
var resourceReaper = new ResourceReaper(sessionId, ryukImage);
initTimeout = Equals(default(TimeSpan), initTimeout) ? TimeSpan.FromSeconds(10) : initTimeout;
try
{
StateChanged?.Invoke(null, new ResourceReaperStateEventArgs(resourceReaper, ResourceReaperState.Created));
await resourceReaper.resourceReaperContainer.StartAsync(ct)
.ConfigureAwait(false);
StateChanged?.Invoke(null, new ResourceReaperStateEventArgs(resourceReaper, ResourceReaperState.InitializingConnection));
using (var initTimeoutCts = new CancellationTokenSource(initTimeout))
{
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(initTimeoutCts.Token, ct))
{
resourceReaper.maintainConnectionTask = resourceReaper.MaintainRyukConnection(ryukInitializedTaskSource, linkedCts.Token);
await ryukInitializedTaskSource.Task
.ConfigureAwait(false);
}
}
StateChanged?.Invoke(null, new ResourceReaperStateEventArgs(resourceReaper, ResourceReaperState.MaintainingConnection));
}
catch (Exception)
{
await resourceReaper.DisposeAsync()
.ConfigureAwait(false);
throw;
}
return resourceReaper;
}
/// <inheritdoc />
[PublicAPI]
public async ValueTask DisposeAsync()
{
if (this.disposed)
{
return;
}
this.disposed = true;
this.maintainConnectionCts.Cancel();
try
{
// Close connection before disposing ResourceReaper.
await this.maintainConnectionTask
.ConfigureAwait(false);
}
catch (Exception)
{
// Ignore
}
this.maintainConnectionCts.Dispose();
if (this.resourceReaperContainer != null)
{
await this.resourceReaperContainer.DisposeAsync()
.ConfigureAwait(false);
}
}
/// <summary>
/// Establishes and maintains the connection to the running Ryuk container.
///
/// This is split into two phases:
///
/// 1) Initialization
/// Tries to establish a connection to Ryuk. After establishment, sends a Docker resource filter to Ryuk. Ryuk will acknowledge the reception of the filter.
/// On termination, Ryuk will delete all Docker resources matching the filter. You can cancel the initialization with <see cref="ct" />.
///
/// 2) Maintenance
/// After initialization, we need to keep the connection to Ryuk open. If we lose the connection for any reason, Ryuk allows reconnecting within 10 seconds.
/// It's not necessary to send the filter again after reconnecting.
/// </summary>
/// <param name="ryukInitializedTaskSource">The task that completes after the initialization.</param>
/// <param name="ct">The cancellation token to cancel the <see cref="ResourceReaper" /> initialization. This will not cancel the maintained connection.</param>
private async Task MaintainRyukConnection(TaskCompletionSource<bool> ryukInitializedTaskSource, CancellationToken ct)
{
var endpoint = new IPEndPoint(IPAddress.Loopback, this.resourceReaperContainer.GetMappedPublicPort(RyukPort));
while (!this.maintainConnectionCts.IsCancellationRequested && (!ct.IsCancellationRequested || ryukInitializedTaskSource.Task.IsCompleted))
{
using (var tcpClient = new TcpClient())
{
try
{
tcpClient.Connect(endpoint);
var stream = tcpClient.GetStream();
var filter = $"label={ResourceReaperSessionLabel}={this.SessionId:D}\n";
var sendBytes = Encoding.ASCII.GetBytes(filter);
var readBytes = new byte[tcpClient.ReceiveBufferSize];
if (!ryukInitializedTaskSource.Task.IsCompleted)
{
using (var messageBuffer = new MemoryStream())
{
#if NETSTANDARD2_1_OR_GREATER
await stream.WriteAsync(new ReadOnlyMemory<byte>(sendBytes), ct)
.ConfigureAwait(false);
#else
await stream.WriteAsync(sendBytes, 0, sendBytes.Length, ct)
.ConfigureAwait(false);
#endif
await stream.FlushAsync(ct)
.ConfigureAwait(false);
bool hasAcknowledge;
do
{
#if NETSTANDARD2_1_OR_GREATER
var numberOfBytes = await stream.ReadAsync(new Memory<byte>(readBytes), ct)
.ConfigureAwait(false);
#else
var numberOfBytes = await stream.ReadAsync(readBytes, 0, readBytes.Length, ct)
.ConfigureAwait(false);
#endif
var indexOfNewLine = Array.IndexOf(readBytes, (byte)'\n');
if (indexOfNewLine == -1)
{
// We have not received the entire message yet. Read from stream again.
messageBuffer.Write(readBytes, 0, numberOfBytes);
hasAcknowledge = false;
}
else
{
messageBuffer.Write(readBytes, 0, indexOfNewLine);
hasAcknowledge = "ack".Equals(Encoding.ASCII.GetString(messageBuffer.ToArray()), StringComparison.OrdinalIgnoreCase);
messageBuffer.SetLength(0);
}
}
while (!hasAcknowledge);
ryukInitializedTaskSource.SetResult(true);
}
}
while (!this.maintainConnectionCts.IsCancellationRequested)
{
// Keep the connection to Ryuk up.
#if NETSTANDARD2_1_OR_GREATER
_ = await stream.ReadAsync(new Memory<byte>(readBytes), this.maintainConnectionCts.Token)
.ConfigureAwait(false);
#else
_ = await stream.ReadAsync(readBytes, 0, readBytes.Length, this.maintainConnectionCts.Token)
.ConfigureAwait(false);
#endif
}
}
catch (OperationCanceledException)
{
// Ignore cancellation.
}
catch (SocketException e)
{
this.resourceReaperContainer.Logger.CanNotConnectToResourceReaper(endpoint, e);
}
catch (Exception e)
{
this.resourceReaperContainer.Logger.LostConnectionToResourceReaper(endpoint, e);
}
finally
{
await Task.Delay(TimeSpan.FromSeconds(1), default)
.ConfigureAwait(false);
}
}
}
if (ryukInitializedTaskSource.Task.IsCompleted)
{
StateChanged?.Invoke(null, new ResourceReaperStateEventArgs(this, ResourceReaperState.ConnectionTerminated));
return;
}
if (ct.IsCancellationRequested)
{
ryukInitializedTaskSource.SetException(new ResourceReaperException("Initialization has been cancelled."));
}
else
{
ryukInitializedTaskSource.SetException(new ResourceReaperException("Initialization failed."));
}
}
}
}