Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-968914 Break waiting on pool destroy #953

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Snowflake.Data.Tests/UnitTests/Session/FixedZeroCounterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public void TestInitialZero()
{
// arrange
var counter = new FixedZeroCounter();

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

// assert
Assert.AreEqual(0, count);
}
Expand All @@ -24,25 +24,38 @@ 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());
}

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

// act
counter.Reset();

// assert
Assert.AreEqual(0, counter.Count());
}
}
}
15 changes: 15 additions & 0 deletions Snowflake.Data.Tests/UnitTests/Session/NonNegativeCounterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,20 @@ public void TestDecreaseDoesNotGoBelowZero()
// assert
Assert.AreEqual(0, counter.Count());
}

[Test]
public void TestReset()
{
// arrange
var counter = new NonNegativeCounter();
counter.Increase();
counter.Increase();

// act
counter.Reset();

// assert
Assert.AreEqual(0, counter.Count());
}
}
}
22 changes: 16 additions & 6 deletions Snowflake.Data.Tests/UnitTests/Session/NonWaitingQueueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public void TestWaitDoesNotHangAndReturnsFalse()
// arrange
var nonWaitingQueue = new NonWaitingQueue();
var watch = new Stopwatch();

// act
watch.Start();
var result = nonWaitingQueue.Wait(10000, CancellationToken.None);
watch.Stop();

// assert
Assert.IsFalse(result);
Assert.LessOrEqual(watch.ElapsedMilliseconds, 50);
Expand All @@ -31,10 +31,10 @@ public void TestNoOneIsWaiting()
// arrange
var nonWaitingQueue = new NonWaitingQueue();
nonWaitingQueue.Wait(10000, CancellationToken.None);

// act
var isAnyoneWaiting = nonWaitingQueue.IsAnyoneWaiting();

// assert
Assert.IsFalse(isAnyoneWaiting);
}
Expand All @@ -47,9 +47,19 @@ public void TestWaitingDisabled()

// act
var isWaitingEnabled = nonWaitingQueue.IsWaitingEnabled();

// assert
Assert.IsFalse(isWaitingEnabled);
}

[Test]
public void TestReset()
{
// arrange
var nonWaitingQueue = new NonWaitingQueue();

// act/assert
Assert.DoesNotThrow(() => nonWaitingQueue.Reset());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ public class SessionCreationTokenCounterTest
{
private static readonly TimeSpan s_longTime = TimeSpan.FromSeconds(30);
private static readonly TimeSpan s_shortTime = TimeSpan.FromMilliseconds(50);

[Test]
public void TestGrantSessionCreation()
{
// arrange
var tokens = new SessionCreationTokenCounter(s_longTime);

// act
tokens.NewToken();

// assert
Assert.AreEqual(1, tokens.Count());

// act
tokens.NewToken();

// assert
Assert.AreEqual(2, tokens.Count());
}
Expand All @@ -38,16 +38,16 @@ public void TestCompleteSessionCreation()
var tokens = new SessionCreationTokenCounter(s_longTime);
var token1 = tokens.NewToken();
var token2 = tokens.NewToken();

// act
tokens.RemoveToken(token1);

// assert
Assert.AreEqual(1, tokens.Count());

// act
tokens.RemoveToken(token2);

// assert
Assert.AreEqual(0, tokens.Count());
}
Expand All @@ -59,7 +59,7 @@ public void TestCompleteUnknownTokenDoesNotThrowExceptions()
var tokens = new SessionCreationTokenCounter(s_longTime);
tokens.NewToken();
var unknownToken = new SessionCreationToken(SFSessionHttpClientProperties.DefaultConnectionTimeout);

// act
tokens.RemoveToken(unknownToken);

Expand All @@ -80,7 +80,22 @@ public void TestCompleteCleansExpiredTokens()

// act
tokens.RemoveToken(token);


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

[Test]
public void TestResetTokens()
{
// arrange
var tokens = new SessionCreationTokenCounter(s_longTime);
tokens.NewToken();
tokens.NewToken();

// act
tokens.Reset();

// assert
Assert.AreEqual(0, tokens.Count());
}
Expand Down
39 changes: 31 additions & 8 deletions Snowflake.Data.Tests/UnitTests/Session/WaitingQueueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void TestWaitForTheResourceUntilTimeout()
// arrange
var queue = new WaitingQueue();
var watch = new Stopwatch();

// act
watch.Start();
var result = queue.Wait(50, CancellationToken.None);
Expand All @@ -38,7 +38,7 @@ public void TestWaitForTheResourceUntilCancellation()
watch.Start();
var result = queue.Wait(30000, cancellationSource.Token);
watch.Stop();

// assert
Assert.IsFalse(result);
Assert.That(watch.ElapsedMilliseconds, Is.InRange(45, 1500)); // sometimes Wait takes a bit smaller amount of time than it should. Thus we expect it to be greater than 45, not just 50.
Expand All @@ -56,7 +56,7 @@ public void TestWaitUntilResourceAvailable()
Thread.Sleep(50);
queue.OnResourceIncrease();
});

