Skip to content

Commit cbb735e

Browse files
committed
Code changes to create a client option to enable or disable the request/ response stream conversation.
1 parent 2687d7b commit cbb735e

3 files changed

Lines changed: 141 additions & 2 deletions

File tree

Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ public System.Text.Json.JsonSerializerOptions UseSystemTextJsonSerializerWithOpt
452452
/// The default value is 'true'.
453453
/// </summary>
454454
internal bool EnableAsyncCacheExceptionNoSharing { get; set; } = true;
455+
456+
/// <summary>
457+
/// Gets or sets the boolean flag to convert a text stream to binary and vice versa. When enabled, the request and response stream
458+
/// would be converted to the desired target serialization type. This client option will remain internal only since the consumer of
459+
/// this flag will be the internal components of the cosmos db ecosystem. The default value for this parameter is 'true'.
460+
/// </summary>
461+
internal bool EnableStreamConversationForBinaryEncoding { get; set; } = true;
455462

456463
/// <summary>
457464
/// (Direct/TCP) Controls the amount of idle time after which unused connections are closed.

Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,8 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
934934
// engine, which does not support binary encoded content at the moment. For long term, since trigger operations won't
935935
// be supported in the backend, avoiding the binary encoding in such cases, will be the ideal approach.
936936
if (ConfigurationManager.IsBinaryEncodingEnabled()
937-
&& !ContainerCore.IsTriggerPresentInRequestOptions(requestOptions))
937+
&& !ContainerCore.IsTriggerPresentInRequestOptions(requestOptions)
938+
&& this.ClientContext.ClientOptions.EnableStreamConversationForBinaryEncoding)
938939
{
939940
streamPayload = CosmosSerializationUtil.TrySerializeStreamToTargetFormat(
940941
targetSerializationFormat: ContainerCore.GetTargetRequestSerializationFormat(),
@@ -957,7 +958,8 @@ private async Task<ResponseMessage> ProcessItemStreamAsync(
957958
cancellationToken: cancellationToken);
958959

959960
// Convert Binary Stream to Text.
960-
if (targetResponseSerializationFormat.HasValue
961+
if (this.ClientContext.ClientOptions.EnableStreamConversationForBinaryEncoding
962+
&& targetResponseSerializationFormat.HasValue
961963
&& (requestOptions == null || !requestOptions.EnableBinaryResponseOnPointOperations)
962964
&& responseMessage?.Content is CloneableStream outputCloneableStream)
963965
{

Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosItemTests.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,136 @@ public async Task CreateDropItemStreamTest(bool binaryEncodingEnabledInClient, b
768768
{
769769
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);
770770
}
771+
}
772+
773+
[TestMethod]
774+
[Owner("dkunda")]
775+
[DataRow(true, true, DisplayName = "Test scenario when binary encoding is enabled at client level and stream conversation for binary encoding is enabled.")]
776+
[DataRow(true, false, DisplayName = "Test scenario when binary encoding is enabled at client level and stream conversation for binary encoding is disabled.")]
777+
[DataRow(false, true, DisplayName = "Test scenario when binary encoding is disabled at client level and stream conversation for binary encoding is enabled.")]
778+
[DataRow(false, false, DisplayName = "Test scenario when binary encoding is disabled at client level and stream conversation for binary encoding is disabled.")]
779+
public async Task CreateItemStream_WithEnableBinaryResponseOptions_ShouldSkipStreamConversation(
780+
bool binaryEncodingEnabledInClient,
781+
bool enableStreamConversationForBinaryEncoding)
782+
{
783+
Cosmos.Database database = null;
784+
Container container = null;
785+
try
786+
{
787+
string databaseName = "binary-encoding-db";
788+
string containerName = "binary-encoding-container";
789+
if (binaryEncodingEnabledInClient)
790+
{
791+
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True");
792+
}
793+
794+
(string endpoint, string authKey) = TestCommon.GetAccountInfo();
795+
CosmosClientOptions clientOptions = new CosmosClientOptions()
796+
{
797+
EnableStreamConversationForBinaryEncoding = enableStreamConversationForBinaryEncoding,
798+
};
799+
800+
CosmosClient cosmosClient = new (
801+
endpoint,
802+
authKey,
803+
clientOptions);
804+
805+
DatabaseResponse dbResponse = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);
806+
database = dbResponse.Database;
807+
808+
ContainerProperties properties = new (id: containerName, partitionKeyPath: "/pk");
809+
container = await database.CreateContainerIfNotExistsAsync(properties);
810+
811+
ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();
812+
CosmosSerializerCore cosmosSerializer = new CosmosSerializerCore();
813+
using (Stream stream = cosmosSerializer.ToStream<ToDoActivity>(
814+
testItem,
815+
canUseBinaryEncodingForPointOperations: binaryEncodingEnabledInClient))
816+
{
817+
if (binaryEncodingEnabledInClient)
818+
{
819+
AssertOnResponseSerializationBinaryType(stream);
820+
}
821+
else
822+
{
823+
AssertOnResponseSerializationTextType(stream);
824+
}
825+
826+
using (ResponseMessage response = await container.CreateItemStreamAsync(
827+
streamPayload: stream,
828+
partitionKey: new Cosmos.PartitionKey(testItem.pk)))
829+
{
830+
Assert.IsNotNull(response);
831+
Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
832+
Assert.IsTrue(response.Headers.RequestCharge > 0);
833+
Assert.IsNotNull(response.Headers.ActivityId);
834+
Assert.IsNotNull(response.Headers.ETag);
835+
Assert.IsNotNull(response.Diagnostics);
836+
Assert.IsTrue(!string.IsNullOrEmpty(response.Diagnostics.ToString()));
837+
Assert.IsTrue(response.Diagnostics.GetClientElapsedTime() > TimeSpan.Zero);
838+
839+
if (enableStreamConversationForBinaryEncoding)
840+
{
841+
AssertOnResponseSerializationTextType(response.Content);
842+
}
843+
else
844+
{
845+
if (binaryEncodingEnabledInClient)
846+
{
847+
AssertOnResponseSerializationBinaryType(response.Content);
848+
}
849+
else
850+
{
851+
AssertOnResponseSerializationTextType(response.Content);
852+
}
853+
}
854+
}
855+
}
856+
857+
using (ResponseMessage response = await container.ReadItemStreamAsync(
858+
id: testItem.id,
859+
partitionKey: new Cosmos.PartitionKey(testItem.pk)))
860+
{
861+
Assert.IsNotNull(response);
862+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
863+
Assert.IsTrue(response.Headers.RequestCharge > 0);
864+
Assert.IsNotNull(response.Headers.ActivityId);
865+
Assert.IsNotNull(response.Headers.ETag);
866+
Assert.IsNotNull(response.Diagnostics);
867+
Assert.IsTrue(!string.IsNullOrEmpty(response.Diagnostics.ToString()));
868+
Assert.IsTrue(response.Diagnostics.GetClientElapsedTime() > TimeSpan.Zero);
869+
870+
if (enableStreamConversationForBinaryEncoding)
871+
{
872+
AssertOnResponseSerializationTextType(response.Content);
873+
}
874+
else
875+
{
876+
if (binaryEncodingEnabledInClient)
877+
{
878+
AssertOnResponseSerializationBinaryType(response.Content);
879+
}
880+
else
881+
{
882+
AssertOnResponseSerializationTextType(response.Content);
883+
}
884+
}
885+
}
886+
}
887+
finally
888+
{
889+
Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, null);
890+
891+
if (container != null)
892+
{
893+
await container.DeleteContainerStreamAsync();
894+
}
895+
896+
if (database != null)
897+
{
898+
await database.DeleteAsync();
899+
}
900+
}
771901
}
772902

773903
[TestMethod]

0 commit comments

Comments
 (0)