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

[MetricsAdvisor] Overwriting patch serialization methods to support sending null #22457

Merged
merged 7 commits into from
Jul 6, 2021

Conversation

kinelski
Copy link
Member

@kinelski kinelski commented Jul 6, 2021

Part of #21509.

The service allows sending null during an Update operation to reset a property to its default value. So an Update has the three possible scenarios for each property:

  • Send a value: the value will be updated.
  • Do not send a value: the value will be left as it is.
  • Send null: the property will be reset to its default value.

CodeGen does not support this differentiation. Currently, our code only supports the first two scenarios. If a property is null, we'll simply not send it. This prevents users from resetting settings to default but, most important, it prevents users from removing configurations. For example, we have the following class in one of our configuration APIs:

public class MetricWholeSeriesDetectionCondition
{
   public SmartDetectionCondition SmartDetectionCondition { get; set; }

   public HardThresholdCondition HardThresholdCondition { get; set; }
}

Each property adds a new behavior to the configuration. You need to specify at least one of them, and both can be set for the same config. Let's say the customer does not want to use the HardThresholdCondition anymore. Naturally, they would do:

var config = client.GetConfig(...);

config.WholeSeriesDetectionCondition.HardThresholdCondition = null;
client.UpdateConfig(config);

However, since CodeGen does not send the property when it's null, we're not sending anything to the service, and they have no way of knowing we want to remove this behavior.

This PR fixes this behavior for Data Feeds only (next PRs will contain other classes). Implementation details explained in the comments.

