Skip to content

Commit

Permalink
Diagnostics : Adds Continuation Token from PkRange cache (#3180)
Browse files Browse the repository at this point in the history
Diagnostics : Adds Continuation Token from PkRange cache. Right now, NoOpTrace has been passed in most of the cases for PartitionKeyRangeCache call. In future PRs, we will be removing NoOpTrace from these calls to get the proper diagnostics.
  • Loading branch information
sourabh1007 authored May 13, 2022
1 parent 69d5ef4 commit ba47e73
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Microsoft.Azure.Cosmos/src/Routing/PartitionKeyRangeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ private async Task<CollectionRoutingMap> GetRoutingMapForCollectionAsync(
throw new NotFoundException($"{DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)}: GetRoutingMapForCollectionAsync(collectionRid: {collectionRid}), Range information either doesn't exist or is not complete.");
}

trace.AddDatum($"PKRangeCache Info({DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture)})",
new PartitionKeyRangeCacheTraceDatum(
previousContinuationToken: previousRoutingMap?.ChangeFeedNextIfNoneMatch,
continuationToken: routingMap.ChangeFeedNextIfNoneMatch));
return routingMap;
}

Expand Down
1 change: 1 addition & 0 deletions Microsoft.Azure.Cosmos/src/Tracing/ITraceDatumVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ internal interface ITraceDatumVisitor
void Visit(ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum);
void Visit(CpuHistoryTraceDatum cpuHistoryTraceDatum);
void Visit(ClientConfigurationTraceDatum clientConfigurationTraceDatum);
void Visit(PartitionKeyRangeCacheTraceDatum partitionKeyRangeCacheTraceDatum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tracing.TraceData
{
internal class PartitionKeyRangeCacheTraceDatum : TraceDatum
{
public string PreviousContinuationToken { get; }
public string ContinuationToken { get; }

public PartitionKeyRangeCacheTraceDatum(string previousContinuationToken, string continuationToken)
{
this.PreviousContinuationToken = previousContinuationToken;
this.ContinuationToken = continuationToken;
}

internal override void Accept(ITraceDatumVisitor traceDatumVisitor)
{
traceDatumVisitor.Visit(this);
}
}
}
14 changes: 14 additions & 0 deletions Microsoft.Azure.Cosmos/src/Tracing/TraceWriter.TraceJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,19 @@ public void Visit(StoreResult storeResult)
this.jsonWriter.WriteObjectEnd();
}

public void Visit(PartitionKeyRangeCacheTraceDatum partitionKeyRangeCacheTraceDatum)
{
this.jsonWriter.WriteObjectStart();

this.jsonWriter.WriteFieldName("Previous Continuation Token");
this.WriteStringValueOrNull(partitionKeyRangeCacheTraceDatum.PreviousContinuationToken);

this.jsonWriter.WriteFieldName("Continuation Token");
this.WriteStringValueOrNull(partitionKeyRangeCacheTraceDatum.ContinuationToken);

this.jsonWriter.WriteObjectEnd();
}

private void WriteJsonUriArray(string propertyName, IEnumerable<TransportAddressUri> uris)
{
this.jsonWriter.WriteFieldName(propertyName);
Expand Down Expand Up @@ -560,6 +573,7 @@ private void WriteDateTimeStringValue(DateTime value)
this.jsonWriter.WriteStringValue(value.ToString("o", CultureInfo.InvariantCulture));
}
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,15 @@ public override string ToString()
{
return this.toStringValue;
}

public void Visit(PartitionKeyRangeCacheTraceDatum partitionKeyRangeCacheTraceDatum)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"Previous Continuation Token: {partitionKeyRangeCacheTraceDatum.PreviousContinuationToken ?? "<null>"}");
stringBuilder.AppendLine($"Continuation Token: {partitionKeyRangeCacheTraceDatum.ContinuationToken ?? "<null>"}");

this.toStringValue = stringBuilder.ToString();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
using System.Threading;
using Microsoft.Azure.Cosmos.Routing;
using Newtonsoft.Json.Linq;
using System.Linq;

[TestClass]
public class CosmosItemSessionTokenTests : BaseCosmosClientHelper
Expand Down Expand Up @@ -371,6 +372,18 @@ public async Task InvalidSessionTokenAfterContainerRecreationAndCollectionCacheR
while (queryIteratorOldContainer.HasMoreResults)
{
FeedResponse<JObject> response = await queryIteratorOldContainer.ReadNextAsync();
if(i == 0)
{
string diagnosticString = response.Diagnostics.ToString();
Assert.IsTrue(diagnosticString.Contains("PKRangeCache Info("));
JObject diagnosticJobject = JObject.Parse(diagnosticString);
JToken actualToken = diagnosticJobject.SelectToken("$.children[0].children[?(@.name=='Get Partition Key Ranges')].children[?(@.name=='Try Get Overlapping Ranges')].data");
JToken actualNode = actualToken.Children().First().First();

Assert.IsTrue(actualNode["Previous Continuation Token"].ToString().Length == 0);
Assert.IsTrue(actualNode["Continuation Token"].ToString().Length > 0);
}

itemCountOldContainer += response.Count;
}

Expand Down

0 comments on commit ba47e73

Please sign in to comment.