Skip to content

Commit

Permalink
Pool/SNOW-937189 busy sessions in pool (3/4) (#818)
Browse files Browse the repository at this point in the history
### Description
Connection Pool v2:
- introduction of a counter of busy sessions created by Pool Manager
- pool size counts both: Idle and Busy sessions

### Checklist
- [x] Code compiles correctly
- [x] Code is formatted according to [Coding
Conventions](../CodingConventions.md)
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing (`dotnet test`)
- [x] Extended the README / documentation, if necessary
- [x] Provide JIRA issue id (if possible) or GitHub issue id in PR name
  • Loading branch information
sfc-gh-knozderko authored Nov 28, 2023
1 parent bf7a13e commit 56f0b81
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 103 deletions.
85 changes: 82 additions & 3 deletions Snowflake.Data.Tests/IntegrationTests/ConnectionMultiplePoolsIT.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using NUnit.Framework;
using Snowflake.Data.Client;
Expand Down Expand Up @@ -33,7 +34,28 @@ public static void AfterAllTests()
}

[Test]
public void TestNewConnectionPoolFull()
public void TestReuseSessionInConnectionPool() // old name: TestConnectionPool
{
var conn1 = new SnowflakeDbConnection(ConnectionString);
conn1.Open();
Assert.AreEqual(ConnectionState.Open, conn1.State);
conn1.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

var conn2 = new SnowflakeDbConnection();
conn2.ConnectionString = ConnectionString;
conn2.Open();
Assert.AreEqual(ConnectionState.Open, conn2.State);
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

conn2.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(ConnectionState.Closed, conn2.State);
}

[Test]
public void TestReuseSessionInConnectionPoolReachingMaxConnections() // old name: TestConnectionPoolFull
{
var pool = SnowflakeDbConnectionPool.GetPool(ConnectionString);
pool.SetMaxPoolSize(2);
Expand All @@ -48,7 +70,7 @@ public void TestNewConnectionPoolFull()
conn2.Open();
Assert.AreEqual(ConnectionState.Open, conn2.State);

Assert.AreEqual(0, pool.GetCurrentPoolSize());
Assert.AreEqual(2, pool.GetCurrentPoolSize());
conn1.Close();
conn2.Close();
Assert.AreEqual(2, pool.GetCurrentPoolSize());
Expand All @@ -64,7 +86,7 @@ public void TestNewConnectionPoolFull()
Assert.AreEqual(ConnectionState.Open, conn4.State);

conn3.Close();
Assert.AreEqual(1, pool.GetCurrentPoolSize()); // TODO: when SNOW-937189 complete should be 2
Assert.AreEqual(2, pool.GetCurrentPoolSize());
conn4.Close();
Assert.AreEqual(2, pool.GetCurrentPoolSize());

Expand All @@ -74,6 +96,63 @@ public void TestNewConnectionPoolFull()
Assert.AreEqual(ConnectionState.Closed, conn4.State);
}

[Test]
public void TestBusyAndIdleConnectionsCountedInPoolSize()
{
// arrange
var pool = SnowflakeDbConnectionPool.GetPool(ConnectionString);
pool.SetMaxPoolSize(2);
var connection = new SnowflakeDbConnection();
connection.ConnectionString = ConnectionString;

// act
connection.Open();

// assert
Assert.AreEqual(1, pool.GetCurrentPoolSize());

// act
connection.Close();

// assert
Assert.AreEqual(1, pool.GetCurrentPoolSize());
}

[Test]
[Ignore("Enable when disabling pooling in connection string enabled - SNOW-902632")]
public void TestConnectionPoolNotPossibleToDisableForAllPools()
{
// act
var thrown = Assert.Throws<Exception>(() => SnowflakeDbConnectionPool.SetPooling(false));

// assert
Assert.IsNotNull(thrown);
}

[Test]
public void TestConnectionPoolDisable()
{
// arrange
var pool = SnowflakeDbConnectionPool.GetPool(ConnectionString);
pool.SetPooling(false);
var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = ConnectionString;

// act
conn1.Open();

// assert
Assert.AreEqual(ConnectionState.Open, conn1.State);
Assert.AreEqual(0, pool.GetCurrentPoolSize());

// act
conn1.Close();

// assert
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(0, pool.GetCurrentPoolSize());
}

[Test]
public void TestNewConnectionPoolClean()
{
Expand Down
58 changes: 2 additions & 56 deletions Snowflake.Data.Tests/IntegrationTests/ConnectionPoolCommonIT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,6 @@ public void TestBasicConnectionPool()
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());
}

[Test]
public void TestConnectionPool()
{
var conn1 = new SnowflakeDbConnection(ConnectionString);
conn1.Open();
Assert.AreEqual(ConnectionState.Open, conn1.State);
conn1.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

var conn2 = new SnowflakeDbConnection();
conn2.ConnectionString = ConnectionString;
conn2.Open();
Assert.AreEqual(ConnectionState.Open, conn2.State);
Assert.AreEqual(0, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

conn2.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(ConnectionState.Closed, conn2.State);
}

[Test]
public void TestConnectionPoolIsFull()
{
Expand Down Expand Up @@ -273,25 +252,9 @@ void ThreadProcess2(string connstr)
conn1.Close();
SnowflakeDbConnectionPool.ClearAllPools();
SnowflakeDbConnectionPool.SetMaxPoolSize(0);
SnowflakeDbConnectionPool.SetPooling(false);
}

[Test]
public void TestConnectionPoolDisable()
{
var pool = SnowflakeDbConnectionPool.GetPool(ConnectionString);
pool.SetPooling(false);

var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = ConnectionString;
conn1.Open();
Assert.AreEqual(ConnectionState.Open, conn1.State);
conn1.Close();

Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(0, pool.GetCurrentPoolSize());
SnowflakeDbConnectionPool.SetPooling(true);
}

[Test]
public void TestConnectionPoolWithDispose()
{
Expand All @@ -305,22 +268,5 @@ public void TestConnectionPoolWithDispose()
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(0, SnowflakeDbConnectionPool.GetPool(conn1.ConnectionString).GetCurrentPoolSize());
}

[Test]
public void TestConnectionPoolTurnOff()
{
SnowflakeDbConnectionPool.SetPooling(false);
SnowflakeDbConnectionPool.SetPooling(true);
SnowflakeDbConnectionPool.SetMaxPoolSize(1);

var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = ConnectionString;
conn1.Open();
Assert.AreEqual(ConnectionState.Open, conn1.State);
conn1.Close();

Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,28 @@ public static void AfterAllTests()
}

[Test]
public void TestConnectionPoolFull()
public void TestPoolContainsClosedConnections() // old name: TestConnectionPool
{
var conn1 = new SnowflakeDbConnection(ConnectionString);
conn1.Open();
Assert.AreEqual(ConnectionState.Open, conn1.State);
conn1.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

var conn2 = new SnowflakeDbConnection();
conn2.ConnectionString = ConnectionString;
conn2.Open();
Assert.AreEqual(ConnectionState.Open, conn2.State);
Assert.AreEqual(0, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());

conn2.Close();
Assert.AreEqual(1, SnowflakeDbConnectionPool.GetPool(ConnectionString).GetCurrentPoolSize());
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(ConnectionState.Closed, conn2.State);
}

[Test]
public void TestPoolContainsAtMostMaxPoolSizeConnections() // old name: TestConnectionPoolFull
{
SnowflakeDbConnectionPool.SetMaxPoolSize(2);

Expand Down Expand Up @@ -71,7 +92,54 @@ public void TestConnectionPoolFull()
Assert.AreEqual(ConnectionState.Closed, conn4.State);
SnowflakeDbConnectionPool.ClearAllPools();
}

[Test]
public void TestConnectionPoolDisableFromPoolManagerLevel()
{
// arrange
SnowflakeDbConnectionPool.SetPooling(false);
var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = ConnectionString;

// act
conn1.Open();

// assert
Assert.AreEqual(ConnectionState.Open, conn1.State);
Assert.AreEqual(0, SnowflakeDbConnectionPool.GetCurrentPoolSize());

// act
conn1.Close();

// assert
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(0, SnowflakeDbConnectionPool.GetCurrentPoolSize());
}

[Test]
public void TestConnectionPoolDisable()
{
// arrange
var pool = SnowflakeDbConnectionPool.GetPool(ConnectionString);
pool.SetPooling(false);
var conn1 = new SnowflakeDbConnection();
conn1.ConnectionString = ConnectionString;

// act
conn1.Open();

// assert
Assert.AreEqual(ConnectionState.Open, conn1.State);
Assert.AreEqual(0, pool.GetCurrentPoolSize());

// act
conn1.Close();

// assert
Assert.AreEqual(ConnectionState.Closed, conn1.State);
Assert.AreEqual(0, pool.GetCurrentPoolSize());
}

[Test]
public void TestConnectionPoolClean()
{
Expand Down
24 changes: 6 additions & 18 deletions Snowflake.Data.Tests/UnitTests/ConnectionPoolManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
*/

using System;
using System.Collections.Generic;
using System.Security;
using System.Threading;
Expand Down Expand Up @@ -160,30 +161,17 @@ public void TestSetTimeoutForAllPools()
}

[Test]
public void TestSetPoolingDisabledForAllPools()
[Ignore("Enable when disabling pooling in connection string enabled - SNOW-902632")]
public void TestSetPoolingDisabledForAllPoolsNotPossible()
{
// Arrange
var sessionPool1 = _connectionPoolManager.GetPool(ConnectionString1, _password);
_connectionPoolManager.GetPool(ConnectionString1, _password);

// Act
_connectionPoolManager.SetPooling(false);

// Assert
Assert.AreEqual(false, sessionPool1.GetPooling());
}

[Test]
public void TestSetPoolingEnabledBack()
{
// Arrange
var sessionPool1 = _connectionPoolManager.GetPool(ConnectionString1, _password);
_connectionPoolManager.SetPooling(false);

// Act
_connectionPoolManager.SetPooling(true);
var thrown = Assert.Throws<Exception>(() => _connectionPoolManager.SetPooling(false));

// Assert
Assert.AreEqual(true, sessionPool1.GetPooling());
Assert.IsNotNull(thrown);
}

[Test]
Expand Down
48 changes: 48 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Session/FixedZeroCounterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using NUnit.Framework;
using Snowflake.Data.Core.Session;

namespace Snowflake.Data.Tests.UnitTests.Session
{
[TestFixture]
public class FixedZeroCounterTest
{
[Test]
public void TestInitialZero()
{
// arrange
var counter = new FixedZeroCounter();

// act
var count = counter.Count();

// assert
Assert.AreEqual(0, count);
}

[Test]
public void TestZeroAfterIncrease()
{
// arrange
var counter = new FixedZeroCounter();

// act
counter.Increase();

// assert
Assert.AreEqual(0, counter.Count());
}

[Test]
public void TestZeroAfterDecrease()
{
// arrange
var counter = new FixedZeroCounter();

// act
counter.Decrease();

// assert
Assert.AreEqual(0, counter.Count());
}
}
}
Loading

0 comments on commit 56f0b81

Please sign in to comment.