@kinelski kinelski added the Client This issue points to a problem in the data-plane of the library. label Jul 6, 2021
@kinelski kinelski self-assigned this Jul 6, 2021
Comment on lines -25 to -29
if (Optional.IsDefined(DataFeedName))
{
writer.WritePropertyName("dataFeedName");
writer.WriteStringValue(DataFeedName);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see, the serialization method does not send null to the service. Unfortunately, we need to overwrite all serialization methods for Patch models (used during Update operations only) in order to send null when a property is not present.


namespace Azure.AI.MetricsAdvisor.Models
{
internal partial class DataFeedDetailPatch : IUtf8JsonSerializable
Copy link
Member Author

@kinelski kinelski Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to another folder (Models/CodeGenInternal/PatchModels)

writer.WritePropertyName("dataSourceParameter");
writer.WriteObjectValue(DataSourceParameter);
}
SerializeCommonProperties(writer);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All <...>DataFeedPatch models, including this one, are subclasses of DataFeedDetailPatch. I put serialization of all properties of the base class in SerializeCommonProperties to avoid repeating (a lot of) code, specially since we have 13 subtypes.

Comment on lines +18 to +22
if (Optional.IsDefined(ApiKey))
{
writer.WritePropertyName("apiKey");
writer.WriteStringValue(ApiKey);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not sending null for secrets, though. There's a good reason for this. The service does not return secrets to us, so this would happen:

var dataFeed = client.GetDataFeed(...);

// Here, dataFeed.DataSource.ApiKey is null because it was not returned by the service.

// Update some properties here

// This would send apiKey = null to the service, even though we didn't touch it.
client.UpdateDataFeed(dataFeed);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and will that cause an error in the service? if this was allowed before, would it be better to add an entry to the changelog saying how this "bug" got fixed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and will that cause an error in the service?

Correct. We get back an error message saying the secret is required.

if this was allowed before, would it be better to add an entry to the changelog saying how this "bug" got fixed?

We were not overwriting the serialization method before, so we were not sending null. My point in this comment is "let's keep the ApiKey behaving as it already was. If we change it, it breaks".


namespace Azure.AI.MetricsAdvisor
{
internal static class Utf8JsonWriterExtensions
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convenience methods to make serialization code shorter. We can reduce:

if (value != null)
{
    writer.WritePropertyName(...);
    writer.Write<Something>Value(...);
}
else
{
  writer.WriteNull(propertyName);
}

to a single line.


string dataFeedName = Recording.GenerateAlphaNumericId("dataFeed");
DataFeedSource dataSource = CreateMockDataFeedSource(dataSourceKind);
var dataFeedToCreate = new DataFeed()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set initial values for required properties and for anything that can be updated.

Comment on lines +1513 to +1524
dataFeedToUpdate.IngestionSettings.IngestionStartOffset = null;
dataFeedToUpdate.IngestionSettings.IngestionRetryDelay = null;
dataFeedToUpdate.IngestionSettings.StopRetryAfter = null;
dataFeedToUpdate.IngestionSettings.DataSourceRequestConcurrency = null;
dataFeedToUpdate.Schema.TimestampColumn = null;
dataFeedToUpdate.Description = null;
dataFeedToUpdate.RollupSettings.RollupType = null;
dataFeedToUpdate.RollupSettings.AutoRollupMethod = null;
dataFeedToUpdate.RollupSettings.RollupIdentificationValue = null;
dataFeedToUpdate.MissingDataPointFillSettings = null;
dataFeedToUpdate.AccessMode = null;
dataFeedToUpdate.ActionLinkTemplate = null;
Copy link
Member Author

@kinelski kinelski Jul 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set everything that can be updated to null to restore the default values.

Comment on lines +1528 to +1539
Assert.That(updatedDataFeed.IngestionSettings.IngestionStartOffset, Is.EqualTo(TimeSpan.Zero));
Assert.That(updatedDataFeed.IngestionSettings.IngestionRetryDelay, Is.EqualTo(TimeSpan.FromSeconds(-1)));
Assert.That(updatedDataFeed.IngestionSettings.StopRetryAfter, Is.EqualTo(TimeSpan.FromSeconds(-1)));
Assert.That(updatedDataFeed.IngestionSettings.DataSourceRequestConcurrency, Is.EqualTo(-1));
Assert.That(updatedDataFeed.Schema.TimestampColumn, Is.Empty);
Assert.That(updatedDataFeed.Description, Is.Empty);
Assert.That(updatedDataFeed.RollupSettings.RollupType, Is.EqualTo(DataFeedRollupType.NoRollupNeeded));
Assert.That(updatedDataFeed.RollupSettings.AutoRollupMethod, Is.EqualTo(DataFeedAutoRollupMethod.None));
Assert.That(updatedDataFeed.RollupSettings.RollupIdentificationValue, Is.Null);
Assert.That(updatedDataFeed.MissingDataPointFillSettings.FillType, Is.EqualTo(DataFeedMissingDataPointFillType.SmartFilling));
Assert.That(updatedDataFeed.AccessMode, Is.EqualTo(DataFeedAccessMode.Private));
Assert.That(updatedDataFeed.ActionLinkTemplate, Is.Empty);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if default values are correct.

}

[RecordedTest]
public async Task UpdateAzureBlobDataFeedAuthenticationWithNullSetsToDefault()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following tests handle properties specific to each data feed source. The test above was for common properties only.

@kinelski kinelski requested a review from maririos July 6, 2021 08:42
@check-enforcer
Copy link

check-enforcer bot commented Jul 6, 2021

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them. In order to bootstrap pipelines for a new service, please perform following steps:

For data-plane/track 2 SDKs Issue the following command as a pull request comment:

/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run net - [service] - ci

For track 1 management-plane SDKs

Please open a separate PR and to your service SDK path in this file. Once that PR has been merged, you can re-run the pipeline to trigger the verification.

Copy link
Member

@maririos maririos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a changelog entry and some samples will be good to showcase this new feature to the user

Comment on lines +18 to +22
if (Optional.IsDefined(ApiKey))
{
writer.WritePropertyName("apiKey");
writer.WriteStringValue(ApiKey);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and will that cause an error in the service? if this was allowed before, would it be better to add an entry to the changelog saying how this "bug" got fixed?

{
internal static class Utf8JsonWriterExtensions
{
public static void WriteNullStringValue(this Utf8JsonWriter writer, string propertyName, string value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really liked the suggestion. Created an issue to track it (#22472). Won't update the code now since it's only an internal change and it may affect 4 open PRs right now.

@kinelski
Copy link
Member Author

kinelski commented Jul 6, 2021

Looks like a changelog entry and some samples will be good to showcase this new feature to the user

Added a changelog entry in the New Features section to highlight it. Wasn't sure where to put it, since technically it's a breaking change. Setting a property to null in an Update operation before this change would make literally no change in the service, so I don't expect to have customers actually affected by this.

Regarding samples, I made a small addition to the UpdateDataFeedAsync code sample we have after your suggestion:

// Some properties, such as IngestionStartOffset, can be reset to their default value
// when set to null during an Update operation. Check the API documentation to verify
// when a property supports this feature.

dataFeed.IngestionSettings.IngestionStartOffset = null;

response = await adminClient.UpdateDataFeedAsync(dataFeed);
// ... Print new value

Do you think it may be worth adding more properties to the sample?

I'm going through all docstrings in the library and fixing anything that sounds off or poor written. I'm also adding remarks pointing out when a property can be reset to its default value during an Update operation.

The Update scenario is not one of our core scenarios for this library, so this is not a big change from the customers' point of view (even though it gave us so much trouble in terms of code). Update and management operations in general usually are a single-time event, so we expect users to do this through the portal more often.

@maririos
Copy link
Member

maririos commented Jul 6, 2021

Do you think it may be worth adding more properties to the sample?

Not all of them but make sure that in the more advanced samples this is showcased.
The addition to the docstrings I think is more important so hopefully with that user can figure out this behavior.

If, after release, we hear from the community that they need more documentation, then you can add more specific samples

@kinelski kinelski merged commit aa319b4 into Azure:camaiaor/ma-beta5 Jul 6, 2021
@kinelski kinelski deleted the ma-patchDataFeed branch July 6, 2021 20:56
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
azure-sdk pushed a commit to azure-sdk/azure-sdk-for-net that referenced this pull request Feb 22, 2023
Networking 2022-09-01 release (Azure#22639)

* Adds base for updating Microsoft.Network from version stable/2022-07-01 to version 2022-09-01

* Updates readme

* Updates API version in new specs and examples

* Added flowlog property in virtual network (Azure#21790)

Co-authored-by: Krishna Mishra <krmishr@microsoft.com>

* commit1 (Azure#22111)

Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>

* adding auth status property to circuit (Azure#22024)

* Make auth status readonly (Azure#22365)

* make auth status read only

* fixing model validation

* prettier fix

* Add support for State flag in Custom Rule (Azure#22457)

* Fix LRO header model validation (Azure#22506)

* Add new status code for application gateway custom error page (Azure#22151)

* Add new status code for application gateway custom error page

* Fix prettier

* Adding words to Custom-Words list

* Fix missing resource id in application gateway list example (Azure#22509)

* Resolving merge conflicts with main branch

---------

Co-authored-by: Mikhail <mitryakh@microsoft.com>
Co-authored-by: KRISHNA MISHRA <krishmi139@gmail.com>
Co-authored-by: Krishna Mishra <krmishr@microsoft.com>
Co-authored-by: Khushboo Baheti <37917868+Khushboo-Baheti@users.noreply.github.com>
Co-authored-by: Khushboo Baheti <khbaheti@microsoft.com>
Co-authored-by: utbarn-ms <66377251+utbarn-ms@users.noreply.github.com>
Co-authored-by: tejasshah7 <49326906+tejasshah7@users.noreply.github.com>
Co-authored-by: Prateek Sachan <42961174+prateek2211@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Metrics Advisor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants