Skip to content

Conversation

@emaccaull
Copy link
Contributor

@emaccaull emaccaull commented May 12, 2025

Initializing multiple pools in parallel incurred significant lock contention on poolMap because this lock was held while any Pool was being constructed (which could take several seconds).

Because of this, connecting to independent databases in parallel was not possible (Pool creation was serialized), severely limiting overall performance.

Changes

  • Add PoolHolder to defer creation of Pool and act as a lock for each pool key.
  • Insert a new PoolHolder while holding the poolMap lock, reducing global thread contention.
  • Threads looking up the same key will acquire the same PoolHolder and that PoolHolder will ensure that only one Pool is created. Threads looking up unrelated PoolHolder will not be blocked by Pool creation.
  • Make registerJxm/unRegisterJmx thread safe. When Pool creation happened in parallel, there were errors in the logs because Pool was attempted to be registered more than one.

Testing

This change should have no impact on functionality. Performance should be improved when creating many Pools in parallel. Performance should remain the same when creating Pool instances sequentially.

Initializing multiple pools in parallel incurred significant lock
contention on `poolMap` because this lock was held while any `Pool` was
being constructed (which could take several seconds).

Because of this, connecting to independent databases in parallel was not
possible (Pool creation was serialized), severely limiting overall
performance.

*Changes*

* Add `PoolHolder` to defer creation of `Pool` and act as a lock for
  each pool key.
* Insert a new `PoolHolder` while holding the `poolMap` lock, reducing
  global thread contention.
* Threads looking up the same key will acquire the same `PoolHolder` and
  that `PoolHolder` will ensure that only one `Pool` is created. Threads
  looking up unrelated `PoolHolder` will not be blocked by `Pool`
  creation.
* Make registerJxm/unRegisterJmx thread safe. When Pool creation
  happened in parallel, there were errors in the logs because `Pool` was
  attempted to be registered more than one.

*Testing*

This change should have no impact on functionality. Performance should
be improved when creating many Pools in parallel. Performance should
remain the same when creating `Pool` instances sequentially.

* Unit test added to validate that many pools can be created in parallel
  without issue.
* Tested against ~20 real DB servers and saw performance improvement.
  All connections were created at the same time on separate threads.
@rusher rusher merged commit 9df73f7 into mariadb-corporation:develop May 23, 2025
2 checks passed
@rusher
Copy link
Collaborator

rusher commented May 23, 2025

nice addition. thanks

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.

2 participants