{
		public override IPooledDbConnection CreateConnection (IConnectionPool pool, DatabaseConnectionSettings settings, out string error)
		{
			SqlConnectionStringBuilder builder = null;
			try {	
				if (settings.UseConnectionString) {
					builder = new SqlConnectionStringBuilder (settings.ConnectionString);
				} else {
					builder = new SqlConnectionStringBuilder ();
					builder.InitialCatalog = settings.Database;
					builder.UserID = settings.Username;
					builder.Password = settings.Password;
					builder.DataSource = String.Concat (settings.Server, ",", settings.Port);
					//builder.NetworkLibrary = "DBMSSOCN";
				}
				builder.Pooling = false;
				SqlConnection connection = new SqlConnection (builder.ToString ());
				connection.Open ();
				
				error = null;
				return new SqlServerPooledDbConnection (pool, connection);
			} catch (Exception e) {
				error = e.Message;
				return null;
			}
 public MongoDbSchemaProvider(IConnectionPool connectionPool)
     : base(connectionPool)
 {
     AddSupportedSchemaActions (SchemaType.Database, SchemaActions.All);
     AddSupportedSchemaActions (SchemaType.Table, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
     AddSupportedSchemaActions (SchemaType.User, SchemaActions.None);
 }
		public SealedVirtualCluster(VirtualCluster cluster, IConnectionPool pool, TestableDateTimeProvider dateTimeProvider)
		{
			this._cluster = cluster;
			this._connectionPool = pool;
			this._connection = new VirtualClusterConnection(cluster, dateTimeProvider);
			this._dateTimeProvider = dateTimeProvider;
		}
		protected AbstractSchemaProvider (IConnectionPool connectionPool)
		{
			if (connectionPool == null)
				throw new ArgumentNullException ("connectionPool");
			
			this.connectionPool = connectionPool;
		}
 public void OneTimeSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address);
     var connectionPoolConfig = new PoolConfiguration();
     _connectionPool = new ConnectionPool<Connection>(connectionPoolConfig, ipEndpoint);
     _ioService = new PooledIOService(_connectionPool);
 }
 public void TestFixtureSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address);
     var connectionPoolConfig = new PoolConfiguration { UseSsl = false };
     _connectionPool = new ConnectionPool<Connection>(connectionPoolConfig, ipEndpoint);
     _ioService = new PooledIOService(_connectionPool);
 }
	{
		public override IPooledDbConnection CreateConnection (IConnectionPool pool, 
		                                                      DatabaseConnectionSettings settings, 
		                                                      out string error)
		{
			string connStr = null;
			try {	
				if (settings.UseConnectionString) {
					connStr = settings.ConnectionString;
				} else {
					//"Server=Server;Port=1234;Database=Test;Uid=UserName;Pwd=asdasd;"
					//Default port is 3306. Enter value -1 to use a named pipe connection. 
					if (settings.Port > 0)
						connStr = String.Format ("Server={0};Port={1};Database={2};Uid={3};Pwd={4};",
							settings.Server, settings.Port, settings.Database, settings.Username, settings.Password);
					else
						connStr = String.Format ("Server={0};Database={2};Uid={3};Pwd={3};",
							settings.Server, settings.Database, settings.Username, settings.Password);
						
				}
				connStr = SetConnectionStringParameter (connStr, String.Empty, "Pooling", "false");
				MySqlConnection connection = new MySqlConnection (connStr);
				connection.Open ();
				
				error = null;
				return new MySqlPooledDbConnection (pool, connection);
			} catch (Exception e) {
				error = e.Message;
				return null;
			}
	{
		public override IPooledDbConnection CreateConnection (IConnectionPool pool, DatabaseConnectionSettings settings, out string error)
		{
			string connStr = null;
			try {	
				if (settings.UseConnectionString) {
					connStr = settings.ConnectionString;
				} else {
					//User ID=root;Password=myPassword;Host=localhost;Port=5432;Database=myDataBase;Pooling=true;Min Pool Size=0;Max Pool Size=100;Connection Lifetime=0;
					if (settings.Port > 0)
						connStr = String.Format ("User ID={0};Password={1};Host={2};Port={3};Database={4};",
							settings.Username, settings.Password, settings.Server, settings.Port, settings.Database);
					else
						connStr = String.Format ("User ID={0};Password={1};Host={2};Database={3};",
							settings.Username, settings.Password, settings.Server, settings.Database);
						
				}
				connStr = SetConnectionStringParameter (connStr, String.Empty, "Pooling", "false");
				NpgsqlConnection connection = new NpgsqlConnection (connStr);
				connection.Open ();
				
				error = null;
				return new NpgsqlPooledDbConnection (pool, connection);
			} catch (Exception e) {
				error = e.Message;
				return null;
			}
        public override IPooledDbConnection CreateConnection(IConnectionPool pool, 
		                                                      DatabaseConnectionSettings settings, 
		                                                      out string error)
        {
            string connStr = null;
            try {
                if (settings.UseConnectionString) {
                    connStr = settings.ConnectionString;
                } else {
                    //"mongodb://[username:password@]hostname[:port][/[database][?options]]"
                    //Default port is 27017.
                    if(!string.IsNullOrEmpty(settings.Username) && !string.IsNullOrEmpty(settings.Password)) {
                        connStr = String.Format("mongodb://{3}:{4}@{0}:{1}/{2}",
                        settings.Server, settings.Port, settings.Database, settings.Username, settings.Password);
                    } else {
                        connStr = String.Format("mongodb://{0}:{1}/{2}",
                        settings.Server, settings.Port, settings.Database);
                    }

                }
                MongoDbConnection connection = new MongoDbConnection(connStr, settings.Database);
                connection.Open();
                error = null;
                return new MongoDbPooledDbConnection (pool, connection);
            } catch (Exception e) {
                error = e.Message;
                return null;
            }
        }
 public void TestFixtureSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(Address);
     var connectionPoolConfig = new PoolConfiguration();
     _connectionPool = new ConnectionPool<EapConnection>(connectionPoolConfig, ipEndpoint);
     _ioStrategy = new DefaultIOStrategy(_connectionPool);
 }
 public void SetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address);
     var factory = DefaultConnectionFactory.GetGeneric<Connection>();
     _configuration = new PoolConfiguration(MaxSize, MinSize, WaitTimeout, RecieveTimeout, ShutdownTimeout, SendTimeout, ConnectTimeout, MaxConnectionAcquireCount);
     _connectionPool = new ConnectionPool<Connection>(_configuration, ipEndpoint, factory, new DefaultConverter());
     _connectionPool.Initialize();
 }
Exemple #12
0
        public Node(string host, int port, IConnectionPool masterConnectionPool, params IConnectionPool[] slaveConnectionPools)
        {
            Host = host;
            Port = port;

            _masterConnectionPool = masterConnectionPool;
            _slaveConnectionPools = slaveConnectionPools;
        }
		protected AbstractSchemaProvider (IConnectionPool connectionPool)
		{
			if (connectionPool == null)
				throw new ArgumentNullException ("connectionPool");
			
			this.connectionPool = connectionPool;
			this.supportedActions = new Dictionary<SchemaType, SchemaActions> ();
		}
 public virtual void TestFixtureSetUp()
 {
     EndPoint = UriExtensions.GetEndPoint(Address);
     var connectionPoolConfig = new PoolConfiguration();
     _connectionPool = new ConnectionPool<Connection>(connectionPoolConfig, EndPoint);
     _ioService = new PooledIOService(_connectionPool);
     Transcoder = new DefaultTranscoder();
 }
 public void SetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(Address);
     var factory = DefaultConnectionFactory.GetDefault();
     _configuration = new PoolConfiguration(MaxSize, MinSize, WaitTimeout, RecieveTimeout, ShutdownTimeout, SendTimeout);
     _connectionPool = new DefaultConnectionPool(_configuration, ipEndpoint, factory);
     _connectionPool.Initialize();
 }
 public void SetUp()
 {
     var ipEndpoint = Node.GetEndPoint(Server);
     var factory = ConnectionFactory.GetDefault();
     _config = new ConnectionPoolConfig(MaxSize, MinSize, WaitTimeout, RecieveTimeout, ShutdownTimeout, SendTimeout);
     _connectionPool = new DefaultConnectionPool(_config, ipEndpoint, factory);
     _connectionPool.Initialize();
 }
 public void TestFixtureSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address);
     var connectionPoolConfig = new PoolConfiguration();
     _connectionPool = new ConnectionPool<Connection>(connectionPoolConfig, ipEndpoint);
     _connectionPool.Initialize();
     _ioService = new PooledIOService(_connectionPool, null);
 }
		public VirtualizedCluster(VirtualCluster cluster, IConnectionPool pool, TestableDateTimeProvider dateTimeProvider, ConnectionSettings settings)
		{
			this._dateTimeProvider = dateTimeProvider;
			this._settings = settings;
			this._fixedRequestPipeline = new FixedPipelineFactory(settings, this._dateTimeProvider);

			this._cluster = cluster;
			this._connectionPool = pool;
		}
        public void Setup()
        {
            var mockConnection = new Mock<IConnection>();
            mockConnection.SetupGet(x => x.IsAuthenticated).Returns(false);

            var mockConnectionPool = new Mock<IConnectionPool>();
            mockConnectionPool.Setup(x => x.Acquire()).Returns(mockConnection.Object);

            _connectionPool = mockConnectionPool.Object;
        }
		public PostgresQueryManager(
			IConnectionPool connections,
			Func<NpgsqlConnection, NpgsqlTransaction, IPostgresDatabaseQuery> queryFactory)
		{
			Contract.Requires(connections != null);
			Contract.Requires(queryFactory != null);

			this.Connections = connections;
			this.QueryFactory = queryFactory;
		}
        public virtual void SetUp()
        {
            var factory = ConnectionFactory.GetDefault();
            _connectionPool = new DefaultConnectionPool(_defaultClientConfig.ConnectionPoolConfiguration, Node.GetEndPoint(Server), factory);
            _connectionPool.Initialize();

            _provider = new FileSystemConfigProvider(_defaultClientConfig, ConnectionFactory.GetDefault());
            _configInfo = _provider.GetCached();
            _vBucket = _configInfo.HashToVBucket(Key);
        }
		public void AssertCreateView(IConnectionPool pool)
		{
			/** 
			*/
			var threads = Enumerable.Range(0, 50)
				.Select(i => CreateReadAndUpdateThread(pool))
				.ToList();

			foreach (var t in threads) t.Start();
			foreach (var t in threads) t.Join();
		}
		public Thread CreateReadAndUpdateThread(IConnectionPool pool) => new Thread(() =>
		{
			for (var i = 0; i < 1000; i++)
			foreach (var v in CallGetNext(pool))
			{
				if (this.Random.Next(10) % 2 == 0)
				{
					pool.Reseed(Update);
				}
			}
		});
 public void TestFixtureSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address.Replace("11210", "11207"));
     var connectionPoolConfig = new PoolConfiguration
     {
         UseSsl = true
     };
     _connectionPool = new ConnectionPool<SslConnection>(connectionPoolConfig, ipEndpoint);
     _connectionPool.Initialize();
     _ioService = new PooledIOService(_connectionPool, null);
 }
