Skip to content

Commit

Permalink
Batch API: Adds Session token support (#1870)
Browse files Browse the repository at this point in the history
* Session token support for transactional batch

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

* Contract change

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

* code review fix

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

* Session token error message less strict check

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

* Change

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

* Adding session consistency

Signed-off-by: Rakesh Kumar <rakkuma@microsoft.com>

Co-authored-by: Matias Quaranta <ealsur@users.noreply.github.com>
  • Loading branch information
rakkuma and ealsur authored Feb 12, 2021
1 parent 87674f1 commit 648f2b0
Show file tree
Hide file tree
Showing 5 changed files with 411 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
public class TransactionalBatchRequestOptions : RequestOptions
{
/// <summary>
/// Gets or sets the token for use with session consistency in the Azure Cosmos DB service.
/// </summary>
/// <value>
/// The token for use with session consistency.
/// </value>
///
/// <remarks>
/// One of the <see cref="ConsistencyLevel"/> for Azure Cosmos DB is Session. In fact, this is the default level applied to accounts.
/// <para>
/// When working with Session consistency, each batch request with write operation to Azure Cosmos DB is assigned a new SessionToken.
/// The CosmosClient will use this token internally with each read/query/batch request to ensure that the set
/// consistency level is maintained.
///
/// <para>
/// In some scenarios you need to manage this Session yourself;
/// Consider a web application with multiple nodes, each node will have its own instance of <see cref="CosmosClient"/>
/// If you wanted these nodes to participate in the same session (to be able read your own writes consistently across web tiers)
/// you would have to send the SessionToken from <see cref="TransactionalBatchResponse"/> of the write action on one node
/// to the client tier, using a cookie or some other mechanism, and have that token flow back to the web tier for subsequent reads.
/// If you are using a round-robin load balancer which does not maintain session affinity between requests, such as the Azure Load Balancer,
/// the read could potentially land on a different node to the write request, where the session was created.
/// </para>
///
/// <para>
/// If you do not flow the Azure Cosmos DB SessionToken across as described above you could end up with inconsistent read results for a period of time.
/// </para>
///
/// </para>
/// <see href="https://docs.microsoft.com/azure/cosmos-db/consistency-levels" />
/// </remarks>
public string SessionToken { get; set; }

/// <summary>
/// Gets or sets the consistency level required for the request in the Azure Cosmos DB service.
/// </summary>
Expand All @@ -34,6 +67,8 @@ public ConsistencyLevel? ConsistencyLevel
/// <param name="request">The <see cref="RequestMessage"/></param>
internal override void PopulateRequestOptions(RequestMessage request)
{
RequestOptions.SetSessionToken(request, this.SessionToken);

base.PopulateRequestOptions(request);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private static async Task<TransactionalBatchResponse> PopulateFromContentAsync(
{
foreach (TransactionalBatchOperationResult result in results)
{
if ((int)result.StatusCode != (int)StatusCodes.FailedDependency)
if ((int)result.StatusCode != (int)StatusCodes.FailedDependency && (int)result.StatusCode >= (int)StatusCodes.StartingErrorCode)
{
responseStatusCode = result.StatusCode;
responseSubStatusCode = result.SubStatusCode;
Expand Down
Loading

0 comments on commit 648f2b0

Please sign in to comment.