Skip to content

fix: Avoid importing mysql as part of the Job Wrapper - #8983

Open
fstagni wants to merge 1 commit into
DIRACGrid:integrationfrom
fstagni:fix/8980-lazy-mysql-import
Open

fstagni wants to merge 1 commit into
DIRACGrid:integrationfrom
fstagni:fix/8980-lazy-mysql-import

Conversation

@fstagni

@fstagni fstagni commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #8980

Summary

Importing the MySQLdb client used to happen at module import time, so every component that transitively imports DIRAC.Core.Utilities.MySQL (e.g. via DIRAC.Core.Base.DB, or just for the _quotedList helper) required the mysql client to be installed — even when it never contacts a database. This made pilot-side components fail in minimal pilot environments where the mysql client is missing.

  import chain (before):
  JobAgent / PushJobAgent / SingularityComputingElement
+   → WorkloadManagementSystem.Utilities.Utils
+     → WMS.DB.JobDB / TaskQueueDB / JobLoggingDB (module level)
+       → Core.Base.DB
+         → Core.Utilities.MySQL
+           → import MySQLdb   ← fails when the client is missing

Two changes:

  • DIRAC/Core/Utilities/MySQL.py: MySQLdb is now imported lazily, only when an actual database connection is attempted (MySQL.__init__ and ConnectionPool.__newConn). This covers every transitive import of DB modules anywhere in the code base.
  • DIRAC/WorkloadManagementSystem/Utilities/Utils.py: the JobDB/TaskQueueDB/JobLoggingDB imports moved from module level into rescheduleJobs(), the only place where they are instantiated. The DB classes are only used by server-side components.

Evidence

Importing the utilities no longer loads the mysql client, while instantiating a connection still does:

1) after importing MySQL utils + Core.Base.DB, MySQLdb loaded: False
2) after instantiating MySQL,               MySQLdb loaded: True

Pilot-side agent modules import with no mysql module in sys.modules:

after JobAgent / PushJobAgent / SingularityCE / StalledJobAgent imports: []

A full run of JobWrapperTemplate (as the pilot executes it) imports no mysql module and behaves unchanged.

Tests: all Core/Utilities (472) and WorkloadManagementSystem (494) tests pass. ruff check back to the pre-existing baseline.

Merge Danger

Door: two-way — trivially revertible, no data or behavior change for components that actually use the databases.

Blast Radius: WMS client/agent imports. The only functional change is when import MySQLdb happens: server-side DB instantiation now raises ImportError at connection time instead of at module import time, which is what the issue asks for.

@fstagni
fstagni requested a review from atsareg as a code owner November 26, 2026 14:05
Importing the MySQLdb client at module import time forced every component
that transitively imports DIRAC.Core.Utilities.MySQL (e.g. via
DIRAC.Core.Base.DB or the _quotedList helper) to have the mysql client
installed, even when it never contacts a database. This made pilot-side
components (JobAgent, PushJobAgent, SingularityComputingElement, and
through them the Job Wrapper) fail in minimal pilot environments where
the mysql client is not installed.

- import MySQLdb lazily in DIRAC/Core/Utilities/MySQL.py, only when an
  actual database connection is attempted (MySQL.__init__ and
  ConnectionPool.__newConn)
- move the JobDB/TaskQueueDB/JobLoggingDB imports of
  DIRAC.WorkloadManagementSystem.Utilities.Utils from module level into
  rescheduleJobs(), the only place where they are instantiated

Fixes DIRACGrid#8980
@fstagni
fstagni force-pushed the fix/8980-lazy-mysql-import branch from 7789362 to 45821fc Compare November 26, 2026 15:04
@maxnoe

maxnoe commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Just updated the issue, in recent dirac it also affects opensearch

This branch has not been deployed

No deployments
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.

[Feature]: Avoid importing mysq/opensearchl as part of the Job Wrapper

2 participants