Exemple #25
0
		public Thread CreateThreadCallingGetNext(IConnectionPool pool, ConcurrentBag<int> startingPositions) => new Thread(() =>
		{
			/** CallGetNext is a generator that calls GetNext() indefinitely using a local cursor */
			var seenPorts = CallGetNext(pool).Take(NumberOfNodes * 10).ToList();
			var startPosition = seenPorts.First();
			startingPositions.Add(startPosition);
			//first seenNode is e.g 9202 then start counting at 2;
			var i = (startPosition - 9200) % NumberOfNodes;
			foreach (var port in seenPorts)
				port.Should().Be(9200 + (i++ % NumberOfNodes));
		});
 public void OneTimeSetUp()
 {
     var ipEndpoint = UriExtensions.GetEndPoint(_address.Replace("11210", "11207"));
     var connectionPoolConfig = new PoolConfiguration
     {
         UseSsl = true,
         Uri = new Uri(ConfigurationManager.AppSettings["bootstrapUrl"])
     };
     _connectionPool = new ConnectionPool<SslConnection>(connectionPoolConfig, ipEndpoint);
     _connectionPool.Initialize();
     _ioService = new PooledIOService(_connectionPool, null);
 }
        public void TestFixtureSetUp()
        {
            _endPoint = UriExtensions.GetEndPoint(_address);
            var connectionPoolConfig = new PoolConfiguration
            {
                MinSize = 1,
                MaxSize = 1
            };
            _connectionPool = new ConnectionPool<Connection>(connectionPoolConfig, _endPoint);

            _ioService = new PooledIOService(_connectionPool);
        }
 public void SetUp()
 {
     _address = string.Format("{0}:11207", ConfigurationManager.AppSettings["serverIp"]);
     var ipEndpoint = UriExtensions.GetEndPoint(_address);
     var factory = DefaultConnectionFactory.GetGeneric<SslConnection>();
     var converter = new DefaultConverter();
     _configuration = new PoolConfiguration(MaxSize, MinSize, WaitTimeout, RecieveTimeout, ShutdownTimeout, SendTimeout, ConnectTimeout, MaxConnectionAcquireCount)
     {
         UseSsl = true
     };
     _connectionPool = new ConnectionPool<SslConnection>(_configuration, ipEndpoint, factory, converter);
     _connectionPool.Initialize();
 }
		public SqliteSchemaProvider (IConnectionPool connectionPool)
			: base (connectionPool)
		{
			AddSupportedSchemaActions (SchemaType.Database, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.Table, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.View, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.TableColumn, SchemaActions.All);
			AddSupportedSchemaActions (SchemaType.ProcedureParameter, SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.Trigger, SchemaActions.All);
			AddSupportedSchemaActions (SchemaType.PrimaryKeyConstraint, SchemaActions.Create | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.CheckConstraint, SchemaActions.Create | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.UniqueConstraint, SchemaActions.Create | SchemaActions.Schema);
			AddSupportedSchemaActions (SchemaType.Constraint, SchemaActions.Create | SchemaActions.Schema);
		}
        public void TestFixtureSetup()
        {
            _config = new ConnectionPoolConfig(10, 5, 1000, 1000, 1000, 1000);

            var factory = ConnectionFactory.GetDefault();
            _connectionPool = new DefaultConnectionPool(_config, Node.GetEndPoint(Server), factory);
            _connectionPool.Initialize();

            _provider = new FileSystemConfigProvider(_defaultClientConfig, ConnectionFactory.GetDefault());
            _configInfo = _provider.GetCached();

            _ioStrategy = new AwaitableIOStrategy(_connectionPool, null);
            _state = _state = new ClusterState(_defaultClientConfig);
        }
		private void AssertCreateView(IConnectionPool pool)
		{
			/** So what order do we expect? Imagine the following:
			*
			* . Thread A calls `CreateView()` first without a local cursor and takes the current value from the internal global cursor, which is `0`
			* . Thread B calls `CreateView()` second without a local cursor and therefore starts at `1`
			* . After this, each thread should walk the nodes in successive order using their local cursor. For example, Thread A might
			* get 0,1,2,3,5 and thread B will get 1,2,3,4,0.
			*/
			var startingPositions = Enumerable.Range(0, NumberOfNodes)
				.Select(i => pool.CreateView().First())
				.Select(n => n.Uri.Port)
				.ToList();

			var expectedOrder = Enumerable.Range(9200, NumberOfNodes);
			startingPositions.Should().ContainInOrder(expectedOrder);

			/**
			* What the above code just proved is that each call to `CreateView()` gets assigned the next available node.
			*
			* Lets up the ante:
			*
			* . Call `CreateView()` over `NumberOfNodes * 2` threads
			* . On each thread, call `CreateView()` `NumberOfNodes * 10` times using a local cursor.
			*
			* We'll validate that each thread sees all the nodes and that they wrap over, for example, after node 9209
			* comes 9200 again
			*/
			var threadedStartPositions = new ConcurrentBag<int>();
			var threads = Enumerable.Range(0, 20)
				.Select(i => CreateThreadCallingCreateView(pool, threadedStartPositions))
				.ToList();

			foreach (var t in threads) t.Start();
			foreach (var t in threads) t.Join();

			/**
			* Each thread reported the first node it started off. Let's make sure we see each node twice
			* because we started `NumberOfNodes * 2` threads
			*/
			var grouped = threadedStartPositions.GroupBy(p => p).ToList();
			grouped.Count.Should().Be(NumberOfNodes);
			grouped.Select(p => p.Count()).Should().OnlyContain(p => p == 2);
		}
Exemple #32
0
        public ProducerChannelFactory(
            Guid correlationId,
            IRegisterEvent eventRegister,
            IConnectionPool connectionPool,
            IExecute executor,
            ProducerOptions options)
        {
            _correlationId  = correlationId;
            _eventRegister  = eventRegister;
            _connectionPool = connectionPool;
            _executor       = executor;
            _sequenceId     = new SequenceId(options.InitialSequenceId);

            _commandProducer = new CommandProducer
            {
                ProducerName = options.ProducerName,
                Topic        = options.Topic
            };
        }
        public void Setup()
        {
            _clusterId             = new ClusterId();
            _connectionPool        = Substitute.For <IConnectionPool>();
            _connectionPoolFactory = Substitute.For <IConnectionPoolFactory>();
            _connectionPoolFactory.CreateConnectionPool(null, null)
            .ReturnsForAnyArgs(_connectionPool);

            _endPoint                   = new DnsEndPoint("localhost", 27017);
            _heartbeatConnection        = new MockConnection();
            _heartbeatConnectionFactory = Substitute.For <IConnectionFactory>();
            _heartbeatConnectionFactory.CreateConnection(null, null)
            .ReturnsForAnyArgs(_heartbeatConnection);

            _listener = Substitute.For <IServerListener>();
            _settings = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject = new ClusterableServer(_settings, _clusterId, _endPoint, _connectionPoolFactory, _heartbeatConnectionFactory, _listener);
        }
        public override IPooledDbConnection CreateConnection(IConnectionPool pool, DatabaseConnectionSettings settings, out string error)
        {
            SqlConnectionStringBuilder builder = null;

            try {
                if (settings.UseConnectionString)
                {
                    builder = new SqlConnectionStringBuilder(settings.ConnectionString);
                }
                else
                {
                    builder = new SqlConnectionStringBuilder();
                    builder.InitialCatalog = settings.Database;
                    if (settings.UseIntegratedSecurity)
                    {
                        builder.IntegratedSecurity = true;
                    }
                    else
                    {
                        builder.UserID   = settings.Username;
                        builder.Password = settings.Password;
                    }
                    if (settings.Port == 0)                     // Don't assign default port
                    {
                        builder.DataSource = settings.Server;
                    }
                    else
                    {
                        builder.DataSource = String.Concat(settings.Server, ",", settings.Port);
                    }
                    //builder.NetworkLibrary = "DBMSSOCN";
                }
                builder.Pooling = false;
                SqlConnection connection = new SqlConnection(builder.ToString());
                connection.Open();

                error = null;
                return(new SqlServerPooledDbConnection(pool, connection));
            } catch (Exception e) {
                error = e.Message;
                return(null);
            }
        }
