-
Notifications
You must be signed in to change notification settings - Fork 16
feat(audience): add DiskStore and EventQueue for persistent event batching (SDK-127) #690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b9ebac5
feat(audience): add DiskStore and EventQueue for persistent event bat…
nattb8 4abcade
fix(audience): volatile _disposed + tighter shutdown ordering
ImmutableJeffrey 14b57a1
refactor(audience): rename FlushAsync to FlushSync in EventQueue
ImmutableJeffrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| namespace Immutable.Audience | ||
| { | ||
| /// <summary> | ||
| /// File-per-event persistent store. Each event is written as an atomic | ||
| /// <c>{ticks}_{uuid}.json</c> file inside <c>imtbl_audience/queue/</c>. | ||
| /// </summary> | ||
| internal sealed class DiskStore | ||
| { | ||
| private readonly string _queueDir; | ||
|
|
||
| internal DiskStore(string persistentDataPath) | ||
| { | ||
| _queueDir = Path.Combine(persistentDataPath, "imtbl_audience", "queue"); | ||
| Directory.CreateDirectory(_queueDir); | ||
| } | ||
|
|
||
| /// <summary>Atomically writes <paramref name="json"/> as a new event file.</summary> | ||
| internal void Write(string json) | ||
| { | ||
| var fileName = $"{DateTime.UtcNow.Ticks}_{Guid.NewGuid():N}.json"; | ||
| var finalPath = Path.Combine(_queueDir, fileName); | ||
| var tmpPath = finalPath + ".tmp"; | ||
|
|
||
| File.WriteAllText(tmpPath, json); | ||
|
|
||
| try | ||
| { | ||
| File.Move(tmpPath, finalPath); | ||
| } | ||
| catch (IOException) | ||
| { | ||
| // Destination already exists (unlikely but safe to handle) | ||
| File.Delete(finalPath); | ||
| File.Move(tmpPath, finalPath); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns up to <paramref name="maxSize"/> file paths, oldest first. | ||
| /// Files older than <see cref="Constants.StaleEventDays"/> days are deleted and excluded. | ||
| /// </summary> | ||
| internal IReadOnlyList<string> ReadBatch(int maxSize) | ||
| { | ||
| if (maxSize <= 0) | ||
| return Array.Empty<string>(); | ||
|
|
||
| maxSize = Math.Min(maxSize, Constants.MaxBatchSize); | ||
|
|
||
| var cutoff = DateTime.UtcNow.AddDays(-Constants.StaleEventDays); | ||
|
|
||
| var result = new List<string>(); | ||
|
|
||
| // Sort by filename (ticks prefix) → oldest first | ||
| var files = Directory.GetFiles(_queueDir, "*.json") | ||
| .OrderBy(f => Path.GetFileName(f), StringComparer.Ordinal); | ||
|
|
||
| foreach (var path in files) | ||
| { | ||
| if (result.Count >= maxSize) | ||
| break; | ||
|
|
||
| // Stale check: parse ticks from filename prefix | ||
| var name = Path.GetFileNameWithoutExtension(path); | ||
| var underscoreIdx = name.IndexOf('_'); | ||
| if (underscoreIdx > 0 && long.TryParse(name.Substring(0, underscoreIdx), out var ticks)) | ||
| { | ||
| var fileTime = new DateTime(ticks, DateTimeKind.Utc); | ||
| if (fileTime < cutoff) | ||
| { | ||
| TryDelete(path); | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| result.Add(path); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| /// <summary>Deletes the event files at <paramref name="paths"/>.</summary> | ||
| internal void Delete(IEnumerable<string> paths) | ||
| { | ||
| foreach (var path in paths) | ||
| TryDelete(path); | ||
| } | ||
|
|
||
| /// <summary>Returns the total number of event files currently on disk.</summary> | ||
| internal int Count() => Directory.GetFiles(_queueDir, "*.json").Length; | ||
|
|
||
| private static void TryDelete(string path) | ||
| { | ||
| try { File.Delete(path); } | ||
| catch (IOException) { } | ||
| catch (UnauthorizedAccessException) { } | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Threading; | ||
|
|
||
| namespace Immutable.Audience | ||
| { | ||
| /// <summary> | ||
| /// Thread-safe, disk-persistent batch event queue for the Audience SDK. | ||
| /// | ||
| /// <para>Enqueue is lock-free and safe to call from any thread. A background | ||
| /// drain thread moves events from the in-memory <see cref="ConcurrentQueue{T}"/> | ||
| /// to <see cref="DiskStore"/>, flushing either on a time interval or when the | ||
| /// in-memory batch reaches <see cref="AudienceConfig.FlushSize"/>.</para> | ||
| /// | ||
| /// <para>Call <see cref="Shutdown"/> before process exit to flush remaining events | ||
| /// and stop the drain thread cleanly.</para> | ||
| /// </summary> | ||
| internal sealed class EventQueue : IDisposable | ||
| { | ||
| private readonly DiskStore _store; | ||
| private readonly int _flushIntervalMs; | ||
| private readonly int _flushSize; | ||
|
|
||
| private readonly ConcurrentQueue<string> _memory = new ConcurrentQueue<string>(); | ||
| private readonly CancellationTokenSource _cts = new CancellationTokenSource(); | ||
| private readonly Thread _drainThread; | ||
| private readonly ManualResetEventSlim _flushGate = new ManualResetEventSlim(false); | ||
|
|
||
| // Volatile so all threads see the shutdown signal immediately. | ||
| private volatile bool _disposed; | ||
|
|
||
| /// <param name="store">Pre-created <see cref="DiskStore"/> for this queue.</param> | ||
| /// <param name="flushIntervalSeconds">How often to drain to disk regardless of batch size.</param> | ||
| /// <param name="flushSize">Drain to disk immediately when this many events are queued.</param> | ||
| internal EventQueue(DiskStore store, int flushIntervalSeconds, int flushSize) | ||
| { | ||
| _store = store ?? throw new ArgumentNullException(nameof(store)); | ||
| _flushIntervalMs = Math.Max(1, flushIntervalSeconds) * 1000; | ||
| _flushSize = Math.Max(1, flushSize); | ||
|
|
||
| _drainThread = new Thread(DrainLoop) | ||
| { | ||
| IsBackground = true, | ||
| Name = "imtbl-audience-drain" | ||
| }; | ||
| _drainThread.Start(); | ||
| } | ||
|
|
||
| /// <summary>Enqueues a JSON-serialised event. Lock-free; safe from any thread.</summary> | ||
| internal void Enqueue(string json) | ||
| { | ||
| if (_disposed) return; | ||
|
|
||
| _memory.Enqueue(json); | ||
|
|
||
| // Signal the drain thread early if we've hit the flush-size threshold | ||
| if (_memory.Count >= _flushSize) | ||
| _flushGate.Set(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Drains the in-memory queue and persists all events to disk immediately. | ||
| /// Blocks until the drain is complete. | ||
| /// </summary> | ||
| internal void FlushSync() | ||
| { | ||
| DrainMemoryToDisk(); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| /// <summary> | ||
| /// Flushes all pending events to disk and stops the drain thread. | ||
| /// Safe to call multiple times. | ||
| /// </summary> | ||
| internal void Shutdown() | ||
| { | ||
| if (_disposed) return; | ||
|
|
||
| // Stop accepting new events first — closes the race window where | ||
| // events enqueued between Cancel and final drain would be lost. | ||
| _disposed = true; | ||
|
|
||
| // Signal the drain thread to exit, then wait for it. | ||
| _cts.Cancel(); | ||
| _flushGate.Set(); | ||
| _drainThread.Join(TimeSpan.FromSeconds(5)); | ||
|
|
||
| // Final drain: anything enqueued before _disposed was set. | ||
| DrainMemoryToDisk(); | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // Background drain loop | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| private void DrainLoop() | ||
| { | ||
| while (!_cts.IsCancellationRequested) | ||
| { | ||
| // Wait for flush gate or interval timeout | ||
| _flushGate.Wait(_flushIntervalMs); | ||
| _flushGate.Reset(); | ||
|
|
||
| if (_cts.IsCancellationRequested) | ||
| break; | ||
|
|
||
| DrainMemoryToDisk(); | ||
| } | ||
| } | ||
|
|
||
| private void DrainMemoryToDisk() | ||
| { | ||
| while (_memory.TryDequeue(out var json)) | ||
| { | ||
| try | ||
| { | ||
| _store.Write(json); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| // Best-effort: if we can't write, discard rather than block the drain | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------- | ||
| // IDisposable | ||
| // ----------------------------------------------------------------- | ||
|
|
||
| public void Dispose() | ||
| { | ||
| Shutdown(); | ||
| _cts.Dispose(); | ||
| _flushGate.Dispose(); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
src/Packages/Audience/Runtime/Transport/EventQueue.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer overflow in flush interval kills drain thread
Medium Severity
Math.Max(1, flushIntervalSeconds) * 1000performs uncheckedintmultiplication that silently overflows forflushIntervalSecondsvalues ≥ 2,147,484 (~24.8 days), producing a negative_flushIntervalMs. When the drain thread calls_flushGate.Wait(_flushIntervalMs)with that negative value, it throwsArgumentOutOfRangeException(only -1 is valid for negative). SinceDrainLoophas no try-catch, the background thread dies silently and events are never persisted to disk. A user settingInt32.MaxValueto disable interval flushing would trigger this immediately.Additional Locations (1)
src/Packages/Audience/Runtime/Transport/EventQueue.cs#L99-L100Reviewed by Cursor Bugbot for commit 14b57a1. Configure here.