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-3757: Redirect read/write retries to other mongos if possible #1304

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
9 changes: 4 additions & 5 deletions src/MongoDB.Driver.Core/Core/Bindings/ChannelReadBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Connections;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers;

Expand Down Expand Up @@ -51,7 +50,7 @@ public ChannelReadBinding(IServer server, IChannelHandle channel, ReadPreference
_session = Ensure.IsNotNull(session, nameof(session));
}

// properties
// properties
/// <inheritdoc/>
public ReadPreference ReadPreference
{
Expand All @@ -77,14 +76,14 @@ public void Dispose()
}

/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
adelinowona marked this conversation as resolved.
Show resolved Hide resolved
{
ThrowIfDisposed();
return GetReadChannelSourceHelper();
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return Task.FromResult<IChannelSourceHandle>(GetReadChannelSourceHelper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Misc;
Expand Down Expand Up @@ -72,43 +73,43 @@ public void Dispose()
}

/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
return GetWriteChannelSource(cancellationToken); // ignore mayUseSecondary
return GetWriteChannelSource(cancellationToken, deprioritizedServers); // ignore mayUseSecondary
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
return GetWriteChannelSourceAsync(cancellationToken); // ignore mayUseSecondary
return GetWriteChannelSourceAsync(cancellationToken, deprioritizedServers); // ignore mayUseSecondary
}

// private methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers;

namespace MongoDB.Driver.Core.Bindings
{
Expand Down Expand Up @@ -60,43 +62,43 @@ public ICoreSessionHandle Session

// methods
/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
return GetWriteChannelSource(cancellationToken); // ignore mayUseSecondary
return GetWriteChannelSource(cancellationToken, deprioritizedServers); // ignore mayUseSecondary
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
return GetWriteChannelSourceAsync(cancellationToken); // ignore mayUseSecondary
return GetWriteChannelSourceAsync(cancellationToken, deprioritizedServers); // ignore mayUseSecondary
}

/// <inheritdoc/>
Expand Down
31 changes: 19 additions & 12 deletions src/MongoDB.Driver.Core/Core/Bindings/IBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Servers;
Expand Down Expand Up @@ -49,18 +50,20 @@ public interface IReadBinding : IBinding
ReadPreference ReadPreference { get; }

/// <summary>
/// Gets a channel source for read operations.
/// Gets a channel source for read operations and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken);
IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);

/// <summary>
/// Gets a channel source for read operations.
/// Gets a channel source for read operations and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken);
Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);
}

/// <summary>
Expand All @@ -69,34 +72,38 @@ public interface IReadBinding : IBinding
public interface IWriteBinding : IBinding
{
/// <summary>
/// Gets a channel source for write operations.
/// Gets a channel source for write operations and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken);
IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);

/// <summary>
/// Gets a channel source for write operations that may use a secondary.
/// Gets a channel source for write operations that may use a secondary and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="mayUseSecondary">The may use secondary criteria.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken);
IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);

/// <summary>
/// Gets a channel source for write operations.
/// Gets a channel source for write operations and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken);
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);

/// <summary>
/// Gets a channel source for write operations that may use a secondary.
/// Gets a channel source for write operations that may use a secondary and takes an optional collection of servers for deprioritization.
/// </summary>
/// <param name="mayUseSecondary">The may use secondary criteria.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken);
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null);
}

/// <summary>
Expand Down
13 changes: 5 additions & 8 deletions src/MongoDB.Driver.Core/Core/Bindings/ReadBindingHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers;

namespace MongoDB.Driver.Core.Bindings
{
Expand Down Expand Up @@ -61,19 +59,18 @@ public ICoreSessionHandle Session
get { return _reference.Instance.Session; }
}

// methods
/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetReadChannelSource(cancellationToken);
return _reference.Instance.GetReadChannelSource(cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetReadChannelSourceAsync(cancellationToken);
return _reference.Instance.GetReadChannelSourceAsync(cancellationToken, deprioritizedServers);
}

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

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Clusters;
Expand Down Expand Up @@ -65,18 +66,18 @@ public ICoreSessionHandle Session

// methods
/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
var server = _cluster.SelectServerAndPinIfNeeded(_session, _serverSelector, cancellationToken);
var server = _cluster.SelectServerAndPinIfNeeded(_session, _serverSelector, deprioritizedServers, cancellationToken);
return GetChannelSourceHelper(server);
}

/// <inheritdoc/>
public async Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public async Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
var server = await _cluster.SelectServerAndPinIfNeededAsync(_session, _serverSelector, cancellationToken).ConfigureAwait(false);
var server = await _cluster.SelectServerAndPinIfNeededAsync(_session, _serverSelector, deprioritizedServers, cancellationToken).ConfigureAwait(false);
return GetChannelSourceHelper(server);
}

Expand Down
30 changes: 13 additions & 17 deletions src/MongoDB.Driver.Core/Core/Bindings/ReadWriteBindingHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Operations;
using MongoDB.Driver.Core.Servers;

namespace MongoDB.Driver.Core.Bindings
{
Expand Down Expand Up @@ -62,47 +59,46 @@ public ICoreSessionHandle Session
get { return _reference.Instance.Session; }
}

// methods
/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetReadChannelSource(cancellationToken);
return _reference.Instance.GetReadChannelSource(cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetReadChannelSourceAsync(cancellationToken);
return _reference.Instance.GetReadChannelSourceAsync(cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetWriteChannelSource(cancellationToken);
return _reference.Instance.GetWriteChannelSource(cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetWriteChannelSource(mayUseSecondary, cancellationToken);
return _reference.Instance.GetWriteChannelSource(mayUseSecondary, cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetWriteChannelSourceAsync(cancellationToken);
return _reference.Instance.GetWriteChannelSourceAsync(cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken, IReadOnlyCollection<ServerDescription> deprioritizedServers = null)
{
ThrowIfDisposed();
return _reference.Instance.GetWriteChannelSourceAsync(mayUseSecondary, cancellationToken);
return _reference.Instance.GetWriteChannelSourceAsync(mayUseSecondary, cancellationToken, deprioritizedServers);
}

/// <inheritdoc/>
Expand Down