Skip to content
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

Session: Fixes token being sent if request had a different consistency level on Gateway mode #2965

Merged
merged 7 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Microsoft.Azure.Cosmos/src/GatewayStoreModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ internal static async Task ApplySessionTokenAsync(
}

string requestConsistencyLevel = request.Headers[HttpConstants.HttpHeaders.ConsistencyLevel];
bool requestHasConsistencySet = !string.IsNullOrEmpty(requestConsistencyLevel);
ealsur marked this conversation as resolved.
Show resolved Hide resolved

bool sessionConsistency =
defaultConsistencyLevel == ConsistencyLevel.Session ||
(!string.IsNullOrEmpty(requestConsistencyLevel)
(!requestHasConsistencySet && defaultConsistencyLevel == ConsistencyLevel.Session) ||
(requestHasConsistencySet
&& string.Equals(requestConsistencyLevel, GatewayStoreModel.sessionConsistencyAsString, StringComparison.OrdinalIgnoreCase));

bool isMultiMasterEnabledForRequest = globalEndpointManager.CanUseMultipleWriteLocations(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,39 @@ await GatewayStoreModel.ApplySessionTokenAsync(
});
}

[TestMethod]
public async Task TestRequestOverloadRemovesSessionToken()
{
INameValueCollection headers = new StoreRequestNameValueCollection();
headers.Set(HttpConstants.HttpHeaders.ConsistencyLevel, ConsistencyLevel.Eventual.ToString());

DocumentServiceRequest dsrNoSessionToken = DocumentServiceRequest.CreateFromName(
OperationType.Read,
"Test",
ResourceType.Document,
AuthorizationTokenType.PrimaryMasterKey,
headers);

string dsrSessionToken = Guid.NewGuid().ToString();
Mock<ISessionContainer> sMock = new Mock<ISessionContainer>();
sMock.Setup(x => x.ResolveGlobalSessionToken(dsrNoSessionToken)).Returns(dsrSessionToken);

Mock<IGlobalEndpointManager> globalEndpointManager = new Mock<IGlobalEndpointManager>();
await this.GetGatewayStoreModelForConsistencyTest(async (gatewayStoreModel) =>
{
await GatewayStoreModel.ApplySessionTokenAsync(
dsrNoSessionToken,
ConsistencyLevel.Session,
sMock.Object,
partitionKeyRangeCache: new Mock<PartitionKeyRangeCache>(null, null, null).Object,
clientCollectionCache: new Mock<ClientCollectionCache>(new SessionContainer("testhost"), gatewayStoreModel, null, null).Object,
globalEndpointManager: new Mock<IGlobalEndpointManager>().Object);

// Should not add the session token because the request is lower
Assert.IsNull(dsrNoSessionToken.Headers[HttpConstants.HttpHeaders.SessionToken]);
});
}

[TestMethod]
public async Task TestErrorResponsesProvideBody()
{
Expand Down