Skip to content

Commit d72beef

Browse files
committed
CSHARP-2105: Move server session pool from MongoClient to Cluster
1 parent 53c1523 commit d72beef

22 files changed

+446
-175
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* Copyright 2017 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson;
18+
19+
namespace MongoDB.Driver
20+
{
21+
/// <summary>
22+
/// A server session.
23+
/// </summary>
24+
/// <seealso cref="MongoDB.Driver.ICoreServerSession" />
25+
internal sealed class CoreServerSession : ICoreServerSession
26+
{
27+
#region static
28+
// private static methods
29+
private static BsonDocument GenerateSessionId()
30+
{
31+
var guid = Guid.NewGuid();
32+
var id = new BsonBinaryData(guid, GuidRepresentation.Standard);
33+
return new BsonDocument("id", id);
34+
}
35+
#endregion
36+
37+
// private fields
38+
private readonly BsonDocument _id;
39+
private DateTime? _lastUsedAt;
40+
private long _transactionNumber;
41+
42+
// constructors
43+
public CoreServerSession()
44+
{
45+
_id = GenerateSessionId();
46+
_transactionNumber = 0;
47+
}
48+
49+
// public properties
50+
/// <inheritdoc />
51+
public BsonDocument Id => _id;
52+
53+
/// <inheritdoc />
54+
public DateTime? LastUsedAt => _lastUsedAt;
55+
56+
// public methods
57+
/// <inheritdoc />
58+
public long AdvanceTransactionNumber()
59+
{
60+
return ++_transactionNumber;
61+
}
62+
63+
/// <inheritdoc />
64+
public void Dispose()
65+
{
66+
}
67+
68+
/// <inheritdoc />
69+
public void WasUsed()
70+
{
71+
_lastUsedAt = DateTime.UtcNow;
72+
}
73+
}
74+
}

src/MongoDB.Driver/ServerSessionPool.cs renamed to src/MongoDB.Driver.Core/Core/Bindings/CoreServerSessionPool.cs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,26 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using MongoDB.Driver.Core.Clusters;
1819
using MongoDB.Driver.Core.Misc;
1920

