Skip to content

Fix SQL container throughput update to preserve existing throughput buckets #28378

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

Open
wants to merge 3 commits into
base: Az.CosmosDB-preview
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions src/CosmosDB/CosmosDB.Test/ScenarioTests/SqlOperationsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,23 @@ public void TestSqlThroughputCmdlets()

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSqlThroughputBucketsCmdlets()
public void TestSqlThroughputBucketsManualCmdlets()
{
TestRunner.RunTestScript("Test-SqlThroughputBucketsCmdlets");
TestRunner.RunTestScript("Test-SqlThroughputBucketsCmdlets-ManualContainer");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSqlThroughputBucketsAutoscaleCmdlets()
{
TestRunner.RunTestScript("Test-SqlThroughputBucketsCmdlets-AutoscaleContainer");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestSqlThroughputBucketsMigrationCmdlets()
{
TestRunner.RunTestScript("Test-SqlThroughputBucketsCmdlets-Migration");
}

[Fact]
Expand Down
218 changes: 149 additions & 69 deletions src/CosmosDB/CosmosDB.Test/ScenarioTests/SqlOperationsTests.ps1

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/CosmosDB/CosmosDB/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Added support for preserving existing throughput buckets during SQL container throughput updates.

## Version 2.0.7
* Added support for throughput buckets.
Expand Down
35 changes: 21 additions & 14 deletions src/CosmosDB/CosmosDB/Helpers/ThroughputHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,23 @@ public static ThroughputSettingsUpdateParameters CreateThroughputSettingsObject(
AutoscaleMaxThroughput = autoscaleMaxThroughput;

ThroughputHelper.ThroughputValidation(validateBothPresent:true);
List<ThroughputBucketResource> buckets = ToBucketList(throughputBucketsObject);
ThroughputSettingsUpdateParameters throughputSettingsUpdateParameters = new ThroughputSettingsUpdateParameters();

if (throughput != null)
{
throughputSettingsUpdateParameters.Resource = new ThroughputSettingsResource
{
Throughput = throughput.Value,
ThroughputBuckets = throughputBucketsObject != null && throughputBucketsObject.Length > 0
? new List<ThroughputBucketResource>(throughputBucketsObject.Select(t => new ThroughputBucketResource
{
Id = t.Id,
MaxThroughputPercentage = t.MaxThroughputPercentage
}))
: null
ThroughputBuckets = buckets
};
}
else if (autoscaleMaxThroughput != null)
{
throughputSettingsUpdateParameters.Resource = new ThroughputSettingsResource
{
AutoscaleSettings = new AutoscaleSettingsResource { MaxThroughput = autoscaleMaxThroughput.Value },
ThroughputBuckets = throughputBucketsObject != null && throughputBucketsObject.Length > 0
? new List<ThroughputBucketResource>(throughputBucketsObject.Select(t => new ThroughputBucketResource
{
Id = t.Id,
MaxThroughputPercentage = t.MaxThroughputPercentage
}))
: null
ThroughputBuckets = buckets
};
}

Expand Down Expand Up @@ -97,5 +87,22 @@ private static void ThroughputValidation(bool validateBothPresent = false)
return;
}

private static List<ThroughputBucketResource> ToBucketList(PSThroughputBucket[] throughputBucketsObject)
{
if (throughputBucketsObject == null)
{
return null;
}
if (throughputBucketsObject.Length == 0)
{
return new List<ThroughputBucketResource>();
}
return new List<ThroughputBucketResource>(throughputBucketsObject.Select(t => new ThroughputBucketResource
{
Id = t.Id,
MaxThroughputPercentage = t.MaxThroughputPercentage
}));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// ----------------------------------------------------------------------------------

using System.Management.Automation;
using System.Linq;
using Microsoft.Azure.Commands.CosmosDB.Models;
using Microsoft.Azure.Commands.CosmosDB.Helpers;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
Expand Down Expand Up @@ -65,5 +66,19 @@ public override void CallSDKMethod(ThroughputSettingsUpdateParameters throughput
}
}

protected override PSThroughputBucket[] GetExistingThroughputBuckets()
{
// Read current throughput to preserve existing buckets when user omits the parameter
ThroughputSettingsGetResults current = CosmosDBManagementClient.SqlResources
.GetSqlContainerThroughputWithHttpMessagesAsync(ResourceGroupName, AccountName, DatabaseName, Name)
.GetAwaiter().GetResult().Body;

System.Collections.Generic.IList<ThroughputBucketResource> buckets = current?.Resource?.ThroughputBuckets;
if (buckets == null)
{
return null;
}
return buckets.Select(b => new PSThroughputBucket(b)).ToArray();
}
}
}
9 changes: 8 additions & 1 deletion src/CosmosDB/CosmosDB/UpdateAzCosmosDBThroughput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ public override void ExecuteCmdlet()
PopulateFromInputObject();
}

ThroughputSettingsUpdateParameters throughputSettingsUpdateParameters = ThroughputHelper.CreateThroughputSettingsObject(Throughput, AutoscaleMaxThroughput, ThroughputBucketsObject);
// Determine effective throughput buckets:
// - If user omitted the parameter (null), fetch and preserve existing buckets via hook
// - If user passed empty array, clear buckets by sending an empty list
// - If user passed values, replace with those values
PSThroughputBucket[] effectiveThroughputBuckets = ThroughputBucketsObject ?? GetExistingThroughputBuckets();

ThroughputSettingsUpdateParameters throughputSettingsUpdateParameters = ThroughputHelper.CreateThroughputSettingsObject(Throughput, AutoscaleMaxThroughput, effectiveThroughputBuckets);

CallSDKMethod(throughputSettingsUpdateParameters);
}

public virtual void PopulateFromParentObject() { }
public virtual void PopulateFromInputObject() { }
public virtual void CallSDKMethod(ThroughputSettingsUpdateParameters throughputSettingsUpdateParameters) { }
protected virtual PSThroughputBucket[] GetExistingThroughputBuckets() { return null; }

}
}
Loading