From a4088ca03232ce76d7d64cbe5d6bc4983b857e39 Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Tue, 10 Aug 2021 01:33:10 +0530 Subject: [PATCH 1/7] Fixing ReadMany API for None Partition key values --- .../src/ReadManyQueryHelper.cs | 33 +++++++++++++++---- .../CosmosReadManyItemsTests.cs | 29 ++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs index 2cb6248c54..6fc3c12697 100644 --- a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs @@ -125,7 +125,8 @@ internal async Task[]> ReadManyTaskHelperAsync(IDictionary foreach ((string id, PartitionKey pk) item in items) { - string effectivePartitionKeyValue = item.pk.InternalKey.GetEffectivePartitionKeyString(this.partitionKeyDefinition); + Documents.Routing.PartitionKeyInternal partitionKeyInternal = await this.GetPartitionKeyInternalAsync(item.pk, cancellationToken); + string effectivePartitionKeyValue = partitionKeyInternal.GetEffectivePartitionKeyString(this.partitionKeyDefinition); PartitionKeyRange partitionKeyRange = collectionRoutingMap.GetRangeByEffectivePartitionKey(effectivePartitionKeyValue); if (partitionKeyRangeItemMap.TryGetValue(partitionKeyRange, out List<(string, PartitionKey)> itemList)) { @@ -260,13 +261,20 @@ private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, Part queryStringBuilder.Append("SELECT * FROM c WHERE ( "); for (int i = startIndex; i < totalItemCount; i++) { - object[] pkValues = items[i].Item2.InternalKey.ToObjectArray(); - - if (pkValues.Length != this.partitionKeyDefinition.Paths.Count) + object[] pkValues; + if (items[i].Item2.IsNone) { - throw new ArgumentException("Number of components in the partition key value does not match the definition."); + pkValues = new object[0]; } - + else + { + pkValues = items[i].Item2.InternalKey.ToObjectArray(); + if (pkValues.Length != this.partitionKeyDefinition.Paths.Count) + { + throw new ArgumentException("Number of components in the partition key value does not match the definition."); + } + } + string pkParamName = "@param_pk" + i; string idParamName = "@param_id" + i; sqlParameters.Add(new SqlParameter(idParamName, items[i].Item1)); @@ -274,7 +282,7 @@ private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, Part queryStringBuilder.Append("( "); queryStringBuilder.Append("c.id = "); queryStringBuilder.Append(idParamName); - for (int j = 0; j < this.partitionKeySelectors.Count; j++) + for (int j = 0; j < pkValues.Length; j++) { queryStringBuilder.Append(" AND "); queryStringBuilder.Append("c"); @@ -365,6 +373,17 @@ private void CancelCancellationToken(CancellationToken cancellationToken) } } + private async Task GetPartitionKeyInternalAsync(PartitionKey partitionKey, + CancellationToken cancellationToken) + { + if (partitionKey.IsNone) + { + return await this.container.GetNonePartitionKeyValueAsync(NoOpTrace.Singleton, cancellationToken).ConfigureAwait(false); + } + + return partitionKey.InternalKey; + } + private class ReadManyFeedResponseEnumerable : IEnumerable { private readonly List> typedResponses; diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs index e09a457d01..4613566f14 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs @@ -391,6 +391,35 @@ await container.CreateItemAsync( await database.DeleteAsync(); } + [TestMethod] + public async Task ReadManyItemsFromNonPartitionedContainers() + { + ContainerInternal container = await NonPartitionedContainerHelper.CreateNonPartitionedContainer(this.database, + Guid.NewGuid().ToString()); + for (int i = 0; i < 5; i++) + { + await NonPartitionedContainerHelper.CreateItemInNonPartitionedContainer(container, "id" + i.ToString()); + } + + // read using PartitionKey.None pk value + List<(string, PartitionKey)> itemList = new List<(string, PartitionKey)>(); + for (int i = 0; i < 10; i++) + { + itemList.Add(("id" + i.ToString(), PartitionKey.None)); + } + + using (ResponseMessage responseMessage = await container.ReadManyItemsStreamAsync(itemList)) + { + Assert.IsNotNull(responseMessage); + Assert.IsTrue(responseMessage.Headers.RequestCharge > 0); + Assert.IsNotNull(responseMessage.Diagnostics); + + ToDoActivity[] items = this.cosmosClient.ClientContext.SerializerCore.FromFeedStream( + CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope(responseMessage.Content)); + Assert.AreEqual(items.Length, 5); + } + } + [TestMethod] [DataRow(HttpStatusCode.NotFound)] public async Task ReadManyExceptionsTest(HttpStatusCode statusCode) From 48b22eb41f81135270e95e6e4cd2ea9e0460f830 Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Tue, 10 Aug 2021 20:57:22 +0530 Subject: [PATCH 2/7] addressing comments --- .../src/ReadManyQueryHelper.cs | 33 ++++++++++++------- .../Resource/Container/ContainerCore.Items.cs | 12 ++++++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs index 6fc3c12697..5b11e50a89 100644 --- a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs @@ -42,14 +42,22 @@ public override async Task ExecuteReadManyRequestAsync(IReadOnl ITrace trace, CancellationToken cancellationToken) { - string resourceId = await this.container.GetCachedRIDAsync(cancellationToken); - IDictionary> partitionKeyRangeItemMap = - await this.CreatePartitionKeyRangeItemListMapAsync(items, cancellationToken); + IDictionary> partitionKeyRangeItemMap; + string resourceId; + try + { + resourceId = await this.container.GetCachedRIDAsync(cancellationToken); + partitionKeyRangeItemMap = await this.CreatePartitionKeyRangeItemListMapAsync(items, trace, cancellationToken); + } + catch (CosmosException ex) + { + return ex.ToCosmosResponseMessage(request: null); + } List[] queryResponses = await this.ReadManyTaskHelperAsync(partitionKeyRangeItemMap, - readManyRequestOptions, - trace, - cancellationToken); + readManyRequestOptions, + trace, + cancellationToken); return this.CombineStreamsFromQueryResponses(queryResponses, resourceId, trace); // also disposes the response messages } @@ -60,7 +68,7 @@ public override async Task> ExecuteReadManyRequestAsync(IRead CancellationToken cancellationToken) { IDictionary> partitionKeyRangeItemMap = - await this.CreatePartitionKeyRangeItemListMapAsync(items, cancellationToken); + await this.CreatePartitionKeyRangeItemListMapAsync(items, trace, cancellationToken); List[] queryResponses = await this.ReadManyTaskHelperAsync(partitionKeyRangeItemMap, readManyRequestOptions, @@ -116,6 +124,7 @@ internal async Task[]> ReadManyTaskHelperAsync(IDictionary private async Task>> CreatePartitionKeyRangeItemListMapAsync( IReadOnlyList<(string, PartitionKey)> items, + ITrace trace, CancellationToken cancellationToken = default) { CollectionRoutingMap collectionRoutingMap = await this.container.GetRoutingMapAsync(cancellationToken); @@ -125,7 +134,8 @@ internal async Task[]> ReadManyTaskHelperAsync(IDictionary foreach ((string id, PartitionKey pk) item in items) { - Documents.Routing.PartitionKeyInternal partitionKeyInternal = await this.GetPartitionKeyInternalAsync(item.pk, cancellationToken); + Documents.Routing.PartitionKeyInternal partitionKeyInternal = + await this.GetPartitionKeyInternalAsync(item.pk, trace, cancellationToken); string effectivePartitionKeyValue = partitionKeyInternal.GetEffectivePartitionKeyString(this.partitionKeyDefinition); PartitionKeyRange partitionKeyRange = collectionRoutingMap.GetRangeByEffectivePartitionKey(effectivePartitionKeyValue); if (partitionKeyRangeItemMap.TryGetValue(partitionKeyRange, out List<(string, PartitionKey)> itemList)) @@ -373,15 +383,16 @@ private void CancelCancellationToken(CancellationToken cancellationToken) } } - private async Task GetPartitionKeyInternalAsync(PartitionKey partitionKey, + private ValueTask GetPartitionKeyInternalAsync(PartitionKey partitionKey, + ITrace trace, CancellationToken cancellationToken) { if (partitionKey.IsNone) { - return await this.container.GetNonePartitionKeyValueAsync(NoOpTrace.Singleton, cancellationToken).ConfigureAwait(false); + return await this.container.GetNonePartitionKeyValueAsync(trace, cancellationToken).ConfigureAwait(false); } - return partitionKey.InternalKey; + return new ValueTask(partitionKey.InternalKey); } private class ReadManyFeedResponseEnumerable : IEnumerable diff --git a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs index a16969cd56..37e5c80a47 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs @@ -298,7 +298,17 @@ public async Task ReadManyItemsStreamAsync( throw new ArgumentNullException(nameof(trace)); } - ReadManyHelper readManyHelper = new ReadManyQueryHelper(await this.GetPartitionKeyDefinitionAsync(), + PartitionKeyDefinition partitionKeyDefinition; + try + { + partitionKeyDefinition = await this.GetPartitionKeyDefinitionAsync(); + } + catch (CosmosException ex) + { + ex.ToCosmosResponseMessage(request: null); + } + + ReadManyHelper readManyHelper = new ReadManyQueryHelper(partitionKeyDefinition, this); return await readManyHelper.ExecuteReadManyRequestAsync(items, From 66495ab5d256f01abb182eecae7b31ed5d42d87c Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Wed, 11 Aug 2021 04:02:04 +0530 Subject: [PATCH 3/7] fixing build --- .../src/Resource/Container/ContainerCore.Items.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs index 37e5c80a47..b659289a02 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs @@ -298,7 +298,7 @@ public async Task ReadManyItemsStreamAsync( throw new ArgumentNullException(nameof(trace)); } - PartitionKeyDefinition partitionKeyDefinition; + PartitionKeyDefinition partitionKeyDefinition = null; try { partitionKeyDefinition = await this.GetPartitionKeyDefinitionAsync(); From ec52b389ca719a8779fa6355a742f9bde1551d3c Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Wed, 11 Aug 2021 19:17:04 +0530 Subject: [PATCH 4/7] fixing build --- Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs index 5b11e50a89..f543797151 100644 --- a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs @@ -389,7 +389,8 @@ private void CancelCancellationToken(CancellationToken cancellationToken) { if (partitionKey.IsNone) { - return await this.container.GetNonePartitionKeyValueAsync(trace, cancellationToken).ConfigureAwait(false); + return new ValueTask( + this.container.GetNonePartitionKeyValueAsync(trace, cancellationToken)); } return new ValueTask(partitionKey.InternalKey); From d89882bed9db2c7bafd8ff88e379389f1fe29443 Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Wed, 18 Aug 2021 12:04:35 +0530 Subject: [PATCH 5/7] Fixing none pk values query creation --- .../src/ReadManyQueryHelper.cs | 55 +++++++------- .../CosmosReadManyItemsTests.cs | 75 +++++++++++++++++-- 2 files changed, 98 insertions(+), 32 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs index f543797151..7bcc251dd1 100644 --- a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs @@ -270,21 +270,7 @@ private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, Part queryStringBuilder.Append("SELECT * FROM c WHERE ( "); for (int i = startIndex; i < totalItemCount; i++) - { - object[] pkValues; - if (items[i].Item2.IsNone) - { - pkValues = new object[0]; - } - else - { - pkValues = items[i].Item2.InternalKey.ToObjectArray(); - if (pkValues.Length != this.partitionKeyDefinition.Paths.Count) - { - throw new ArgumentException("Number of components in the partition key value does not match the definition."); - } - } - + { string pkParamName = "@param_pk" + i; string idParamName = "@param_id" + i; sqlParameters.Add(new SqlParameter(idParamName, items[i].Item1)); @@ -292,18 +278,37 @@ private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, Part queryStringBuilder.Append("( "); queryStringBuilder.Append("c.id = "); queryStringBuilder.Append(idParamName); - for (int j = 0; j < pkValues.Length; j++) + if (items[i].Item2.IsNone) { - queryStringBuilder.Append(" AND "); - queryStringBuilder.Append("c"); - queryStringBuilder.Append(this.partitionKeySelectors[j]); - queryStringBuilder.Append(" = "); - - string pkParamNameForSinglePath = pkParamName + j; - sqlParameters.Add(new SqlParameter(pkParamNameForSinglePath, pkValues[j])); - queryStringBuilder.Append(pkParamNameForSinglePath); + for (int j = 0; j < this.partitionKeySelectors.Count; j++) + { + queryStringBuilder.Append(" AND "); + queryStringBuilder.Append("IS_DEFINED(c"); + queryStringBuilder.Append(this.partitionKeySelectors[j]); + queryStringBuilder.Append(") = false"); + } } - + else + { + object[] pkValues = items[i].Item2.InternalKey.ToObjectArray(); + if (pkValues.Length != this.partitionKeyDefinition.Paths.Count) + { + throw new ArgumentException("Number of components in the partition key " + + "value does not match the definition."); + } + for (int j = 0; j < this.partitionKeySelectors.Count; j++) + { + queryStringBuilder.Append(" AND "); + queryStringBuilder.Append("c"); + queryStringBuilder.Append(this.partitionKeySelectors[j]); + queryStringBuilder.Append(" = "); + + string pkParamNameForSinglePath = pkParamName + j; + sqlParameters.Add(new SqlParameter(pkParamNameForSinglePath, pkValues[j])); + queryStringBuilder.Append(pkParamNameForSinglePath); + } + } + queryStringBuilder.Append(" )"); if (i < totalItemCount - 1) diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs index 4613566f14..e460f89a4c 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs @@ -391,6 +391,25 @@ await container.CreateItemAsync( await database.DeleteAsync(); } + [TestMethod] + public async Task ReadManyWithNonePkValues() + { + for (int i = 0; i < 5; i++) + { + await this.Container.CreateItemAsync(new ActivityWithNoPk("id" + i.ToString()), + PartitionKey.None); + } + + List<(string, PartitionKey)> itemList = new List<(string, PartitionKey)>(); + for (int i = 0; i < 5; i++) + { + itemList.Add(("id" + i.ToString(), PartitionKey.None)); + } + + FeedResponse feedResponse = await this.Container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponse.Count, 5); + } + [TestMethod] public async Task ReadManyItemsFromNonPartitionedContainers() { @@ -408,16 +427,31 @@ public async Task ReadManyItemsFromNonPartitionedContainers() itemList.Add(("id" + i.ToString(), PartitionKey.None)); } - using (ResponseMessage responseMessage = await container.ReadManyItemsStreamAsync(itemList)) + FeedResponse feedResponse = await container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponse.Count, 5); + + // Start inserting documents with same id but new pk values + for (int i = 0; i < 5; i++) { - Assert.IsNotNull(responseMessage); - Assert.IsTrue(responseMessage.Headers.RequestCharge > 0); - Assert.IsNotNull(responseMessage.Diagnostics); + await container.CreateItemAsync(new ActivityWithSystemPk("id" + i.ToString(), "newPK"), + new PartitionKey("newPK")); + } - ToDoActivity[] items = this.cosmosClient.ClientContext.SerializerCore.FromFeedStream( - CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope(responseMessage.Content)); - Assert.AreEqual(items.Length, 5); + FeedResponse feedResponseWithPK = await container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponseWithPK.Count, 5); + + for (int i = 0; i < 5; i++) + { + itemList.Add(("id" + i.ToString(), new PartitionKey("newPK"))); } + feedResponseWithPK = await container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponseWithPK.Count, 10); + + // Mixing both None and non-None pk values + itemList = new List<(string, PartitionKey)> + { ("id0", PartitionKey.None), ("id0", new PartitionKey("newPK")) }; + feedResponse = await container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponse.Count, 2); } [TestMethod] @@ -537,5 +571,32 @@ public override async Task SendAsync(RequestMessage requestMess return await base.SendAsync(requestMessage, cancellationToken); } } + + private class ActivityWithNoPk + { + public ActivityWithNoPk(string id) + { + this.id = id; + } + +#pragma warning disable IDE1006 // Naming Styles + public string id { get; set; } +#pragma warning restore IDE1006 // Naming Styles + } + + private class ActivityWithSystemPk + { + public ActivityWithSystemPk(string id, string pk) + { + this.id = id; + this._partitionKey = pk; + } + +#pragma warning disable IDE1006 // Naming Styles + public string id { get; set; } +#pragma warning restore IDE1006 // Naming Styles + + public string _partitionKey { get; set; } + } } } From a4ad806b8c79dc38240e0015adca02c974b8803e Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Thu, 19 Aug 2021 17:27:36 +0530 Subject: [PATCH 6/7] fixing tests --- .../src/ReadManyQueryHelper.cs | 4 +- .../CosmosReadManyItemsTests.cs | 46 ++++++++++++++----- .../GatewayClientSideRequestStatsTests.cs | 2 + 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs index 7bcc251dd1..2e23048825 100644 --- a/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs +++ b/Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs @@ -280,11 +280,11 @@ private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, Part queryStringBuilder.Append(idParamName); if (items[i].Item2.IsNone) { - for (int j = 0; j < this.partitionKeySelectors.Count; j++) + foreach (string partitionKeySelector in this.partitionKeySelectors) { queryStringBuilder.Append(" AND "); queryStringBuilder.Append("IS_DEFINED(c"); - queryStringBuilder.Append(this.partitionKeySelectors[j]); + queryStringBuilder.Append(partitionKeySelector); queryStringBuilder.Append(") = false"); } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs index e460f89a4c..5e3975f828 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/CosmosReadManyItemsTests.cs @@ -408,6 +408,12 @@ await this.Container.CreateItemAsync(new ActivityWithNoPk("id" + i.ToString()), FeedResponse feedResponse = await this.Container.ReadManyItemsAsync(itemList); Assert.AreEqual(feedResponse.Count, 5); + int j = 0; + foreach (ActivityWithNoPk item in feedResponse.Resource) + { + Assert.AreEqual(item.id, "id" + j); + j++; + } } [TestMethod] @@ -422,7 +428,7 @@ public async Task ReadManyItemsFromNonPartitionedContainers() // read using PartitionKey.None pk value List<(string, PartitionKey)> itemList = new List<(string, PartitionKey)>(); - for (int i = 0; i < 10; i++) + for (int i = 0; i < 5; i++) { itemList.Add(("id" + i.ToString(), PartitionKey.None)); } @@ -437,21 +443,35 @@ await container.CreateItemAsync(new ActivityWithSystemPk("id" + i.ToString(), "n new PartitionKey("newPK")); } - FeedResponse feedResponseWithPK = await container.ReadManyItemsAsync(itemList); - Assert.AreEqual(feedResponseWithPK.Count, 5); + feedResponse = await container.ReadManyItemsAsync(itemList); + Assert.AreEqual(feedResponse.Count, 5); + int j = 0; + foreach (ActivityWithNoPk item in feedResponse.Resource) + { + Assert.AreEqual(item.id, "id" + j); + j++; + } for (int i = 0; i < 5; i++) { itemList.Add(("id" + i.ToString(), new PartitionKey("newPK"))); } - feedResponseWithPK = await container.ReadManyItemsAsync(itemList); + FeedResponse feedResponseWithPK = await container.ReadManyItemsAsync(itemList); Assert.AreEqual(feedResponseWithPK.Count, 10); - - // Mixing both None and non-None pk values - itemList = new List<(string, PartitionKey)> - { ("id0", PartitionKey.None), ("id0", new PartitionKey("newPK")) }; - feedResponse = await container.ReadManyItemsAsync(itemList); - Assert.AreEqual(feedResponse.Count, 2); + j = 0; + foreach (ActivityWithSystemPk item in feedResponseWithPK.Resource) + { + Assert.AreEqual(item.id, "id" + (j % 5)); + if (j > 4) + { + Assert.AreEqual(item._partitionKey, "newPK"); + } + else + { + Assert.IsNull(item._partitionKey); + } + j++; + } } [TestMethod] @@ -586,17 +606,19 @@ public ActivityWithNoPk(string id) private class ActivityWithSystemPk { - public ActivityWithSystemPk(string id, string pk) + public ActivityWithSystemPk(string id, string _partitionKey) { this.id = id; - this._partitionKey = pk; + this._partitionKey = _partitionKey; } #pragma warning disable IDE1006 // Naming Styles public string id { get; set; } #pragma warning restore IDE1006 // Naming Styles +#pragma warning disable IDE1006 // Naming Styles public string _partitionKey { get; set; } +#pragma warning restore IDE1006 // Naming Styles } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayClientSideRequestStatsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayClientSideRequestStatsTests.cs index c01a449da9..8d65a5e7f0 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayClientSideRequestStatsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.EmulatorTests/GatewayClientSideRequestStatsTests.cs @@ -41,6 +41,7 @@ public async Task GatewayRequestStatsTest() { ToDoActivity item = ToDoActivity.CreateRandomToDoActivity(); ItemResponse response = await this.Container.CreateItemAsync(item); + ClientSideRequestStatisticsTraceDatum datum = this.GetClientSideRequestStatsFromTrace(((CosmosTraceDiagnostics)response.Diagnostics).Value, "Transport"); Assert.IsNotNull(datum.HttpResponseStatisticsList); Assert.AreEqual(datum.HttpResponseStatisticsList.Count, 1); @@ -69,6 +70,7 @@ public async Task GatewayRetryRequestStatsTest(string uriToThrow, string traceTo using (CosmosClient cosmosClient = TestCommon.CreateCosmosClient(options)) { Container container = cosmosClient.GetContainer(this.Database.Id, this.Container.Id); + ItemResponse response = await container.ReadItemAsync(item.id, new PartitionKey(item.pk)); ClientSideRequestStatisticsTraceDatum datum = this.GetClientSideRequestStatsFromTrace(((CosmosTraceDiagnostics)response.Diagnostics).Value, traceToFind); Assert.IsNotNull(datum.HttpResponseStatisticsList); From 77f4995ff29654b6d6dde6bb6bab21ef602fa202 Mon Sep 17 00:00:00 2001 From: Asket Agarwal Date: Fri, 20 Aug 2021 02:14:07 +0530 Subject: [PATCH 7/7] missing return statement --- .../src/Resource/Container/ContainerCore.Items.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs index b659289a02..e7a9205377 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Container/ContainerCore.Items.cs @@ -298,14 +298,14 @@ public async Task ReadManyItemsStreamAsync( throw new ArgumentNullException(nameof(trace)); } - PartitionKeyDefinition partitionKeyDefinition = null; + PartitionKeyDefinition partitionKeyDefinition; try { partitionKeyDefinition = await this.GetPartitionKeyDefinitionAsync(); } catch (CosmosException ex) { - ex.ToCosmosResponseMessage(request: null); + return ex.ToCosmosResponseMessage(request: null); } ReadManyHelper readManyHelper = new ReadManyQueryHelper(partitionKeyDefinition,