ValueError:5次尝试提交事务失败

时间:2019-01-31 10:36:16

标签: python firebase google-cloud-platform google-cloud-firestore firebase-admin

TL; DR 事务未按预期更新计数器

场景:

我有名为projectsactivities的子集合。 项目集合用于存储有关任何正在进行的项目的信息,而活动集合可以用作对项目集合中发生的任何更改的审核跟踪。

我已经编写了一个云功能,该功能可触发活动集合on create event。在活动集合中创建文档后,我需要在项目子集合中由activityCount标识的文档中更新名为projectId的计数器。

云功能(出于简洁而删除了某些部分)如下:

@firestore.transactional
def update_activity_count_in_transaction(transaction, activity):
    """
    updates the activity count for a project
    """
    try:
        # get the project details of associated project at home/{orgId}/projects/{projectId}
        project = activity.project.doc_ref.get(transaction=transaction)

        # get the activity count from the transactional snapshot
        activity_count = project.get(FireBaseConfig.ACTIVITY_COUNT)

        # increment the counter using transaction
        transaction.update(activity.project.doc_ref, {FireBaseConfig.ACTIVITY_COUNT: activity_count + 1})
    except KeyError as err:
        print(err)

        # create activityCount key if not present in projects collection and increment the counter using transaction
        transaction.update(activity.project.doc_ref, {FireBaseConfig.ACTIVITY_COUNT: 1})
    print('transaction success')

    return True


def activity_on_create(event, context):
    """
    update the activity count in projects sub collection
    This is invoked on create event at home/{orgId}/activities/{activityId}
    """

    # parse the event data received from cloud functions into a FireStoreActivity object
    activity = FireStoreActivity(event, context)
    try:
        if not activity.project.id:
            print('no project id')
            return True

        # get firestore transaction object
        # activity.transaction() is equivalent to firestore.client().transaction()
        transaction = activity.transaction()

        # update the counter using transaction
        update_activity_count_in_transaction(transaction, activity)
    except Exception as err:
        pass
    return True

以下是这样做时的错误:

ValueError: Failed to commit transaction in 5 attempts

跟踪:

File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 333, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 199, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 196, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 149, in activity_on_create
    raise err
  File "/user_code/main.py", line 146, in activity_on_create
    update_activity_count_in_transaction(transaction, activity)
  File "/env/local/lib/python3.7/site-packages/google/cloud/firestore_v1beta1/transaction.py", line 327, in __call__
    raise ValueError(msg)
ValueError: Failed to commit transaction in 5 attempts.
  

注意:此错误是随机发生的,并非始终存在。但这确实发生了。

关于这里可能出什么问题的任何想法?

1 个答案:

答案 0 :(得分:2)

如果由于并发大量写入同一目标文档而导致事务提交失败,Firestore客户端将在放弃前重试事务.claim { display: none; } 次。如果是Python Firestore客户端,则默认为N = 5。您可以通过将N参数传递给max_attempts方法调用来控制此参数。