Event raised when a cluster's topology changes.
Inheritance: System.EventArgs
Esempio n. 1
0
        private async void OnClusterChange(object source, ClusterChangedEvent args)
        {
            var logger = LoggerManager.GetLogger("CqlSharp.Cluster.Changes");

            if (args.Change.Equals(ClusterChange.New))
            {
                //get the connection from which we received the event
                var connection = (Connection)source;

                //get the new peer
                using (var result = await ExecQuery(connection, "select rpc_address, data_center, rack, tokens from system.peers where peer = '" + args.Node + "'", logger).ConfigureAwait(false))
                {
                    if (await result.ReadAsync().ConfigureAwait(false))
                    {
                        var newNode = new Node((IPAddress)result["rpc_address"], this)
                        {
                            DataCenter = (string)result["data_center"],
                            Rack       = (string)result["rack"],
                            Tokens     = (ISet <string>)result["tokens"]
                        };


                        if (InDiscoveryScope(_nodes.First(), newNode, _config.DiscoveryScope))
                        {
                            logger.LogInfo("{0} added to the cluster", newNode);
                            _nodes.Add(newNode);
                        }
                        else
                        {
                            logger.LogVerbose("new {0} is ignored as it does not fit in the discovery scope", newNode);
                        }
                    }
                }
            }
            else if (args.Change.Equals(ClusterChange.Removed))
            {
                Node removedNode = _nodes.FirstOrDefault(node => args.Node.Equals(node.Address));
                if (removedNode != null)
                {
                    _nodes.Remove(removedNode);
                    logger.LogInfo("{0} was removed from the cluster", removedNode);
                }
                else
                {
                    logger.LogVerbose("Node with address {0} was removed but not used within the current configuration", args.Node);
                }
            }
            else if (args.Change.Equals(ClusterChange.Up))
            {
                Node upNode = _nodes.FirstOrDefault(node => args.Node.Equals(node.Address));

                if (upNode != null)
                {
                    using (logger.ThreadBinding())
                    {
                        upNode.Reactivate();
                    }
                }
            }
        }
Esempio n. 2
0
        private async void OnClusterChange(object source, ClusterChangedEvent args)
        {
            var logger = LoggerManager.GetLogger("CqlSharp.Cluster.Changes");

            if (args.Change.Equals(ClusterChange.New))
            {
                //get the connection from which we received the event
                var connection = (Connection)source;

                //get the new peer
                using (var result = await ExecQuery(connection, "select rpc_address, data_center, rack, tokens from system.peers where peer = '" + args.Node + "'", logger).ConfigureAwait(false))
                {
                    if (await result.ReadAsync().ConfigureAwait(false))
                    {
                        var newNode = new Node((IPAddress)result["rpc_address"], this)
                                            {
                                                DataCenter = (string)result["data_center"],
                                                Rack = (string)result["rack"],
                                                Tokens = (ISet<string>)result["tokens"]
                                            };


                        if (InDiscoveryScope(_nodes.First(), newNode, _config.DiscoveryScope))
                        {
                            logger.LogInfo("{0} added to the cluster", newNode);
                            _nodes.Add(newNode);
                        }
                        else
                        {
                            logger.LogVerbose("new {0} is ignored as it does not fit in the discovery scope", newNode);
                        }
                    }
                }
            }
            else if (args.Change.Equals(ClusterChange.Removed))
            {
                Node removedNode = _nodes.FirstOrDefault(node => args.Node.Equals(node.Address));
                if (removedNode != null)
                {
                    _nodes.Remove(removedNode);
                    logger.LogInfo("{0} was removed from the cluster", removedNode);
                }
                else
                {
                    logger.LogVerbose("Node with address {0} was removed but not used within the current configuration", args.Node);
                }
            }
            else if (args.Change.Equals(ClusterChange.Up))
            {
                Node upNode = _nodes.FirstOrDefault(node => args.Node.Equals(node.Address));

                if (upNode != null)
                {
                    using (logger.ThreadBinding())
                    {
                        upNode.Reactivate();
                    }
                }
            }
        }
Esempio n. 3
0
        private async void OnClusterChange(object source, ClusterChangedEvent args)
        {
            if(_disposed)
                return;

            var logger = LoggerManager.GetLogger("CqlSharp.Cluster.Changes");

            try
            {
                if(args.Change.Equals(ClusterChange.New) || args.Change.Equals(ClusterChange.Removed))
                {
                    //delay as Cassandra is typically to early with sending these changes (Gossip needs to settle)
                    await Task.Delay(5000).AutoConfigureAwait();

                    logger.LogVerbose("Cluster changed: {0} is {1}", args.Node, args.Change);

                    //get the connection from which we received the event
                    var connection = (Connection)source;
                    var node = connection.Node;

                    //refetch the cluster configuration
                    await
                        GetClusterInfoAsync(node, logger, CancellationToken.None)
                            .AutoConfigureAwait();
                }
                else if(args.Change.Equals(ClusterChange.Up))
                {
                    Node upNode = _nodes.FirstOrDefault(node => args.Node.Equals(node.Address));

                    if(upNode != null)
                    {
                        //delay as Cassandra is typically to early with sending these changes (Gossip needs to settle)
                        await Task.Delay(5000).AutoConfigureAwait();

                        using(logger.ThreadBinding())
                        {
                            upNode.Reactivate();
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                logger.LogError("Exception occured while handling cluster change {0} - {1}: {2}", args.Node, args.Change,
                                ex);
            }
        }