Exemple #35
0
        public MultiplexingConnection(IConnectionPool connectionPool, Socket socket)
        {
            Socket        = socket;
            LocalEndPoint = socket.LocalEndPoint;
            EndPoint      = socket.RemoteEndPoint;

            ConnectionPool = connectionPool;

            _statesInFlight = new ConcurrentDictionary <uint, IState>();

            //allocate a buffer
            _receiveBuffer       = new byte[1024 * 16];
            _receiveBufferLength = 0;

            //Start a dedicated background thread for receiving server responses.
            _receiveThread = new Thread(ReceiveThreadBody);
            _receiveThread.IsBackground = true;
            _receiveThread.Start();
        }
Exemple #36
0
        public RabbitMqDistributedEventBus(
            IOptions <RabbitMqDistributedEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IServiceProvider serviceProvider,
            IOptions <DistributedEventBusOptions> distributedEventBusOptions)
        {
            ConnectionPool                     = connectionPool;
            Serializer                         = serializer;
            ServiceProvider                    = serviceProvider;
            DistributedEventBusOptions         = distributedEventBusOptions.Value;
            RabbitMqDistributedEventBusOptions = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
            EventTypes       = new ConcurrentDictionary <string, Type>();

            ConsumerChannel = CreateConsumerChannel();
            Subscribe(DistributedEventBusOptions.Handlers);
        }
        public RabbitMqDistributedEventBus(
            IOptions <AbpRabbitMqEventBusOptions> options,
            IConnectionPool connectionPool,
            IRabbitMqSerializer serializer,
            IServiceScopeFactory serviceScopeFactory,
            IOptions <AbpDistributedEventBusOptions> distributedEventBusOptions,
            IRabbitMqMessageConsumerFactory messageConsumerFactory,
            ICurrentTenant currentTenant)
            : base(serviceScopeFactory, currentTenant)
        {
            ConnectionPool                = connectionPool;
            Serializer                    = serializer;
            MessageConsumerFactory        = messageConsumerFactory;
            AbpDistributedEventBusOptions = distributedEventBusOptions.Value;
            AbpRabbitMqEventBusOptions    = options.Value;

            HandlerFactories = new ConcurrentDictionary <Type, List <IEventHandlerFactory> >();
            EventTypes       = new ConcurrentDictionary <string, Type>();
        }
