2
votes

I have a .Net application who interact with a mongoDb database on CosmosDb.

I have a find request who looks like this :

public async Task<int?> GetCurrentZonierVersionNumberAsync(string productCode, string risqueType)
{
    if (string.IsNullOrEmpty(productCode) || string.IsNullOrEmpty(risqueType))
    {
        throw new ArgumentNullException();
    }

    var filter = Builders<Zonier>.Filter.And(
        Builders<Zonier>.Filter.Eq(x => x.ProductCode, productCode),
        Builders<Zonier>.Filter.Eq(c => c.RisqueType, risqueType));
    Zonier result = null;

    try
    {
        var query = _collection.Find(filter).SortByDescending(x => x.Version).Limit(1).FirstOrDefaultAsync();

        await _retryPolicy.ExecuteAsync(async () =>
        {
            result = await query;
        });
    }
    catch (TimeoutException ex)
    {
        throw new DataBaseReadingException(ex.Message, ExceptionCodeConstants.DataBaseReadingExceptionCode);
    }


    return result?.Version;
}

Unfortunately I get the following error message on azure :

Unhandled Exception: MongoDB.Driver.MongoCommandException: Command find failed: The provided cross partition query can not be directly served by the gateway. This is a first chance (internal) exception that all newer clients will know how to handle gracefully. This exception is traced, but unless you see it bubble up as an exception (which only happens on older SDK clients), then you can safely ignore this message.

I dont understand what is happening. Can someone please explain me the error message. I have the 2.4.4 version of the mongo driver. I'm aware I should update it, but I'm scared it will hide the issue without resolving it. Should I add this type of exception inside the retry policy of Polly ?

1
Updated to the last version of the mongodb driver and still got the same issue.Xavier W.
How is the table partitioned?James
There is a partition key on _id fieldXavier W.
see Partitioning in Azure Cosmos DB for a start.James
@James It helped me a lot to understand how it works. I have another issue but it seems to be not related to this topic. Thank you a lot !Xavier W.

1 Answers

0
votes

Since we have established that your Cosmos DB has millions of records, and is currently partitioned on _id (a field which you aren't including in your query) then this is going to result in a huge cross-partition query.

I'm not familiar with the error, however, it may be a resource-related error in that the query is too big to execute? The only way you could rule that out would be to perform a query that includes the PK or re-model your DB to use a PK thats more suited to the type of querying you are performing.

Furthermore, if you do a bit of digging on the error itself, there have been two separate occasions where the error itself has in fact been the result of a bug:

So worth investigating your toolset and making sure you are all up to date with SDK versions etc.