-
Notifications
You must be signed in to change notification settings - Fork 279
Add MaxResponseSizeMB to runtime config. #2242
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
Merged
rohkhann
merged 14 commits into
main
from
dev/rohkhann/AddingInMaxResponseSizeToRuntimeConfig
Jun 5, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0040aa5
Add MaxResponse Size to runtime config.
rohkhann 51e4437
Adding maxResponseSizeMb to the runtimeconfig.
rohkhann 4168a3c
Fixing comments.
rohkhann 9b1c317
Nit fix on comments.
rohkhann fe5d13d
Merge branch 'main' into dev/rohkhann/AddingInMaxResponseSizeToRuntim…
rohkhann dcabcba
Fixing unit test issues.
rohkhann 0718513
Merge branch 'dev/rohkhann/AddingInMaxResponseSizeToRuntimeConfig' of…
rohkhann d6b3ef8
MaxResponseSizeMB
rohkhann 7cd7bde
Adding convertor for host options.
rohkhann 7134699
nit fixes.
rohkhann 68abba8
Fix recursing code.
rohkhann aed7642
Fix recursing code.
rohkhann c6aca6e
Addressing comments.
rohkhann 40e77c3
fixing nit comments.
rohkhann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Azure.DataApiBuilder.Config.ObjectModel; | ||
|
||
namespace Azure.DataApiBuilder.Config.Converters; | ||
|
||
/// <summary> | ||
/// Defines how DAB reads and writes host options. | ||
/// </summary> | ||
internal class HostOptionsConvertorFactory : JsonConverterFactory | ||
{ | ||
/// <inheritdoc/> | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
return typeToConvert.IsAssignableTo(typeof(HostOptions)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return new HostOptionsConverter(); | ||
} | ||
|
||
private class HostOptionsConverter : JsonConverter<HostOptions> | ||
{ | ||
/// <summary> | ||
/// Defines how DAB reads host options and defines which values are | ||
/// used to instantiate HostOptions. | ||
/// Uses default deserialize. | ||
/// </summary> | ||
/// <exception cref="JsonException">Thrown when improperly formatted host options are provided.</exception> | ||
public override HostOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
// Remove the converter so we don't recurse. | ||
JsonSerializerOptions jsonSerializerOptions = new(options); | ||
jsonSerializerOptions.Converters.Remove(jsonSerializerOptions.Converters.First(c => c is HostOptionsConvertorFactory)); | ||
return JsonSerializer.Deserialize<HostOptions>(ref reader, jsonSerializerOptions); | ||
} | ||
|
||
/// <summary> | ||
/// When writing the HostOptions back to a JSON file, only write the MaxResponseSizeMB property | ||
/// if the property is user provided. This avoids polluting the written JSON file with a property | ||
/// the user most likely ommitted when writing the original DAB runtime config file. | ||
/// This Write operation is only used when a RuntimeConfig object is serialized to JSON. | ||
/// </summary> | ||
public override void Write(Utf8JsonWriter writer, HostOptions value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("cors"); | ||
JsonSerializer.Serialize(writer, value.Cors, options); | ||
writer.WritePropertyName("authentication"); | ||
JsonSerializer.Serialize(writer, value.Authentication, options); | ||
writer.WritePropertyName("mode"); | ||
JsonSerializer.Serialize(writer, value.Mode, options); | ||
|
||
if (value?.UserProvidedMaxResponseSizeMB is true) | ||
{ | ||
writer.WritePropertyName("max-response-size-mb"); | ||
JsonSerializer.Serialize(writer, value.MaxResponseSizeMB, options); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net; | ||
using System.Text.Json.Serialization; | ||
using Azure.DataApiBuilder.Service.Exceptions; | ||
|
||
namespace Azure.DataApiBuilder.Config.ObjectModel; | ||
|
||
public record HostOptions(CorsOptions? Cors, AuthenticationOptions? Authentication, HostMode Mode = HostMode.Production); | ||
public record HostOptions | ||
{ | ||
/// <summary> | ||
/// Dab engine can at maximum handle 158 MB of data in a single response from a source. | ||
/// Json deserialization of a response into a string has a limit of 166,666,666 bytes which when converted to MB is 158 MB. | ||
/// ref: enforcing code: | ||
/// .net8: https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs#L80 | ||
/// .net6: https://github.com/dotnet/runtime/blob/v8.0.0/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs#75 | ||
/// ref: Json constant: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs#L80 | ||
/// </summary> | ||
private const int MAX_RESPONSE_LENGTH_DAB_ENGINE_MB = 158; | ||
|
||
/// <summary> | ||
/// Dab engine default response length. As of now this is same as max response length. | ||
/// </summary> | ||
private const int DEFAULT_RESPONSE_LENGTH_DAB_ENGINE_MB = 158; | ||
|
||
[JsonPropertyName("cors")] | ||
public CorsOptions? Cors { get; init; } | ||
|
||
[JsonPropertyName("authentication")] | ||
public AuthenticationOptions? Authentication { get; init; } | ||
|
||
[JsonPropertyName("mode")] | ||
public HostMode Mode { get; init; } | ||
|
||
[JsonPropertyName("max-response-size-mb")] | ||
public int? MaxResponseSizeMB { get; init; } = null; | ||
|
||
public HostOptions(CorsOptions? Cors, AuthenticationOptions? Authentication, HostMode Mode = HostMode.Production, int? MaxResponseSizeMB = null) | ||
{ | ||
this.Cors = Cors; | ||
this.Authentication = Authentication; | ||
this.Mode = Mode; | ||
this.MaxResponseSizeMB = MaxResponseSizeMB; | ||
|
||
if (this.MaxResponseSizeMB is not null) | ||
{ | ||
this.MaxResponseSizeMB = this.MaxResponseSizeMB == -1 ? MAX_RESPONSE_LENGTH_DAB_ENGINE_MB : (int)this.MaxResponseSizeMB; | ||
if (this.MaxResponseSizeMB < 1 || this.MaxResponseSizeMB > MAX_RESPONSE_LENGTH_DAB_ENGINE_MB) | ||
{ | ||
throw new DataApiBuilderException( | ||
message: $"{nameof(RuntimeConfig.Runtime.Host.MaxResponseSizeMB)} cannot be 0, exceed {MAX_RESPONSE_LENGTH_DAB_ENGINE_MB}MB or be less than -1", | ||
statusCode: HttpStatusCode.ServiceUnavailable, | ||
subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError); | ||
} | ||
|
||
UserProvidedMaxResponseSizeMB = true; | ||
} | ||
else | ||
{ | ||
this.MaxResponseSizeMB = DEFAULT_RESPONSE_LENGTH_DAB_ENGINE_MB; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Flag which informs CLI and JSON serializer whether to write MaxResponseSizeMB. | ||
/// property and value to the runtime config file. | ||
/// When user doesn't provide the MaxResponseSizeMB property/value or provides a null value, which signals DAB to use the default, | ||
/// the DAB CLI will not write the default value to a serialized config. | ||
/// This is because the user's intent is to use DAB's default value which could change | ||
/// and DAB CLI writing the property and value would lose the user's intent. | ||
/// This is because if the user were to use the CLI created config, MaxResponseSizeMB | ||
/// property/value specified would be interpreted by DAB as "user explicitly set MaxResponseSizeMB. | ||
/// UserProvidedMaxResponseSizeMB is true only when a user provides a non-null value | ||
/// </summary> | ||
[JsonIgnore(Condition = JsonIgnoreCondition.Always)] | ||
[MemberNotNullWhen(true, nameof(MaxResponseSizeMB))] | ||
public bool UserProvidedMaxResponseSizeMB { get; init; } = false; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.