Exemple #38
0
 public ConsumerChannelFactory(
     Guid correlationId,
     IRegisterEvent eventRegister,
     IConnectionPool connectionPool,
     CommandSubscribe subscribe,
     uint messagePrefetchCount,
     BatchHandler <TMessage> batchHandler,
     IMessageFactory <TMessage> messageFactory,
     IEnumerable <IDecompressorFactory> decompressorFactories)
 {
     _correlationId         = correlationId;
     _eventRegister         = eventRegister;
     _connectionPool        = connectionPool;
     _subscribe             = subscribe;
     _messagePrefetchCount  = messagePrefetchCount;
     _batchHandler          = batchHandler;
     _messageFactory        = messageFactory;
     _decompressorFactories = decompressorFactories;
 }
        public void Setup()
        {
            _clusterId             = new ClusterId();
            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _connectionPool        = Substitute.For <IConnectionPool>();
            _connectionPoolFactory = Substitute.For <IConnectionPoolFactory>();
            _connectionPoolFactory.CreateConnectionPool(null, null)
            .ReturnsForAnyArgs(_connectionPool);

            _endPoint                   = new DnsEndPoint("localhost", 27017);
            _heartbeatConnection        = new MockConnection();
            _heartbeatConnectionFactory = Substitute.For <IConnectionFactory>();
            _heartbeatConnectionFactory.CreateConnection(null, null)
            .ReturnsForAnyArgs(_heartbeatConnection);

            _capturedEvents = new EventCapturer();
            _settings       = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject = new ClusterableServer(_clusterId, _clusterConnectionMode, _settings, _endPoint, _connectionPoolFactory, _heartbeatConnectionFactory, _capturedEvents);
        }
Exemple #40
0
        internal Connection(IConnectionPool connectionPool, Socket socket, IByteConverter converter, BufferAllocator allocator)
            : base(socket, converter)
        {
            ConnectionPool = connectionPool;
            Configuration  = ConnectionPool.Configuration;

            //set the max close attempts so that a connection in use is not disposed
            MaxCloseAttempts = Configuration.MaxCloseAttempts;

            _allocator = allocator;

            //create a seae with an accept socket and completed event
            _eventArgs = new SocketAsyncEventArgs();
            _eventArgs.AcceptSocket = socket;
            _eventArgs.Completed   += OnCompleted;

            //set the buffer to use with this saea instance
            _allocator.SetBuffer(_eventArgs);
            Offset = _eventArgs.Offset;
        }
Exemple #41
0
        protected async Task RunScalingLogic(IConnectionPool connectionPool)
        {
            var size = connectionPool.Size;

            if (size > connectionPool.MinimumSize)
            {
                // See if we should scale down

                if (connectionPool.GetConnections().Any(p => p.IdleTime >= IdleConnectionTimeout))
                {
                    _logger.LogInformation(
                        "Detected idle connections, scaling down connection pool {endpoint}",
                        _redactor.SystemData(connectionPool.EndPoint));

                    // We have at least one connection going idle, so scale down by 1 so it's gradual.
                    // We'll reevaluate on the next cycle if we need to scale down more.
                    await connectionPool.ScaleAsync(-1).ConfigureAwait(false);

                    // Don't do any further checks
                    return;
                }
            }

            if (size < connectionPool.MaximumSize)
            {
                // See if we should scale up

                var backPressure = connectionPool.PendingSends;

                if (backPressure > BackPressureThreshold)
                {
                    // We should scale up
                    // We'll reevaluate on the next cycle if we need to scale up more.

                    _logger.LogInformation(
                        "Detected {count} back pressure, scaling up connection pool {endpoint}",
                        backPressure, _redactor.SystemData(connectionPool.EndPoint));
                    await connectionPool.ScaleAsync(1).ConfigureAwait(false);
                }
            }
        }
