Skip to content

Commit 6be2595

Browse files
committed
feat (MySQL): introduce DIRAC_MYSQL_CONNECTION_GRACE_TIME to allow for non reused connection
1 parent ccc050e commit 6be2595

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

docs/source/AdministratorGuide/ServerInstallations/environment_variable_configuration.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ DIRAC_M2CRYPTO_SSL_CIPHERS
5050
DIRAC_M2CRYPTO_SSL_METHODS
5151
If set, overwrites the default SSL methods accepted. It should be a colon separated list. See :py:mod:`DIRAC.Core.DISET`
5252

53+
DIRAC_MYSQL_CONNECTION_GRACE_TIME
54+
If set to an integer, default of the grace time before the connection is reused. See :py:class:`DIRAC.Core.Utilities.MySQL.ConnectionPool`
55+
5356
DIRAC_MYSQL_OPTIMIZER_TRACES_PATH
5457
If set, it should point to an existing directory, where MySQL Optimizer traces will be stored. See :py:func:`DIRAC.Core.Utilities.MySQL.captureOptimizerTraces`
5558

src/DIRAC/Core/Utilities/MySQL.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,21 @@ class ConnectionPool:
327327
Management of connections per thread
328328
"""
329329

330-
def __init__(self, host, user, passwd, port=3306, graceTime=600):
330+
# The idea of the grace time is that a given thread should not keep a connection object for too long
331+
# However, in case of long running queries, this is unavoidable.
332+
# If the DISET/HTTPs connection timeout is longer than the MySQL grace time,
333+
# the connection will be reallocated to another thread, and that does not go down well
334+
# From the mysqlclient doc ("threadsafety" of https://mysqlclient.readthedocs.io/user_guide.html#mysqldb)
335+
# "The general upshot of this is: Don’t share connections between threads."
336+
# The best thing to do, if we don't want to have a lock, is to set this value to 0
337+
# so it is not used. LHCb will run in prod with the value 0 for a while, and if no issue arrises,
338+
# it will become the default
339+
try:
340+
MYSQL_CONNECTION_GRACE_TIME = int(os.environ.get("DIRAC_MYSQL_CONNECTION_GRACE_TIME"))
341+
except Exception:
342+
MYSQL_CONNECTION_GRACE_TIME = 600
343+
344+
def __init__(self, host, user, passwd, port=3306, graceTime=MYSQL_CONNECTION_GRACE_TIME):
331345
self.__host = host
332346
self.__user = user
333347
self.__passwd = passwd
@@ -446,7 +460,7 @@ def clean(self, now=False):
446460
data = self.__assigned[thid]
447461
except KeyError:
448462
continue
449-
if now - data[2] > self.__graceTime:
463+
if self.__graceTime and now - data[2] > self.__graceTime:
450464
self.__pop(thid)
451465

452466
def transactionStart(self, dbName):

0 commit comments

Comments
 (0)