-
Notifications
You must be signed in to change notification settings - Fork 533
[Internal] DTS: Adds logic to build request and parse response #5576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kirankumarkolli
merged 14 commits into
master
from
users/Meghana-Palaparthi/DTS_Committer
Feb 6, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a488913
Initial commit
Meghana-Palaparthi 8be22cf
Add documentation for param
Meghana-Palaparthi 70a7d9a
Parse databaseResourceId
Meghana-Palaparthi a277641
Address comments
Meghana-Palaparthi ba3b618
Get idempotency token from response
Meghana-Palaparthi 461d091
fix
Meghana-Palaparthi 1ab4718
Merge branch 'master' into users/Meghana-Palaparthi/DTS_Committer
Meghana-Palaparthi b4d732a
Address comments
Meghana-Palaparthi 1dc2396
Update Resource URI and add happy path tests
Meghana-Palaparthi 7af14c4
update resource body to nested json in serializer
Meghana-Palaparthi f0d2170
Merge branch 'master' into users/Meghana-Palaparthi/DTS_Committer
Meghana-Palaparthi f339276
address comments - deserialize directly
Meghana-Palaparthi 8b17ae0
fix
Meghana-Palaparthi 7cdc859
Merge branch 'master' into users/Meghana-Palaparthi/DTS_Committer
kirankumarkolli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Microsoft.Azure.Cosmos/src/DistributedTransaction/DistributedTransactionCommitter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Cosmos.Core.Trace; | ||
| using Microsoft.Azure.Cosmos.Tracing; | ||
| using Microsoft.Azure.Documents; | ||
|
|
||
| internal class DistributedTransactionCommitter | ||
| { | ||
| // TODO: Move to HttpConstants.HttpHeaders once DTC headers are added centrally | ||
| private const string IdempotencyTokenHeader = "x-ms-dtc-operation-id"; | ||
|
|
||
| private readonly IReadOnlyList<DistributedTransactionOperation> operations; | ||
| private readonly CosmosClientContext clientContext; | ||
|
|
||
| public DistributedTransactionCommitter( | ||
| IReadOnlyList<DistributedTransactionOperation> operations, | ||
| CosmosClientContext clientContext) | ||
| { | ||
| this.operations = operations ?? throw new ArgumentNullException(nameof(operations)); | ||
| this.clientContext = clientContext ?? throw new ArgumentNullException(nameof(clientContext)); | ||
| } | ||
|
|
||
| public async Task<DistributedTransactionResponse> CommitTransactionAsync(CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| await DistributedTransactionCommitterUtils.ResolveCollectionRidsAsync( | ||
| this.operations, | ||
| this.clientContext, | ||
| cancellationToken); | ||
|
|
||
| DistributedTransactionServerRequest serverRequest = await DistributedTransactionServerRequest.CreateAsync( | ||
| this.operations, | ||
| this.clientContext.SerializerCore, | ||
| cancellationToken); | ||
|
|
||
| return await this.ExecuteCommitAsync(serverRequest, cancellationToken); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| DefaultTrace.TraceError($"Distributed transaction failed: {ex.Message}"); | ||
| // await this.AbortTransactionAsync(cancellationToken); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| private async Task<DistributedTransactionResponse> ExecuteCommitAsync( | ||
| DistributedTransactionServerRequest serverRequest, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| using (ITrace trace = Trace.GetRootTrace("Execute Distributed Transaction Commit", TraceComponent.Batch, TraceLevel.Info)) | ||
| { | ||
| using (MemoryStream bodyStream = serverRequest.TransferBodyStream()) | ||
| { | ||
| ResponseMessage responseMessage = await this.clientContext.ProcessResourceOperationStreamAsync( | ||
| resourceUri: "/dtc/ops", | ||
| resourceType: ResourceType.Document, // TODO: Update to a new ResourceType specific to DTC | ||
| operationType: OperationType.Batch, // TODO: Update to a new OperationType specific to DTC | ||
| requestOptions: null, | ||
| cosmosContainerCore: null, | ||
| partitionKey: null, | ||
| itemId: null, | ||
| streamPayload: bodyStream, | ||
| requestEnricher: requestMessage => this.EnrichRequestMessage(requestMessage, serverRequest), | ||
| trace: trace, | ||
| cancellationToken: cancellationToken); | ||
|
|
||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| return await DistributedTransactionResponse.FromResponseMessageAsync( | ||
| responseMessage, | ||
| serverRequest, | ||
| this.clientContext.SerializerCore, | ||
| serverRequest.IdempotencyToken, | ||
| trace, | ||
| cancellationToken); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void EnrichRequestMessage(RequestMessage requestMessage, DistributedTransactionServerRequest serverRequest) | ||
| { | ||
| // Set DTC-specific headers | ||
| requestMessage.Headers.Add(IdempotencyTokenHeader, serverRequest.IdempotencyToken.ToString()); | ||
| requestMessage.UseGatewayMode = true; | ||
| } | ||
|
|
||
| private Task AbortTransactionAsync(CancellationToken cancellationToken) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
Microsoft.Azure.Cosmos/src/DistributedTransaction/DistributedTransactionCommitterUtils.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Cosmos.Tracing; | ||
| using Microsoft.Azure.Documents; | ||
|
|
||
| internal class DistributedTransactionCommitterUtils | ||
| { | ||
| public static async Task ResolveCollectionRidsAsync( | ||
| IReadOnlyList<DistributedTransactionOperation> operations, | ||
| CosmosClientContext clientContext, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| IEnumerable<IGrouping<string, DistributedTransactionOperation>> groupedOperations = operations | ||
| .GroupBy(op => $"/dbs/{op.Database}/colls/{op.Container}"); | ||
|
|
||
| foreach (IGrouping<string, DistributedTransactionOperation> group in groupedOperations) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| string collectionPath = group.Key; | ||
| ContainerProperties containerProperties = await clientContext.GetCachedContainerPropertiesAsync( | ||
| collectionPath, | ||
| NoOpTrace.Singleton, | ||
| cancellationToken); | ||
|
|
||
| string containerResourceId = containerProperties.ResourceId; | ||
| ResourceId resourceId = ResourceId.Parse(containerResourceId); | ||
| string databaseResourceId = resourceId.DatabaseId.ToString(); | ||
|
|
||
| foreach (DistributedTransactionOperation operation in group) | ||
| { | ||
| operation.CollectionResourceId = containerResourceId; | ||
| operation.DatabaseResourceId = databaseResourceId; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.