Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmos DB: Enable TTL on lease container creation #865

Merged
merged 3 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/WebJobs.Extensions.CosmosDB/CosmosDBUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ await CreateDatabaseAndCollectionIfNotExistAsync(context.Service, context.Resolv
context.ResolvedAttribute.PartitionKey, context.ResolvedAttribute.ContainerThroughput);
}

internal static async Task CreateDatabaseAndCollectionIfNotExistAsync(CosmosClient service, string databaseName, string containerName, string partitionKey, int throughput)
internal static async Task CreateDatabaseAndCollectionIfNotExistAsync(CosmosClient service, string databaseName, string containerName, string partitionKey, int throughput, bool setTTL = false)
{
await service.CreateDatabaseIfNotExistsAsync(databaseName);

Expand All @@ -59,7 +59,23 @@ internal static async Task CreateDatabaseAndCollectionIfNotExistAsync(CosmosClie
}
catch (CosmosException cosmosException) when (cosmosException.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await database.CreateContainerAsync(containerName, partitionKey, desiredThroughput);
ContainerProperties containerProperties = new ContainerProperties()
{
Id = containerName
};

if (!string.IsNullOrEmpty(partitionKey))
{
containerProperties.PartitionKeyPath = partitionKey;
}

if (setTTL)
{
// Enabling TTL on the container without any defined time. TTL is set on the individual items.
containerProperties.DefaultTimeToLive = -1;
}

await database.CreateContainerAsync(containerProperties, desiredThroughput);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ private static async Task CreateLeaseCollectionIfNotExistsAsync(CosmosClient cos
{
try
{
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(cosmosClient, databaseName, collectionName, LeaseCollectionRequiredPartitionKey, throughput);
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(cosmosClient, databaseName, collectionName, LeaseCollectionRequiredPartitionKey, throughput, setTTL: true);
}
catch (CosmosException cosmosException)
when (cosmosException.StatusCode == System.Net.HttpStatusCode.BadRequest
&& cosmosException.Message.Contains("invalid for Gremlin API"))
{
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(cosmosClient, databaseName, collectionName, LeaseCollectionRequiredPartitionKeyFromGremlin, throughput);
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(cosmosClient, databaseName, collectionName, LeaseCollectionRequiredPartitionKeyFromGremlin, throughput, setTTL: true);
}
}

Expand Down
8 changes: 3 additions & 5 deletions test/WebJobs.Extensions.CosmosDB.Tests/CosmosDBTestUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class CosmosDBTestUtility
public const string DatabaseName = "ItemDB";
public const string ContainerName = "ItemCollection";

public static Mock<Container> SetupCollectionMock(Mock<CosmosClient> mockService, Mock<Database> mockDatabase, string partitionKeyPath = null, int throughput = 0)
public static Mock<Container> SetupCollectionMock(Mock<CosmosClient> mockService, Mock<Database> mockDatabase, string partitionKeyPath, int throughput = 0, bool setTTL = false)
{
var mockContainer = new Mock<Container>(MockBehavior.Strict);

Expand All @@ -46,8 +46,7 @@ public static Mock<Container> SetupCollectionMock(Mock<CosmosClient> mockService
if (throughput == 0)
{
mockDatabase
.Setup(m => m.CreateContainerAsync(It.Is<string>(i => i == ContainerName),
It.Is<string>(p => p == partitionKeyPath),
.Setup(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.Id == ContainerName && cp.PartitionKeyPath == partitionKeyPath && (!setTTL || cp.DefaultTimeToLive == -1)),
It.Is<int?>(t => t == null),
It.IsAny<RequestOptions>(),
It.IsAny<CancellationToken>()))
Expand All @@ -56,8 +55,7 @@ public static Mock<Container> SetupCollectionMock(Mock<CosmosClient> mockService
else
{
mockDatabase
.Setup(m => m.CreateContainerAsync(It.Is<string>(i => i == ContainerName),
It.Is<string>(p => p == partitionKeyPath),
.Setup(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.Id == ContainerName && cp.PartitionKeyPath == partitionKeyPath && (!setTTL || cp.DefaultTimeToLive == -1)),
It.Is<int?>(t => t == throughput),
It.IsAny<RequestOptions>(),
It.IsAny<CancellationToken>()))
Expand Down
36 changes: 34 additions & 2 deletions test/WebJobs.Extensions.CosmosDB.Tests/CosmosDBUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ public async Task CreateIfNotExists_DoesNotSetThroughput_IfZero()
// Arrange
var mockService = new Mock<CosmosClient>(MockBehavior.Strict);
var context = CosmosDBTestUtility.CreateContext(mockService.Object, throughput: 0);
CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService));
CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService), "/id");

// Act
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);

// Assert
mockService.VerifyAll();
}

[Fact]
public async Task CreateIfNotExists_SetsThroughput()
{
// Arrange
int throughput = 1000;
var mockService = new Mock<CosmosClient>(MockBehavior.Strict);
var context = CosmosDBTestUtility.CreateContext(mockService.Object, throughput: throughput);
CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService), "/id", throughput: throughput);

// Act
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);
Expand All @@ -46,13 +62,29 @@ public async Task CreateIfNotExists_SetsPartitionKey_IfSpecified()
mockService.VerifyAll();
}

