Example #1
0
        public async Task InitializeAsync_WithContainerPartitionedBySystemPK()
        {
            ContainerProperties containerProperties = new ContainerProperties
            {
                PartitionKey = new Documents.PartitionKeyDefinition()
                {
                    IsSystemKey = true
                }
            };


            Mock <ContainerInternal> leaseContainerMock = new Mock <ContainerInternal>();

            leaseContainerMock.Setup(c => c.GetCachedContainerPropertiesAsync(
                                         It.Is <bool>(b => b == false),
                                         It.IsAny <ITrace>(),
                                         It.IsAny <CancellationToken>()))
            .ReturnsAsync(containerProperties);

            await DocumentServiceLeaseStoreManagerBuilder.InitializeAsync(
                Mock.Of <ContainerInternal>(),
                leaseContainerMock.Object,
                Guid.NewGuid().ToString(),
                Guid.NewGuid().ToString());
        }
Example #2
0
        public async Task InitializeAsync_WithContainerPartitionedByRandom()
        {
            ContainerProperties containerProperties = new ContainerProperties
            {
                PartitionKey = new Documents.PartitionKeyDefinition()
                {
                    Paths = new System.Collections.ObjectModel.Collection <string>()
                    {
                        "/random"
                    }
                }
            };


            Mock <ContainerInternal> leaseContainerMock = new Mock <ContainerInternal>();

            leaseContainerMock.Setup(c => c.GetCachedContainerPropertiesAsync(
                                         It.Is <bool>(b => b == false),
                                         It.IsAny <ITrace>(),
                                         It.IsAny <CancellationToken>()))
            .ReturnsAsync(containerProperties);

            await Assert.ThrowsExceptionAsync <ArgumentException>(() => DocumentServiceLeaseStoreManagerBuilder.InitializeAsync(
                                                                      Mock.Of <ContainerInternal>(),
                                                                      leaseContainerMock.Object,
                                                                      Guid.NewGuid().ToString(),
                                                                      Guid.NewGuid().ToString()));
        }
