Skip to content

Commit 3a1ae47

Browse files
committed
fix: make sandbox cleaning parameters configurable via settings
Move hardcoded batch_size, delete_chunk_size, and semaphore concurrency values into SandboxStoreSettings so they can be tuned per deployment.
1 parent f25667b commit 3a1ae47

3 files changed

Lines changed: 45 additions & 5 deletions

File tree

diracx-core/src/diracx/core/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,24 @@ class SandboxStoreSettings(ServiceSettingsBase):
276276
deletion). Default: 50.
277277
"""
278278

279+
clean_batch_size: int = 50_000
280+
"""Number of sandbox candidates to select per batch during cleaning.
281+
282+
Each batch runs SELECT → S3 delete → DB delete sequentially. Default: 50000.
283+
"""
284+
285+
clean_delete_chunk_size: int = 1000
286+
"""Number of sandbox DB rows to delete per chunk during cleaning.
287+
288+
Smaller chunks mean shorter transactions and less lock contention. Default: 1000.
289+
"""
290+
291+
clean_max_concurrent_db_deletes: int = 10
292+
"""Maximum number of concurrent DB delete chunks during cleaning.
293+
294+
Controls parallelism of database DELETE operations. Default: 10.
295+
"""
296+
279297
_client: S3Client = PrivateAttr()
280298

281299
@contextlib.asynccontextmanager

diracx-logic/src/diracx/logic/jobs/sandboxes.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,6 @@ async def insert_sandbox(
201201
async def clean_sandboxes(
202202
sandbox_metadata_db: SandboxMetadataDB,
203203
settings: SandboxStoreSettings,
204-
*,
205-
batch_size: int = 50000,
206204
) -> int:
207205
"""Delete sandboxes that are not assigned to any job.
208206
@@ -214,12 +212,12 @@ async def clean_sandboxes(
214212
Args:
215213
sandbox_metadata_db: Database connection (not yet entered).
216214
settings: Sandbox store settings with S3 client.
217-
batch_size: Number of sandboxes to process per batch.
218215
219216
Returns:
220217
Total number of sandboxes deleted.
221218
222219
"""
220+
batch_size = settings.clean_batch_size
223221
total_deleted = 0
224222
cursor = 0
225223
batch_num = 0
@@ -294,8 +292,8 @@ async def clean_sandboxes(
294292
# transaction to avoid locking millions of rows in a single DELETE).
295293
# Up to 10 chunks run concurrently via a semaphore.
296294
t0 = time.monotonic()
297-
delete_chunk_size = 1000
298-
sem = asyncio.Semaphore(10)
295+
delete_chunk_size = settings.clean_delete_chunk_size
296+
sem = asyncio.Semaphore(settings.clean_max_concurrent_db_deletes)
299297

300298
async def _delete_chunk(chunk: list[int], _sem: asyncio.Semaphore = sem) -> int:
301299
async with _sem, sandbox_metadata_db:

docs/admin/reference/env-variables.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ Maximum number of connections in the S3 client connection pool.
157157
Higher values allow more parallel S3 requests (e.g. during bulk sandbox
158158
deletion). Default: 50.
159159

160+
### `DIRACX_SANDBOX_STORE_CLEAN_BATCH_SIZE`
161+
162+
*Optional*, default value: `50000`
163+
164+
Number of sandbox candidates to select per batch during cleaning.
165+
166+
Each batch runs SELECT → S3 delete → DB delete sequentially. Default: 50000.
167+
168+
### `DIRACX_SANDBOX_STORE_CLEAN_DELETE_CHUNK_SIZE`
169+
170+
*Optional*, default value: `1000`
171+
172+
Number of sandbox DB rows to delete per chunk during cleaning.
173+
174+
Smaller chunks mean shorter transactions and less lock contention. Default: 1000.
175+
176+
### `DIRACX_SANDBOX_STORE_CLEAN_MAX_CONCURRENT_DB_DELETES`
177+
178+
*Optional*, default value: `10`
179+
180+
Maximum number of concurrent DB delete chunks during cleaning.
181+
182+
Controls parallelism of database DELETE operations. Default: 10.
183+
160184
## OTELSettings
161185

162186
Settings for the Open Telemetry Configuration.

0 commit comments

Comments
 (0)