Skip to content

Fix config being loaded twice #293

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 5 commits into from
Mar 27, 2021
Merged

Fix config being loaded twice #293

merged 5 commits into from
Mar 27, 2021

Conversation

ryandens
Copy link
Member

@ryandens ryandens commented Mar 22, 2021

Description

Resolves #223 (kind of). This bug no longer seems to be present, but as a result of the investigation into #223 a small thread-safety issue was noticed in the double-checked locking implementation. To fix it, I pulled the implementation of memoizing the AgentConfig into a separate class. This made it easier to inject a test double (specifically, a spy) to verify that the config is properly memoized.

Testing

Added test cases on the memoization and resetting of the memoized value.

Checklist:

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

Documentation

No new documentation required

This solution was based off of/inspired by

*/
private static final class CountingAgentConfigSupplier implements Supplier<Config.AgentConfig> {

private final AtomicInteger count = new AtomicInteger(0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This AtomicInteger is probably overkill, but I left it in on the off chance someone wants to expand on this test case later. I originally wrote this test to try to reproduce the double-checked locking race condition that can occur by using a multi-threaded scheduled executor service to ask for the AgentConfig 10,000 times via separate callables all calling get on the same AgentConfigSupplier instance and verifying that the inner CountingAgentConfigSupplier was only invoked once. I wasn't able to reproduce the bug in that fashion, so I pulled out the test case and simply tested the supplier "the normal way". Since CountingAgentConfigSupplier is initialized BeforeEach, there isn't necessarily a need to have this class be thread-safe and we could simply make this an int. Happy to back it out to avoid complexity or keep it in if we want to expand the test cases around this more.

@@ -51,7 +51,8 @@ private HypertraceConfig() {}
// so avoiding for perf reasons
private static volatile boolean servletCausingException;

private static AgentConfig agentConfig;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together. I like the testability, but on the other hand, I would like to keep it as simple as possible. Would it be enough to just use volatile on this property and not use the supplier approach?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no problem! I think in addition to making this field volatile, we would also want to synchronize the write to this field in HypertraceConfig.reset() unless you're not worried about this class being thread-safe when APIs exposed for testing purpose are in use.

@pavolloffay pavolloffay changed the title 🐛 Issue #223 config double checked locking Fix config being loaded twice Mar 23, 2021
@pavolloffay
Copy link
Member

thanks @ryandens

@pavolloffay pavolloffay merged commit 33bc28f into hypertrace:main Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hypertrace config is being loaded twice
2 participants