Skip to content

Commit 0df25bf

Browse files
SNOW-2139007 Clear query context cache before returning session to pool (#1186)
1 parent 9acd180 commit 0df25bf

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

Snowflake.Data.Tests/UnitTests/Session/SessionPoolTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text.RegularExpressions;
34
using NUnit.Framework;
45
using Snowflake.Data.Client;
@@ -280,6 +281,45 @@ public void TestConnectionCachePoolingDisabledForNewAuthenticators(string connec
280281
Assert.IsFalse(isSessionReturnedToPool);
281282
}
282283

284+
[Test]
285+
public void TestShouldClearQueryContextCacheOnReturningToConnectionCache()
286+
{
287+
// arrange
288+
var session = CreateSessionWithCurrentStartTime("account=testAccount;user=testUser;password=testPassword");
289+
var pool = SessionPool.CreateSessionCache();
290+
var contextElement = new QueryContextElement(123, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), 1, "context");
291+
var context = new ResponseQueryContext { Entries = new List<ResponseQueryContextElement> { new(contextElement) } };
292+
session.UpdateQueryContextCache(context);
293+
Assert.AreEqual(1, session.GetQueryContextRequest().Entries.Count);
294+
295+
// act
296+
var isSessionReturnedToPool = pool.AddSession(session, false);
297+
298+
// assert
299+
Assert.IsTrue(isSessionReturnedToPool);
300+
Assert.AreEqual(0, session.GetQueryContextRequest().Entries.Count);
301+
}
302+
303+
[Test]
304+
public void TestShouldClearQueryContextCacheOnReturningToConnectionPool()
305+
{
306+
// arrange
307+
var connectionString = "account=testAccount;user=testUser;password=testPassword";
308+
var session = CreateSessionWithCurrentStartTime(connectionString);
309+
var pool = SessionPool.CreateSessionPool(connectionString, null, null, null);
310+
var contextElement = new QueryContextElement(123, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), 1, "context");
311+
var context = new ResponseQueryContext { Entries = new List<ResponseQueryContextElement> { new(contextElement) } };
312+
session.UpdateQueryContextCache(context);
313+
Assert.AreEqual(1, session.GetQueryContextRequest().Entries.Count);
314+
315+
// act
316+
var isSessionReturnedToPool = pool.AddSession(session, false);
317+
318+
// assert
319+
Assert.IsTrue(isSessionReturnedToPool);
320+
Assert.AreEqual(0, session.GetQueryContextRequest().Entries.Count);
321+
}
322+
283323
private SFSession CreateSessionWithCurrentStartTime(string connectionString)
284324
{
285325
var session = new SFSession(connectionString, new SessionPropertiesContext());

Snowflake.Data/Core/QueryContextCache.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ public void ClearCache()
189189
_logger.Debug($"clearCache() returns. Number of entries in cache now {_cacheSet.Count}");
190190
}
191191

192+
public void ClearCacheSafely()
193+
{
194+
lock (_qccLock)
195+
{
196+
ClearCache();
197+
}
198+
}
199+
192200
public void SetCapacity(int cap)
193201
{
194202
// check without locking first for performance reason

Snowflake.Data/Core/Session/SFSession.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,14 @@ internal void UpdateSessionParameterMap(List<NameValueParameter> parameterList)
528528
}
529529
}
530530

531+
internal void ClearQueryContextCache()
532+
{
533+
if (!_disableQueryContextCache)
534+
{
535+
_queryContextCache.ClearCacheSafely();
536+
}
537+
}
538+
531539
internal void UpdateQueryContextCache(ResponseQueryContext queryContext)
532540
{
533541
if (!_disableQueryContextCache)

Snowflake.Data/Core/Session/SessionPool.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ internal bool AddSession(SFSession session, bool ensureMinPoolSize)
527527
return false;
528528
}
529529

530+
session.ClearQueryContextCache();
530531
var result = ReturnSessionToPool(session, ensureMinPoolSize);
531532
var wasSessionReturnedToPool = result.Item1;
532533
var sessionCreationTokens = result.Item2;

0 commit comments

Comments
 (0)