Skip to content

Commit 1a83967

Browse files
author
rstam
committed
Added an overload of RequestStart that lets the caller specify which server instance they want the request tied to. This allows client code to direct queries to a specific member of a replica set.
1 parent cba9142 commit 1a83967

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

Driver/Core/MongoServer.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,42 @@ public virtual IDisposable RequestStart(MongoDatabase initialDatabase, bool slav
10011001
}
10021002
else
10031003
{
1004-
var connection = AcquireConnection(initialDatabase, slaveOk);
1004+
var serverInstance = ChooseServerInstance(slaveOk);
1005+
var connection = serverInstance.AcquireConnection(initialDatabase);
1006+
request = new Request(connection, slaveOk);
1007+
_requests.Add(threadId, request);
1008+
}
1009+
1010+
return new RequestStartResult(this);
1011+
}
1012+
}
1013+
1014+
/// <summary>
1015+
/// Lets the server know that this thread is about to begin a series of related operations that must all occur
1016+
/// on the same connection. The return value of this method implements IDisposable and can be placed in a
1017+
/// using statement (in which case RequestDone will be called automatically when leaving the using statement).
1018+
/// </summary>
1019+
/// <param name="initialDatabase">One of the databases involved in the related operations.</param>
1020+
/// <param name="serverInstance">The server instance this request should be tied to.</param>
1021+
/// <returns>A helper object that implements IDisposable and calls <see cref="RequestDone"/> from the Dispose method.</returns>
1022+
public virtual IDisposable RequestStart(MongoDatabase initialDatabase, MongoServerInstance serverInstance)
1023+
{
1024+
lock (_serverLock)
1025+
{
1026+
int threadId = Thread.CurrentThread.ManagedThreadId;
1027+
Request request;
1028+
if (_requests.TryGetValue(threadId, out request))
1029+
{
1030+
if (serverInstance != request.Connection.ServerInstance)
1031+
{
1032+
throw new InvalidOperationException("The server instance passed to a nested call to RequestStart does not match the server instance of the current Request.");
1033+
}
1034+
request.NestingLevel++;
1035+
}
1036+
else
1037+
{
1038+
var connection = serverInstance.AcquireConnection(initialDatabase);
1039+
var slaveOk = serverInstance.IsSecondary;
10051040
request = new Request(connection, slaveOk);
10061041
_requests.Add(threadId, request);
10071042
}
@@ -1355,7 +1390,6 @@ public Request(MongoConnection connection, bool slaveOk)
13551390
public MongoConnection Connection
13561391
{
13571392
get { return _connection; }
1358-
set { _connection = value; }
13591393
}
13601394

13611395
public int NestingLevel

DriverOnlineTests/Core/MongoServerTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,52 @@ public void TestReconnect()
8181
Assert.AreEqual(MongoServerState.Connected, _server.State);
8282
}
8383

84+
[Test]
85+
public void TestRequestStart()
86+
{
87+
using (_server.RequestStart(_database))
88+
{
89+
}
90+
}
91+
92+
[Test]
93+
public void TestRequestStartPrimary()
94+
{
95+
using (_server.RequestStart(_database, _server.Primary))
96+
{
97+
}
98+
}
99+
100+
[Test]
101+
public void TestRequestStartPrimaryNested()
102+
{
103+
using (_server.RequestStart(_database, _server.Primary))
104+
{
105+
using (_server.RequestStart(_database, _server.Primary))
106+
{
107+
}
108+
}
109+
}
110+
111+
[Test]
112+
public void TestRequestStartSlaveOk()
113+
{
114+
using (_server.RequestStart(_database, true))
115+
{
116+
}
117+
}
118+
119+
[Test]
120+
public void TestRequestStartSlaveOkNested()
121+
{
122+
using (_server.RequestStart(_database, false))
123+
{
124+
using (_server.RequestStart(_database, true))
125+
{
126+
}
127+
}
128+
}
129+
84130
[Test]
85131
public void TestVersion()
86132
{

0 commit comments

Comments
 (0)