Esempio n. 1
0
        public void Test_NoOp()
        {
            var noop   = new Noop(new DefaultTranscoder(), OperationLifespanTimeout);
            var result = IOStrategy.Execute(noop);

            Assert.IsTrue(result.Success);
        }
        public void Test_NoOp()
        {
            var noop   = new Noop(new AutoByteConverter());
            var result = IOStrategy.Execute(noop);

            Assert.IsTrue(result.Success);
        }
Esempio n. 3
0
        private void _heartBeatTimer_Elapsed(object sender, ElapsedEventArgs args)
        {
            Log.InfoFormat("Checking if node {0} is down: {1}", EndPoint, _isDown);
            _heartBeatTimer.Stop();
            if (_isDown)
            {
                IConnection connection = null;
                try
                {
                    //once we can connect, we need a sasl mechanism for auth
                    CreateSaslMechanismIfNotExists();

                    //if we have a sasl mechanism, we just try a noop
                    connection = _ioStrategy.ConnectionPool.Acquire();
                    var noop = new Noop(new DefaultTranscoder(), 1000);

                    var result = _ioStrategy.Execute(noop);
                    if (result.Success)
                    {
                        Log.InfoFormat("Successfully connected and marking node {0} as up.", EndPoint);
                        _isDown = false;
                        _heartBeatTimer.Stop();
                    }
                    else
                    {
                        Log.InfoFormat("The node {0} is still down: {1}", EndPoint, result.Status);
                    }
                }
                catch (Exception e)
                {
                    Log.InfoFormat("The node {0} is still down: {1}", EndPoint, e.Message);
                    //the node is down or unreachable
                    _isDown = true;
                    Log.Debug(e);
                }
                finally
                {
                    //will be null if the node is dead
                    if (connection != null)
                    {
                        if (_isDown)
                        {
                            if (connection.Socket.Connected)
                            {
                                connection.IsDead = false;
                            }
                            _heartBeatTimer.Start();
                        }
                        _ioStrategy.ConnectionPool.Release(connection);
                    }
                    else
                    {
                        _heartBeatTimer.Start();
                    }
                }
            }
        }
Esempio n. 4
0
        public async Task NoopTest()
        {
            Noop noop = new Noop(new ActionModel
            {
                Folders = new MainFolder(Directory.GetCurrentDirectory())
            });
            string path = await noop.GetSubFolder(new FileFormatters());

            Assert.AreEqual(string.Empty, path);
        }
Esempio n. 5
0
        /// <summary>
        /// If a data only node is flaged as down, this method will be invoked every <see cref="ClientConfiguration.NodeAvailableCheckInterval"/>.
        /// When invoked, it will attempt to get a connection and perform a NOOP on it.
        /// If the NOOP succeeds, then the node will be put back into rotation.
        /// </summary>
        void CheckDataNode()
        {
            IConnection connection = null;

            try
            {
                //once we can connect, we need a sasl mechanism for auth
                CreateSaslMechanismIfNotExists();

                //if we have a sasl mechanism, we just try a noop
                connection = _ioService.ConnectionPool.Acquire();
                var noop = new Noop(new DefaultTranscoder(), 1000);

                var result = _ioService.Execute(noop);
                if (result.Success)
                {
                    Log.InfoFormat("Successfully connected and marking data node {0} as up.", EndPoint);
                    _isDown = false;
                    _heartBeatTimer.Stop();
                }
                else
                {
                    Log.InfoFormat("The data node {0} is still down: {1}", EndPoint, result.Status);
                }
            }
            // ReSharper disable once CatchAllClause
            catch (Exception e)
            {
                // ReSharper disable once HeapView.ObjectAllocation
                Log.InfoFormat("The data node {0} is still down: {1}", EndPoint, e.Message);
                //the node is down or unreachable
                _isDown = true;
                Log.Debug(e);
            }
            finally
            {
                //will be null if the node is dead
                if (connection != null)
                {
                    if (_isDown)
                    {
                        if (connection.Socket.Connected)
                        {
                            connection.IsDead = false;
                        }
                        _heartBeatTimer.Start();
                    }
                    _ioService.ConnectionPool.Release(connection);
                }
                else
                {
                    _heartBeatTimer.Start();
                }
            }
        }
Esempio n. 6
0
        public Pop3CommandResult ExecuteNoop()
        {
            Noop cm = new Noop();
            Pop3CommandResult rs = null;
            String            s  = "";

            this.EnsureOpen();
            s  = this.Execute(cm);
            rs = new Pop3CommandResult(s);
            return(rs);
        }
Esempio n. 7
0
        public void RecogniseADwell()
        {
            var duration = 5;
            var code     = new Gcode(4, new List <Argument> {
                new Argument(ArgumentKind.P, duration * 1000, Span.Empty)
            }, Span.Empty);
            var shouldBe = new Noop(initialState, duration);

            var got = operations.GcodeOp(code, initialState);

            Assert.Equal(shouldBe, got);
        }
Esempio n. 8
0
        public void ExecuteNoop(Action <Pop3CommandResult> callbackFunction)
        {
            Noop           cm = null;
            EndGetResponse md = null;

            md = new EndGetResponse(delegate(String responseString)
            {
                callbackFunction(new Pop3CommandResult(responseString));
            });
            this.EnsureOpen();
            cm = new Noop();
            this.BeginExecute(cm, md);
        }
        public void Test_ExecuteAsync()
        {
            var tcs       = new TaskCompletionSource <object>();
            var operation = new Noop(new DefaultTranscoder(), OperationLifespan);

            operation.Completed = s =>
            {
                Assert.IsNull(s.Exception);

                var buffer = s.Data.ToArray();
                operation.Read(buffer, 0, buffer.Length);
                var result = operation.GetResult();
                Assert.IsTrue(result.Success);
                Assert.IsNull(result.Exception);
                Assert.IsNullOrEmpty(result.Message);
                tcs.SetResult(result);
                return(tcs.Task);
            };
        }
Esempio n. 10
0
        private static async Task <ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> > > GetEndpointDiagnosticsAsync(ClusterContext context,
                                                                                                                                    IEnumerable <IClusterNode> clusterNodes, bool ping, ICollection <ServiceType> serviceTypes, CancellationToken token)
        {
            var endpoints = new ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> >();

            IOperationConfigurator operationConfigurator = ping
               ? context.ServiceProvider.GetRequiredService <IOperationConfigurator>()
               : null;

            foreach (var clusterNode in clusterNodes)
            {
                if (serviceTypes.Contains(ServiceType.KeyValue) && clusterNode.HasKv)
                {
                    var kvEndpoints = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("kv", new List <IEndpointDiagnostics>());

                    foreach (var connection in clusterNode.ConnectionPool.GetConnections())
                    {
                        var endPointDiagnostics =
                            CreateEndpointHealth(clusterNode.Owner?.Name, DateTime.UtcNow, connection, ping);

                        if (ping)
                        {
                            await RecordLatencyAsync(endPointDiagnostics, async() =>
                            {
                                try
                                {
                                    using var op = new Noop();
                                    operationConfigurator.Configure(op);

                                    using var ctp = token == CancellationToken.None ?
                                                    CancellationTokenPairSource.FromTimeout(context.ClusterOptions.KvTimeout) :
                                                    CancellationTokenPairSource.FromExternalToken(token);
                                    await clusterNode.ExecuteOp(connection, op, ctp.TokenPair).ConfigureAwait(false);
                                }
                                catch (ObjectDisposedException)
                                {
                                    //Ignore as the ping is on a timer is a race condition when the connection is closed
                                }
                            }).ConfigureAwait(false);
Esempio n. 11
0
        public void Test()
        {
            var keyValues = new Dictionary <string, string>();

            using (var cluster = new Cluster())
            {
                using (var bucket = cluster.OpenBucket())
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var key   = "MGETKEY" + i;
                        var value = "{\"val:\"MGETVALUE" + i + "\"}";
                        bucket.Upsert(key, value);
                        keyValues.Add(key, value);
                    }

                    foreach (var keyValue in keyValues)
                    {
                        Console.WriteLine(bucket.Get <dynamic>(keyValue.Key).Value);
                    }
                }
            }

            var operations = new ConcurrentDictionary <uint, IOperation>();

            foreach (var keyValue in keyValues)
            {
                var getk = new GetK <dynamic>(keyValue.Key, GetVBucket(), Converter, Transcoder);
                operations.TryAdd(getk.Opaque, getk);
            }
            var noop = new Noop(Converter);

            operations.TryAdd(noop.Opaque, noop);

            var results = IOStrategy.Execute <dynamic>(operations);
        }
Esempio n. 12
0
        private static async Task <ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> > > GetEndpointDiagnosticsAsync(ClusterContext context,
                                                                                                                                    IEnumerable <IClusterNode> clusterNodes, bool ping, ICollection <ServiceType> serviceTypes, CancellationToken token)
        {
            var endpoints = new ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> >();

            IOperationConfigurator operationConfigurator = ping
               ? context.ServiceProvider.GetRequiredService <IOperationConfigurator>()
               : null;

            foreach (var clusterNode in clusterNodes)
            {
                if (serviceTypes.Contains(ServiceType.KeyValue) && clusterNode.HasKv)
                {
                    var kvEndpoints = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("kv", new List <IEndpointDiagnostics>());

                    foreach (var connection in clusterNode.ConnectionPool.GetConnections())
                    {
                        var endPointDiagnostics =
                            CreateEndpointHealth(clusterNode.Owner?.Name, DateTime.UtcNow, connection);

                        if (ping)
                        {
                            await RecordLatencyAsync(endPointDiagnostics, async() =>
                            {
                                var op = new Noop();
                                operationConfigurator.Configure(op);
                                await clusterNode.ExecuteOp(connection, op, token).ConfigureAwait(false);
                            }).ConfigureAwait(false);
                        }

                        kvEndpoints.Add(endPointDiagnostics);
                    }
                }

                if (serviceTypes.Contains(ServiceType.Views) && clusterNode.HasViews)
                {
                    if (clusterNode.Owner is CouchbaseBucket bucket)
                    {
                        var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("view", new List <IEndpointDiagnostics>());
                        var endPointDiagnostics = CreateEndpointHealth(bucket.Name, ServiceType.Views, DateTime.UtcNow, clusterNode.LastViewActivity, clusterNode.EndPoint);

                        if (ping)
                        {
                            await RecordLatencyAsync(endPointDiagnostics,
                                                     async() => await bucket.ViewQueryAsync <object, object>("p", "p").ConfigureAwait(false))
                            .ConfigureAwait(false);
                        }

                        kvEndpoints.Add(endPointDiagnostics);
                    }
                }

                if (serviceTypes.Contains(ServiceType.Query) && clusterNode.HasQuery)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("n1ql", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Query, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        await RecordLatencyAsync(endPointDiagnostics,
                                                 () => context.Cluster.QueryAsync <dynamic>("SELECT 1;"))
                        .ConfigureAwait(false);
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }

                if (serviceTypes.Contains(ServiceType.Analytics) && clusterNode.HasAnalytics)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("cbas", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Analytics, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        await RecordLatencyAsync(endPointDiagnostics,
                                                 () => context.Cluster.AnalyticsQueryAsync <dynamic>("SELECT 1;"))
                        .ConfigureAwait(false);
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }

                if (serviceTypes.Contains(ServiceType.Search) && clusterNode.HasSearch)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("fts", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Search, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        var index = "ping";
                        await RecordLatencyAsync(endPointDiagnostics,
                                                 () => context.Cluster.SearchQueryAsync(index, new NoOpQuery())).ConfigureAwait(false);
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }
            }

            return(endpoints);
        }
Esempio n. 13
0
        internal static ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> > GetEndpointDiagnostics(ClusterContext context, IEnumerable <IClusterNode> clusterNodes, bool ping,
                                                                                                                  ICollection <ServiceType> serviceTypes, CancellationToken token)
        {
            var endpoints = new ConcurrentDictionary <string, IEnumerable <IEndpointDiagnostics> >();

            foreach (var clusterNode in clusterNodes)
            {
                if (serviceTypes.Contains(ServiceType.KeyValue) && clusterNode.HasKv)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("kv", new List <IEndpointDiagnostics>());
                    var connection          = clusterNode.Connection; //need to make a pool
                    var endPointDiagnostics = CreateEndpointHealth(clusterNode.Owner.Name, DateTime.UtcNow, connection);

                    if (ping)
                    {
                        RecordLatency(endPointDiagnostics, async() =>
                        {
                            var op = new Noop();
                            await clusterNode.ExecuteOp(connection, op, token);
                        });
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }

                if (serviceTypes.Contains(ServiceType.Views) && clusterNode.HasViews)
                {
                    if (clusterNode.Owner is CouchbaseBucket bucket)
                    {
                        var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("view", new List <IEndpointDiagnostics>());
                        var endPointDiagnostics = CreateEndpointHealth(bucket.Name, ServiceType.Views, DateTime.UtcNow, clusterNode.LastViewActivity, clusterNode.EndPoint);

                        if (ping)
                        {
                            RecordLatency(endPointDiagnostics, async() => await bucket.ViewQueryAsync <object, object>("p", "p"));
                        }

                        kvEndpoints.Add(endPointDiagnostics);
                    }
                }

                if (serviceTypes.Contains(ServiceType.Query) && clusterNode.HasQuery)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("n1ql", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Query, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        RecordLatency(endPointDiagnostics, () => context.Cluster.QueryAsync <dynamic>("SELECT 1;"));
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }

                if (serviceTypes.Contains(ServiceType.Analytics) && clusterNode.HasAnalytics)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("cbas", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Analytics, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        RecordLatency(endPointDiagnostics, () => context.Cluster.AnalyticsQueryAsync <dynamic>("SELECT 1;"));
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }

                if (serviceTypes.Contains(ServiceType.Search) && clusterNode.HasSearch)
                {
                    var kvEndpoints         = (List <IEndpointDiagnostics>)endpoints.GetOrAdd("fts", new List <IEndpointDiagnostics>());
                    var endPointDiagnostics = CreateEndpointHealth("Cluster", ServiceType.Search, DateTime.UtcNow, clusterNode.LastQueryActivity, clusterNode.EndPoint);

                    if (ping)
                    {
                        var index = "ping";
                        RecordLatency(endPointDiagnostics, () => context.Cluster.SearchQueryAsync(index, new NoOpQuery()));
                    }

                    kvEndpoints.Add(endPointDiagnostics);
                }
            }

            return(endpoints);
        }
        internal static Dictionary <string, IEnumerable <IEndpointDiagnostics> > GetEndpointDiagnostics(bool ping, ICollection <ServiceType> serviceTypes, params IConfigInfo[] configs)
        {
            var now = DateTime.UtcNow;

            return(configs
                   .Where(config => config.Servers.Any())
                   .Select(config =>
            {
                var endpoints = new Dictionary <string, IEnumerable <IEndpointDiagnostics> >();
                if (serviceTypes.Contains(ServiceType.KeyValue) && config.Servers.Any(server => server.IsDataNode))
                {
                    var noopBytes = new Noop(DefaultTranscoder, 0).Write();
                    endpoints.Add("kv", config.Servers.Where(server => server.IsDataNode)
                                  .SelectMany(server => server.ConnectionPool.Connections)
                                  .Select(connection =>
                    {
                        var endpoint = CreateEndpointHealth(config.BucketName, now, connection);
                        if (ping)
                        {
                            RecordLatency(endpoint, () => connection.Send(noopBytes));
                        }

                        return endpoint;
                    })
                                  );
                }

                if (serviceTypes.Contains(ServiceType.Views) && config.Servers.Any(server => server.IsViewNode))
                {
                    var viewQuery = new ViewQuery("p", "p", "p");
                    endpoints.Add("view", config.Servers.Where(server => server.IsViewNode)
                                  .Select(server =>
                    {
                        var endpoint = CreateEndpointHealth(config.BucketName, now, server.ViewClient, server.EndPoint);
                        if (ping)
                        {
                            RecordLatency(endpoint, () => server.ViewClient.Execute <dynamic>(viewQuery));
                        }

                        return endpoint;
                    })
                                  );
                }

                if (serviceTypes.Contains(ServiceType.Query) && config.Servers.Any(server => server.IsQueryNode))
                {
                    var n1qlQuery = new QueryRequest("SELECT 1;");
                    endpoints.Add("n1ql", config.Servers.Where(server => server.IsQueryNode)
                                  .Select(server =>
                    {
                        var endpoint = CreateEndpointHealth(config.BucketName, now, server.QueryClient, server.EndPoint);
                        if (ping)
                        {
                            RecordLatency(endpoint, () => server.QueryClient.Query <dynamic>(n1qlQuery));
                        }

                        return endpoint;
                    })
                                  );
                }

                if (serviceTypes.Contains(ServiceType.Search) && config.Servers.Any(server => server.IsSearchNode))
                {
                    var searchQuery = new SearchQuery {
                        Index = "ping"
                    };
                    endpoints.Add("fts", config.Servers.Where(server => server.IsSearchNode)
                                  .Select(server =>
                    {
                        var endpoint = CreateEndpointHealth(config.BucketName, now, server.SearchClient, server.EndPoint);
                        if (ping)
                        {
                            RecordLatency(endpoint, () => server.SearchClient.Query(searchQuery));
                        }

                        return endpoint;
                    })
                                  );
                }

                if (serviceTypes.Contains(ServiceType.Analytics) && config.Servers.Any(server => server.IsAnalyticsNode))
                {
                    var analyticsRequest = new AnalyticsRequest("SELECT 1;");
                    endpoints.Add("cbas", config.Servers.Where(server => server.IsAnalyticsNode)
                                  .Select(server =>
                    {
                        var endpoint = CreateEndpointHealth(config.BucketName, now, server.AnalyticsClient, server.EndPoint);
                        if (ping)
                        {
                            RecordLatency(endpoint, () => server.AnalyticsClient.Query <dynamic>(analyticsRequest));
                        }

                        return endpoint;
                    })
                                  );
                }

                return endpoints;
            })
                   .SelectMany(d => d)
                   .ToLookup(x => x.Key, x => x.Value)
                   .ToDictionary(group => group.Key, group => group.SelectMany(value => value)));
        }