ソースを参照

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

tags/1.8.0
QuantumGhost 2ヶ月前
コミット
410fe7293f
コミッターのメールアドレスに関連付けられたアカウントが存在しません

+ 8
- 2
api/events/event_handlers/update_provider_when_message_created.py ファイルの表示

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)
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():


# Prepare values dict for SQLAlchemy update # Prepare values dict for SQLAlchemy update
update_values = {} update_values = {}
if values.last_used is not None:
update_values["last_used"] = values.last_used
# updateing to `last_used` is removed due to performance reason.
# 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)

読み込み中…
キャンセル
保存