Пример #1
0
        private async Task <IPartitionManager> BuildPartitionManagerAsync(ILeaseManager leaseManager)
        {
            this.leaseDocumentClient = this.leaseDocumentClient ?? this.leaseCollectionLocation.CreateDocumentClient();

            DocumentCollection leaseCollection = await this.leaseDocumentClient.GetDocumentCollectionAsync(this.leaseCollectionLocation).ConfigureAwait(false);

            string leaseStoreCollectionLink = leaseCollection.SelfLink;

            string collectionSelfLink       = this.feedCollectionLocation.GetCollectionSelfLink();
            var    factory                  = new CheckpointerObserverFactory(this.observerFactory, this.changeFeedProcessorOptions.CheckpointFrequency);
            var    synchronizer             = new PartitionSynchronizer(this.feedDocumentClient, collectionSelfLink, leaseManager, this.changeFeedProcessorOptions.DegreeOfParallelism, this.changeFeedProcessorOptions.QueryPartitionsMaxBatchSize);
            var    leaseStore               = new LeaseStore(this.leaseDocumentClient, this.leaseCollectionLocation, this.GetLeasePrefix(), leaseStoreCollectionLink);
            var    bootstrapper             = new Bootstrapper(synchronizer, leaseStore, this.lockTime, this.sleepTime);
            var    partitionObserverFactory = new PartitionSupervisorFactory(
                factory,
                leaseManager,
                this.partitionProcessorFactory ?? new PartitionProcessorFactory(this.feedDocumentClient, this.changeFeedProcessorOptions, leaseManager, collectionSelfLink),
                this.changeFeedProcessorOptions);

            var partitionController = new PartitionController(leaseManager, partitionObserverFactory, synchronizer);

            if (this.loadBalancingStrategy == null)
            {
                this.loadBalancingStrategy = new EqualPartitionsBalancingStrategy(this.hostName, this.changeFeedProcessorOptions.MinPartitionCount, this.changeFeedProcessorOptions.MaxPartitionCount, this.changeFeedProcessorOptions.LeaseExpirationInterval);
            }

            var partitionLoadBalancer = new PartitionLoadBalancer(partitionController, leaseManager, this.loadBalancingStrategy, this.changeFeedProcessorOptions.LeaseAcquireInterval);

            return(new PartitionManager(bootstrapper, partitionController, partitionLoadBalancer));
        }
        public async Task MarkInitializedAsync_ShouldSucceed_IfMarkerCreated()
        {
            var client     = Mock.Of <IChangeFeedDocumentClient>();
            var leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            await leaseStore.MarkInitializedAsync();

            Mock.Get(client)
            .Verify(c =>
                    c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.Id == storeMarker)),
                    Times.Once);
        }
        public async Task LockInitializationAsync_ShouldThrow_IfLockThrows()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>()))
            .ThrowsAsync(DocumentExceptionHelpers.CreateRequestRateTooLargeException());

            var       leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            Exception exception  = await Record.ExceptionAsync(() => leaseStore.LockInitializationAsync(lockTime));

            Assert.IsAssignableFrom <DocumentClientException>(exception);
        }
        public async Task LockInitializationAsync_ShouldReturnTrue_IfLockSucceeds()
        {
            var  client     = Mock.Of <IChangeFeedDocumentClient>();
            var  leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            bool isLocked   = await leaseStore.LockInitializationAsync(lockTime);

            Assert.True(isLocked);

            Mock.Get(client)
            .Verify(c =>
                    c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.TimeToLive == (int)lockTime.TotalSeconds && d.Id == "prefix.lock")),
                    Times.Once);
        }
        public async Task IsInitializedAsync_ShouldReturnFalse_IfNoDocument()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.ReadDocumentAsync(It.Is <Uri>(uri => uri.ToString().EndsWith(storeMarker))))
            .ThrowsAsync(DocumentExceptionHelpers.CreateNotFoundException());

            var  leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            bool isInited   = await leaseStore.IsInitializedAsync();

            Assert.False(isInited);
        }
        public async Task IsInitializedAsync_ShouldReturnTrue_IfDocumentExist()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.ReadDocumentAsync(It.Is <Uri>(uri => uri.ToString().EndsWith(storeMarker))))
            .ReturnsAsync(CreateResponse());

            var  leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            bool isInited   = await leaseStore.IsInitializedAsync();

            Assert.True(isInited);
        }
        public async Task MarkInitializedAsync_ShouldSucceed_IfMarkerConflicts()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>()))
            .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException());

            var leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            await leaseStore.MarkInitializedAsync();

            Mock.Get(client)
            .Verify(c =>
                    c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.Id == storeMarker)),
                    Times.Once);
        }
        public async Task LockInitializationAsync_ShouldReturnFalse_IfLockConflicts()
        {
            var client = Mock.Of <IChangeFeedDocumentClient>();

            Mock.Get(client)
            .Setup(c => c.CreateDocumentAsync(It.IsAny <string>(), It.IsAny <object>()))
            .ThrowsAsync(DocumentExceptionHelpers.CreateConflictException());

            var  leaseStore = new LeaseStore(client, collectionInfo, containerNamePrefix, leaseStoreCollectionLink);
            bool isLocked   = await leaseStore.LockInitializationAsync(lockTime);

            Assert.False(isLocked);

            Mock.Get(client)
            .Verify(c =>
                    c.CreateDocumentAsync(leaseStoreCollectionLink, It.Is <Document>(d => d.TimeToLive == (int)lockTime.TotalSeconds && d.Id == "prefix.lock")),
                    Times.Once);
        }