Exemple #42
0
        public ElasticsearchReporter(string[] elasticsearchUris, string indexName
                                     , string apiId       = null, string apiKey   = null
                                     , string username    = null, string password = null
                                     , int?timeoutSeconds = null, bool debug      = false)
        {
            _uris      = elasticsearchUris;
            _indexName = indexName;
            _debug     = debug;
            //https://www.elastic.co/guide/en/elasticsearch/client/net-api/2.x/connection-pooling.html
            if (null == elasticsearchUris || 0 == elasticsearchUris.Length)
            {
                throw new ArgumentException("Uri list should not be empty", nameof(elasticsearchUris));
            }
            IConnectionPool pool = elasticsearchUris.Length == 1 ? new SingleNodeConnectionPool(new Uri(elasticsearchUris.First())) as IConnectionPool :
                                   new StaticConnectionPool(_uris.Select(u => new Uri(u)));
            var _settings = new ConnectionSettings(pool)
                            .DefaultIndex(indexName)
                            //.DefaultMappingFor<ElasticsearchErrorDocument>(m => m)
                            //.IndexName())
            ;

            if (!string.IsNullOrEmpty(apiId))
            {
                _settings.ApiKeyAuthentication(apiId, apiKey);
            }
            if (!string.IsNullOrEmpty(username))
            {
                _settings.BasicAuthentication(username, password);
                _settings.ServerCertificateValidationCallback((o, certificate, chain, errors) => true);
            }
            if (timeoutSeconds.HasValue)
            {
                _settings = _settings.RequestTimeout(TimeSpan.FromSeconds(timeoutSeconds.Value))
                            .MaxDeadTimeout(TimeSpan.FromSeconds(timeoutSeconds.Value));
            }
            if (_debug)
            {
                _settings = _settings.DisableDirectStreaming();
            }
            _client = new ElasticsearchImpl.ElasticClient(new ElasticClient(_settings));
        }
        /// <summary>
        /// Initializes a new instance of <see cref="EsIndexFixture{TDoc, TConnectionProvider}"/>
        /// </summary>
        protected EsIndexFixture(TConnectionProvider connectionProvider)
        {
            _connection = connectionProvider.Provide();

            var settings = new ConnectionSettings(_connection);

            settings.DisableDirectStreaming();
            settings.OnRequestCompleted(details =>
            {
                try
                {
                    Output?.WriteLine(ApiCallDumper.ApiCallToDump(details));
                }
                catch (InvalidOperationException e) when(e.Message == "There is no currently active test.")
                {
                    //Do nothing
                }
            });

            EsClient = new ElasticClient(settings);
        }
        /// <summary>
        /// new
        /// </summary>
        /// <param name="clientProtocolHandlerFactory"></param>
        /// <param name="socketBufferSize"></param>
        /// <param name="messageBufferSize"></param>
        /// <param name="millisecondsSendTimeout"></param>
        /// <param name="millisecondsReceiveTimeout"></param>
        /// <exception cref="ArgumentNullException">protocol is null</exception>
        public SocketClient(ISyncClientProtocolHandlerFactory <ISyncClientProtocolHandler <TMessageInfo, TMessage>, TMessageInfo, TMessage> clientProtocolHandlerFactory,
                            int socketBufferSize,
                            int messageBufferSize,
                            int millisecondsSendTimeout,
                            int millisecondsReceiveTimeout)
            : base(socketBufferSize, messageBufferSize)
        {
            this._protocolHandlerFactory = clientProtocolHandlerFactory ?? throw new ArgumentNullException("protocol");

            this._connectionPool = new SyncPool <TMessageInfo, TMessage>();

            this._millisecondsSendTimeout    = millisecondsSendTimeout;
            this._millisecondsReceiveTimeout = millisecondsReceiveTimeout;

            this._pendingQueue   = new PendingSendQueue(this);
            this._receivingQueue = new ReceivingQueue <TMessageInfo, TMessage>(this);

            this._endPointManager            = new EndPointManager <TMessageInfo, TMessage>(this);
            this._endPointManager.Connected += this.OnEndPointConnected;
            this._endPointManager.Already   += this.OnEndPointAlready;
        }
Exemple #45
0
        public ProducerChannelFactory(
            Guid correlationId,
            IRegisterEvent eventRegister,
            IConnectionPool connectionPool,
            IExecute executor,
            ProducerOptions options,
            ICompressorFactory?compressorFactory)
        {
            _correlationId  = correlationId;
            _eventRegister  = eventRegister;
            _connectionPool = connectionPool;
            _executor       = executor;

            _commandProducer = new CommandProducer
            {
                ProducerName = options.ProducerName,
                Topic        = options.Topic
            };

            _compressorFactory = compressorFactory;
        }
Exemple #46
0
        // constructors
        public Server(ClusterId clusterId, ClusterConnectionMode clusterConnectionMode, ServerSettings settings, EndPoint endPoint, IConnectionPoolFactory connectionPoolFactory, IServerMonitorFactory serverMonitorFactory, IEventSubscriber eventSubscriber)
        {
            Ensure.IsNotNull(clusterId, nameof(clusterId));
            _clusterConnectionMode = clusterConnectionMode;
            _settings = Ensure.IsNotNull(settings, nameof(settings));
            _endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint));
            Ensure.IsNotNull(connectionPoolFactory, nameof(connectionPoolFactory));
            Ensure.IsNotNull(serverMonitorFactory, nameof(serverMonitorFactory));
            Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));

            _serverId       = new ServerId(clusterId, endPoint);
            _connectionPool = connectionPoolFactory.CreateConnectionPool(_serverId, endPoint);
            _state          = new InterlockedInt32(State.Initial);
            _monitor        = serverMonitorFactory.Create(_serverId, _endPoint);

            eventSubscriber.TryGetEventHandler(out _openingEventHandler);
            eventSubscriber.TryGetEventHandler(out _openedEventHandler);
            eventSubscriber.TryGetEventHandler(out _closingEventHandler);
            eventSubscriber.TryGetEventHandler(out _closedEventHandler);
            eventSubscriber.TryGetEventHandler(out _descriptionChangedEventHandler);
        }
        private IConnectionPool CreateConnectionPool()
        {
            Uri[] uris = new Uri[_option.Urls.Length];
            for (int i = 0; i < _option.Urls.Length; i++)
            {
                uris[i] = new Uri(_option.Urls[i]);
            }

            IConnectionPool connectionPool = null;

            if (uris.Length == 1)
            {
                connectionPool = new SingleNodeConnectionPool(uris[0]);
            }
            else
            {
                connectionPool = new SniffingConnectionPool(uris);
            }

            return(connectionPool);
        }
