diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java index 27a1e4456001..19b2bbc3cc30 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java @@ -68,8 +68,6 @@ import java.util.EnumSet; import java.util.List; import java.util.Locale; -import java.util.Map; -import java.util.Set; import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -146,7 +144,7 @@ public void beforeTest( AsyncDocumentClient asyncDocumentClient = ReflectionUtils.getAsyncDocumentClient(this.client.asyncClient()); RxDocumentClientImpl rxDocumentClient = (RxDocumentClientImpl) asyncDocumentClient; - Set writeRegions = this.getAvailableRegionNames(rxDocumentClient, true); + List writeRegions = this.getAvailableWriteRegionNames(rxDocumentClient); assertThat(writeRegions).isNotNull().isNotEmpty(); this.preferredRegion = writeRegions.iterator().next(); @@ -1511,7 +1509,7 @@ private void validateMetrics(Tag expectedOperationTag, Tag expectedRequestTag, i this.assertMetrics( "cosmos.client.op.regionsContacted", true, - Tag.of(TagName.RegionName.toString(), this.preferredRegion)); + Tag.of(TagName.RegionName.toString(), this.preferredRegion.toLowerCase(Locale.ROOT))); } if (this.getEffectiveMetricCategories().contains(MetricCategory.RequestSummary)) { @@ -1531,7 +1529,7 @@ private void validateMetrics(Tag expectedOperationTag, Tag expectedRequestTag, i this.assertMetrics( "cosmos.client.req.rntbd.latency", true, - Tag.of(TagName.RegionName.toString(), this.preferredRegion)); + Tag.of(TagName.RegionName.toString(), this.preferredRegion.toLowerCase(Locale.ROOT))); this.assertMetrics("cosmos.client.req.rntbd.backendLatency", true, expectedRequestTag); this.assertMetrics("cosmos.client.req.rntbd.requests", true, expectedRequestTag); Meter reportedRntbdRequestCharge = @@ -1548,7 +1546,7 @@ private void validateMetrics(Tag expectedOperationTag, Tag expectedRequestTag, i this.assertMetrics( "cosmos.client.req.gw.latency", true, - Tag.of(TagName.RegionName.toString(), this.preferredRegion)); + Tag.of(TagName.RegionName.toString(), this.preferredRegion.toLowerCase(Locale.ROOT))); } this.assertMetrics("cosmos.client.req.gw.backendLatency", false, expectedRequestTag); this.assertMetrics("cosmos.client.req.gw.requests", true, expectedRequestTag); @@ -1654,7 +1652,7 @@ private Meter assertMetrics(String prefix, boolean expectedToFind, Tag withTag) } } - private Set getAvailableRegionNames(RxDocumentClientImpl rxDocumentClient, boolean isWriteRegion) { + private List getAvailableWriteRegionNames(RxDocumentClientImpl rxDocumentClient) { try { GlobalEndpointManager globalEndpointManager = ReflectionUtils.getGlobalEndpointManager(rxDocumentClient); LocationCache locationCache = ReflectionUtils.getLocationCache(globalEndpointManager); @@ -1665,22 +1663,13 @@ private Set getAvailableRegionNames(RxDocumentClientImpl rxDocumentClien Class DatabaseAccountLocationsInfoClass = Class.forName("com.azure.cosmos.implementation.routing" + ".LocationCache$DatabaseAccountLocationsInfo"); + Field availableWriteLocations = DatabaseAccountLocationsInfoClass.getDeclaredField( + "availableWriteLocations"); + availableWriteLocations.setAccessible(true); + @SuppressWarnings("unchecked") + List list = (List) availableWriteLocations.get(locationInfo); + return list; - if (isWriteRegion) { - Field availableWriteEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( - "availableWriteEndpointByLocation"); - availableWriteEndpointByLocation.setAccessible(true); - @SuppressWarnings("unchecked") - Map map = (Map) availableWriteEndpointByLocation.get(locationInfo); - return map.keySet(); - } else { - Field availableReadEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( - "availableReadEndpointByLocation"); - availableReadEndpointByLocation.setAccessible(true); - @SuppressWarnings("unchecked") - Map map = (Map) availableReadEndpointByLocation.get(locationInfo); - return map.keySet(); - } } catch (Exception error) { fail(error.toString()); diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosDiagnosticsTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosDiagnosticsTest.java index f692a2433031..6e1d15752f82 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosDiagnosticsTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/CosmosDiagnosticsTest.java @@ -1725,13 +1725,13 @@ private void validateRegionContacted(CosmosDiagnostics cosmosDiagnostics, Cosmos Class DatabaseAccountLocationsInfoClass = Class.forName("com.azure.cosmos.implementation.routing" + ".LocationCache$DatabaseAccountLocationsInfo"); - Field availableWriteEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( - "availableWriteEndpointByLocation"); - availableWriteEndpointByLocation.setAccessible(true); + Field availableWriteLocations = DatabaseAccountLocationsInfoClass.getDeclaredField( + "availableWriteLocations"); + availableWriteLocations.setAccessible(true); @SuppressWarnings("unchecked") - Map map = (Map) availableWriteEndpointByLocation.get(locationInfo); - String regionName = map.keySet().iterator().next(); + List list = (List) availableWriteLocations.get(locationInfo); + String regionName = list.get(0); assertThat(cosmosDiagnostics.getContactedRegionNames().size()).isEqualTo(1); assertThat(cosmosDiagnostics.getContactedRegionNames().iterator().next()).isEqualTo(regionName.toLowerCase()); } diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/SessionNotAvailableRetryTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/SessionNotAvailableRetryTest.java index 7f19b29db84f..69b2f05aa862 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/SessionNotAvailableRetryTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/SessionNotAvailableRetryTest.java @@ -43,7 +43,6 @@ import reactor.core.publisher.Mono; import java.lang.reflect.Field; -import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -51,7 +50,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -354,7 +352,7 @@ public void sessionNotAvailableRetryWithoutPreferredList(OperationType operation } catch (CosmosException ex) { assertThat(ex.getStatusCode()).isEqualTo(HttpConstants.StatusCodes.NOTFOUND); assertThat(ex.getDiagnostics().getContactedRegionNames().size()).isEqualTo(1); - String regionName = getAvailableRegionNames(rxDocumentClient, true).iterator().next(); + String regionName = getRegionNames(rxDocumentClient).iterator().next(); assertThat(ex.getDiagnostics().getContactedRegionNames().iterator().next()).isEqualTo(regionName.toLowerCase()); } @@ -409,7 +407,7 @@ private Map getRegionMap(DatabaseAccount databaseAccount, boolea return regionMap; } - private Set getAvailableRegionNames(RxDocumentClientImpl rxDocumentClient, boolean isWriteRegion) throws Exception { + private List getRegionNames(RxDocumentClientImpl rxDocumentClient) throws Exception { GlobalEndpointManager globalEndpointManager = ReflectionUtils.getGlobalEndpointManager(rxDocumentClient); LocationCache locationCache = ReflectionUtils.getLocationCache(globalEndpointManager); @@ -420,21 +418,12 @@ private Set getAvailableRegionNames(RxDocumentClientImpl rxDocumentClien Class DatabaseAccountLocationsInfoClass = Class.forName("com.azure.cosmos.implementation.routing" + ".LocationCache$DatabaseAccountLocationsInfo"); - if (isWriteRegion) { - Field availableWriteEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( - "availableWriteEndpointByLocation"); - availableWriteEndpointByLocation.setAccessible(true); - @SuppressWarnings("unchecked") - Map map = (Map) availableWriteEndpointByLocation.get(locationInfo); - return map.keySet(); - } else { - Field availableReadEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( - "availableReadEndpointByLocation"); - availableReadEndpointByLocation.setAccessible(true); - @SuppressWarnings("unchecked") - Map map = (Map) availableReadEndpointByLocation.get(locationInfo); - return map.keySet(); - } + Field availableWriteEndpointByLocation = DatabaseAccountLocationsInfoClass.getDeclaredField( + "availableWriteLocations"); + availableWriteEndpointByLocation.setAccessible(true); + @SuppressWarnings("unchecked") + List list = (List) availableWriteEndpointByLocation.get(locationInfo); + return list; } private class RntbdTransportClientTest extends TransportClient { diff --git a/sdk/cosmos/azure-cosmos/CHANGELOG.md b/sdk/cosmos/azure-cosmos/CHANGELOG.md index bfb7a19cca5f..e7d40c20774a 100644 --- a/sdk/cosmos/azure-cosmos/CHANGELOG.md +++ b/sdk/cosmos/azure-cosmos/CHANGELOG.md @@ -7,6 +7,7 @@ #### Breaking Changes #### Bugs Fixed +* Fixed an issue where `contactedRegions` shows the wrong region in a multi region account if no preferred regions are specified. - See [PR 41045](https://github.com/Azure/azure-sdk-for-java/pull/41045) #### Other Changes * Added metrics and tracing for ReadMany operations. - See [PR 41042](https://github.com/Azure/azure-sdk-for-java/pull/41042) diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java index a7bbbfc70855..30ad1696aa5e 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/routing/LocationCache.java @@ -422,7 +422,7 @@ public String getRegionName(URI locationEndpoint, com.azure.cosmos.implementatio } //If preferred list is not set, locationEndpoint will be default endpoint, so return the hub region - return this.locationInfo.availableWriteEndpointByLocation.keySet().iterator().next(); + return this.locationInfo.availableWriteLocations.get(0).toLowerCase(Locale.ROOT); } private boolean areEqual(URI url1, URI url2) {