Skip to content

Commit 0e06625

Browse files
leminh98Minh Le (from Dev Box)
andauthored
Query: Fixes ResponseMessage not parsing the IndexMetrics as text in latest sdk (#4397)
* Fix response message not parsing the index advice as text in latest sdk * add test coverage * address code review --------- Co-authored-by: Minh Le (from Dev Box) <[email protected]>
1 parent cb7f923 commit 0e06625

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Microsoft.Azure.Cosmos/src/Handler/ResponseMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal ResponseMessage(
8383
this.CosmosException = cosmosException;
8484
this.Headers = headers ?? new Headers();
8585

86-
this.IndexUtilizationText = ResponseMessage.DecodeIndexMetrics(this.Headers, isBase64Encoded: true);
86+
this.IndexUtilizationText = ResponseMessage.DecodeIndexMetrics(this.Headers, isBase64Encoded: false);
8787

8888
if (requestMessage != null && requestMessage.Trace != null)
8989
{

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ public override IndexMetricsParserTestOutput ExecuteTest(IndexMetricsParserTestI
384384

385385
// Make sure ODE and non-ODE is consistent
386386
Assert.AreEqual(indexMetricsNonODE, indexMetricsODE);
387+
388+
// ----------------------------
389+
// Test stream API
390+
// ----------------------------
391+
// Execute without ODE
392+
string indexMetricsNonODEStreaming = RunStreamAPITest(input.Query, enableOptimisticDirectExecution: false);
393+
394+
// Execute with ODE
395+
string indexMetricsODEStreaming = RunStreamAPITest(input.Query, enableOptimisticDirectExecution: true);
396+
397+
// Make sure ODE and non-ODE is consistent
398+
Assert.AreEqual(indexMetricsNonODEStreaming, indexMetricsODEStreaming);
387399

388400
return new IndexMetricsParserTestOutput(indexMetricsNonODE);
389401
}
@@ -403,7 +415,42 @@ private static string RunTest(string query, bool enableOptimisticDirectExecution
403415
{
404416
FeedResponse<CosmosElement> page = itemQuery.ReadNextAsync().Result;
405417
Assert.IsTrue(page.Headers.AllKeys().Length > 1);
418+
if (roundTripCount > 0)
419+
{
420+
if (page.IndexMetrics != null)
421+
{
422+
Assert.Fail("Expected only Index Metrics on first round trip. Current round trip %n", roundTripCount);
423+
}
424+
}
425+
else
426+
{
427+
Assert.IsNotNull(page.Headers.Get(HttpConstants.HttpHeaders.IndexUtilization), "Expected index utilization headers for query");
428+
Assert.IsNotNull(page.IndexMetrics, "Expected index metrics response for query");
429+
430+
indexMetrics = page.IndexMetrics;
431+
}
432+
433+
roundTripCount++;
434+
}
435+
436+
return indexMetrics;
437+
}
438+
439+
private static string RunStreamAPITest(string query, bool enableOptimisticDirectExecution)
440+
{
441+
QueryRequestOptions requestOptions = new QueryRequestOptions() { PopulateIndexMetrics = true, EnableOptimisticDirectExecution = enableOptimisticDirectExecution };
406442

443+
using FeedIterator itemQuery = testContainer.GetItemQueryStreamIterator(
444+
queryText: query,
445+
requestOptions: requestOptions);
446+
447+
// Index Metrics is returned fully on the first page so no need to worry about result set
448+
int roundTripCount = 0;
449+
string indexMetrics = null;
450+
while (itemQuery.HasMoreResults)
451+
{
452+
ResponseMessage page = itemQuery.ReadNextAsync().Result;
453+
Assert.IsTrue(page.Headers.AllKeys().Length > 1);
407454
if (roundTripCount > 0)
408455
{
409456
if (page.IndexMetrics != null)

0 commit comments

Comments
 (0)