Skip to content

Commit

Permalink
debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-knozderko committed Jan 2, 2024
1 parent 2bb889f commit 1b763f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NUnit.Framework;
using Snowflake.Data.Client;
using Snowflake.Data.Core.Session;
using Snowflake.Data.Log;
using Snowflake.Data.Tests.Util;

namespace Snowflake.Data.Tests.IntegrationTests
Expand All @@ -16,6 +17,7 @@ namespace Snowflake.Data.Tests.IntegrationTests
public class ConnectionMultiplePoolsIT: SFBaseTest
{
private readonly PoolConfig _previousPoolConfig = new PoolConfig();
private static readonly SFLogger s_logger = SFLoggerFactory.GetLogger<ConnectionMultiplePoolsIT>();

[SetUp]
public new void BeforeTest()
Expand Down Expand Up @@ -104,7 +106,7 @@ public void TestReuseSessionInConnectionPoolReachingMaxConnections() // old name
public void TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimit()
{
// arrange
var connectionString = ConnectionString;
var connectionString = ConnectionString + "application=TestWaitForMaxSize1";
var pool = SnowflakeDbConnectionPool.GetPool(connectionString);
Assert.AreEqual(0, pool.GetCurrentPoolSize(), "expecting pool to be empty");
pool.SetMaxPoolSize(2);
Expand All @@ -115,10 +117,13 @@ public void TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimit()

// act
watch.Start();
var start = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var thrown = Assert.Throws<SnowflakeDbException>(() => OpenedConnection(connectionString));
var stop = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
watch.Stop();

// assert
s_logger.Debug($"TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimit - start at: {start}, stop at: {stop}");
Assert.That(thrown.Message, Does.Contain("Unable to connect. Could not obtain a connection from the pool within a given timeout"));
Assert.GreaterOrEqual(watch.ElapsedMilliseconds, 1000);
Assert.LessOrEqual(watch.ElapsedMilliseconds, 1500);
Expand All @@ -133,7 +138,7 @@ public void TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimit()
public void TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimitAsync()
{
// arrange
var connectionString = ConnectionString;
var connectionString = ConnectionString + "application=TestWaitForMaxSize2";
var pool = SnowflakeDbConnectionPool.GetPool(connectionString);
Assert.AreEqual(0, pool.GetCurrentPoolSize(), "expecting pool to be empty");
pool.SetMaxPoolSize(2);
Expand Down Expand Up @@ -165,7 +170,7 @@ public void TestWaitForTheIdleConnectionWhenExceedingMaxConnectionsLimitAsync()
public void TestWaitInAQueueForAnIdleSession()
{
// arrange
var connectionString = ConnectionString + "application=TestWaitInAQueueForAnIdleSession";
var connectionString = ConnectionString + "application=TestWaitForMaxSize3";
var pool = SnowflakeDbConnectionPool.GetPool(connectionString);
Assert.AreEqual(0, pool.GetCurrentPoolSize(), "the pool is expected to be empty");
pool.SetMaxPoolSize(2);
Expand Down
16 changes: 12 additions & 4 deletions Snowflake.Data/Core/Session/SessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,37 @@ private SFSession WaitForSession(string connStr)
var timeout = _waitingQueue.GetWaitingTimeoutMillis();
s_logger.Debug($"SessionPool::WaitForSession for {timeout} millis timeout");
var beforeWaitingTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
string debugApplicationName = "";
if (connStr.EndsWith("application=TestWaitForMaxSize1"))
debugApplicationName = "TestWaitForMaxSize1";
if (connStr.EndsWith("application=TestWaitForMaxSize2"))
debugApplicationName = "TestWaitForMaxSize2";
if (connStr.EndsWith("application=TestWaitForMaxSize3"))
debugApplicationName = "TestWaitForMaxSize3";
s_logger.Debug($"Wait for session started at: {beforeWaitingTime} for: {debugApplicationName}");
long nowTime = beforeWaitingTime;
while (nowTime < beforeWaitingTime + timeout) // we loop to handle the case if someone overtook us after being woken or session which we were promised has just expired
{
var timeoutLeft = beforeWaitingTime + timeout - nowTime;
var successful = _waitingQueue.Wait((int) timeoutLeft, CancellationToken.None);
if (!successful)
{
s_logger.Debug($"SessionPool::WaitForSession - woken without a session granted");
s_logger.Debug($"SessionPool::WaitForSession - woken without a session granted for: {debugApplicationName}");
throw WaitingFailedException();
}
s_logger.Debug($"SessionPool::WaitForSession - woken with a session granted");
s_logger.Debug($"SessionPool::WaitForSession - woken with a session granted for: {debugApplicationName}");
lock (_sessionPoolLock)
{
var session = ExtractIdleSession(connStr);
if (session != null)
{
s_logger.Debug($"SessionPool::WaitForSession - a session was extracted from idle sessions");
s_logger.Debug($"SessionPool::WaitForSession - a session was extracted from idle sessions for: {debugApplicationName}");
return session;
}
}
nowTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
s_logger.Debug($"SessionPool::WaitForSession - could not find any idle session available withing a given timeout");
s_logger.Debug($"SessionPool::WaitForSession - could not find any idle session available withing a given timeout for: {debugApplicationName} at: {nowTime}");
throw WaitingFailedException();
}

Expand Down

0 comments on commit 1b763f5

Please sign in to comment.