Ejemplo 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);
                }
            }
        }
Ejemplo n.º 2
0
        public void GlobalAddressResolverUpdateAsyncSynchronizationTest()
        {
            SynchronizationContext prevContext = SynchronizationContext.Current;

            try
            {
                TestSynchronizationContext syncContext = new TestSynchronizationContext();
                SynchronizationContext.SetSynchronizationContext(syncContext);
                syncContext.Post(_ =>
                {
                    UserAgentContainer container      = new UserAgentContainer(clientId: 0);
                    FakeMessageHandler messageHandler = new FakeMessageHandler();

                    AccountProperties databaseAccount = new AccountProperties();
                    Mock <IDocumentClientInternal> mockDocumentClient = new Mock <IDocumentClientInternal>();
                    mockDocumentClient.Setup(owner => owner.ServiceEndpoint).Returns(new Uri("https://blabla.com/"));
                    mockDocumentClient.Setup(owner => owner.GetDatabaseAccountInternalAsync(It.IsAny <Uri>(), It.IsAny <CancellationToken>())).ReturnsAsync(databaseAccount);

                    GlobalEndpointManager globalEndpointManager = new GlobalEndpointManager(mockDocumentClient.Object, new ConnectionPolicy());
                    GlobalPartitionEndpointManager partitionKeyRangeLocationCache = new GlobalPartitionEndpointManagerCore(globalEndpointManager);

                    ConnectionPolicy connectionPolicy = new ConnectionPolicy
                    {
                        RequestTimeout = TimeSpan.FromSeconds(10)
                    };

                    GlobalAddressResolver globalAddressResolver = new GlobalAddressResolver(
                        endpointManager: globalEndpointManager,
                        partitionKeyRangeLocationCache: partitionKeyRangeLocationCache,
                        protocol: Documents.Client.Protocol.Tcp,
                        tokenProvider: this.mockTokenProvider.Object,
                        collectionCache: null,
                        routingMapProvider: null,
                        serviceConfigReader: this.mockServiceConfigReader.Object,
                        connectionPolicy: connectionPolicy,
                        httpClient: MockCosmosUtil.CreateCosmosHttpClient(() => new HttpClient(messageHandler)));

                    ConnectionStateListener connectionStateListener = new ConnectionStateListener(globalAddressResolver);
                    connectionStateListener.OnConnectionEvent(ConnectionEvent.ReadEof, DateTime.Now, new Documents.Rntbd.ServerKey(new Uri("https://endpoint.azure.com:4040/")));
                }, state: null);
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(prevContext);
            }
        }
Ejemplo n.º 3
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));
        }