CqlSharp.Network.BalancedConnectionStrategy.GetOrCreateConnection C# (CSharp) Method

GetOrCreateConnection() public method

Gets or creates connection to the cluster.
public GetOrCreateConnection ( ConnectionScope scope, PartitionKey partitionKey ) : Connection
scope ConnectionScope
partitionKey PartitionKey
return Connection
        public Connection GetOrCreateConnection(ConnectionScope scope, PartitionKey partitionKey)
        {
            //provide connections on command level only
            if(scope == ConnectionScope.Connection)
                return null;

            //Sort the nodes by load (used first)
            Node leastUsedNode =
                _nodes.Where(n => n.IsUp).SmallestOrDefault(
                    n => n.ConnectionCount > 0 ? n.Load : _config.NewConnectionTreshold - 1);

            //no node found! weird...
            if(leastUsedNode == null)
                return null;

            //try get a connection from it
            Connection connection = leastUsedNode.GetConnection();

            //smallest connection from smallest node
            if(connection != null && connection.Load < _config.NewConnectionTreshold)
                return connection;

            if(_config.MaxConnections <= 0 || _connectionCount < _config.MaxConnections)
            {
                //try to get a new connection from this smallest node
                Connection newConnection = leastUsedNode.CreateConnection();

                if(newConnection != null)
                {
                    Interlocked.Increment(ref _connectionCount);
                    newConnection.OnConnectionChange +=
                        (c, ev) => { if(ev.Connected == false) Interlocked.Decrement(ref _connectionCount); };

                    return newConnection;
                }
            }

            return connection;
        }

Usage Example

Exemplo n.º 1
0
        public async Task BalancedStrategyLowTreshold()
        {
            using(ShimsContext.Create())
            {
                //create cluster
                var config = new CqlConnectionStringBuilder {NewConnectionTreshold = 5};

                var cluster = new Cluster(config);

                //create nodes
                var n = new Node(IPAddress.Parse("127.0.0.1"), cluster);
                var n2 = new Node(IPAddress.Parse("127.0.0.2"), cluster);
                var n3 = new Node(IPAddress.Parse("127.0.0.3"), cluster);
                var n4 = new Node(IPAddress.Parse("127.0.0.4"), cluster);
                var nodes = new Ring();
                nodes.Update(new List<Node> {n, n2, n3, n4}, "RandomPartitioner",
                             new Logger(new NullLogger(), LogLevel.None));

                ShimAllConnections();

                var logger = cluster.LoggerManager.GetLogger("BalancedStrategyLowTresholdTest");

                IConnectionStrategy strategy = new BalancedConnectionStrategy(nodes, config);

                const int nr = 8;

                for(int i = 0; i < nr; i++)
                {
                    Connection connection;

                    using(logger.ThreadBinding())
                    {
                        connection = strategy.GetOrCreateConnection(ConnectionScope.Command, PartitionKey.None);
                    }

                    await
                        connection.SendRequestAsync(new QueryFrame("", CqlConsistency.Any, null), logger, 10,
                                                    CancellationToken.None);
                }

                Assert.AreEqual(nodes.Sum(nd => nd.ConnectionCount), nr);
                Assert.IsTrue(nodes.All(nd => nd.ConnectionCount == nr/4));
            }
        }
All Usage Examples Of CqlSharp.Network.BalancedConnectionStrategy::GetOrCreateConnection