Skip to content

Commit

Permalink
feat (MySQL): introduce DIRAC_MYSQL_CONNECTION_GRACE_TIME to allow fo…
Browse files Browse the repository at this point in the history
…r non reused connection
  • Loading branch information
chaen committed Dec 5, 2023
1 parent ccc050e commit 6be2595
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ DIRAC_M2CRYPTO_SSL_CIPHERS
DIRAC_M2CRYPTO_SSL_METHODS
If set, overwrites the default SSL methods accepted. It should be a colon separated list. See :py:mod:`DIRAC.Core.DISET`

DIRAC_MYSQL_CONNECTION_GRACE_TIME
If set to an integer, default of the grace time before the connection is reused. See :py:class:`DIRAC.Core.Utilities.MySQL.ConnectionPool`

DIRAC_MYSQL_OPTIMIZER_TRACES_PATH
If set, it should point to an existing directory, where MySQL Optimizer traces will be stored. See :py:func:`DIRAC.Core.Utilities.MySQL.captureOptimizerTraces`

Expand Down
18 changes: 16 additions & 2 deletions src/DIRAC/Core/Utilities/MySQL.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,21 @@ class ConnectionPool:
Management of connections per thread
"""

def __init__(self, host, user, passwd, port=3306, graceTime=600):
# The idea of the grace time is that a given thread should not keep a connection object for too long
# However, in case of long running queries, this is unavoidable.
# If the DISET/HTTPs connection timeout is longer than the MySQL grace time,
# the connection will be reallocated to another thread, and that does not go down well
# From the mysqlclient doc ("threadsafety" of https://mysqlclient.readthedocs.io/user_guide.html#mysqldb)
# "The general upshot of this is: Don’t share connections between threads."
# The best thing to do, if we don't want to have a lock, is to set this value to 0
# so it is not used. LHCb will run in prod with the value 0 for a while, and if no issue arrises,
# it will become the default
try:
MYSQL_CONNECTION_GRACE_TIME = int(os.environ.get("DIRAC_MYSQL_CONNECTION_GRACE_TIME"))
except Exception:
MYSQL_CONNECTION_GRACE_TIME = 600

def __init__(self, host, user, passwd, port=3306, graceTime=MYSQL_CONNECTION_GRACE_TIME):
self.__host = host
self.__user = user
self.__passwd = passwd
Expand Down Expand Up @@ -446,7 +460,7 @@ def clean(self, now=False):
data = self.__assigned[thid]
except KeyError:
continue
if now - data[2] > self.__graceTime:
if self.__graceTime and now - data[2] > self.__graceTime:
self.__pop(thid)

def transactionStart(self, dbName):
Expand Down

0 comments on commit 6be2595

Please sign in to comment.