Skip to content

Commit

Permalink
Merge branch 'users/jawilley/direct/3.23.0' of https://github.com/Azu…
Browse files Browse the repository at this point in the history
…re/azure-cosmos-dotnet-v3 into users/jawilley/direct/3.23.0
  • Loading branch information
Jake Willey committed Oct 25, 2021
2 parents 466cecb + 8aa808b commit 462f4fe
Show file tree
Hide file tree
Showing 17 changed files with 607 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
[Serializable]

#if PREVIEW
public
#else
internal
#endif
class ChangeFeedProcessorUserException : Exception
public class ChangeFeedProcessorUserException : Exception
{
private static readonly string DefaultMessage = "Exception has been thrown by the change feed processor delegate.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,49 @@ public Task<ResponseMessage> PatchItemStreamAsync(
cancellationToken: cancellationToken);
}

public Task<ResponseMessage> PatchItemStreamAsync(
string id,
PartitionKey partitionKey,
Stream streamPayload,
ITrace trace,
ItemRequestOptions requestOptions = null,
CancellationToken cancellationToken = default)
{
if (trace == null)
{
throw new ArgumentNullException(nameof(trace));
}

if (partitionKey == null)
{
throw new ArgumentNullException(nameof(partitionKey));
}

if (id == null)
{
throw new ArgumentNullException(nameof(id));
}

if (streamPayload == null)
{
throw new ArgumentNullException(nameof(streamPayload));
}

if (trace == null)
{
throw new ArgumentNullException(nameof(trace));
}

return this.ProcessItemStreamAsync(
partitionKey: partitionKey,
itemId: id,
streamPayload: streamPayload,
operationType: OperationType.Patch,
requestOptions: requestOptions,
trace: trace,
cancellationToken: cancellationToken);
}

private ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilderPrivate(
string processorName,
ChangeFeedObserverFactory observerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,19 @@ public override Task<ResponseMessage> PatchItemStreamAsync(
(trace) => base.PatchItemStreamAsync(id, partitionKey, patchOperations, trace, requestOptions, cancellationToken));
}

public override Task<ResponseMessage> PatchItemStreamAsync(
string id,
PartitionKey partitionKey,
Stream streamPayload,
ItemRequestOptions requestOptions = null,
CancellationToken cancellationToken = default)
{
return this.ClientContext.OperationHelperAsync(
nameof(PatchItemStreamAsync),
requestOptions,
(trace) => base.PatchItemStreamAsync(id, partitionKey, streamPayload, trace, requestOptions, cancellationToken));
}

