简体   繁体   English

c#中使用foreach重复更新数据实体时的争用错误

[英]Contention error when repeatedly updating a data entity with a foreach in c #

How is it possible to optimize the update of an entity, when this entity is updated many times per second by request of the server ?.当服务器的请求每秒更新该实体多次时,如何优化实体的更新? The method used for the update is developed as follows:用于更新的方法开发如下:

public async Task<bool> Update(T entity, string key)
    {
        try
        {
            MakeConnection();
            var trans = _db.BeginTransaction();
            var type = typeof(T);
            var keyFactory = _db.CreateKeyFactory(type.Name);
            var task = trans.Lookup(keyFactory.CreateKey(key));
            if (task != null)
            {
                _cache.KeyCache[type.Name] = _db.CreateKeyFactory(type.Name);
                var entities = _mapper.Map<Entity>(entity);
                task = entities;
                trans.Update(task);
            }
            await trans.CommitAsync();
        }
        catch (Exception e)
        {
            throw new InvalidOperationException($"An error occurred, {e}");
        }
        return true;
    }

and this is the error:这是错误:

InvalidOperationException: An error occurred, Grpc.Core.RpcException: Status(StatusCode=Aborted, Detail="too much contention on these datastore entities. please try again. entity groups: [(app=p~********, *********, "76fcb9c1-009e-****-b54f-68a45e9c****")]") InvalidOperationException: 发生错误,Grpc.Core.RpcException: Status(StatusCode=Aborted, Detail="对这些数据存储实体的争用过多。请重试。实体组:[(app=p~********) , *********, "76fcb9c1-009e-****-b54f-68a45e9c****")]")

There is a limit to the frequency with which you can update an entity, due to the distributed fault tolerance which DataStore provides.由于 DataStore 提供的分布式容错能力,您可以更新实体的频率存在限制。 It's recommend updating once per second at most, although in practice you can likely exceed this several times over.建议最多每秒更新一次,但实际上您可能会超过此数倍。

You can read the documentation about Datastore contention.您可以阅读有关数据存储争用的文档 According to the doc, if you're expecting to update a single entity or write to an entity group more than several times per second, it's best to re-work your design early-on to avoid possible contention once your application is deployed.根据该文档,如果您希望每秒更新单个实体或多次写入实体组,最好尽早重新设计您的设计,以避免在部署应用程序后可能出现争用。

One of the possible options is to use Sharding counters .一种可能的选择是使用Sharding counters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM