private async Task RefreshAsync(DocumentServiceRequest request, CancellationToken cancellationToken)
        {
            System.Diagnostics.Debug.Assert(request.IsNameBased);
            InternalCache cache            = this.GetCache(request.Headers[HttpConstants.HttpHeaders.Version]);
            string        resourceFullName = PathsHelper.GetCollectionPath(request.ResourceAddress);

            if (request.RequestContext.ResolvedCollectionRid != null)
            {
                // Here we will issue backend call only if cache wasn't already refreshed (if whatever is there corresponds to presiously resolved collection rid).
                await cache.collectionInfoByName.GetAsync(
                    resourceFullName,
                    ContainerProperties.CreateWithResourceId(request.RequestContext.ResolvedCollectionRid),
                    async() =>
                {
                    DateTime currentTime           = DateTime.UtcNow;
                    ContainerProperties collection = await this.GetByNameAsync(request.Headers[HttpConstants.HttpHeaders.Version], resourceFullName, cancellationToken);
                    cache.collectionInfoById.Set(collection.ResourceId, collection);
                    cache.collectionInfoByNameLastRefreshTime.AddOrUpdate(resourceFullName, currentTime,
                                                                          (string currentKey, DateTime currentValue) => currentTime);
                    cache.collectionInfoByIdLastRefreshTime.AddOrUpdate(collection.ResourceId, currentTime,
                                                                        (string currentKey, DateTime currentValue) => currentTime);
                    return(collection);
                },
                    cancellationToken);
            }
            else
            {
                // In case of ForceRefresh directive coming from client, there will be no ResolvedCollectionRid, so we
                // need to refresh unconditionally.
                this.Refresh(request.ResourceAddress, request.Headers[HttpConstants.HttpHeaders.Version]);
            }

            request.RequestContext.ResolvedCollectionRid = null;
        }
        private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(new ServerStoreModel(null), null, null);
            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult(ContainerProperties.CreateWithResourceId("test")));

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(null));


            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            this.InitStoreModels();
        }
        private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(null, new ServerStoreModel(null), null, null);

            ContainerProperties containerProperties = ContainerProperties.CreateWithResourceId("test");

            containerProperties.PartitionKey = partitionKeyDefinition;
            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>(),
                    It.IsAny <ITrace>()
                    )
                ).Returns(Task.FromResult(containerProperties));

            this.collectionCache.Setup(x =>
                                       x.ResolveByNameAsync(
                                           It.IsAny <string>(),
                                           It.IsAny <string>(),
                                           It.IsAny <bool>(),
                                           It.IsAny <ITrace>(),
                                           It.IsAny <IClientSideRequestStatistics>(),
                                           It.IsAny <CancellationToken>())).Returns(Task.FromResult(containerProperties));

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>(),
                    It.IsAny <ITrace>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(null));

            List <PartitionKeyRange> result = new List <PartitionKeyRange>
            {
                new PartitionKeyRange()
                {
                    MinInclusive = Documents.Routing.PartitionKeyInternal.MinimumInclusiveEffectivePartitionKey,
                    MaxExclusive = Documents.Routing.PartitionKeyInternal.MaximumExclusiveEffectivePartitionKey,
                    Id           = "0"
                }
            };

            this.partitionKeyRangeCache
            .Setup(m => m.TryGetOverlappingRangesAsync(
                       It.IsAny <string>(),
                       It.IsAny <Documents.Routing.Range <string> >(),
                       It.IsAny <ITrace>(),
                       It.IsAny <bool>()))
            .Returns(Task.FromResult((IReadOnlyList <PartitionKeyRange>)result));

            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            this.InitStoreModels();
        }
        public async Task PartitionKeyRangeGoneTracePlumbingTest()
        {
            ITrace trace = Trace.GetRootTrace("TestTrace");
            PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition();

            partitionKeyDefinition.Paths.Add("pk");

            const string        collectionRid       = "DvZRAOvLgDM=";
            ContainerProperties containerProperties = ContainerProperties.CreateWithResourceId(collectionRid);

            containerProperties.Id           = "TestContainer";
            containerProperties.PartitionKey = partitionKeyDefinition;


            Mock <Common.CollectionCache> collectionCache = new Mock <Common.CollectionCache>(MockBehavior.Strict);

            collectionCache.Setup(c => c.ResolveCollectionAsync(It.IsAny <DocumentServiceRequest>(), default, trace))
        public static void SetupContainerProperties(
            Mock <IHttpHandler> mockHttpHandler,
            string regionEndpoint,
            string databaseName,
            string containerName,
            string containerRid)
        {
            ContainerProperties containerProperties = ContainerProperties.CreateWithResourceId(containerRid);

            containerProperties.Id             = containerName;
            containerProperties.IndexingPolicy = new Cosmos.IndexingPolicy()
            {
                IndexingMode  = Cosmos.IndexingMode.Consistent,
                Automatic     = true,
                IncludedPaths = new Collection <Cosmos.IncludedPath>()
                {
                    new Cosmos.IncludedPath()
                    {
                        Path = @"/*"
                    }
                },
                ExcludedPaths = new Collection <Cosmos.ExcludedPath>()
                {
                    new Cosmos.ExcludedPath()
                    {
                        Path = @"/_etag"
                    }
                }
            };
            containerProperties.PartitionKey = new PartitionKeyDefinition()
            {
                Paths = new Collection <string>()
                {
                    "/pk"
                }
            };

            Uri containerUri = new Uri($"{regionEndpoint}/dbs/{databaseName}/colls/{containerName}");

            mockHttpHandler.Setup(x => x.SendAsync(It.Is <HttpRequestMessage>(x => x.RequestUri == containerUri), It.IsAny <CancellationToken>()))
            .Returns(() => Task.FromResult(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(containerProperties))
            }));
        }