// act
watch.Start();
var result = queue.Wait(30000, CancellationToken.None);
Expand All @@ -72,10 +72,10 @@ public void TestWaitingEnabled()
{
// arrange
var queue = new WaitingQueue();

// act
var isWaitingEnabled = queue.IsWaitingEnabled();

// assert
Assert.IsTrue(isWaitingEnabled);
}
Expand All @@ -85,10 +85,10 @@ public void TestNoOneIsWaiting()
{
// arrange
var queue = new WaitingQueue();

// act
var isAnyoneWaiting = queue.IsAnyoneWaiting();

// assert
Assert.IsFalse(isAnyoneWaiting);
}
Expand All @@ -109,9 +109,32 @@ public void TestSomeoneIsWaiting()

// act
var isAnyoneWaiting = queue.IsAnyoneWaiting();

// assert
Assert.IsTrue(isAnyoneWaiting);
}

[Test]
[Retry(2)]
public void TestReturnUnsuccessfulOnResetWhileWaiting()
{
// arrange
var queue = new WaitingQueue();
var watch = new Stopwatch();
Task.Run(() =>
{
Thread.Sleep(50);
queue.Reset();
});

// act
watch.Start();
var result = queue.Wait(30000, CancellationToken.None);
watch.Stop();

// assert
Assert.IsFalse(result);
Assert.That(watch.ElapsedMilliseconds, Is.InRange(50, 1500));
}
}
}
2 changes: 1 addition & 1 deletion Snowflake.Data/Core/Session/ConnectionPoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void ClearAllPools()
s_logger.Debug("ConnectionPoolManager::ClearAllPools");
foreach (var sessionPool in _pools.Values)
{
sessionPool.ClearSessions();
sessionPool.DestroyPool();
}
_pools.Clear();
}
Expand Down
2 changes: 2 additions & 0 deletions Snowflake.Data/Core/Session/ISessionCreationTokenCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ internal interface ISessionCreationTokenCounter
void RemoveToken(SessionCreationToken creationToken);

int Count();

void Reset();
}
}
2 changes: 2 additions & 0 deletions Snowflake.Data/Core/Session/IWaitingQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ internal interface IWaitingQueue
int WaitingCount();

bool IsWaitingEnabled();

void Reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ namespace Snowflake.Data.Core.Session
{
internal class NonCountingSessionCreationTokenCounter: ISessionCreationTokenCounter
{
private static readonly TimeSpan s_irrelevantCreateSessionTimeout = SFSessionHttpClientProperties.DefaultConnectionTimeout; // in case of old caching pool or pooling disabled we do not remove expired ones nor even store them
private static readonly TimeSpan s_irrelevantCreateSessionTimeout = SFSessionHttpClientProperties.DefaultConnectionTimeout; // in case of old caching pool or pooling disabled we do not remove expired ones nor even store them

public SessionCreationToken NewToken() => new SessionCreationToken(s_irrelevantCreateSessionTimeout);

public void RemoveToken(SessionCreationToken creationToken)
{
}

public int Count() => 0;

public void Reset()
{
}
}
}
4 changes: 4 additions & 0 deletions Snowflake.Data/Core/Session/NonWaitingQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ public bool IsWaitingEnabled()
{
return false;
}

public void Reset()
{
}
}
}
4 changes: 2 additions & 2 deletions Snowflake.Data/Core/Session/SFSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ internal void close()
{
// Nothing to do if the session is not open
if (!IsEstablished()) return;
logger.Debug($"Closing session with id: {sessionId}, user: {_user ?? string.Empty}, database: {database}, schema: {schema}, role: {role}, warehouse: {warehouse}, connection start timestamp: {_startTime}");
logger.Debug($"Closing session with id: {sessionId}, user: {_user}, database: {database}, schema: {schema}, role: {role}, warehouse: {warehouse}, connection start timestamp: {_startTime}");
stopHeartBeatForThisSession();

// Send a close session request
Expand Down Expand Up @@ -306,7 +306,7 @@ internal async Task CloseAsync(CancellationToken cancellationToken)
{
// Nothing to do if the session is not open
if (!IsEstablished()) return;
logger.Debug($"Closing session with id: {sessionId}, user: {_user ?? string.Empty}, database: {database}, schema: {schema}, role: {role}, warehouse: {warehouse}, connection start timestamp: {_startTime}");
logger.Debug($"Closing session with id: {sessionId}, user: {_user}, database: {database}, schema: {schema}, role: {role}, warehouse: {warehouse}, connection start timestamp: {_startTime}");
stopHeartBeatForThisSession();

// Send a close session request
Expand Down
13 changes: 13 additions & 0 deletions Snowflake.Data/Core/Session/SessionCreationTokenCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,18 @@ public int Count()
_tokenLock.ExitReadLock();
}
}

public void Reset()
{
_tokenLock.EnterWriteLock();
try
{
_tokens.Clear();
}
finally
{
_tokenLock.ExitWriteLock();
}
}
}
}
Loading
Loading