Skip to content

Commit 3ba5efe

Browse files
committed
fix: use temporary table for SB deletions
1 parent db28585 commit 3ba5efe

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
""" SandboxMetadataDB class is a front-end to the metadata for sandboxes
22
"""
3-
from DIRAC import S_ERROR, S_OK, gLogger
3+
from DIRAC import S_ERROR, S_OK
44
from DIRAC.ConfigurationSystem.Client.Helpers import Registry
55
from DIRAC.Core.Base.DB import DB
66
from DIRAC.Core.Security import Properties
@@ -246,7 +246,7 @@ def unassignEntities(self, entities, requesterName, requesterGroup):
246246
return None
247247
conds = self.__entitiesByRequesterCond(requesterName, requesterGroup)
248248

249-
sqlCmd = "CREATE TEMPORARY TABLE to_delete_EntityId (EntityId VARCHAR(128) NOT NULL, PRIMARY KEY (EntityId)) ENGINE=MEMORY;"
249+
sqlCmd = "CREATE TEMPORARY TABLE to_delete_EntityId (EntityId VARCHAR(128) NOT NULL, PRIMARY KEY (EntityId)) ENGINE=MEMORY;"
250250
returnValueOrRaise(self._update(sqlCmd))
251251
try:
252252
sqlCmd = "INSERT INTO to_delete_EntityId (EntityId) VALUES ( %s )"
@@ -313,16 +313,30 @@ def getUnusedSandboxes(self):
313313
sqlCmd = f"SELECT SBId, SEName, SEPFN FROM `sb_SandBoxes` WHERE SEPFN not like '/S3/%' AND (( {' ) OR ( '.join(sqlCond)} ))"
314314
return self._query(sqlCmd)
315315

316+
@convertToReturnValue
316317
def deleteSandboxes(self, SBIdList):
317318
"""
318-
Delete sandboxes
319+
Delete sandboxes using a temporary table for efficiency and consistency.
319320
"""
320-
sqlSBList = ", ".join([str(sbid) for sbid in SBIdList])
321-
for table in ("sb_SandBoxes", "sb_EntityMapping"):
322-
sqlCmd = f"DELETE FROM `{table}` WHERE SBId IN ( {sqlSBList} )"
323-
result = self._update(sqlCmd)
324-
if not result["OK"]:
325-
return result
321+
if not SBIdList:
322+
return S_OK()
323+
# Create temporary table
324+
sqlCmd = "CREATE TEMPORARY TABLE to_delete_SBId (SBId INTEGER(10) UNSIGNED NOT NULL, PRIMARY KEY (SBId)) ENGINE=MEMORY;"
325+
returnValueOrRaise(self._update(sqlCmd))
326+
try:
327+
# Insert SBIds into temporary table
328+
sqlCmd = "INSERT INTO to_delete_SBId (SBId) VALUES (%s)"
329+
returnValueOrRaise(self._updatemany(sqlCmd, [(sbid,) for sbid in SBIdList]))
330+
# Delete from sb_EntityMapping first (to respect FK constraints if any)
331+
sqlCmd = "DELETE FROM `sb_EntityMapping` WHERE SBId IN (SELECT SBId FROM to_delete_SBId)"
332+
returnValueOrRaise(self._update(sqlCmd))
333+
# Delete from sb_SandBoxes
334+
sqlCmd = "DELETE FROM `sb_SandBoxes` WHERE SBId IN (SELECT SBId FROM to_delete_SBId)"
335+
returnValueOrRaise(self._update(sqlCmd))
336+
finally:
337+
# Drop temporary table
338+
sqlCmd = "DROP TEMPORARY TABLE to_delete_SBId"
339+
returnValueOrRaise(self._update(sqlCmd))
326340
return S_OK()
327341

328342
def getSandboxId(self, SEName, SEPFN, requesterName, requesterGroup, field="SBId"):

0 commit comments

Comments
 (0)