Skip to content

Commit 41ea350

Browse files
committed
Address request charge review findings
1 parent bf65ce4 commit 41ea350

6 files changed

Lines changed: 18 additions & 37 deletions

File tree

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/ListCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ private async Task<CommandState> ListContainerItemsAsync(ConnectedState state, S
236236
}
237237
catch (Exception ex) when (ex is not OperationCanceledException)
238238
{
239-
return new ErrorCommandState(ex) { RequestCharge = returnState.RequestCharge };
239+
RequestChargeContext.Record(returnState.RequestCharge ?? 0);
240+
throw new CommandException("ls", ex);
240241
}
241242

242243
using (queryDocument)

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/PrintCommand.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,38 @@ private async Task<CommandState> PrintItemAsync(Container container, Cancellatio
5454
try
5555
{
5656
using var response = await container.ReadItemStreamAsync(this.Id, new PartitionKey(this.PartitionKey), cancellationToken: token);
57+
RequestChargeContext.Record(response.Headers.RequestCharge);
5758

5859
if (response.IsSuccessStatusCode)
5960
{
60-
commandState.RequestCharge = response.Headers.RequestCharge;
6161
using var reader = new StreamReader(response.Content);
6262
var content = await reader.ReadToEndAsync();
6363

6464
// Parse the content as JSON for structured output
65-
var jsonDocument = System.Text.Json.JsonDocument.Parse(content);
66-
commandState.Result = new ShellJson(jsonDocument.RootElement);
65+
using var jsonDocument = System.Text.Json.JsonDocument.Parse(content);
66+
commandState.Result = new ShellJson(jsonDocument.RootElement.Clone());
6767
}
6868
else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
6969
{
70-
return new ErrorCommandState(new CommandException("print", MessageService.GetString("command-print-error-item_not_found", new Dictionary<string, object>
70+
throw new CommandException("print", MessageService.GetString("command-print-error-item_not_found", new Dictionary<string, object>
7171
{
7272
{ "id", this.Id ?? "(null)" },
7373
{ "partitionKey", this.PartitionKey ?? "(null)" },
74-
})))
75-
{
76-
RequestCharge = response.Headers.RequestCharge,
77-
};
74+
}));
7875
}
7976
else
8077
{
81-
return new ErrorCommandState(new CommandException("print", MessageService.GetString("command-print-error-request_failed", new Dictionary<string, object>
78+
throw new CommandException("print", MessageService.GetString("command-print-error-request_failed", new Dictionary<string, object>
8279
{
8380
{ "id", this.Id ?? "(null)" },
8481
{ "status", (int)response.StatusCode },
85-
})))
86-
{
87-
RequestCharge = response.Headers.RequestCharge,
88-
};
82+
}));
8983
}
9084
}
9185
catch (CosmosException ex)
9286
{
93-
return new ErrorCommandState(new CommandException("print", MessageService.GetArgsString("command-print-error-reading_item", "message", CommandException.GetDisplayMessage(ex)), ex))
94-
{
95-
RequestCharge = ex.RequestCharge > 0 ? ex.RequestCharge : null,
96-
};
87+
RequestChargeContext.Record(ex.RequestCharge);
88+
throw new CommandException("print", MessageService.GetArgsString("command-print-error-reading_item", "message", CommandException.GetDisplayMessage(ex)), ex);
9789
}
9890

9991
return commandState;

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/QueryCommand.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -634,14 +634,8 @@ private async Task<CommandState> ExecuteExplainAsync(Container container, ShellI
634634
double requestCharge = response?.Headers.RequestCharge ?? 0;
635635
if (response is not null && !response.IsSuccessStatusCode)
636636
{
637-
try
638-
{
639-
await this.ThrowIfRequestFailedAsync(response, shell);
640-
}
641-
catch (Exception ex) when (ex is not OperationCanceledException)
642-
{
643-
return new ErrorCommandState(ex) { RequestCharge = requestCharge > 0 ? requestCharge : null };
644-
}
637+
RequestChargeContext.Record(requestCharge);
638+
await this.ThrowIfRequestFailedAsync(response, shell);
645639
}
646640

647641
var cumulative = response?.Diagnostics.GetQueryMetrics()?.CumulativeMetrics;
@@ -723,15 +717,8 @@ private async Task<CommandState> ExecuteQueryAsync(Container container, ShellInt
723717
var pageRequestCharge = response.Headers.RequestCharge;
724718
if (!response.IsSuccessStatusCode)
725719
{
726-
try
727-
{
728-
await this.ThrowIfRequestFailedAsync(response, shell);
729-
}
730-
catch (Exception ex) when (ex is not OperationCanceledException)
731-
{
732-
var failedCharge = totalRequestCharge + pageRequestCharge;
733-
return new ErrorCommandState(ex) { RequestCharge = failedCharge > 0 ? failedCharge : null };
734-
}
720+
RequestChargeContext.Record(totalRequestCharge + pageRequestCharge);
721+
await this.ThrowIfRequestFailedAsync(response, shell);
735722
}
736723

737724
if (response.Content == null)

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/ReplaceCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ private static async Task<ReplaceSummary> ReplaceArrayAsync(Container container,
130130

131131
if (failCount > 0)
132132
{
133+
RequestChargeContext.Record(charge);
133134
throw new CommandException(
134135
"replace",
135136
MessageService.GetArgsString(

CosmosDBShell/Azure.Data.Cosmos.Shell.Commands/RmCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private async Task<ExitCode> RemoveItemsFromContainerAsync(ConnectedState state,
249249
totalCharge += response.Headers.RequestCharge;
250250

251251
using var streamReader = new StreamReader(response.Content);
252-
var queryDocument = JsonDocument.Parse(await streamReader.ReadToEndAsync());
252+
using var queryDocument = JsonDocument.Parse(await streamReader.ReadToEndAsync());
253253

254254
foreach (var element in queryDocument.RootElement.GetProperty("Documents").EnumerateArray())
255255
{

CosmosDBShell/Azure.Data.Cosmos.Shell.Core/ShellInterpreter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ private void SetSessionMaxRequestCharge(ShellObject value)
20032003
double requestChargeTotal;
20042004
lock (this.sessionRequestChargeLock)
20052005
{
2006-
if (maximum != this.sessionMaxRequestCharge)
2006+
if (Math.Abs(maximum - this.sessionMaxRequestCharge) > 1e-9)
20072007
{
20082008
this.sessionRequestChargeWarningIssued = false;
20092009
}

0 commit comments

Comments
 (0)