2021
namespace MongoDB.Driver
2122
{
22-
/// <summary>
23-
/// A server session pool.
24-
/// </summary>
25-
/// <seealso cref="MongoDB.Driver.IServerSessionPool" />
26-
internal class ServerSessionPool : IServerSessionPool
23+
internal class CoreServerSessionPool : ICoreServerSessionPool
2724
{
2825
// private fields
29-
private readonly IMongoClient _client;
26+
private readonly ICluster _cluster;
3027
private readonly object _lock = new object();
31-
private readonly List<IServerSession> _pool = new List<IServerSession>();
28+
private readonly List<ICoreServerSession> _pool = new List<ICoreServerSession>();
3229

3330
// constructors
34-
/// <summary>
35-
/// Initializes a new instance of the <see cref="ServerSessionPool" /> class.
36-
/// </summary>
37-
/// <param name="client">The client.</param>
38-
public ServerSessionPool(IMongoClient client)
31+
public CoreServerSessionPool(ICluster cluster)
3932
{
40-
_client = Ensure.IsNotNull(client, nameof(client));
33+
_cluster = Ensure.IsNotNull(cluster, nameof(cluster));
4134
}
4235

4336
/// <inheritdoc />
44-
public IServerSession AcquireSession()
37+
public ICoreServerSession AcquireSession()
4538
{
4639
lock (_lock)
4740
{
@@ -56,18 +49,18 @@ public IServerSession AcquireSession()
5649
{
5750
var removeCount = _pool.Count - i; // the one we're about to return and any about to expire ones we skipped over
5851
_pool.RemoveRange(i, removeCount);
59-
return new ReleaseOnDisposeServerSession(pooledSession, this);
52+
return new ReleaseOnDisposeCoreServerSession(pooledSession, this);
6053
}
6154
}
6255

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

66-
return new ReleaseOnDisposeServerSession(new ServerSession(), this);
59+
return new ReleaseOnDisposeCoreServerSession(new CoreServerSession(), this);
6760
}
6861

6962
/// <inheritdoc />
70-
public void ReleaseSession(IServerSession session)
63+
public void ReleaseSession(ICoreServerSession session)
7164
{
7265
lock (_lock)
7366
{
@@ -99,9 +92,9 @@ public void ReleaseSession(IServerSession session)
9992
}
10093

10194
// private methods
102-
private bool IsAboutToExpire(IServerSession session)
95+
private bool IsAboutToExpire(ICoreServerSession session)
10396
{
104-
var logicalSessionTimeout = _client.Cluster.Description.LogicalSessionTimeout;
97+
var logicalSessionTimeout = _cluster.Description.LogicalSessionTimeout;
10598
if (!session.LastUsedAt.HasValue || !logicalSessionTimeout.HasValue)
10699
{
107100
return true;
@@ -115,13 +108,13 @@ private bool IsAboutToExpire(IServerSession session)
115108
}
116109

117110
// nested types
118-
internal sealed class ReleaseOnDisposeServerSession : WrappingServerSession
111+
internal sealed class ReleaseOnDisposeCoreServerSession : WrappingCoreServerSession
119112
{
120113
// private fields
121-
private readonly IServerSessionPool _pool;
114+
private readonly ICoreServerSessionPool _pool;
122115

123116
// constructors
124-
public ReleaseOnDisposeServerSession(IServerSession wrapped, IServerSessionPool pool)
117+
public ReleaseOnDisposeCoreServerSession(ICoreServerSession wrapped, ICoreServerSessionPool pool)
125118
: base(wrapped, ownsWrapped: false)
126119
{
127120
_pool = Ensure.IsNotNull(pool, nameof(pool));

src/MongoDB.Driver/IServerSessionPool.cs renamed to src/MongoDB.Driver.Core/Core/Bindings/ICoreServerSessionPool.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ namespace MongoDB.Driver
1818
/// <summary>
1919
/// A server session pool.
2020
/// </summary>
21-
internal interface IServerSessionPool
21+
internal interface ICoreServerSessionPool
2222
{
2323
// methods
2424
/// <summary>
2525
/// Acquires a server session.
2626
/// </summary>
2727
/// <returns>A server session.</returns>
28-
IServerSession AcquireSession();
28+
ICoreServerSession AcquireSession();
2929

3030
/// <summary>
3131
/// Releases a server session.
3232
/// </summary>
3333
/// <param name="serverSession">The server session.</param>
34-
void ReleaseSession(IServerSession serverSession);
34+
void ReleaseSession(ICoreServerSession serverSession);
3535
}
3636
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Copyright 2017 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using MongoDB.Bson;
18+
19+
namespace MongoDB.Driver
20+
{
21+
/// <summary>
22+
/// The interface for a core server session.
23+
/// </summary>
24+
public interface ICoreServerSession : IDisposable
25+
{
26+
// properties
27+
/// <summary>
28+
/// Gets the session Id.
29+
/// </summary>
30+
/// <value>
31+
/// The session Id.
32+
/// </value>
33+
BsonDocument Id { get; }
34+
35+
/// <summary>
36+
/// Gets the time this server session was last used (in UTC).
37+
/// </summary>
38+
/// <value>
39+
/// The time this server session was last used (in UTC).
40+
/// </value>
41+
DateTime? LastUsedAt { get; }
42+
43+
/// <summary>
44+
/// Gets the next transaction number.
45+
/// </summary>
46+
/// <returns>The transaction number.</returns>
47+
long AdvanceTransactionNumber();
48+
49+
// methods
50+
/// <summary>
51+
/// Called by the driver when the session is used (i.e. sent to the server).
52+
/// </summary>
53+
void WasUsed();
54+
}
55+
}

src/MongoDB.Driver/WrappingServerSession.cs renamed to src/MongoDB.Driver.Core/Core/Bindings/WrappingCoreServerSession.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919

2020
namespace MongoDB.Driver
2121
{
22-
internal abstract class WrappingServerSession : IServerSession
22+
internal abstract class WrappingCoreServerSession : ICoreServerSession
2323
{
2424
// private fields
2525
protected bool _disposed;
2626
private readonly bool _ownsWrapped;
27-
private readonly IServerSession _wrapped;
27+
private readonly ICoreServerSession _wrapped;
2828

2929
// constructors
30-
public WrappingServerSession(IServerSession wrapped, bool ownsWrapped)
30+
public WrappingCoreServerSession(ICoreServerSession wrapped, bool ownsWrapped)
3131
{
3232
_wrapped = Ensure.IsNotNull(wrapped, nameof(wrapped));
3333
_ownsWrapped = ownsWrapped;
@@ -52,7 +52,7 @@ public DateTime? LastUsedAt
5252
}
5353
}
5454

55-
public IServerSession Wrapped
55+
public ICoreServerSession Wrapped
5656
{
5757
get
5858
{

src/MongoDB.Driver.Core/Core/Clusters/Cluster.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ internal abstract class Cluster : ICluster
7070
private readonly object _serverSelectionWaitQueueLock = new object();
7171
private int _serverSelectionWaitQueueSize;
7272
private readonly IClusterableServerFactory _serverFactory;
73+
private readonly ICoreServerSessionPool _serverSessionPool;
7374
private readonly ClusterSettings _settings;
7475
private readonly InterlockedInt32 _state;
7576

@@ -96,6 +97,8 @@ protected Cluster(ClusterSettings settings, IClusterableServerFactory serverFact
9697
eventSubscriber.TryGetEventHandler(out _selectingServerEventHandler);
9798
eventSubscriber.TryGetEventHandler(out _selectedServerEventHandler);
9899
eventSubscriber.TryGetEventHandler(out _selectingServerFailedEventHandler);
100+
101+
_serverSessionPool = new CoreServerSessionPool(this);
99102
}
100103

101104
// events
@@ -124,6 +127,11 @@ public ClusterSettings Settings
124127
}
125128

126129
// methods
130+
public ICoreServerSession AcquireServerSession()
131+
{
132+
return _serverSessionPool.AcquireSession();
133+
}
134+
127135
protected IClusterableServer CreateServer(EndPoint endPoint)
128136
{
129137
return _serverFactory.CreateServer(_clusterId, _clusterClock, endPoint);

src/MongoDB.Driver.Core/Core/Clusters/ICluster.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public interface ICluster : IDisposable
6464
ClusterSettings Settings { get; }
6565

6666
// methods
67+
/// <summary>
68+
/// Acquires a core server session.
69+
/// </summary>
70+
/// <returns>A core server session.</returns>
71+
ICoreServerSession AcquireServerSession();
72+
6773
/// <summary>
6874
/// Initializes the cluster.
6975
/// </summary>

src/MongoDB.Driver.Core/MongoDB.Driver.Core.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@
9191
<Compile Include="Core\Bindings\ChannelReadBinding.cs" />
9292
<Compile Include="Core\Bindings\ChannelReadWriteBinding.cs" />
9393
<Compile Include="Core\Bindings\ChannelSourceReadWriteBinding.cs" />
94+
<Compile Include="Core\Bindings\CoreServerSession.cs" />
95+
<Compile Include="Core\Bindings\CoreServerSessionPool.cs" />
9496
<Compile Include="Core\Bindings\CoreSessionHandle.cs" />
97+
<Compile Include="Core\Bindings\ICoreServerSessionPool.cs" />
98+
<Compile Include="Core\Bindings\ICoreServerSesssion.cs" />
9599
<Compile Include="Core\Bindings\ICoreSession.cs" />
96100
<Compile Include="Core\Bindings\NoCoreSession.cs" />
97101
<Compile Include="Core\Bindings\ReferenceCountedCoreSession.cs" />
102+
<Compile Include="Core\Bindings\WrappingCoreServerSession.cs" />
98103
<Compile Include="Core\Bindings\WrappingCoreSession.cs" />
99104
<Compile Include="Core\Connections\ClientDocumentHelper.cs" />
100105
<Compile Include="Core\Misc\ArrayFiltersFeature.cs" />

src/MongoDB.Driver/MongoClient.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class MongoClient : MongoClientBase
3636
// private fields
3737
private readonly ICluster _cluster;
3838
private readonly IOperationExecutor _operationExecutor;
39-
private readonly IServerSessionPool _serverSessionPool;
4039
private readonly MongoClientSettings _settings;
4140

4241
// constructors
@@ -57,7 +56,6 @@ public MongoClient(MongoClientSettings settings)
5756
_settings = Ensure.IsNotNull(settings, nameof(settings)).FrozenCopy();
5857
_cluster = ClusterRegistry.Instance.GetOrCreateCluster(_settings.ToClusterKey());
5958
_operationExecutor = new OperationExecutor(this);
60-
_serverSessionPool = new ServerSessionPool(this);
6159
}
6260

6361
/// <summary>
@@ -252,7 +250,8 @@ public override IMongoClient WithWriteConcern(WriteConcern writeConcern)
252250
// private methods
253251
private IServerSession AcquireServerSession()
254252
{
255-
return _serverSessionPool.AcquireSession();
253+
var coreServerSession = _cluster.AcquireServerSession();
254+
return new ServerSession(coreServerSession);
256255
}
257256

258257
private bool AreSessionsSupported(CancellationToken cancellationToken)

src/MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
<Compile Include="IndexKeysDefinition.cs" />
115115
<Compile Include="IndexOptionDefaults.cs" />
116116
<Compile Include="InsertOneOptions.cs" />
117-
<Compile Include="IServerSessionPool.cs" />
118117
<Compile Include="Linq\AggregateQueryableExecutionModel.cs" />
119118
<Compile Include="Linq\ExecutionPlanBuilder.cs" />
120119
<Compile Include="Linq\ExpressionHelper.cs" />
@@ -306,7 +305,6 @@
306305
<Compile Include="IServerSession.cs" />
307306
<Compile Include="ReferenceCountedClientSession.cs" />
308307
<Compile Include="ServerSession.cs" />
309-
<Compile Include="ServerSessionPool.cs" />
310308
<Compile Include="SortDefinitionBuilder.cs" />
311309
<Compile Include="FilterDefinitionBuilder.cs" />
312310
<Compile Include="IAggregateFluentExtensions.cs" />
@@ -370,7 +368,6 @@
370368
<Compile Include="ReplaceOneResult.cs" />
371369
<Compile Include="UpdateResult.cs" />
372370
<Compile Include="WrappingClientSession.cs" />
373-
<Compile Include="WrappingServerSession.cs" />
374371
<Compile Include="WriteConcernError.cs" />
375372
<Compile Include="BulkWriteResult.cs" />
376373
<Compile Include="BulkWriteUpsert.cs" />

0 commit comments

Comments
 (0)