2

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 ?

Stennie
  • 63,885
  • 14
  • 149
  • 175
Xavier W.
  • 1,270
  • 3
  • 21
  • 47
  • Updated to the last version of the mongodb driver and still got the same issue. – Xavier W. Jul 16 '19 at 15:28
  • How is the table partitioned? – James Jul 18 '19 at 12:46
  • There is a partition key on `_id` field – Xavier W. Jul 18 '19 at 13:10
  • that's gonna be a _real_ issue as this grows, how many expected records? – James Jul 18 '19 at 13:20
  • few millions. The DBA from my company forced me to use the _id partition key. Is it the reason why I got this issue ? Should I give it a try with another key ? – Xavier W. Jul 18 '19 at 13:51
  • well the reason I ask is because it looks your query is resulting in a cross-partition query, and given your data is partitioned by `_id` that makes total sense. And given the number of _logical_ partitions it has to check (in your case, millions) I'd say this is more than likely the problem. – James Jul 18 '19 at 14:12
  • Ok well I have a partition key who makes more sense for me. I will give it a try and told you how it is going.Do you have some doc about how to chose a good partition key and about how bad `_id` is as partition key so I can share it with the DBA ? – Xavier W. Jul 18 '19 at 14:26
  • 1
    see [Partitioning in Azure Cosmos DB](https://learn.microsoft.com/en-us/azure/cosmos-db/partitioning-overview) for a start. – James Jul 18 '19 at 14:39
  • Changed my partitionkey, delete my collection. I got the same message and my collection is empty.. `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.` – Xavier W. Jul 18 '19 at 14:44
  • so what PK are you using now and are you actually including it in the query? – James Jul 18 '19 at 14:45
  • Indeed it was not included in the query. Every query must include the `partitionkey` or it will be an error otherwise ? `mongod` used to scan all node if a request doesn't include the `partition key` – Xavier W. Jul 18 '19 at 14:48
  • if querying across _millions_ of records, then yeah, you're gonna want to be querying with a PK otherwise you need to scan all the logical partitions. I'm not saying it _has_ to be that way, but I imagine that's why the exception is being thrown, it looks like it's basically saying it can't serve the request you are asking for. Also, what sort of RU/s are we talking about? If you check your logs for those non-PK requests, you should see they'll be spiking – James Jul 18 '19 at 14:55
  • Please reach out to askcomosmongoapi [at] microsoft [dot] com so that the product team can help you. – Siddhesh Vethe Jul 19 '19 at 17:21
  • @James can you make a response, so I give you the bounty – Xavier W. Jul 23 '19 at 08:17
  • @XavierW. sure I can write an answer, did the PK rework help sort your issue? – James Jul 23 '19 at 09:09
  • 1
    @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. Jul 23 '19 at 09:18
  • @XavierW. excellent, glad I could help, just writing an answer as we speak. – James Jul 23 '19 at 09:19

1 Answers1

0

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.

James
  • 80,725
  • 18
  • 167
  • 237