Botframework-sdk: Unable to set the TTL for CosmosDB userstate container

Created on 10 Jun 2020  路  4Comments  路  Source: microsoft/botframework-sdk

This is not for asking questions or filing bugs

To get help with an issue with your bot

To file an issue against a component please go to the components repo

Issue

When creating the CosmosDB container, there should be a way to set the TTL for the user state

Proposed change

Describe the proposed solution

Component Impact

Describe which components need to be updated

Customer Impact

Describe the impact on customers

Tracking Status

Dotnet SDK

  • [ ] PR
  • [ ] Merged

Javascript SDK

  • [ ] PR
  • [ ] Merged

Java SDK

  • [ ] PR
  • [ ] Merged

Python SDK

  • [ ] PR
  • [ ] Merged

Emulator

  • [ ] PR
  • [ ] Merged

Samples

  • [ ] PR
  • [ ] Merged

Docs

  • [ ] PR
  • [ ] Merged

Tools

  • [ ] PR
  • [ ] Merged

[dcr]

Bot Services customer-replied-to customer-reported

Most helpful comment

If the goal is for the sdk to set ttl on the container, one option is to use an initializer to ensure the database container has the desired Time To Live, before creating the IStorage:

public static class CosmosDbStorageInitializer 
{
    public static async Task<IStorage> GetStorageAsync(CosmosDbPartitionedStorageOptions options, int timeToLiveInSeconds = -1)
    {
        using (var client = new CosmosClient(
            options.CosmosDbEndpoint,
            options.AuthKey,
            options.CosmosClientOptions ?? new CosmosClientOptions()))
        {
            var containerResponse = await client
                .GetDatabase(options.DatabaseId)
                .DefineContainer(options.ContainerId, "/id")
                .WithDefaultTimeToLive(timeToLiveInSeconds)
                .WithIndexingPolicy().WithAutomaticIndexing(false).Attach()
                .CreateIfNotExistsAsync(options.ContainerThroughput)
                .ConfigureAwait(false);
        }

        return new CosmosDbPartitionedStorage(options);
    }
} 

All 4 comments

@nmishr At one point, we decided not to support this in the Bot Framework SDK since it just adds additional complexity that very few customers are asking for. However, you can set TTL manually after the SDK creates the container.

If the goal is for the sdk to set ttl on the container, one option is to use an initializer to ensure the database container has the desired Time To Live, before creating the IStorage:

public static class CosmosDbStorageInitializer 
{
    public static async Task<IStorage> GetStorageAsync(CosmosDbPartitionedStorageOptions options, int timeToLiveInSeconds = -1)
    {
        using (var client = new CosmosClient(
            options.CosmosDbEndpoint,
            options.AuthKey,
            options.CosmosClientOptions ?? new CosmosClientOptions()))
        {
            var containerResponse = await client
                .GetDatabase(options.DatabaseId)
                .DefineContainer(options.ContainerId, "/id")
                .WithDefaultTimeToLive(timeToLiveInSeconds)
                .WithIndexingPolicy().WithAutomaticIndexing(false).Attach()
                .CreateIfNotExistsAsync(options.ContainerThroughput)
                .ConfigureAwait(false);
        }

        return new CosmosDbPartitionedStorage(options);
    }
} 

I have received the suggestion about adding reference to TTL in the docs as part of the R10 docs pillar. Along with that, we have another open issue for ensuring we call out that the newer partitioned storage provider doesn't create the database for you. I think both items could be referenced in an update to the existing cosmos db docs, adding links to the Cosmos TTL docs and also referencing the option Eric specified above.

I'm going to close this issue as there are now two workarounds and there's a new docs issue that will address this for the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hailiang-wang picture hailiang-wang  路  3Comments

akakoychenko picture akakoychenko  路  3Comments

peterbozso picture peterbozso  路  3Comments

mattlanham picture mattlanham  路  3Comments

vaditya04 picture vaditya04  路  3Comments