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

CSHARP-2423 Fixed Connection Pools lock contention #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 21 additions & 42 deletions src/MongoDB.Driver.Core/Core/Bindings/CoreServerSessionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Misc;
Expand All @@ -24,8 +25,7 @@ internal class CoreServerSessionPool : ICoreServerSessionPool
{
// private fields
private readonly ICluster _cluster;
private readonly object _lock = new object();
private readonly List<ICoreServerSession> _pool = new List<ICoreServerSession>();
private readonly ConcurrentQueue<ICoreServerSession> _pool = new ConcurrentQueue<ICoreServerSession>();

// constructors
public CoreServerSessionPool(ICluster cluster)
Expand All @@ -36,24 +36,20 @@ public CoreServerSessionPool(ICluster cluster)
/// <inheritdoc />
public ICoreServerSession AcquireSession()
{
lock (_lock)
// try to find first non-expired session in our FIFO buffer
// if none found - create new session

ICoreServerSession pooledSession;
while (_pool.TryDequeue(out pooledSession))
{
for (var i = _pool.Count - 1; i >= 0; i--)
if (IsAboutToExpire(pooledSession))
{
var pooledSession = _pool[i];
if (IsAboutToExpire(pooledSession))
{
pooledSession.Dispose();
}
else
{
var removeCount = _pool.Count - i; // the one we're about to return and any about to expire ones we skipped over
_pool.RemoveRange(i, removeCount);
return new ReleaseOnDisposeCoreServerSession(pooledSession, this);
}
pooledSession.Dispose();
}
else
{
return new ReleaseOnDisposeCoreServerSession(pooledSession, this);
}

_pool.Clear(); // they're all about to expire
}

return new ReleaseOnDisposeCoreServerSession(new CoreServerSession(), this);
Expand All @@ -62,32 +58,15 @@ public ICoreServerSession AcquireSession()
/// <inheritdoc />
public void ReleaseSession(ICoreServerSession session)
{
lock (_lock)
{
var removeCount = 0;
for (var i = 0; i < _pool.Count; i++)
{
var pooledSession = _pool[i];
if (IsAboutToExpire(pooledSession))
{
pooledSession.Dispose();
removeCount++;
}
else
{
break;
}
}
_pool.RemoveRange(0, removeCount);
// if session is not expired - put it in the FIFO pool

if (IsAboutToExpire(session))
{
session.Dispose();
}
else
{
_pool.Add(session);
}
if (IsAboutToExpire(session))
{
session.Dispose();
}
else
{
_pool.Enqueue(session);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/MongoDB.Driver.Core/Core/Clusters/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ public ClusterDescription Description
{
get
{
lock (_descriptionLock)
{
// we do not need lock here, because reading and writing refs is thread safe
// and the worst thing we can came up with - outdated description,
// but race condition is impossible
//lock (_descriptionLock)
//{
return _description;
}
//}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
Expand Down Expand Up @@ -712,15 +713,14 @@ public void Dispose()

private class ListConnectionHolder
{
private readonly object _lock = new object();
private readonly List<PooledConnection> _connections;
private readonly ConcurrentQueue<PooledConnection> _connections;

private readonly Action<ConnectionPoolRemovingConnectionEvent> _removingConnectionEventHandler;
private readonly Action<ConnectionPoolRemovedConnectionEvent> _removedConnectionEventHandler;

public ListConnectionHolder(IEventSubscriber eventSubscriber)
{
_connections = new List<PooledConnection>();
_connections = new ConcurrentQueue<PooledConnection>();

eventSubscriber.TryGetEventHandler(out _removingConnectionEventHandler);
eventSubscriber.TryGetEventHandler(out _removedConnectionEventHandler);
Expand All @@ -730,59 +730,56 @@ public int Count
{
get
{
lock (_lock)
{
return _connections.Count;
}
return _connections.Count;
}
}

public void Clear()
{
lock (_lock)
PooledConnection connection;
while (_connections.TryDequeue(out connection))
{
foreach (var connection in _connections)
{
RemoveConnection(connection);
}
_connections.Clear();
RemoveConnection(connection);
}
}

public void Prune()
{
lock (_lock)
// try to clear our FIFO buffer of expired connections
// if first not expired connection is found - it will be returned back to the pool

PooledConnection expired;
while (_connections.TryDequeue(out expired))
{
for (int i = 0; i < _connections.Count; i++)
if (expired.IsExpired)
{
if (_connections[i].IsExpired)
{
RemoveConnection(_connections[i]);
_connections.RemoveAt(i);
break;
}
RemoveConnection(expired);
}
else
{
_connections.Enqueue(expired);
break;
}
}
}

public PooledConnection Acquire()
{
lock (_lock)
// try to find first not expired connection

PooledConnection connection;
while (_connections.TryDequeue(out connection))
{
if (_connections.Count > 0)
if (connection.IsExpired)
{
var connection = _connections[_connections.Count - 1];
_connections.RemoveAt(_connections.Count - 1);
if (connection.IsExpired)
{
RemoveConnection(connection);
}
else
{
return connection;
}
RemoveConnection(connection);
}
else
{
return connection;
}
}

return null;
}

Expand All @@ -794,10 +791,7 @@ public void Return(PooledConnection connection)
return;
}

lock (_lock)
{
_connections.Add(connection);
}
_connections.Enqueue(connection);
}

private void RemoveConnection(PooledConnection connection)
Expand Down