opt(api): optimize update contention on the providers table (#24520)

This commit is contained in:
QuantumGhost
2025-08-26 11:41:38 +08:00
committed by GitHub
parent d7869a4d1e
commit 410fe7293f

View File

@@ -85,6 +85,7 @@ def handle(sender: Message, **kwargs):
values=_ProviderUpdateValues(last_used=current_time), values=_ProviderUpdateValues(last_used=current_time),
description="basic_last_used_update", description="basic_last_used_update",
) )
logging.info("provider used, tenant_id=%s, provider_name=%s", tenant_id, provider_name)
updates_to_perform.append(basic_update) updates_to_perform.append(basic_update)
# 2. Check if we need to deduct quota (system provider only) # 2. Check if we need to deduct quota (system provider only)
@@ -186,6 +187,8 @@ def _execute_provider_updates(updates_to_perform: list[_ProviderUpdateOperation]
if not updates_to_perform: if not updates_to_perform:
return return
updates_to_perform = sorted(updates_to_perform, key=lambda i: (i.filters.tenant_id, i.filters.provider_name))
# Use SQLAlchemy's context manager for transaction management # Use SQLAlchemy's context manager for transaction management
# This automatically handles commit/rollback # This automatically handles commit/rollback
with Session(db.engine) as session, session.begin(): with Session(db.engine) as session, session.begin():
@@ -212,10 +215,13 @@ def _execute_provider_updates(updates_to_perform: list[_ProviderUpdateOperation]
# Prepare values dict for SQLAlchemy update # Prepare values dict for SQLAlchemy update
update_values = {} update_values = {}
if values.last_used is not None: # updateing to `last_used` is removed due to performance reason.
update_values["last_used"] = values.last_used # ref: https://github.com/langgenius/dify/issues/24526
if values.quota_used is not None: if values.quota_used is not None:
update_values["quota_used"] = values.quota_used update_values["quota_used"] = values.quota_used
# Skip the current update operation if no updates are required.
if not update_values:
continue
# Build and execute the update statement # Build and execute the update statement
stmt = update(Provider).where(*where_conditions).values(**update_values) stmt = update(Provider).where(*where_conditions).values(**update_values)