Esempio n. 1
0
        public void VerifyAllReadRegionsAreVisited(int numOfReadRegions)
        {
            Mock <IGlobalEndpointManager> mockEndpointManager = new Mock <IGlobalEndpointManager>(MockBehavior.Strict);

            GlobalPartitionEndpointManagerCore failoverManager = new GlobalPartitionEndpointManagerCore(mockEndpointManager.Object);
            List <Uri> readRegions = new List <Uri>();

            for (int i = 0; i < numOfReadRegions; i++)
            {
                readRegions.Add(new Uri($"https://localhost:{i}/"));
            }

            mockEndpointManager.Setup(x => x.ReadEndpoints).Returns(() => new ReadOnlyCollection <Uri>(readRegions));

            // Create a random pk range
            PartitionKeyRange partitionKeyRange = new PartitionKeyRange()
            {
                Id           = "0",
                MinInclusive = "",
                MaxExclusive = "BB"
            };

            PartitionKeyRange partitionKeyRangeNotOverriden = new PartitionKeyRange()
            {
                Id           = "1",
                MinInclusive = "BB",
                MaxExclusive = "FF"
            };

            using DocumentServiceRequest createRequest             = DocumentServiceRequest.Create(OperationType.Create, ResourceType.Document, AuthorizationTokenType.PrimaryMasterKey);
            createRequest.RequestContext.ResolvedPartitionKeyRange = partitionKeyRange;
            createRequest.RequestContext.RouteToLocation(readRegions.First());

            mockEndpointManager.Setup(x => x.CanUseMultipleWriteLocations(createRequest)).Returns(false);

            foreach (Uri region in readRegions)
            {
                Assert.AreEqual(region, createRequest.RequestContext.LocationEndpointToRoute);
                bool tryFailover = failoverManager.TryMarkEndpointUnavailableForPartitionKeyRange(
                    createRequest);

                // If there are no more regions to failover it will return false.
                if (region == readRegions.Last())
                {
                    Assert.IsFalse(tryFailover);
                    Assert.IsFalse(failoverManager.TryAddPartitionLevelLocationOverride(createRequest));
                }
                else
                {
                    Assert.IsTrue(tryFailover);
                    Assert.IsTrue(failoverManager.TryAddPartitionLevelLocationOverride(createRequest));
                    Assert.AreNotEqual(region, createRequest.RequestContext.LocationEndpointToRoute);
                }
            }
        }
Esempio n. 2
0
        public void TestSingleReadRegionScenario()
        {
            Mock <IGlobalEndpointManager>      mockEndpointManager = new Mock <IGlobalEndpointManager>(MockBehavior.Strict);
            GlobalPartitionEndpointManagerCore failoverManager     = new GlobalPartitionEndpointManagerCore(mockEndpointManager.Object);

            mockEndpointManager.Setup(x => x.ReadEndpoints).Returns(() => new ReadOnlyCollection <Uri>(new List <Uri>()
            {
                new Uri("https://localhost:443/")
            }));

            PartitionKeyRange partitionKeyRange = new PartitionKeyRange()
            {
                Id           = "0",
                MinInclusive = "",
                MaxExclusive = "FF"
            };

            Uri routeToLocation = new Uri("https://localhost:443/");

            using DocumentServiceRequest readRequest             = DocumentServiceRequest.Create(OperationType.Read, ResourceType.Document, AuthorizationTokenType.PrimaryMasterKey);
            readRequest.RequestContext.ResolvedPartitionKeyRange = partitionKeyRange;
            readRequest.RequestContext.RouteToLocation(routeToLocation);
            Assert.IsFalse(failoverManager.TryMarkEndpointUnavailableForPartitionKeyRange(
                               readRequest));
            Assert.IsFalse(failoverManager.TryAddPartitionLevelLocationOverride(
                               readRequest));

            using DocumentServiceRequest createRequest             = DocumentServiceRequest.Create(OperationType.Create, ResourceType.Document, AuthorizationTokenType.PrimaryMasterKey);
            createRequest.RequestContext.ResolvedPartitionKeyRange = partitionKeyRange;
            readRequest.RequestContext.RouteToLocation(routeToLocation);
            Assert.IsFalse(failoverManager.TryMarkEndpointUnavailableForPartitionKeyRange(
                               createRequest));
            Assert.IsFalse(failoverManager.TryAddPartitionLevelLocationOverride(
                               createRequest));

            using DocumentServiceRequest databaseRequest = DocumentServiceRequest.Create(OperationType.Read, ResourceType.Database, AuthorizationTokenType.PrimaryMasterKey);
            readRequest.RequestContext.RouteToLocation(routeToLocation);
            Assert.IsFalse(failoverManager.TryMarkEndpointUnavailableForPartitionKeyRange(
                               databaseRequest));
            Assert.IsFalse(failoverManager.TryAddPartitionLevelLocationOverride(
                               databaseRequest));
        }