/
LocalSnapshotStore.cs
382 lines (342 loc) · 13.4 KB
/
LocalSnapshotStore.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//-----------------------------------------------------------------------
// <copyright file="LocalSnapshotStore.cs" company="Akka.NET Project">
// Copyright (C) 2009-2023 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Akka.Dispatch;
using Akka.Event;
using Akka.Configuration;
namespace Akka.Persistence.Snapshot
{
/// <summary>
/// Local file-based <see cref="SnapshotStore"/> implementation.
/// </summary>
/// <remarks>
/// This is the default `akka.peristence.snapshot-store` implementation, when no others are
/// explicitly set via HOCON configuration.
/// </remarks>
public class LocalSnapshotStore : SnapshotStore
{
private static readonly Regex FilenameRegex = new(@"^snapshot-(.+)-(\d+)-(\d+)", RegexOptions.Compiled);
private readonly int _maxLoadAttempts;
private readonly MessageDispatcher _streamDispatcher;
private readonly DirectoryInfo _dir;
private readonly ISet<SnapshotMetadata> _saving;
private static readonly Type WrapperType = typeof(Serialization.Snapshot);
private readonly Akka.Serialization.Serializer _wrapperSerializer;
private readonly Akka.Serialization.Serialization _serialization;
private readonly string _defaultSerializer;
/// <summary>
/// Creates a new <see cref="LocalSnapshotStore"/> instance.
/// </summary>
public LocalSnapshotStore()
{
var config = Context.System.Settings.Config.GetConfig("akka.persistence.snapshot-store.local");
/*
if (config.IsNullOrEmpty())
throw new ConfigurationException($"Cannot create {typeof(LocalSnapshotStore)}: akka.persistence.snapshot-store.local configuration node not found");
*/
_maxLoadAttempts = config.GetInt("max-load-attempts", 0);
_streamDispatcher = Context.System.Dispatchers.Lookup(config.GetString("stream-dispatcher", null));
_dir = new DirectoryInfo(config.GetString("dir", null));
_defaultSerializer = config.GetString("serializer", null);
_serialization = Context.System.Serialization;
_wrapperSerializer = _serialization.FindSerializerForType(WrapperType);
_saving = new SortedSet<SnapshotMetadata>(SnapshotMetadata.Comparer); // saving in progress
_log = Context.GetLogger();
}
private readonly ILoggingAdapter _log;
/// <inheritdoc/>
protected override Task<SelectedSnapshot> LoadAsync(string persistenceId, SnapshotSelectionCriteria criteria)
{
//
// Heuristics:
//
// Select youngest `maxLoadAttempts` snapshots that match upper bound.
// This may help in situations where saving of a snapshot could not be completed because of a VM crash.
// Hence, an attempt to load that snapshot will fail but loading an older snapshot may succeed.
//
var metadata = GetSnapshotMetadata(persistenceId, criteria).Reverse().Take(_maxLoadAttempts).Reverse().ToImmutableArray();
return RunWithStreamDispatcher(() => Load(metadata));
}
/// <inheritdoc/>
protected override Task SaveAsync(SnapshotMetadata metadata, object snapshot)
{
_saving.Add(metadata);
return RunWithStreamDispatcher(() =>
{
Save(metadata, snapshot);
return new object();
});
}
/// <inheritdoc/>
protected override Task DeleteAsync(SnapshotMetadata metadata)
{
_saving.Remove(metadata);
return RunWithStreamDispatcher(() =>
{
// multiple snapshot files here mean that there were multiple snapshots for this seqNr, we delete all of them
// usually snapshot-stores would keep one snapshot per sequenceNr however here in the file-based one we timestamp
// snapshots and allow multiple to be kept around (for the same seqNr) if desired
foreach (var file in GetSnapshotFiles(metadata))
{
file.Delete();
}
return new object();
});
}
/// <inheritdoc/>
protected override async Task DeleteAsync(string persistenceId, SnapshotSelectionCriteria criteria)
{
foreach (var metadata in GetSnapshotMetadata(persistenceId, criteria))
{
await DeleteAsync(metadata);
}
}
/// <inheritdoc/>
protected override bool ReceivePluginInternal(object message)
{
if (message is SaveSnapshotSuccess success)
{
_saving.Remove(success.Metadata);
}
else if (message is SaveSnapshotFailure)
{
// ignore
}
else if (message is DeleteSnapshotsSuccess)
{
// ignore
}
else if (message is DeleteSnapshotsFailure)
{
// ignore
}
else
{
return false;
}
return true;
}
private IEnumerable<FileInfo> GetSnapshotFiles(SnapshotMetadata metadata)
{
return GetSnapshotDir()
.GetFiles("*", SearchOption.TopDirectoryOnly)
.Where(f => SnapshotSequenceNrFilenameFilter(f, metadata));
}
private SelectedSnapshot Load(ImmutableArray<SnapshotMetadata> metadata)
{
var last = metadata.LastOrDefault();
if (last == null)
{
return null;
}
else
{
try
{
return WithInputStream(last, stream =>
{
var snapshot = Deserialize(stream);
return new SelectedSnapshot(last, snapshot.Data);
});
}
catch (Exception ex)
{
var remaining = metadata.RemoveAt(metadata.Length - 1);
_log.Error(ex, $"Error loading snapshot [{last}], remaining attempts: [{remaining.Length}]");
if (remaining.IsEmpty)
{
throw;
}
else
{
return Load(remaining);
}
}
}
}
/// <summary>
/// TBD
/// </summary>
/// <param name="metadata">TBD</param>
/// <param name="snapshot">TBD</param>
protected virtual void Save(SnapshotMetadata metadata, object snapshot)
{
var tempFile = WithOutputStream(metadata, stream =>
{
Serialize(stream, new Serialization.Snapshot(snapshot));
});
var newName = GetSnapshotFileForWrite(metadata);
if (File.Exists(newName.FullName))
{
File.Delete(newName.FullName);
}
tempFile.MoveTo(newName.FullName);
}
private Serialization.Snapshot Deserialize(Stream stream)
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
var snapshot = _wrapperSerializer.FromBinary<Serialization.Snapshot>(buffer);
return snapshot;
}
/// <summary>
/// TBD
/// </summary>
/// <param name="stream">TBD</param>
/// <param name="snapshot">TBD</param>
protected void Serialize(Stream stream, Serialization.Snapshot snapshot)
{
var bytes = _wrapperSerializer.ToBinary(snapshot);
stream.Write(bytes, 0, bytes.Length);
}
/// <summary>
/// TBD
/// </summary>
/// <param name="metadata">TBD</param>
/// <param name="p">TBD</param>
/// <returns>TBD</returns>
protected FileInfo WithOutputStream(SnapshotMetadata metadata, Action<Stream> p)
{
var tmpFile = GetSnapshotFileForWrite(metadata, ".tmp");
WithStream(new BufferedStream(new FileStream(tmpFile.FullName, FileMode.Create)), stream =>
{
p(stream);
stream.Flush();
return new object();
});
return tmpFile;
}
private T WithInputStream<T>(SnapshotMetadata metadata, Func<Stream, T> p)
{
return
WithStream(
new BufferedStream(new FileStream(GetSnapshotFileForWrite(metadata).FullName, FileMode.Open)), p);
}
private T WithStream<T>(Stream stream, Func<Stream, T> p)
{
try
{
var result = p(stream);
return result;
}
finally
{
stream.Dispose();
}
}
// only by PersistenceId and SequenceNr, timestamp is informational - accommodates for older files
protected FileInfo GetSnapshotFileForWrite(SnapshotMetadata metadata, string extension = "")
{
var filename = $"snapshot-{Uri.EscapeDataString(metadata.PersistenceId)}-{metadata.SequenceNr}-{metadata.Timestamp.Ticks}{extension}";
return new FileInfo(Path.Combine(GetSnapshotDir().FullName, filename));
}
private IEnumerable<SnapshotMetadata> GetSnapshotMetadata(string persistenceId, SnapshotSelectionCriteria criteria)
{
var snapshots = GetSnapshotDir()
.EnumerateFiles("snapshot-" + Uri.EscapeDataString(persistenceId) + "-*", SearchOption.TopDirectoryOnly)
.Select(ExtractSnapshotMetadata)
.Where(m=>m.PersistenceId == persistenceId)
.Where(metadata => metadata != null && criteria.IsMatch(metadata) && !_saving.Contains(metadata)).ToList();
snapshots.Sort(SnapshotMetadata.Comparer);
return snapshots;
}
private SnapshotMetadata ExtractSnapshotMetadata(FileInfo fileInfo)
{
var match = FilenameRegex.Match(fileInfo.Name);
if (match.Success)
{
var pid = Uri.UnescapeDataString(match.Groups[1].Value);
var seqNrString = match.Groups[2].Value;
var timestampTicks = match.Groups[3].Value;
if (long.TryParse(seqNrString, out var sequenceNr) && long.TryParse(timestampTicks, out var ticks))
{
return new SnapshotMetadata(pid, sequenceNr, new DateTime(ticks));
}
}
return null;
}
/// <summary>
/// TBD
/// </summary>
protected override void PreStart()
{
GetSnapshotDir();
base.PreStart();
}
private DirectoryInfo GetSnapshotDir()
{
if (!_dir.Exists || (_dir.Attributes & FileAttributes.Directory) == 0)
{
// try to create the directory, on failure double check if someone else beat us to it
Exception exception;
try
{
_dir.Create();
exception = null;
}
catch (Exception e)
{
exception = e;
}
finally
{
_dir.Refresh();
}
if (exception != null || ((_dir.Attributes & FileAttributes.Directory) == 0))
{
throw new IOException("Failed to create snapshot directory " + _dir.FullName, exception);
}
}
return _dir;
}
private static bool SnapshotSequenceNrFilenameFilter(FileInfo fileInfo, SnapshotMetadata metadata)
{
var match = FilenameRegex.Match(fileInfo.Name);
if (match.Success)
{
var pid = Uri.UnescapeDataString(match.Groups[1].Value);
var seqNrString = match.Groups[2].Value;
var timestampTicks = match.Groups[3].Value;
try
{
return pid.Equals(metadata.PersistenceId) &&
long.Parse(seqNrString) == metadata.SequenceNr &&
(metadata.Timestamp == SnapshotMetadata.TimestampNotSpecified ||
long.Parse(timestampTicks) == metadata.Timestamp.Ticks);
}
catch
{
return false;
}
}
return false;
}
private Task<T> RunWithStreamDispatcher<T>(Func<T> fn)
{
var promise = new TaskCompletionSource<T>();
_streamDispatcher.Schedule(() =>
{
try
{
var result = fn();
promise.SetResult(result);
}
catch (Exception e)
{
promise.SetException(e);
}
});
return promise.Task;
}
}
}