Skip to content

Commit 0f8c8db

Browse files
authored
fix: filter experiments thread-safely (#530)
1 parent 555523e commit 0f8c8db

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/Microsoft.ComponentDetection.Orchestrator/Experiments/ExperimentService.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Microsoft.ComponentDetection.Orchestrator.Experiments;
22

33
using System;
4+
using System.Collections.Concurrent;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading.Tasks;
@@ -12,7 +13,7 @@
1213
/// <inheritdoc />
1314
public class ExperimentService : IExperimentService
1415
{
15-
private readonly List<(IExperimentConfiguration Config, ExperimentResults ExperimentResults)> experiments;
16+
private readonly ConcurrentDictionary<IExperimentConfiguration, ExperimentResults> experiments;
1617
private readonly IEnumerable<IExperimentProcessor> experimentProcessors;
1718
private readonly ILogger<ExperimentService> logger;
1819

@@ -27,7 +28,8 @@ public ExperimentService(
2728
IEnumerable<IExperimentProcessor> experimentProcessors,
2829
ILogger<ExperimentService> logger)
2930
{
30-
this.experiments = configs.Select(x => (x, new ExperimentResults())).ToList();
31+
this.experiments = new ConcurrentDictionary<IExperimentConfiguration, ExperimentResults>(
32+
configs.ToDictionary(config => config, _ => new ExperimentResults()));
3133
this.experimentProcessors = experimentProcessors;
3234
this.logger = logger;
3335
}
@@ -61,18 +63,18 @@ public void RecordDetectorRun(IComponentDetector detector, IEnumerable<DetectedC
6163
}
6264
}
6365

64-
private void FilterExperiments(IComponentDetector detector, int count) =>
65-
this.experiments.RemoveAll(experiment =>
66-
{
67-
var shouldRemove = !experiment.Config.ShouldRecord(detector, count);
68-
69-
if (shouldRemove)
70-
{
71-
this.logger.LogDebug("Removing {Experiment} from active experiments", experiment.Config.Name);
72-
}
66+
private void FilterExperiments(IComponentDetector detector, int count)
67+
{
68+
var experimentsToRemove = this.experiments
69+
.Where(x => !x.Key.ShouldRecord(detector, count))
70+
.Select(x => x.Key)
71+
.ToList();
7372

74-
return shouldRemove;
75-
});
73+
foreach (var config in experimentsToRemove.Where(config => this.experiments.TryRemove(config, out _)))
74+
{
75+
this.logger.LogDebug("Removing {Experiment} from active experiments", config.Name);
76+
}
77+
}
7678

7779
/// <inheritdoc />
7880
public async Task FinishAsync()

0 commit comments

Comments
 (0)