[Fact]
public async Task CreateIfNotExists_SetsTTL_IfSpecified()
{
// Arrange
string partitionKeyPath = "partitionKey";
var mockService = new Mock<CosmosClient>(MockBehavior.Strict);

CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService), partitionKeyPath, setTTL: true);

// Act
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(mockService.Object, CosmosDBTestUtility.DatabaseName, CosmosDBTestUtility.ContainerName, partitionKeyPath, throughput: 0, setTTL: true);

// Assert
mockService.VerifyAll();
}

[Fact]
public async Task CreateIfNotExist_Succeeds()
{
// Arrange
var mockService = new Mock<CosmosClient>(MockBehavior.Strict);
CosmosDBContext context = CosmosDBTestUtility.CreateContext(mockService.Object);
CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService));
CosmosDBTestUtility.SetupCollectionMock(mockService, CosmosDBTestUtility.SetupDatabaseMock(mockService), null);

// Act
await CosmosDBUtility.CreateDatabaseAndCollectionIfNotExistAsync(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public async Task ValidCreateIfNotExists(ParameterInfo parameter)
.ThrowsAsync(new CosmosException("not found", System.Net.HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), 0));

mockDatabase
.Setup(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.Setup(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/id" && cp.DefaultTimeToLive == -1), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Mock.Of<ContainerResponse>());

var factoryMock = new Mock<ICosmosDBServiceFactory>(MockBehavior.Strict);
Expand All @@ -362,7 +362,7 @@ public async Task ValidCreateIfNotExists(ParameterInfo parameter)
CosmosDBTriggerAttribute cosmosDBTriggerAttribute = parameter.GetCustomAttribute<CosmosDBTriggerAttribute>(inherit: false);

mockDatabase
.Verify(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);
.Verify(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/id" && cp.DefaultTimeToLive == -1), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Theory]
Expand Down Expand Up @@ -401,11 +401,11 @@ public async Task ValidCreateIfNotExistsForGremlin(ParameterInfo parameter)
.ThrowsAsync(new CosmosException("not found", System.Net.HttpStatusCode.NotFound, 0, Guid.NewGuid().ToString(), 0));

mockDatabase
.Setup(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.Setup(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new CosmosException("invalid for Gremlin API", System.Net.HttpStatusCode.BadRequest, 0, Guid.NewGuid().ToString(), 0));

mockDatabase
.Setup(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/partitionKey"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.Setup(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/partitionKey"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Mock.Of<ContainerResponse>());

var factoryMock = new Mock<ICosmosDBServiceFactory>(MockBehavior.Strict);
Expand All @@ -420,10 +420,10 @@ public async Task ValidCreateIfNotExistsForGremlin(ParameterInfo parameter)
CosmosDBTriggerAttribute cosmosDBTriggerAttribute = parameter.GetCustomAttribute<CosmosDBTriggerAttribute>(inherit: false);

mockDatabase
.Verify(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);
.Verify(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);

mockDatabase
.Verify(m => m.CreateContainerAsync(It.IsAny<string>(), It.Is<string>(pk => pk == "/partitionKey"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);
.Verify(m => m.CreateContainerAsync(It.Is<ContainerProperties>(cp => cp.PartitionKeyPath == "/id"), It.IsAny<int?>(), It.IsAny<RequestOptions>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Theory]
Expand Down