Skip to content

remoteconfig: fine grained locking#2458

Merged
Hellzy merged 9 commits into
mainfrom
francois.mazeau/rc-lock-fix
Jan 8, 2024
Merged

remoteconfig: fine grained locking#2458
Hellzy merged 9 commits into
mainfrom
francois.mazeau/rc-lock-fix

Conversation

@Hellzy

@Hellzy Hellzy commented Dec 20, 2023

Copy link
Copy Markdown
Contributor

What does this PR do?

JIRA: APPSEC-18154

This adds a finer brand of locking to the remoteconfig client in order to allow using the exported API asynchronously.

  • Add RW mutex for products

  • Add RW mutex for capabilities

  • Add RW mutex for callbacks

  • Add RW mutex for productsWithCallback

  • Add async testing of public API

Motivation

The current implementation relies on a global lock in the entry point of the config update routine which means that
the client can't be used when it's receiving an update. This is a problem since callbacks are called during the update
routine, and those callbacks in turn may very well need to register new products/capabilities/callbacks. This leads to a possibility of deadlock.
Adding finer locking (1 mutex/data structure, basically) allows to modify the client's state during a configurations update and in turns makes it possible for callbacks to register capabilities, products, other callbacks, etc...

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.

For Datadog employees:

  • If this PR touches code that handles credentials of any kind, such as Datadog API keys, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Unsure? Have a question? Request a review!

@Hellzy
Hellzy force-pushed the francois.mazeau/rc-lock-fix branch from e909608 to 53e5d7e Compare January 8, 2024 10:01
@Hellzy
Hellzy marked this pull request as ready for review January 8, 2024 10:01
@Hellzy
Hellzy requested a review from a team as a code owner January 8, 2024 10:01
Comment thread internal/remoteconfig/remoteconfig_test.go Outdated
@felixge

felixge commented Jan 8, 2024

Copy link
Copy Markdown
Member

Could you please add a description / motivation for this PR? Thanks!

@pr-commenter

pr-commenter Bot commented Jan 8, 2024

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2024-01-08 13:18:01

Comparing candidate commit 625415c in PR branch francois.mazeau/rc-lock-fix with baseline commit f76870b in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 39 metrics, 2 unstable metrics.

Comment thread internal/remoteconfig/remoteconfig.go Outdated
Comment on lines +94 to +99
_callbacksMu sync.RWMutex
products map[string]struct{}
_productsMu sync.RWMutex
productsWithCallbacks map[string]ProductCallback
capabilities map[Capability]struct{}
_capabilitiesMu sync.RWMutex

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: is there any reason for prefixing these fields with underscore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a personal habit, I can remove the prefixes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's fine. Let me review it again.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm, I think this sets a bad precedence. Using underscore prefixes for go variable names, especially on structs, is highly unorthodox. It'd be nice if we could not do this going forward.

@Hellzy
Hellzy marked this pull request as draft January 8, 2024 10:24
@Hellzy
Hellzy marked this pull request as ready for review January 8, 2024 10:45
Comment thread internal/remoteconfig/remoteconfig.go
Comment thread internal/remoteconfig/remoteconfig_test.go Outdated
@Hellzy
Hellzy requested a review from RomainMuller January 8, 2024 13:02
@Hellzy
Hellzy merged commit d28a899 into main Jan 8, 2024
@Hellzy
Hellzy deleted the francois.mazeau/rc-lock-fix branch January 8, 2024 13:56
Hellzy added a commit that referenced this pull request Jan 8, 2024
…figuration update (#2458)

A deadlock occurred when trying to modify the remote configuration client using its public API while a configuration update was happening. This was due to the global lock performed in the update goroutine.
This change introduces several new RWLocks for various data structures in order to avoid the global lock, and allow updating data in the client while the client is updating its configurations.
Hellzy added a commit that referenced this pull request Jan 8, 2024
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.

4 participants