-
Notifications
You must be signed in to change notification settings - Fork 188
fix: ensure input sandboxes assigned to transformations to avoid premature cleaning #8621
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
Open
ryuwd
wants to merge
8
commits into
DIRACGrid:integration
Choose a base branch
from
ryuwd:roneil-trf-sandbox-assignment
base: integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+430
−3
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
69ea110
feat(transformation): pin input sandboxes to the transformation on cr…
ryuwd 811423a
test(sandbox): cover transformation entity assign/unassign in Sandbox…
ryuwd 93da003
feat(transformation): unassign input sandboxes on clean/archive
ryuwd 4166034
refactor(transformation): accept list- or string-valued InputSandbox …
ryuwd e2d7e98
fix(transformation): fail loudly and leave no bad state on sandbox pi…
ryuwd 41e8dd7
test(transformation): cover sandbox pin/unpin handling; rename test m…
ryuwd c6d3b08
docs(transformation): document input sandboxes and their pinning
ryuwd ea3e979
fix: Don't start services if we can't setup the SandboxMetadataDB
ryuwd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,7 @@ Disadvantages: | |
| Several improvements have been made in the TS to handle scalability, and extensibility issues. | ||
| While the system structure remains intact, "tricks" like threading and caching have been extensively applied. | ||
|
|
||
| It's not possible to use ISB (Input Sandbox) to ship local files as for 'normal' Jobs (this should not be considered, anyway, a disadvantage). | ||
| Local files can't be shipped through the Input Sandbox the way the Job API does for 'normal' Jobs; a transformation instead references already-uploaded sandboxes — see `Input Sandboxes`_. | ||
|
|
||
| ------------ | ||
| Architecture | ||
|
|
@@ -121,6 +121,54 @@ The complete list can be found in the `DIRAC project GitHub repository <https:// | |
|
|
||
| * Transformation: it wraps some functionalities mostly to use the 'TransformationClient' client | ||
|
|
||
| --------------- | ||
| Input Sandboxes | ||
| --------------- | ||
|
|
||
| A transformation's input sandboxes are the ``SB:`` PFNs listed in its body's | ||
| ``InputSandbox`` workflow parameter (``;``-joined). The sandboxes must already be | ||
| uploaded to the SandboxStore; the body references them by PFN, and every job the | ||
| transformation creates receives them — so a production can share one pre-built | ||
| payload (a compiled binary, a large config) across all its jobs. | ||
|
|
||
| Set the ``InputSandbox`` parameter on the body **before** the transformation is | ||
| created — it is pinned at creation, so adding it afterwards would leave the | ||
| sandboxes unpinned. Use the Job API:: | ||
|
|
||
| job.setInputSandbox(["SB:ProductionSandboxSE|/S3/.../payload.tar.bz2"]) | ||
|
|
||
| or add the parameter to the job's workflow directly:: | ||
|
|
||
| from DIRAC.Core.Workflow.Parameter import Parameter | ||
| job.workflow.addParameter( | ||
| Parameter("InputSandbox", ";".join(sbPFNs), "JDL", "", "", True, False, "Input sandbox file list") | ||
| ) | ||
|
|
||
| then use that body to create the transformation, which pins the sandboxes:: | ||
|
|
||
| t.setBody(job.workflow.toXML()) | ||
| t.addTransformation() | ||
|
|
||
| Because a transformation may keep submitting jobs for a long time while the | ||
| SandboxStore cleaner reclaims any sandbox not assigned to an entity, the | ||
| Transformation System **pins** these sandboxes to the transformation: | ||
|
|
||
| - On creation, ``TransformationManager`` assigns each ``SB:`` reference in the | ||
| body to the entity ``Transformation:<id>`` in the ``SandboxMetadataDB``. While | ||
| that mapping exists the cleaner will not remove the sandbox. | ||
| - Pinning is mandatory: if it fails (``SandboxMetadataDB`` unreachable, or the | ||
| author is not authorised for a referenced sandbox), the transformation is | ||
| **rolled back and creation fails** — a created-but-unpinned transformation | ||
| would have its sandbox cleaned and then break every job submitted afterwards. | ||
| - On clean/archive, ``TransformationCleaningAgent`` unassigns | ||
| ``Transformation:<id>`` so the cleaner can reclaim the now-unused sandboxes. If | ||
| the unassignment fails, the transformation's cleaning fails and is retried — | ||
| otherwise the sandboxes would stay pinned to a gone transformation and leak. | ||
| The unassignment is idempotent, so retrying is safe. | ||
|
|
||
| This requires the ``SandboxMetadataDB`` to be reachable from both the | ||
| ``TransformationManager`` service and the ``TransformationCleaningAgent``. | ||
|
Comment on lines
+169
to
+170
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...which is "always", unless someone really tries hard. I would remove this comment. |
||
|
|
||
| ------------- | ||
| Configuration | ||
| ------------- | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,7 @@ def __init__(self, *args, **kwargs): | |
| self.pilotAgentsDB = None | ||
| self.taskQueueDB = None | ||
| self.storageManagementDB = None | ||
| self.sandboxDB = None | ||
|
|
||
| # # transformations types | ||
| self.transformationTypes = None | ||
|
|
@@ -153,6 +154,18 @@ def initialize(self): | |
| except RuntimeError: | ||
| pass | ||
|
|
||
| # SandboxMetadataDB is used to unassign a transformation's input sandboxes at | ||
| # clean/archive time, releasing them for the sandbox-store cleaner. The agent | ||
| # keeps running if it can't load (the unassign step is skipped), but a failure | ||
| # to unassign during cleaning is logged loudly so a leaked assignment (which | ||
| # keeps the sandbox pinned forever) is noticed. | ||
| self.sandboxDB = None | ||
| result = ObjectLoader().loadObject("WorkloadManagementSystem.DB.SandboxMetadataDB", "SandboxMetadataDB") | ||
| if result["OK"]: | ||
| self.sandboxDB = result["Value"]() | ||
| else: | ||
| return result | ||
|
|
||
| return S_OK() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is dead code now. |
||
|
|
||
| ############################################################################# | ||
|
|
@@ -514,6 +527,12 @@ def archiveTransformation(self, transID): | |
| :param int transID: transformation ID | ||
| """ | ||
| self.log.info(f"Archiving transformation {transID}") | ||
| # Release the input-sandbox pin first; if it fails, fail the whole archive so | ||
| # it is retried — otherwise the sandboxes stay pinned to a gone transformation | ||
| # and leak. unassignEntities is idempotent, so the retry is safe. | ||
| res = self._unassignTransformationSandboxes(transID) | ||
| if not res["OK"]: | ||
| return res | ||
| # Clean the jobs in the WMS and any failover requests found | ||
| res = self.cleanTransformationTasks(transID) | ||
| if not res["OK"]: | ||
|
|
@@ -531,11 +550,41 @@ def archiveTransformation(self, transID): | |
| self.log.info(f"Updated status of transformation {transID} to Archived") | ||
| return S_OK() | ||
|
|
||
| def _unassignTransformationSandboxes(self, transID): | ||
| """Remove a transformation's input-sandbox assignment at clean/archive time. | ||
|
|
||
| Drops the ``Transformation:<transID>`` mapping in the SandboxMetadataDB so the | ||
| sandbox-store cleaner can reclaim the (now unused) sandboxes. Returns S_ERROR on | ||
| failure so the caller can fail the cleaning and retry: leaving the assignment in | ||
| place would pin the sandboxes forever and leak them. ``unassignEntities`` is | ||
| idempotent, so retrying — including after the mapping is already gone — is safe. | ||
|
|
||
| :param int transID: transformation ID | ||
| :returns: S_OK / S_ERROR | ||
| """ | ||
| if not self.sandboxDB: | ||
| return S_OK() | ||
| try: | ||
| result = self.sandboxDB.unassignEntities([f"Transformation:{transID}"]) | ||
| except Exception as excp: # pylint: disable=broad-except | ||
| self.log.exception("Unexpected error unassigning sandboxes for transformation", str(transID)) | ||
| return S_ERROR(f"Unexpected error unassigning sandboxes for transformation {transID}: {excp}") | ||
| if not result["OK"]: | ||
| self.log.error("Could not unassign sandboxes for transformation", f"{transID}: {result['Message']}") | ||
| return S_ERROR(f"Could not unassign sandboxes for transformation {transID}: {result['Message']}") | ||
| return S_OK() | ||
|
|
||
| def cleanTransformation(self, transID): | ||
| """This removes what was produced by the supplied transformation, | ||
| leaving only some info and log in the transformation DB. | ||
| """ | ||
| self.log.info("Cleaning transformation", transID) | ||
| # Release the input-sandbox pin first; if it fails, fail the whole clean so it | ||
| # is retried — otherwise the sandboxes stay pinned to a gone transformation and | ||
| # leak. unassignEntities is idempotent, so the retry is safe. | ||
| res = self._unassignTransformationSandboxes(transID) | ||
| if not res["OK"]: | ||
| return res | ||
| res = self.getTransformationDirectories(transID) | ||
| if not res["OK"]: | ||
| self.log.error( | ||
|
|
||
63 changes: 63 additions & 0 deletions
63
src/DIRAC/TransformationSystem/Agent/test/Test_TransformationCleaningAgent.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # pylint: disable=missing-docstring | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from DIRAC.TransformationSystem.Agent.TransformationCleaningAgent import TransformationCleaningAgent | ||
|
|
||
| # The agent's __init__ needs a running config, so these exercise the methods with a | ||
| # stand-in ``self`` (a MagicMock) carrying only the attributes the methods touch. | ||
|
|
||
|
|
||
| def test_unassign_callsDBWithTransformationEntity(): | ||
| agent = MagicMock() | ||
| agent.sandboxDB.unassignEntities.return_value = {"OK": True, "Value": 1} | ||
|
|
||
| res = TransformationCleaningAgent._unassignTransformationSandboxes(agent, 12345) | ||
|
|
||
| assert res["OK"] | ||
| agent.sandboxDB.unassignEntities.assert_called_once_with(["Transformation:12345"]) | ||
| agent.log.error.assert_not_called() | ||
| agent.log.exception.assert_not_called() | ||
|
|
||
|
|
||
| def test_unassign_noDBisOk(): | ||
| agent = MagicMock() | ||
| agent.sandboxDB = None | ||
| # Nothing to unassign: succeed so cleaning is not blocked in DB-less deployments. | ||
| assert TransformationCleaningAgent._unassignTransformationSandboxes(agent, 12345)["OK"] | ||
|
|
||
|
|
||
| def test_unassign_dbErrorIsLoggedAndReturnsError(): | ||
| agent = MagicMock() | ||
| agent.sandboxDB.unassignEntities.return_value = {"OK": False, "Message": "boom"} | ||
|
|
||
| # A failed unassignment must be loud AND returned as an error so the caller fails | ||
| # the cleaning and retries (otherwise the sandboxes leak). | ||
| res = TransformationCleaningAgent._unassignTransformationSandboxes(agent, 12345) | ||
|
|
||
| assert not res["OK"] | ||
| agent.sandboxDB.unassignEntities.assert_called_once_with(["Transformation:12345"]) | ||
| agent.log.error.assert_called_once() | ||
|
|
||
|
|
||
| def test_unassign_unexpectedExceptionIsLoggedAndReturnsError(): | ||
| agent = MagicMock() | ||
| agent.sandboxDB.unassignEntities.side_effect = RuntimeError("db down") | ||
|
|
||
| # An unexpected exception must not propagate, but must be logged loudly and returned | ||
| # as an error so the cleaning is retried. | ||
| res = TransformationCleaningAgent._unassignTransformationSandboxes(agent, 12345) | ||
|
|
||
| assert not res["OK"] | ||
| agent.log.exception.assert_called_once() | ||
|
|
||
|
|
||
| def test_cleanTransformation_failsWhenUnassignFails(): | ||
| # The whole clean must fail (and be retried) if the sandbox can't be unassigned, | ||
| # rather than proceeding and leaking the now-orphaned sandbox assignment. | ||
| agent = MagicMock() | ||
| agent._unassignTransformationSandboxes.return_value = {"OK": False, "Message": "boom"} | ||
|
|
||
| res = TransformationCleaningAgent.cleanTransformation(agent, 12345) | ||
|
|
||
| assert not res["OK"] | ||
| agent.getTransformationDirectories.assert_not_called() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"normal jobs" are "user jobs"?