Exemple #48
0
        public void Start(IConnectionPool connectionPool)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(DefaultConnectionPoolScaleController));
            }

            _connectionPool = connectionPool ?? throw new ArgumentNullException(nameof(connectionPool));

            _logger.LogDebug(
                "Starting connection pool monitor on {endpoint}, idle timeout {idleTimeout}, back pressure threshold {backPressureThreshold}",
                _redactor.SystemData(_connectionPool.EndPoint), IdleConnectionTimeout, BackPressureThreshold);

            using (ExecutionContext.SuppressFlow())
            {
                // We must suppress flow so that the tracing Activity which is current during bootstrap doesn't live on forever
                // as the parent span for all scaling activities.

                Task.Run(MonitorAsync, _cts.Token);
            }
        }
        internal Connection(IConnectionPool connectionPool, Socket socket, SocketAsyncEventArgs eventArgs, IByteConverter converter)
            : base(socket, converter)
        {
            //set the configuration info
            ConnectionPool = connectionPool;
            Configuration  = ConnectionPool.Configuration;

            //set the max close attempts so that a connection in use is not disposed
            MaxCloseAttempts = Configuration.MaxCloseAttempts;

            //Since the config can be changed on the fly create allocator late in the cycle
            _allocator = Configuration.BufferAllocator(Configuration);

            //create a seae with an accept socket and completed event
            _eventArgs = eventArgs;
            _eventArgs.AcceptSocket = socket;
            _eventArgs.Completed   += OnCompleted;

            //set the buffer to use with this saea instance
            _allocator.SetBuffer(_eventArgs);
        }
Exemple #50
0
        public MySqlSchemaProvider(IConnectionPool connectionPool)
            : base(connectionPool)
        {
            AddSupportedSchemaActions(SchemaType.Database, SchemaActions.All);
            AddSupportedSchemaActions(SchemaType.Table, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.View, SchemaActions.All);
            AddSupportedSchemaActions(SchemaType.TableColumn, SchemaActions.All);
            AddSupportedSchemaActions(SchemaType.Trigger, SchemaActions.All);
            AddSupportedSchemaActions(SchemaType.PrimaryKeyConstraint, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.ForeignKeyConstraint, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.CheckConstraint, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.UniqueConstraint, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.Constraint, SchemaActions.Create | SchemaActions.Drop | SchemaActions.Rename | SchemaActions.Schema);
            AddSupportedSchemaActions(SchemaType.User, SchemaActions.Schema);

            if (connectionPool.DatabaseVersion.Major > 4)
            {
                AddSupportedSchemaActions(SchemaType.Procedure, SchemaActions.All);
                AddSupportedSchemaActions(SchemaType.ProcedureParameter, SchemaActions.Schema);
            }
        }
Exemple #51
0
        public void Test_Acquire_2ndRequest_Gets_Connection_From_Pool_While_1stRequest_Waits_For_Opening()
        {
            //Arrange
            var ipEndpoint       = UriExtensions.GetEndPoint(_address);
            var factoryWithDelay = new Func <IConnectionPool <Connection>, IByteConverter, BufferAllocator, Connection>(
                (a, b, c) =>
            {
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //remove sleep and use Moq for threads synchronization
                Thread.Sleep(500);
                return(new Connection(a, socket, b, c));
            });

            _configuration  = new PoolConfiguration(MaxSize, 1, WaitTimeout, RecieveTimeout, ShutdownTimeout, SendTimeout, ConnectTimeout, MaxConnectionAcquireCount, BucketName);
            _connectionPool = new ConnectionPool <Connection>(_configuration, ipEndpoint, factoryWithDelay, new DefaultConverter());
            _connectionPool.Initialize();

            //Act
            var connectionFromPool = _connectionPool.Acquire();
            var task1 = new Task <IConnection>(() => _connectionPool.Acquire());
            var task2 = new Task <IConnection>(() => _connectionPool.Acquire());

            task1.Start();
            Thread.Sleep(100);
            task2.Start();
            //enqueue connection to pool
            //at this point task2 should get released connection
            _connectionPool.Release(connectionFromPool);

            Task.WaitAll(task1, task2);

            var connectionFromFactory      = task1.Result;
            var connectionFromPoolReleased = task2.Result;


            //Assert
            Assert.IsNotNull(connectionFromFactory);
            Assert.AreNotEqual(connectionFromPool, connectionFromFactory);
            Assert.AreEqual(connectionFromPool, connectionFromPoolReleased);
        }
Exemple #52
0
        public ProducerChannelFactory(
            Guid correlationId,
            IRegisterEvent eventRegister,
            IConnectionPool connectionPool,
            string topic,
            string?producerName,
            SchemaInfo schemaInfo,
            ICompressorFactory?compressorFactory)
        {
            _correlationId  = correlationId;
            _eventRegister  = eventRegister;
            _connectionPool = connectionPool;

            _commandProducer = new CommandProducer
            {
                ProducerName = producerName,
                Topic        = topic
            };

            _compressorFactory = compressorFactory;
            _schema            = schemaInfo.PulsarSchema;
        }
Exemple #53
0
        public static void RetutnConnection(string storageName, IDbConnection connection)
        {
            if (null == connection)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The connection is empty from the {0} pool.", storageName);
                }
                throw new ArgumentNullException("connection");
            }
            IConnectionPool pool = GetPool(storageName);

            if (null == pool)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The {0} pool is empty.", storageName);
                }
                return;
            }
            pool.ReturnObject(connection);
        }
Exemple #54
0
        public Connection(IConnectionPool connectionPool, Socket socket, IByteConverter converter, BufferAllocator allocator)
            : base(socket, converter, allocator)
        {
            ConnectionPool = connectionPool;
            Configuration  = ConnectionPool.Configuration;

            //set the max close attempts so that a connection in use is not disposed
            MaxCloseAttempts = Configuration.MaxCloseAttempts;

            //create a seae with an accept socket and completed event
            _eventArgs = new SocketAsyncEventArgs();
            _eventArgs.AcceptSocket = socket;
            _eventArgs.Completed   += OnCompleted;

            //set the buffer to use with this saea instance
            if (!BufferAllocator.SetBuffer(_eventArgs))
            {
                // failed to acquire a buffer because the allocator was exhausted

                throw new BufferUnavailableException("Unable to allocate a buffer for this connection because the BufferAllocator is exhausted.");
            }
        }
