public async Task Test_Filter_Removed_Nodes(string oldConfigPath, string newConfigPath)
        {
            var oldConfig = ResourceHelper.ReadResource <BucketConfig>(oldConfigPath);
            var newConfig = ResourceHelper.ReadResource <BucketConfig>(newConfigPath);

            var options     = new ClusterOptions();
            var bucketNodes = new ConcurrentDictionary <IPEndPoint, IClusterNode>();
            var context     = new ClusterContext(new CancellationTokenSource(), options);

            var ipEndpointService = context.ServiceProvider.GetRequiredService <IIpEndPointService>();

            //load up the initial state after bootstrapping
            foreach (var server in oldConfig.GetNodes())
            {
                var endPoint = await ipEndpointService.GetIpEndPointAsync(server).ConfigureAwait(false);

                var clusterNode = new ClusterNode(context, new Mock <IConnectionPoolFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object,
                                                  new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                                  new Mock <ICircuitBreaker>().Object,
                                                  new Mock <ISaslMechanismFactory>().Object,
                                                  new Mock <IRedactor>().Object,
                                                  endPoint,
                                                  BucketType.Couchbase,
                                                  server,
                                                  NoopRequestTracer.Instance,
                                                  NoopValueRecorder.Instance);

                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            foreach (var nodesExt in newConfig.GetNodes())
            {
                var endPoint = await ipEndpointService.GetIpEndPointAsync(nodesExt).ConfigureAwait(false);

                if (bucketNodes.ContainsKey(endPoint))
                {
                    continue;
                }

                var clusterNode = new ClusterNode(context, new Mock <IConnectionPoolFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object,
                                                  new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                                  new Mock <ICircuitBreaker>().Object, new Mock <ISaslMechanismFactory>().Object,
                                                  new Mock <IRedactor>().Object, endPoint, BucketType.Memcached, nodesExt,
                                                  NoopRequestTracer.Instance,
                                                  NoopValueRecorder.Instance);

                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            await context.PruneNodesAsync(newConfig).ConfigureAwait(false);

            Assert.Equal(newConfig.NodesExt.Count, context.Nodes.Count);
        }
Esempio n. 2
0
        public void Test_Filter_Removed_Nodes(string oldConfigPath, string newConfigPath)
        {
            var oldConfig = ResourceHelper.ReadResource <BucketConfig>(oldConfigPath);
            var newConfig = ResourceHelper.ReadResource <BucketConfig>(newConfigPath);

            var options     = new ClusterOptions();
            var bucketNodes = new ConcurrentDictionary <IPEndPoint, IClusterNode>();
            var context     = new ClusterContext(null, options);

            //load up the initial state after bootstrapping
            foreach (var server in oldConfig.NodesExt)
            {
                var endPoint    = server.GetIpEndPoint(options);
                var clusterNode = new ClusterNode(context, new Mock <IConnectionFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object, new Mock <ITypeTranscoder>().Object,
                                                  new Mock <ICircuitBreaker>().Object,
                                                  new Mock <ISaslMechanismFactory>().Object)
                {
                    EndPoint = endPoint
                };
                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            foreach (var nodesExt in newConfig.NodesExt)
            {
                var endPoint = nodesExt.GetIpEndPoint(options);
                if (bucketNodes.ContainsKey(endPoint))
                {
                    continue;
                }

                var clusterNode = new ClusterNode(context, new Mock <IConnectionFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object, new Mock <ITypeTranscoder>().Object,
                                                  new Mock <ICircuitBreaker>().Object, new Mock <ISaslMechanismFactory>().Object)
                {
                    EndPoint = endPoint
                };
                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            var removed = bucketNodes.Where(x =>
                                            !newConfig.NodesExt.Any(y => x.Key.Equals(y.GetIpEndPoint(options))));

            foreach (var valuePair in removed)
            {
                if (!bucketNodes.TryRemove(valuePair.Key, out var clusterNode))
                {
                    continue;
                }
                context.RemoveNode(clusterNode);
            }

            Assert.Equal(newConfig.NodesExt.Count, bucketNodes.Count);
            Assert.Equal(context.Nodes.Count, bucketNodes.Count);
        }
Esempio n. 3
0
        public async Task Test_Filter_Removed_Nodes(string oldConfigPath, string newConfigPath)
        {
            var oldConfig = ResourceHelper.ReadResource(oldConfigPath, InternalSerializationContext.Default.BucketConfig);
            var newConfig = ResourceHelper.ReadResource(newConfigPath, InternalSerializationContext.Default.BucketConfig);

            var options     = new ClusterOptions();
            var bucketNodes = new ConcurrentDictionary <HostEndpointWithPort, IClusterNode>();
            var context     = new ClusterContext(new CancellationTokenSource(), options);

            var ipEndpointService = context.ServiceProvider.GetRequiredService <IIpEndPointService>();

            //load up the initial state after bootstrapping
            foreach (var server in oldConfig.GetNodes())
            {
                var endPoint    = HostEndpointWithPort.Create(server, options);
                var clusterNode = new ClusterNode(context, new Mock <IConnectionPoolFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object,
                                                  new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                                  new Mock <ICircuitBreaker>().Object,
                                                  new Mock <ISaslMechanismFactory>().Object,
                                                  new TypedRedactor(RedactionLevel.None),
                                                  endPoint,
                                                  server,
                                                  NoopRequestTracer.Instance);

                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            foreach (var nodesExt in newConfig.GetNodes())
            {
                var endPoint = HostEndpointWithPort.Create(nodesExt, options);
                if (bucketNodes.ContainsKey(endPoint))
                {
                    continue;
                }

                var clusterNode = new ClusterNode(context, new Mock <IConnectionPoolFactory>().Object,
                                                  new Mock <ILogger <ClusterNode> >().Object,
                                                  new DefaultObjectPool <OperationBuilder>(new OperationBuilderPoolPolicy()),
                                                  new Mock <ICircuitBreaker>().Object, new Mock <ISaslMechanismFactory>().Object,
                                                  new TypedRedactor(RedactionLevel.None), endPoint, nodesExt,
                                                  NoopRequestTracer.Instance);

                context.AddNode(clusterNode);
                bucketNodes.TryAdd(endPoint, clusterNode);
            }

            context.PruneNodes(newConfig);

            Assert.Equal(newConfig.NodesExt.Count, context.Nodes.Count);
        }
Esempio n. 4
0
        public async Task PruneNodesAsync_Does_Not_Remove_Single_Service_Nodes()
        {
            //Arrange

            var config  = ResourceHelper.ReadResource <BucketConfig>(@"Documents\Configs\rev-36310-service-per-node.json");
            var context = new ClusterContext();

            var dnsResolver = new Mock <IDnsResolver>();
            var service     = new IpEndPointService(dnsResolver.Object, new ClusterOptions());

            var hosts = new List <string> {
                "10.143.194.101", "10.143.194.102", "10.143.194.103", "10.143.194.104"
            };

            hosts.ForEach(async x => context.AddNode(await CreateMockedNode(x, 11210, service).ConfigureAwait(false)));

            //Act

            await context.PruneNodesAsync(config).ConfigureAwait(false);

            //Assert

            foreach (var host in hosts)
            {
                var removed = await service.GetIpEndPointAsync(host, 11210).ConfigureAwait(false);

                Assert.Contains(context.Nodes, node => node.EndPoint.Equals(removed));
            }
        }
        public async Task SearchQueriesUseCorrectPath()
        {
            const string indexName = "test-index";

            var hander = FakeHttpMessageHandler.Create((req) =>
            {
                Assert.Equal("http://localhost:8094/api/index/test-index/query", req.RequestUri.ToString());
                return(new HttpResponseMessage());
            });
            var httpClient = new HttpClient(hander);

            var mockClusterNode = new Mock <IClusterNode>();

            mockClusterNode.Setup(node => node.HasSearch()).Returns(true);
            mockClusterNode.Setup(node => node.SearchUri).Returns(new Uri("http://localhost:8094"));
            mockClusterNode.Setup(x => x.EndPoint).Returns(new IPEndPoint(IPAddress.Any, 8091));

            var options = new ClusterOptions();
            var context = new ClusterContext(null, options);

            context.AddNode(mockClusterNode.Object);
            var client = new SearchClient(context);

            await client.QueryAsync(new SearchQuery { Index = indexName });
        }
        public void PruneNodes_Does_Not_Remove_Single_Service_Nodes()
        {
            //Arrange

            var config = ResourceHelper.ReadResource(@"Documents\Configs\rev-36310-service-per-node.json",
                                                     InternalSerializationContext.Default.BucketConfig);
            var context = new ClusterContext();

            var hosts = new List <string> {
                "10.143.194.101", "10.143.194.102", "10.143.194.103", "10.143.194.104"
            };

            hosts.ForEach(x => context.AddNode(CreateMockedNode(x, 11210)));

            //Act

            context.PruneNodes(config);

            //Assert

            foreach (var host in hosts)
            {
                var removed = new HostEndpointWithPort(host, 11210);

                Assert.Contains(context.Nodes, node => node.EndPoint.Equals(removed));
            }
        }
Esempio n. 7
0
        public async Task QueryAsync_Sets_LastActivity()
        {
            var options = new ClusterOptions().WithServers("http://localhost");
            var context = new ClusterContext(null, options);

            context.AddNode(new ClusterNode(new ClusterContext(null, new ClusterOptions()))
            {
                AnalyticsUri = new Uri("http://localhost:8094/query"),
                EndPoint     = new IPEndPoint(IPAddress.Loopback, 8091),
                NodesAdapter = new NodeAdapter {
                    Analytics = 8094
                }
            });

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("{}")
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(new DefaultSerializer()), context);

            Assert.Null(client.LastActivity);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");
            await client.QueryAsync <dynamic>(queryRequest, CancellationToken.None).ConfigureAwait(false);

            Assert.NotNull(client.LastActivity);
        }
Esempio n. 8
0
        public async Task Test(string file, HttpStatusCode httpStatusCode, Type errorType)
        {
            using (var response = ResourceHelper.ReadResourceAsStream(@"Documents\Query\" + file))
            {
                var buffer = new byte[response.Length];
                response.Read(buffer, 0, buffer.Length);

                var handlerMock = new Mock <HttpMessageHandler>();
                handlerMock.Protected().Setup <Task <HttpResponseMessage> >(
                    "SendAsync",
                    ItExpr.IsAny <HttpRequestMessage>(),
                    ItExpr.IsAny <CancellationToken>()).ReturnsAsync(new HttpResponseMessage
                {
                    StatusCode = httpStatusCode,
                    Content    = new ByteArrayContent(buffer)
                });

                var httpClient = new HttpClient(handlerMock.Object)
                {
                    BaseAddress = new Uri("http://localhost:8091")
                };
                var options = new ClusterOptions().WithBucket("default").WithServers("http://localhost:8901");
                var context = new ClusterContext(null, options);

                var clusterNode = new ClusterNode(context)
                {
                    EndPoint     = new Uri("http://localhost:8091").GetIpEndPoint(8091, false),
                    NodesAdapter = new NodeAdapter(new Node {
                        Hostname = "127.0.0.1"
                    },
                                                   new NodesExt {
                        Hostname = "127.0.0.1", Services = new Couchbase.Core.Configuration.Server.Services
                        {
                            N1Ql = 8093
                        }
                    }, new BucketConfig())
                };
                clusterNode.BuildServiceUris();
                context.AddNode(clusterNode);

                var client = new QueryClient(httpClient, new JsonDataMapper(new DefaultSerializer()), context);

                try
                {
                    await client.QueryAsync <DynamicAttribute>("SELECT * FROM `default`", new QueryOptions());
                }
                catch (Exception e)
                {
                    Assert.True(e.GetType() == errorType);
                }
            }
        }
Esempio n. 9
0
        public void Client_sets_AnalyticsPriority_Header(bool priority)
        {
            var options = new ClusterOptions().WithServers("http://localhost");
            var context = new ClusterContext(null, options);

            context.AddNode(new ClusterNode(context)
            {
                AnalyticsUri = new Uri("http://localhost:8094/query"),
                EndPoint     = new IPEndPoint(IPAddress.Loopback, 8091),
                NodesAdapter = new NodeAdapter {
                    Analytics = 8094
                }
            });

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request =>
            {
                if (priority)
                {
                    Assert.True(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName,
                                                             out var values));
                    Assert.Equal("-1", values.First());
                }
                else
                {
                    Assert.False(request.Headers.TryGetValues(AnalyticsClient.AnalyticsPriorityHeaderName, out _));
                }

                return(new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent("{}")
                });
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(new DefaultSerializer()), context);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");

            queryRequest.Priority(priority);

            client.Query <dynamic>(queryRequest);
        }
Esempio n. 10
0
        public void When_deferred_is_true_query_result_is_DeferredAnalyticsResult()
        {
            var resultJson = JsonConvert.SerializeObject(new
            {
                status = "Success",
                handle = "handle"
            });

            var options = new ClusterOptions().WithServers("http://localhost");
            var context = new ClusterContext(null, options);

            context.AddNode(new ClusterNode(new ClusterContext(null, new ClusterOptions()))
            {
                AnalyticsUri = new Uri("http://localhost:8094/query"),
                EndPoint     = new IPEndPoint(IPAddress.Loopback, 8091),
                NodesAdapter = new NodeAdapter {
                    Analytics = 8094
                }
            });

            var httpClient = new HttpClient(
                FakeHttpMessageHandler.Create(request => new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(resultJson)
            })
                );

            var client = new AnalyticsClient(httpClient, new JsonDataMapper(new DefaultSerializer()), context);

            var queryRequest = new AnalyticsRequest("SELECT * FROM `default`;");
            var result       = client.Query <dynamic>(queryRequest);

            Assert.IsType <AnalyticsDeferredResultHandle <dynamic> >(result.Handle);
            Assert.Equal(QueryStatus.Success, result.MetaData.Status);

            var deferredResult = (AnalyticsDeferredResultHandle <dynamic>)result.Handle;

            Assert.Equal("handle", deferredResult.HandleUri);
        }
        public void PruneNodes_Removes_Rebalanced_Node()
        {
            //Arrange

            var config = ResourceHelper.ReadResource(@"Documents\Configs\config-error.json",
                                                     InternalSerializationContext.Default.BucketConfig);
            var context = new ClusterContext();

            var hosts = new List <string> {
                "10.143.194.101", "10.143.194.102", "10.143.194.103", "10.143.194.104"
            };

            hosts.ForEach(x => context.AddNode(CreateMockedNode(x, 11210)));

            //Act

            context.PruneNodes(config);

            //Assert

            var removed = new HostEndpointWithPort("10.143.194.102", 11210);

            Assert.DoesNotContain(context.Nodes, node => node.EndPoint.Equals(removed));
        }