Azure cosmosdb 具有多个分区的DocumentDb分区计数

Azure cosmosdb 具有多个分区的DocumentDb分区计数,azure-cosmosdb,Azure Cosmosdb,在一个数据库中,我有几个不同实体的集合,它们由PartitionKey属性进行分区。 我正在为每个实体存储库创建新的DocumentClient,并将HashPartitionResolver绑定到它: var hashResolver = new HashPartitionResolver(partitionKeyExtractor, partitionCollectionsSelfLinks, hashGenerator:new HashGenerator()); client.Partit

在一个数据库中,我有几个不同实体的集合,它们由PartitionKey属性进行分区。 我正在为每个实体存储库创建新的DocumentClient,并将HashPartitionResolver绑定到它:

var hashResolver = new HashPartitionResolver(partitionKeyExtractor, partitionCollectionsSelfLinks, hashGenerator:new HashGenerator());
client.PartitionResolvers[db.SelfLink] = hashResolver;
但当我尝试插入实体时:

await _client.UpsertDocumentAsync(Database.SelfLink, entity);
一些实体经常进入错误的分区。我想这可能是因为一个数据库中每个实体的分区计数不同


有人知道是什么导致这个问题吗?我如何解决它?谢谢你的帮助

我终于找到了问题所在。 在反编译HashPartitionResolver和ConsistentHashRing的代码后,我发现ConsistentHashRing有以下代码行:

byte[] hash = hashGenerator.ComputeHash(BitConverter.GetBytes(node.GetHashCode()));
byte[] hash = hashGenerator.ComputeHash(Encoding.UTF8.GetBytes(node));
但在代码的另一部分中有这样一行:

byte[] hash = hashGenerator.ComputeHash(BitConverter.GetBytes(node.GetHashCode()));
byte[] hash = hashGenerator.ComputeHash(Encoding.UTF8.GetBytes(node));
所以当我把第一个改成

byte[] hash = hashGenerator.ComputeHash(Encoding.UTF8.GetBytes(node));

一切正常。我认为这是因为依赖于硬件的位转换器或GetHashCode方法。GitHub中的相关问题:

我建议您添加更多详细信息以获得回复。对于您的特定问题,我没有任何建议,但在一个集合中只存储一种类型的文档是DocumentDB反模式,听起来您可能正在这样做。建议的方法是在每个文档上使用_type=“user”字段或_isUser=true字段。