Пример #6
0
        private void Init()
        {
            this.collectionCache = new Mock <ClientCollectionCache>(new SessionContainer("testhost"), new ServerStoreModel(null), null, null);
            const string pkPath = "/pk";

            this.collectionCache.Setup
                (m =>
                m.ResolveCollectionAsync(
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() =>
            {
                ContainerProperties cosmosContainerSetting = ContainerProperties.CreateWithResourceId("test");
                cosmosContainerSetting.PartitionKey        = new PartitionKeyDefinition()
                {
                    Kind  = PartitionKind.Hash,
                    Paths = new Collection <string>()
                    {
                        pkPath
                    }
                };

                return(Task.FromResult(cosmosContainerSetting));
            });
            this.collectionCache.Setup
                (m =>
                m.ResolveByNameAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() => {
                ContainerProperties containerSettings = ContainerProperties.CreateWithResourceId("test");
                containerSettings.PartitionKey.Paths  = new Collection <string>()
                {
                    pkPath
                };
                return(Task.FromResult(containerSettings));
            });

            this.collectionCache.Setup
                (m =>
                m.ResolveByNameAsync(
                    It.IsAny <string>(),
                    It.IsAny <string>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(() =>
            {
                ContainerProperties cosmosContainerSetting = ContainerProperties.CreateWithResourceId("test");
                cosmosContainerSetting.PartitionKey        = new PartitionKeyDefinition()
                {
                    Kind  = PartitionKind.Hash,
                    Paths = new Collection <string>()
                    {
                        pkPath
                    }
                };

                return(Task.FromResult(cosmosContainerSetting));
            });

            this.partitionKeyRangeCache = new Mock <PartitionKeyRangeCache>(null, null, null);
            this.partitionKeyRangeCache.Setup(
                m => m.TryLookupAsync(
                    It.IsAny <string>(),
                    It.IsAny <CollectionRoutingMap>(),
                    It.IsAny <DocumentServiceRequest>(),
                    It.IsAny <CancellationToken>()
                    )
                ).Returns(Task.FromResult <CollectionRoutingMap>(null));
            this.partitionKeyRangeCache.Setup(
                m => m.TryGetOverlappingRangesAsync(
                    It.IsAny <string>(),
                    It.IsAny <Documents.Routing.Range <string> >(),
                    It.IsAny <bool>()
                    )
                ).Returns((string collectionRid, Documents.Routing.Range <string> range, bool forceRefresh) =>
            {
                return(Task.FromResult <IReadOnlyList <PartitionKeyRange> >(this.ResolveOverlapingPartitionKeyRanges(collectionRid, range, forceRefresh)));
            });

            this.globalEndpointManager = new Mock <GlobalEndpointManager>(this, new ConnectionPolicy());

            SessionContainer sessionContainer = new SessionContainer(this.ServiceEndpoint.Host);

            this.sessionContainer = sessionContainer;
        }