Skip to content

Commit

Permalink
Merge pull request #47 from EventStore/timothycoleman/snapshots
Browse files Browse the repository at this point in the history
[DBX-50] Always store Partials in Snapshots
  • Loading branch information
hayley-jean authored Jul 1, 2024
2 parents c60cc4a + 893dd25 commit 3e071e0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ public PluginDiagnosticsDataCollector(string[] sources, int capacity = 10, OnEve

CollectedEventsByPlugin.AddOrUpdate(
source,
static (_, state) => [state.PluginData],
static (_, state) =>
state.PluginData.CollectionMode == PluginDiagnosticsDataCollectionMode.Partial
? [state.PluginData with { CollectionMode = PluginDiagnosticsDataCollectionMode.Snapshot }]
: [state.PluginData],
static (_, collected, state) => {
// NB: snapshots and partials have EventName "PluginDiagnosticsData" and events do not have that name
// so filtering on the EventName also filters the collection mode.
// Partials never end up in the collection.
switch (state.PluginData.CollectionMode) {
case PluginDiagnosticsDataCollectionMode.Event:
collected.Add(state.PluginData);
Expand All @@ -29,14 +35,14 @@ public PluginDiagnosticsDataCollector(string[] sources, int capacity = 10, OnEve
collected.Add(state.PluginData);
break;
case PluginDiagnosticsDataCollectionMode.Partial:
var events = collected.Where(x => x.EventName == state.PluginData.EventName).ToArray();
var snapshots = collected.Where(x => x.EventName == state.PluginData.EventName).ToArray();

// if no event exists, create new
if (events.Length == 0)
collected.Add(state.PluginData);
// if no snapshot exists, create new
if (snapshots.Length == 0)
collected.Add(state.PluginData with { CollectionMode = PluginDiagnosticsDataCollectionMode.Snapshot});
else {
// update all collected events
foreach (var evt in events) {
// update the snapshot (there should be one)
foreach (var evt in snapshots) {
foreach (var (key, value) in state.PluginData.Data)
evt.Data[key] = value;
}
Expand All @@ -45,6 +51,7 @@ public PluginDiagnosticsDataCollector(string[] sources, int capacity = 10, OnEve
break;
}

// TODO: presumably we don't want to remove the Snapshot
if (collected.Count > state.Capacity)
collected.Remove(collected.Min);

Expand Down
1 change: 1 addition & 0 deletions src/EventStore.Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ protected Plugin(

Name = name ?? pluginType.Name
.Replace("Plugin", "", OrdinalIgnoreCase)
.Replace("Provider", "", OrdinalIgnoreCase)
.Replace("Component", "", OrdinalIgnoreCase)
.Replace("Subsystems", "", OrdinalIgnoreCase)
.Replace("Subsystem", "", OrdinalIgnoreCase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public void can_collect_diagnostics_data_from_plugin() {

plugin.PublishDiagnosticsData(new() { ["enabled"] = plugin.Enabled });

sut.CollectedEvents(plugin.DiagnosticsName).Should().ContainSingle().Which
.Data["enabled"].Should().Be(plugin.Enabled);
var diagnosticsData = sut.CollectedEvents(plugin.DiagnosticsName).Should().ContainSingle().Which;
diagnosticsData.Data["enabled"].Should().Be(plugin.Enabled);
diagnosticsData.CollectionMode.Should().Be(PluginDiagnosticsDataCollectionMode.Snapshot);
}

[Fact]
Expand Down

0 comments on commit 3e071e0

Please sign in to comment.