public override Task<ItemResponse<T>> PatchItemAsync<T>(
string id,
PartitionKey partitionKey,
Expand Down
21 changes: 21 additions & 0 deletions Microsoft.Azure.Cosmos/src/Resource/Container/ContainerInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ public static void ValidatePartitionKey(object partitionKey, RequestOptions requ
throw new ArgumentNullException(nameof(partitionKey));
}

/// <summary>
/// Patches an item in the Azure Cosmos service as an asynchronous operation.
/// </summary>
/// <remarks>
/// By default, resource body will be returned as part of the response. User can request no content by setting <see cref="ItemRequestOptions.EnableContentResponseOnWrite"/> flag to false.
/// </remarks>
/// <param name="id">The Cosmos item id</param>
/// <param name="partitionKey">The partition key for the item.</param>
/// <param name="streamPayload">Represents a stream containing the list of operations to be sequentially applied to the referred Cosmos item.</param>
/// <param name="requestOptions">(Optional) The options for the item request.</param>
/// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
/// <returns>
/// A <see cref="Task"/> containing a <see cref="ResponseMessage"/> which wraps a <see cref="Stream"/> containing the patched resource record.
/// </returns>
public abstract Task<ResponseMessage> PatchItemStreamAsync(
string id,
PartitionKey partitionKey,
Stream streamPayload,
ItemRequestOptions requestOptions = null,
CancellationToken cancellationToken = default);

#if !INTERNAL
public abstract Task<ResponseMessage> DeleteAllItemsByPartitionKeyStreamAsync(
Cosmos.PartitionKey partitionKey,
Expand Down
2 changes: 2 additions & 0 deletions Microsoft.Azure.Cosmos/src/Telemetry/AzureVMMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using Newtonsoft.Json;

[Serializable]
internal sealed class AzureVMMetadata
{
public AzureVMMetadata(Compute compute)
Expand Down
31 changes: 1 addition & 30 deletions Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ internal void Collect(CosmosDiagnostics cosmosDiagnostics,
throw new ArgumentNullException(nameof(cosmosDiagnostics));
}

string regionsContacted = this.GetContactedRegions(cosmosDiagnostics);
string regionsContacted = ClientTelemetryHelper.GetContactedRegions(cosmosDiagnostics);

// Recording Request Latency and Request Charge
OperationInfo payloadKey = new OperationInfo(regionsContacted: regionsContacted?.ToString(),
Expand Down Expand Up @@ -236,35 +236,6 @@ internal void Collect(CosmosDiagnostics cosmosDiagnostics,
}
}

/// <summary>
/// Get comma separated list of regions contacted from the diagnostic
/// </summary>
/// <param name="cosmosDiagnostics"></param>
/// <returns>Comma separated region list</returns>
private string GetContactedRegions(CosmosDiagnostics cosmosDiagnostics)
{
IReadOnlyList<(string regionName, Uri uri)> regionList = cosmosDiagnostics.GetContactedRegions();

if (regionList.Count == 1)
{
return regionList[0].regionName;
}

StringBuilder regionsContacted = new StringBuilder();
foreach ((_, Uri uri) in regionList)
{
if (regionsContacted.Length > 0)
{
regionsContacted.Append(",");

}

regionsContacted.Append(uri);
}

return regionsContacted.ToString();
}

/// <summary>
/// Record CPU and memory usage which will be sent as part of telemetry information
/// </summary>
Expand Down
32 changes: 31 additions & 1 deletion Microsoft.Azure.Cosmos/src/Telemetry/ClientTelemetryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HdrHistogram;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Rntbd;

internal class ClientTelemetryHelper
internal static class ClientTelemetryHelper
{
internal static AzureVMMetadata azMetadata = null;

Expand Down Expand Up @@ -169,5 +170,34 @@ internal static List<OperationInfo> ToListWithMetricsInfo(IDictionary<OperationI
return payloadWithMetricInformation;
}

/// <summary>
/// Get comma separated list of regions contacted from the diagnostic
/// </summary>
/// <param name="cosmosDiagnostics"></param>
/// <returns>Comma separated region list</returns>
internal static string GetContactedRegions(CosmosDiagnostics cosmosDiagnostics)
{
IReadOnlyList<(string regionName, Uri uri)> regionList = cosmosDiagnostics.GetContactedRegions();

if (regionList.Count == 1)
{
return regionList[0].regionName;
}

StringBuilder regionsContacted = new StringBuilder();
foreach ((string name, _) in regionList)
{
if (regionsContacted.Length > 0)
{
regionsContacted.Append(",");

}

regionsContacted.Append(name);
}

return regionsContacted.ToString();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

internal class ClientTelemetryOptions
internal static class ClientTelemetryOptions
{
// ConversionFactor used in Histogram calculation to maintain precision or to collect data in desired unit
internal const double HistogramPrecisionFactor = 100;
Expand Down
4 changes: 3 additions & 1 deletion Microsoft.Azure.Cosmos/src/Telemetry/Compute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Microsoft.Azure.Cosmos.Telemetry
{
using System;
using Newtonsoft.Json;

internal class Compute
[Serializable]
internal sealed class Compute
{
public Compute(string location, string sKU, string azEnvironment, string oSType, string vMSize)
{
Expand Down
11 changes: 2 additions & 9 deletions Microsoft.Azure.Cosmos/src/Telemetry/OperationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class OperationInfo
private string RegionsContacted { get; }

[JsonProperty(PropertyName = "greaterThan1Kb")]
private bool? GreaterThan1Kb { get; }
internal bool? GreaterThan1Kb { get; set; }

[JsonProperty(PropertyName = "databaseName")]
private string DatabaseName { get; }
Expand All @@ -36,9 +36,6 @@ internal sealed class OperationInfo
[JsonProperty(PropertyName = "statusCode")]
public int? StatusCode { get; }

[JsonProperty(PropertyName = "responseSizeInBytes")]
public long? ResponseSizeInBytes { get; }

[JsonProperty(PropertyName = "metricInfo")]
internal MetricInfo MetricInfo { get; set; }

Expand All @@ -57,7 +54,6 @@ internal OperationInfo(string regionsContacted,
int? statusCode)
{
this.RegionsContacted = regionsContacted;
this.ResponseSizeInBytes = responseSizeInBytes;
if (responseSizeInBytes != null)
{
this.GreaterThan1Kb = responseSizeInBytes > ClientTelemetryOptions.OneKbToBytes;
Expand All @@ -77,8 +73,7 @@ public OperationInfo(string regionsContacted,
string operation,
string resource,
string consistency,
int? statusCode,
long? responseSizeInBytes,
int? statusCode,
MetricInfo metricInfo)
{
this.RegionsContacted = regionsContacted;
Expand All @@ -89,7 +84,6 @@ public OperationInfo(string regionsContacted,
this.Resource = resource;
this.Consistency = consistency;
this.StatusCode = statusCode;
this.ResponseSizeInBytes = responseSizeInBytes;
this.MetricInfo = metricInfo;
}

Expand All @@ -103,7 +97,6 @@ public OperationInfo Copy()
this.Resource,
this.Consistency,
this.StatusCode,
this.ResponseSizeInBytes,
null);
}

Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Telemetry/SystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using Newtonsoft.Json;

[Serializable]
internal class SystemInfo
internal sealed class SystemInfo
{
[JsonProperty(PropertyName = "metricInfo")]
internal MetricInfo MetricInfo { get; set; }
Expand Down
23 changes: 23 additions & 0 deletions Microsoft.Azure.Cosmos/src/Util/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,28 @@ internal static bool EqualsTo(this IDictionary<string, JToken> dict1, IDictionar

return true;
}

internal static bool EqualsTo(this IDictionary<string, long> dict1, IDictionary<string, long> dict2)
{
if (dict1 == null && dict2 == null)
{
return true;
}

if (dict1 == null || dict2 == null || dict1.Count != dict2.Count)
{
return false;
}

foreach (KeyValuePair<string, long> pair in dict1)
{
if (!dict2.TryGetValue(pair.Key, out long value) || pair.Value != value)
{
return false;
}
}

return true;
}
}
}
Loading

0 comments on commit 462f4fe

Please sign in to comment.