Example #3
0
        private async Task InitializeAsync()
        {
            string monitoredDatabaseAndContainerRid = await this.monitoredContainer.GetMonitoredDatabaseAndContainerRidAsync();

            string leaseContainerPrefix = this.monitoredContainer.GetLeasePrefix(this.changeFeedLeaseOptions.LeasePrefix, monitoredDatabaseAndContainerRid);

            if (this.documentServiceLeaseStoreManager == null)
            {
                this.documentServiceLeaseStoreManager = await DocumentServiceLeaseStoreManagerBuilder.InitializeAsync(this.leaseContainer, leaseContainerPrefix, this.instanceName).ConfigureAwait(false);
            }

            this.partitionManager = this.BuildPartitionManager();
            this.initialized      = true;
        }
        internal static async Task <DocumentServiceLeaseStoreManager> InitializeLeaseStoreManagerAsync(
            DocumentServiceLeaseStoreManager documentServiceLeaseStoreManager,
            ContainerInternal leaseContainer,
            string leaseContainerPrefix,
            string instanceName)
        {
            if (documentServiceLeaseStoreManager == null)
            {
                ContainerResponse cosmosContainerResponse = await leaseContainer.ReadContainerAsync().ConfigureAwait(false);

                ContainerProperties containerProperties = cosmosContainerResponse.Resource;

                bool isPartitioned =
                    containerProperties.PartitionKey != null &&
                    containerProperties.PartitionKey.Paths != null &&
                    containerProperties.PartitionKey.Paths.Count > 0;
                bool isMigratedFixed = (containerProperties.PartitionKey?.IsSystemKey == true);
                if (isPartitioned &&
                    !isMigratedFixed &&
                    (containerProperties.PartitionKey.Paths.Count != 1 || containerProperties.PartitionKey.Paths[0] != "/id"))
                {
                    throw new ArgumentException("The lease collection, if partitioned, must have partition key equal to id.");
                }

                RequestOptionsFactory requestOptionsFactory = isPartitioned && !isMigratedFixed ?
                                                              (RequestOptionsFactory) new PartitionedByIdCollectionRequestOptionsFactory() :
                                                              (RequestOptionsFactory) new SinglePartitionRequestOptionsFactory();

                DocumentServiceLeaseStoreManagerBuilder leaseStoreManagerBuilder = new DocumentServiceLeaseStoreManagerBuilder()
                                                                                   .WithLeasePrefix(leaseContainerPrefix)
                                                                                   .WithLeaseContainer(leaseContainer)
                                                                                   .WithRequestOptionsFactory(requestOptionsFactory)
                                                                                   .WithHostName(instanceName);

                documentServiceLeaseStoreManager = await leaseStoreManagerBuilder.BuildAsync().ConfigureAwait(false);
            }

            return(documentServiceLeaseStoreManager);
        }
        private async Task <ILeaseStoreManager> GetLeaseStoreManagerAsync(
            DocumentCollectionInfo collectionInfo,
            bool isPartitionKeyByIdRequiredIfPartitioned)
        {
            if (this.LeaseStoreManager == null)
            {
                var leaseDocumentClient = this.leaseDocumentClient ?? this.leaseCollectionLocation.CreateDocumentClient();
                var collection          = await leaseDocumentClient.GetDocumentCollectionAsync(collectionInfo).ConfigureAwait(false);

                bool isPartitioned =
                    collection.PartitionKey != null &&
                    collection.PartitionKey.Paths != null &&
                    collection.PartitionKey.Paths.Count > 0;
                if (isPartitioned && isPartitionKeyByIdRequiredIfPartitioned &&
                    (collection.PartitionKey.Paths.Count != 1 || collection.PartitionKey.Paths[0] != "/id"))
                {
                    throw new ArgumentException("The lease collection, if partitioned, must have partition key equal to id.");
                }

                var requestOptionsFactory = isPartitioned ?
                                            (IRequestOptionsFactory) new PartitionedByIdCollectionRequestOptionsFactory() :
                                            (IRequestOptionsFactory) new SinglePartitionRequestOptionsFactory();

                string leasePrefix = this.GetLeasePrefix();
                var    leaseStoreManagerBuilder = new DocumentServiceLeaseStoreManagerBuilder()
                                                  .WithLeasePrefix(leasePrefix)
                                                  .WithLeaseCollection(this.leaseCollectionLocation)
                                                  .WithLeaseCollectionLink(collection.SelfLink)
                                                  .WithRequestOptionsFactory(requestOptionsFactory)
                                                  .WithHostName(this.HostName);

                leaseStoreManagerBuilder = leaseStoreManagerBuilder.WithLeaseDocumentClient(leaseDocumentClient);

                this.LeaseStoreManager = await leaseStoreManagerBuilder.BuildAsync().ConfigureAwait(false);
            }

            return(this.LeaseStoreManager);
        }
        private async Task InitializeLeaseStoreAsync(CancellationToken cancellationToken)
        {
            if (this.documentServiceLeaseContainer == null)
            {
                string monitoredContainerAndDatabaseRid = await this.monitoredContainer.GetMonitoredDatabaseAndContainerRidAsync();

                string leasePrefix = this.monitoredContainer.GetLeasePrefix(this.processorName, monitoredContainerAndDatabaseRid);
                DocumentServiceLeaseStoreManager documentServiceLeaseStoreManager = await DocumentServiceLeaseStoreManagerBuilder.InitializeAsync(
                    leaseContainer : this.leaseContainer,
                    leaseContainerPrefix : leasePrefix,
                    instanceName : ChangeFeedEstimatorIterator.EstimatorDefaultHostName);

                this.documentServiceLeaseContainer = documentServiceLeaseStoreManager.LeaseContainer;
            }
        }