azure-cosmosdbazure-cosmosdb-changefeed

I want to increase maxdepth to 128 for cosmosjsondotnetserlializer in cosmos db trigger function


I get exception has been thrown by the change feed processor delegate. The reader's MaxDepth of 64 has been exceeded.

I am only looking for workaround to increase maxdepth 128 We cannot change payload structure.

Tried many things but nothing is helpful

Example document to reproduce

{"id": "foo", "a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":{"a":1}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}

Solution

  • This was a fairly recent change in response to this issue (which states that nesting levels of ~20K can cause StackOverFlow exceptions and high levels in general use more CPU/RAM). Unfortunately no new property was added to CosmosSerializationOptions to easily override the 64 selection if inadequate.

    As a POC I was able to get around this issue by changing my Startup.cs to the below

    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddSingleton<ICosmosDBSerializerFactory, MyCosmosDBSerializerFactory>();
        }
    
        public class MyCosmosDBSerializerFactory : ICosmosDBSerializerFactory
        {
            public CosmosSerializer CreateSerializer()
            {
                return new CosmosJsonDotNetSerializer(new CosmosSerializationOptions{ IgnoreNullValues = false});
            }
        }
    }
    

    And temporarily "borrowing" the code from the CosmosJsonDotNetSerializer class in the SDK and changing MaxDepth = 64 to 128.

    That code is "Copyright (c) Microsoft Corporation." however so I wouldn't want to just go ahead and use it in a project - but at least it shows one way of implementing the expected interface with Newtonsoft.Json that you can have a look at and learn from.

    I'm not sure if you need to actually supply any implementation of ToStream at all for the use case of deserializing change feed documents or can just use.

    throw new NotImplementedException(); 
    

    I didn't see it being called but my testing was extremely superficial.

    TBH it is probably quite unwelcome news that you even have to think about this when all you want to do is get your documents available in the function so maybe you should create a feedback request asking for this to be exposed via CosmosSerializationOptions.