Exemple #55
0
        public Transport(
            IConnectionConfigurationValues configurationValues,
            IConnection connection,
            IElasticsearchSerializer serializer,
            IDateTimeProvider dateTimeProvider = null
            )
        {
            this._configurationValues = configurationValues;
            this._connection          = connection ?? new HttpConnection(configurationValues);
            this._serializer          = serializer ?? new ElasticsearchDefaultSerializer();
            this._connectionPool      = this._configurationValues.ConnectionPool;

            this._dateTimeProvider = dateTimeProvider ?? new DateTimeProvider();

            this._lastSniff = this._dateTimeProvider.Now();

            this.Settings.Serializer = this._serializer;
            if (this._connectionPool.AcceptsUpdates && this.Settings.SniffsOnStartup)
            {
                this.Sniff();
            }
        }
        protected ConnectionSettingsBase(
            IConnectionPool connectionPool,
            IConnection connection,
            ConnectionSettings.SourceSerializerFactory sourceSerializerFactory,
            IPropertyMappingProvider propertyMappingProvider
            )
            : base(connectionPool, connection, null)
        {
            var defaultSerializer = new InternalSerializer(this);

            _sourceSerializer = sourceSerializerFactory?.Invoke(defaultSerializer, this) ?? defaultSerializer;
            UseThisRequestResponseSerializer = defaultSerializer;
            _propertyMappingProvider         = propertyMappingProvider ?? new PropertyMappingProvider();

            _defaultTypeNameInferrer  = t => !_defaultTypeName.IsNullOrEmpty() ? _defaultTypeName : t.Name.ToLowerInvariant();
            _defaultFieldNameInferrer = p => p.ToCamelCase();
            _defaultIndices           = new FluentDictionary <Type, string>();
            _defaultTypeNames         = new FluentDictionary <Type, string>();
            _defaultRelationNames     = new FluentDictionary <Type, string>();

            _inferrer = new Inferrer(this);
        }
Exemple #57
0
        public MultiplexingConnection(IConnectionPool connectionPool, Socket socket, IByteConverter converter,
                                      BufferAllocator allocator)
            : base(socket, converter, allocator)
        {
            ConnectionPool = connectionPool;
            Configuration  = ConnectionPool.Configuration;

            //set the max close attempts so that a connection in use is not disposed
            MaxCloseAttempts = Configuration.MaxCloseAttempts;

            _statesInFlight = new ConcurrentDictionary <uint, IState>();
            _statePool      = new ConcurrentQueue <SyncState>();

            //allocate a buffer
            _receiveBuffer       = new byte[Configuration.BufferSize];
            _receiveBufferLength = 0;

            //Start a dedicated background thread for receiving server responses.
            _receiveThread = new Thread(ReceiveThreadBody);
            _receiveThread.IsBackground = true;
            _receiveThread.Start();
        }
Exemple #58
0
        public static void Setup()
        {
            var assambly = Assembly.GetAssembly(typeof(Program));
            var config   = Configuration.Create()
                           .UseAutofac()
                           .RegisterCommonComponents()
                           .UseLog4Net()
                           .UseJsonNet()
                           .UseQuartz(new Assembly[] { assambly })
                           .UseRabbitMQ("localhost", "/", "guest", "guest")
                           .UseRedisCache()
                           .UseMassTransit(new Assembly[] { assambly })
                           .UseKafka("");

            using (var scope = ObjectContainer.Current.BeginLifetimeScope())
            {
                _logger         = scope.Resolve <ILoggerFactory>().Create(typeof(Program).Name);
                _bus            = scope.Resolve <IBus>();
                _conn           = scope.Resolve <IConnectionPool>();
                factory         = scope.Resolve <IConsumerClientFactory>();
                _consumerClient = scope.Resolve <IKafkaPersisterConnection>();
            }
        }
        public override IPooledDbConnection CreateConnection(IConnectionPool pool, DatabaseConnectionSettings settings, out string error)
        {
            string connStr = null;

            try {
                if (settings.UseConnectionString)
                {
                    connStr = settings.ConnectionString;
                }
                else
                {
                    connStr = String.Concat("URI=file:", settings.Database);
                }
                SqliteConnection connection = new SqliteConnection(connStr);
                connection.Open();

                error = null;
                return(new SqlitePooledDbConnection(pool, connection));
            } catch (Exception e) {
                error = e.Message;
                return(null);
            }
        }
Exemple #60
0
        /// <summary>
        /// Initializes a new instance of <see cref="IEsClientProvider"/>
        /// </summary>
        public EsClientProvider(
            ElasticsearchOptions options,
            ILogger <EsClientProvider> logger = null)
        {
            _connectionPool = new SingleNodeConnectionPool(new Uri(options.Url));

            var settings = new ConnectionSettings(_connectionPool);

            if (logger != null)
            {
                var log = logger.Dsl();

                settings.DisableDirectStreaming();
                settings.OnRequestCompleted(details =>
                {
                    log.Debug("ElasticSearch request completed")
                    .AndFactIs("dump", ApiCallDumper.ApiCallToDump(details))
                    .Write();
                });
            }

            _client = new ElasticClient(settings);
        }