-
Notifications
You must be signed in to change notification settings - Fork 152
Minor refactor and added tests for DynamicConfigurationManager
#7652
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 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c5f0695
Refactor DynamicConfigurationManager to make it more testable
andrewlock cf0902f
Add unit tests for CombineApmTracingConfiguration to document expecta…
andrewlock d4d72c3
Reduce duration of lock
andrewlock a73cff2
Minor refactor to remove allocation
andrewlock ebdd094
nit: remove lock as we're not really threadsafe anyway and it ads a v…
andrewlock 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
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
244 changes: 244 additions & 0 deletions
244
tracer/test/Datadog.Trace.Tests/Configuration/DynamicConfigurationManagerTests.cs
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,244 @@ | ||
| // <copyright file="DynamicConfigurationManagerTests.cs" company="Datadog"> | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using Datadog.Trace.Configuration; | ||
| using Datadog.Trace.RemoteConfigurationManagement; | ||
| using FluentAssertions; | ||
| using Xunit; | ||
|
|
||
| namespace Datadog.Trace.Tests.Configuration; | ||
|
|
||
| public class DynamicConfigurationManagerTests | ||
| { | ||
| private const string ProductName = DynamicConfigurationManager.ProductName; | ||
| private static int _version; | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenNoConfiguration_ReturnsEmptyCollection() | ||
| { | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new(); | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new(); | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new(); | ||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEmpty(); | ||
| applyDetails.Should().BeEmpty(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenUnknownProducts_ReturnsEmptyCollection() | ||
| { | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new(); | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new() | ||
| { | ||
| { "Something else", [CreateConfig()] }, | ||
| { "Blah", [CreateConfig()] }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new(); | ||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEmpty(); | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenNoConfigsByProduct_ReturnsEmptyCollection() | ||
| { | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new(); | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new(); | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new() | ||
| { | ||
| { ProductName, [RemoteConfigurationPath.FromPath("datadog/1/a/b/c"), RemoteConfigurationPath.FromPath("employee/1/2/3")] }, | ||
| { "Blep", [RemoteConfigurationPath.FromPath("datadog/1/d/b/c"), RemoteConfigurationPath.FromPath("employee/4/3/2")] }, | ||
| }; | ||
|
|
||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEmpty(); | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenExistingConfigIsRemoved_RemovesConfigAndLeavesRemaining() | ||
| { | ||
| const string pathToRemove = "datadog/1/a/some-random-id/c"; | ||
| const string expectedPath = "employee/1/other-ID/3"; | ||
| var configToRemove = CreateConfig(pathToRemove); | ||
| var expected = CreateConfig(expectedPath); | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new() | ||
| { | ||
| { configToRemove.Path.Id, CreateConfig(pathToRemove) }, | ||
| { expected.Path.Id, expected }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new(); | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new() | ||
| { | ||
| { ProductName, [RemoteConfigurationPath.FromPath(pathToRemove), RemoteConfigurationPath.FromPath("datadog/2/dont/remove/me")] }, | ||
| }; | ||
|
|
||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEquivalentTo([expected]); | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenNewConfigIsProvided_ReplacesExistingConfig() | ||
| { | ||
| const string pathToRemove = "datadog/1/a/some-random-id/c"; | ||
| const string expectedPath = "employee/1/other-ID/3"; | ||
| var configToRemove = CreateConfig(pathToRemove); | ||
| var previous = CreateConfig(expectedPath); | ||
| var updated = CreateConfig(expectedPath); // different config for same id | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new() | ||
| { | ||
| { configToRemove.Path.Id, CreateConfig(pathToRemove) }, | ||
| { previous.Path.Id, previous }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new() | ||
| { | ||
| { ProductName, [updated] }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new() | ||
| { | ||
| { ProductName, [RemoteConfigurationPath.FromPath(pathToRemove), RemoteConfigurationPath.FromPath("datadog/2/dont/remove/me")] }, | ||
| }; | ||
|
|
||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEquivalentTo([updated]); | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenNewConfigIsProvided_ReplacesExistingConfigRegardlessOfRemovedConfig() | ||
| { | ||
| const string pathToRemove = "datadog/1/a/some-random-id/c"; | ||
| const string expectedPath = "employee/1/other-ID/3"; | ||
| var configToRemove = CreateConfig(pathToRemove); | ||
| var previous = CreateConfig(expectedPath); | ||
| var updated = CreateConfig(expectedPath); // different config for same id | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new() | ||
| { | ||
| { configToRemove.Path.Id, CreateConfig(pathToRemove) }, | ||
| { previous.Path.Id, previous }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new() | ||
| { | ||
| { ProductName, [updated] }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new() | ||
| { | ||
| // Note that we're _not_ explicitly removing the pathToRemove config, but as it's not in the "config by product", it gets removed | ||
| { ProductName, [RemoteConfigurationPath.FromPath("datadog/2/dont/remove/me")] }, | ||
| }; | ||
|
|
||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEquivalentTo([updated]); | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CombineApmTracingConfiguration_WhenNewConfigIsProvided_OverwritesNewAndExistingConfig() | ||
| { | ||
| const string pathToRemove = "datadog/1/a/some-random-id/c"; | ||
| const string expectedPath = "employee/1/other-ID/3"; | ||
| var configToRemove = CreateConfig(pathToRemove); | ||
| var previous = CreateConfig(expectedPath); | ||
| var updated1 = CreateConfig(expectedPath); // different configs for same id | ||
| var updated2 = CreateConfig(expectedPath); // different configs for same id | ||
| Dictionary<string, RemoteConfiguration> activeConfigs = new() | ||
| { | ||
| { configToRemove.Path.Id, CreateConfig(pathToRemove) }, | ||
| { previous.Path.Id, previous }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct = new() | ||
| { | ||
| { ProductName, [updated1, updated2] }, | ||
| }; | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct = new() | ||
| { | ||
| // Note that we're _not_ explicitly removing the pathToRemove config, but as it's not in the "config by product", it gets removed | ||
| { ProductName, [RemoteConfigurationPath.FromPath("datadog/2/dont/remove/me")] }, | ||
| }; | ||
|
|
||
| List<ApplyDetails> applyDetails = []; | ||
| var results = DynamicConfigurationManager.CombineApmTracingConfiguration( | ||
| new(activeConfigs), // create a copy to avoid mutating the original | ||
| configByProduct, | ||
| removedConfigByProduct, | ||
| applyDetails); | ||
|
|
||
| results.Should().BeEquivalentTo([updated2]); // updated1 is replaced | ||
| AssertApplyDetails(applyDetails, activeConfigs, configByProduct, removedConfigByProduct); | ||
| } | ||
|
|
||
| private static void AssertApplyDetails( | ||
| List<ApplyDetails> applyDetails, | ||
| Dictionary<string, RemoteConfiguration> originalConfig, | ||
| Dictionary<string, List<RemoteConfiguration>> configByProduct, | ||
| Dictionary<string, List<RemoteConfigurationPath>> removedConfigByProduct) | ||
| { | ||
| var removed = removedConfigByProduct | ||
| .Where(x => x.Key == ProductName) | ||
| .SelectMany(x => x.Value) | ||
| .Where(x => originalConfig.ContainsKey(x.Id)) // Only acknowledge the configs that we actually have | ||
| .Select(x => ApplyDetails.FromOk(x.Path)); | ||
| var added = configByProduct | ||
| .Where(x => x.Key == ProductName) | ||
| .SelectMany(x => x.Value) | ||
| .Select(x => ApplyDetails.FromOk(x.Path.Path)); | ||
| List<ApplyDetails> expected = [..removed, ..added]; | ||
|
|
||
| applyDetails.Should().BeEquivalentTo(expected); | ||
| } | ||
|
|
||
| private static RemoteConfiguration CreateConfig(string path = null) | ||
| { | ||
| var version = Interlocked.Increment(ref _version); | ||
| path ??= $"datadog/{version}/product-{version}/id-{version}/path"; | ||
| return new RemoteConfiguration( | ||
| RemoteConfigurationPath.FromPath(path), | ||
| contents: [], | ||
| length: 0, | ||
| hashes: new(), | ||
| version: Interlocked.Increment(ref _version)); // use version to create unique config values | ||
| } | ||
| } |
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.
The previous implementation held
_configLockacross both the mutation of_activeConfigurationsand the call toApplyMergedConfiguration. After the refactor the lock now wraps onlyCombineApmTracingConfiguration, whileApplyMergedConfiguration(valuesToApply)runs without synchronization. If two configuration updates arrive at about the same time, one thread can capturevaluesToApplyfor an older configuration, release the lock, another thread updates the dictionary and applies the new configuration, and then the first thread applies the stale snapshot. The tracer ends up running with an out-of-date configuration while_activeConfigurationscontains the newer state, and_configurationTelemetryis mutated concurrently as well. Please keep theApplyMergedConfigurationcall inside the lock (or provide equivalent ordering guarantees) so updates remain serialized.Useful? React with 👍 / 👎.
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.
Yeah, that's fair, it suggests to me that we should just drop the lock entirely 🤔 This is called in a single-threaded manner by the Remote config manager anyway 🤔
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.
It looks reasonable to me
Fine if we do, fine if we don't though
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.
I remove the lock entirely as it seemed more likely to add a veneer of thread-safety without technically being safe