Skip to content

Commit

Permalink
perf(config): make closing of database connections optional (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonadoni committed Aug 23, 2024
1 parent e046989 commit 13738ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
6 changes: 6 additions & 0 deletions reana_job_controller/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

"""Flask application configuration."""

from distutils.util import strtobool
import os

from reana_commons.config import REANA_COMPONENT_PREFIX

from werkzeug.utils import import_string

REANA_DB_CLOSE_POOL_CONNECTIONS = bool(
strtobool(os.getenv("REANA_DB_CLOSE_POOL_CONNECTIONS", "false"))
)
"""Determine whether to close each database connection when it is returned to the pool."""

CACHE_ENABLED = False
"""Determines if jobs caching is enabled."""

Expand Down
29 changes: 19 additions & 10 deletions reana_job_controller/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ def receive_checkin(dbapi_connection, connection_record):
# Given the current architecture of REANA, job-controller needs to connect to the
# database in order to, among other things, update the details of jobs. However,
# it can happen that for long periods of time job-controller does not need to access
# the database, for example when waiting for long-lasting jobs to finish. For this
# reason, each connection is closed before being returned to the connection pool, so
# that job-controller does not unnecessarily use one or more of the available
# connection slots of PostgreSQL. Keeping one connection open for the whole
# duration of the workflow is not possible, as that would limit the number of
# workflows that can be run in parallel.
# the database, for example when waiting for long-lasting jobs to finish. During this
# time, connections that are kept in the pool still consume the available connection
# slots of PostgreSQL, even though they are not being used. This effectively limits
# the number of workflows that can run in parallel.
#
# To improve scalability, we should consider refactoring job-controller to avoid
# accessing the database, or at least consider using external connection pooling
# mechanisms such as pgBouncer.
connection_record.close()
# There are a few options to avoid this:
# - enable external connection pooling with PgBouncer in the Helm chart, so that
# many more connections can be opened at the same time
# - increase the number of connection slots of PostgreSQL, but this increases the
# memory/cpu needed by the database
# - close each connection as soon as it is returned to the pool
#
# Note that closing connections every time they are returned to the pool means that
# every new transaction will need to open a connection to the database. This
# impacts how fast jobs can be spawned, as opening a connection takes quite some time
# (tens of millisecond). For this reason, connections are not closed by default when
# they are returned to the pool, but this behaviour can be customised with env
# variables.
if config.REANA_DB_CLOSE_POOL_CONNECTIONS:
connection_record.close()

Check warning on line 48 in reana_job_controller/factory.py

View check run for this annotation

Codecov / codecov/patch

reana_job_controller/factory.py#L47-L48

Added lines #L47 - L48 were not covered by tests


def shutdown_session(response_or_exc):
Expand Down

0 comments on commit 13738ad

Please sign in to comment.