Skip to content
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

Fixes #33974 - Change the pool size to threads + 4 #1161

Merged
merged 1 commit into from
May 16, 2024

Conversation

ShimShtein
Copy link
Member

According to an investigation described in https://community.theforeman.org/t/rails-connection-pool-size-optimizations/36675 foreman process spawns 4 additional threads that consume DB connection during the startup. Hence the amount of acctive DB connections should be the amount of puma threads + 4 additional threads.

@ShimShtein ShimShtein force-pushed the update_db_pool_size branch from 7bda3a9 to 0780c61 Compare April 24, 2024 11:33
@ShimShtein ShimShtein changed the title Fixes #1976728 - Change the pool size to threads + 4 Fixes #33974 - Change the pool size to threads + 4 Apr 24, 2024
@ShimShtein
Copy link
Member Author

@pablomh Thanks for all the testing and experimenting.
What do you think, is it enough?

@pablomh
Copy link

pablomh commented Apr 24, 2024

Hi @ShimShtein, according to our data, this change should be very welcome :)

These are the results of our testing that justify this change:

  • Chart showing the number of registration errors with different DB pool values when performing concurrent registrations on a "medium" sized Satellite server

image

  • In it we can see where only modifying the DB pool value improves the registration success rate, getting no errors until reaching >200 concurrent registration requests with values of DB pool >= 8.

  • Chart showing the number of DB pool exhaustion errors (could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use) with different DB pool values when performing concurrent registrations on a "medium" sized Satellite server

image

  • In it we can see how all the DB pool exhaustion errors dissapear with values of DB pool >= 8.

  • Chart showing the number of DB pool connection retries appearing in the logs when performing concurrent registrations on a "medium" sized Satellite server

image

  • In it we can see how all the DB pool connection retries dissapear with values of DB pool >= 9.

Based on this testing, we agree that the minimum Foreman DB pool value where all these issues are addressed is 9.

Copy link
Member

@ehelms ehelms left a comment

Choose a reason for hiding this comment

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

@evgeni / @ekohl any concerns with this approach to changing the pool size?

@ehelms
Copy link
Member

ehelms commented May 3, 2024

@ShimShtein This will reset the default value, to really fix this we need to also reset this in the installer through a migration.

@ShimShtein
Copy link
Member Author

@ehelms Added a migration PR: theforeman/foreman-installer#937

@@ -226,7 +226,7 @@
String[1] $db_password = $foreman::params::db_password,
Optional[String[1]] $db_sslmode = undef,
Optional[String[1]] $db_root_cert = undef,
Integer[0] $db_pool = 5,
Integer[0] $db_pool = 9,
Copy link
Member

Choose a reason for hiding this comment

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

Should we make this:

Suggested change
Integer[0] $db_pool = 9,
Optional[Integer[0]] $db_pool = undef,

That way, if we change it in the future it automatically gets recalculated. We would need to change the part in config.pp to deal with it by using pick() instead of max().

The DB migration could then simply be .delete('db_pool') if db_pool == 5.

@ShimShtein ShimShtein force-pushed the update_db_pool_size branch from 0780c61 to 6d1f3b7 Compare May 9, 2024 08:42
@ShimShtein
Copy link
Member Author

@ekohl fixed both here and the installer

@@ -68,7 +68,9 @@
'database' => $foreman::db_database,
'username' => $foreman::db_username,
'password' => $foreman::db_password,
'db_pool' => max($foreman::db_pool, $foreman::foreman_service_puma_threads_max),
# Set the pool size to at least the amount of puma threads + 4 threads that are spawned automatically by the process.
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to document that the 4 is just needed for Katello.

@@ -226,7 +226,7 @@
String[1] $db_password = $foreman::params::db_password,
Optional[String[1]] $db_sslmode = undef,
Optional[String[1]] $db_root_cert = undef,
Integer[0] $db_pool = 5,
Optional[Integer[0]] $db_pool = undef,
Copy link
Member

Choose a reason for hiding this comment

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

On line 87 there's documentation for this parameter that's shown in the installer when using --full-help. Perhaps you can update it to describe the behavior.

'db_pool' => max($foreman::db_pool, $foreman::foreman_service_puma_threads_max),
# Set the pool size to at least the amount of puma threads + 4 threads that are spawned automatically by the process.
# db_pool is optional, and undef means "use default" and the second part of the max statement will be set
'db_pool' => max(pick($foreman::db_pool, 0), $foreman::foreman_service_puma_threads_max + 4),
Copy link
Member

Choose a reason for hiding this comment

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

I'd keep this simple so you still provide the user with full control. For example if the + 4 part does give problems you still provide a way to override it.

Suggested change
'db_pool' => max(pick($foreman::db_pool, 0), $foreman::foreman_service_puma_threads_max + 4),
'db_pool' => pick($foreman::db_pool, $foreman::foreman_service_puma_threads_max + 4),

@ShimShtein ShimShtein force-pushed the update_db_pool_size branch from 6d1f3b7 to 5b43a41 Compare May 12, 2024 13:25
Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

@evgeni Debian 12 is failing:

   current directory:
  /usr/share/foreman/vendor/ruby/3.1.0/gems/psych-5.1.2/ext/psych
  /usr/bin/ruby3.1 -I /usr/lib/ruby/vendor_ruby -r
  ./siteconf20240512-4799-xgqy0g.rb extconf.rb
  checking for yaml.h... no
  yaml.h not found
  *** extconf.rb failed ***
  Could not create Makefile due to some reason, probably lack of necessary
  libraries and/or headers.  Check the mkmf.log file for more details.  You may
  need configuration options.

But I thought theforeman/foreman-packaging@60435ac would address that.

# $db_pool:: Database 'production' size of connection pool. When running as a reverse proxy,
# the value of `$foreman_service_puma_threads_max` is used if it's higher than `$db_pool`.
# $db_pool:: Database 'production' size of connection pool. If the value is not set, it will be
# set by default to the amount of puma threads + 4 (for threads that are spawn by the system)
Copy link
Member

Choose a reason for hiding this comment

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

Took me a while to figure this out. Would this be clearer?

Suggested change
# set by default to the amount of puma threads + 4 (for threads that are spawn by the system)
# set by default to the amount of puma threads + 4 (for internal system threads)

@ShimShtein ShimShtein force-pushed the update_db_pool_size branch from 5b43a41 to c20c0ca Compare May 16, 2024 09:34
According to an investigation described in https://community.theforeman.org/t/rails-connection-pool-size-optimizations/36675 foreman process spawns 4 additional threads that consume DB connection during the startup. Hence the amount of acctive DB connections should be the amount of puma threads + 4 additional threads.
@ShimShtein ShimShtein force-pushed the update_db_pool_size branch from c20c0ca to 19f67cb Compare May 16, 2024 09:36
@ShimShtein
Copy link
Member Author

Should be a bit more clear now.

@ekohl ekohl merged commit b2feca1 into theforeman:master May 16, 2024
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants