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 7 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
17 changes: 14 additions & 3 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 Down Expand Up @@ -90,6 +89,18 @@ public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken ca
return Task.FromResult<IChannelSourceHandle>(GetReadChannelSourceHelper());
}

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

/// <inheritdoc />
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken)
{
return GetReadChannelSourceAsync(cancellationToken);
}

private IChannelSourceHandle GetReadChannelSourceHelper()
{
return new ChannelSourceHandle(new ChannelChannelSource(_server, _channel.Fork(), _session.Fork()));
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 @@ -85,32 +86,68 @@ public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken ca
return Task.FromResult(GetChannelSourceHelper());
}

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

/// <inheritdoc />
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken)
{
return GetReadChannelSourceAsync(cancellationToken);
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

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

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
{
return GetWriteChannelSource(cancellationToken); // ignore mayUseSecondary
}

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

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

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

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

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

// private methods
private IChannelSourceHandle GetChannelSourceHelper()
{
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 @@ -73,32 +75,68 @@ public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken ca
return Task.FromResult(GetChannelSourceHelper());
}

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

/// <inheritdoc />
public Task<IChannelSourceHandle> GetReadChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken)
{
return GetReadChannelSourceAsync(cancellationToken);
}

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return GetChannelSourceHelper();
}

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

/// <inheritdoc/>
public IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken)
{
return GetWriteChannelSource(cancellationToken); // ignore mayUseSecondary
}

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

/// <inheritdoc/>
public Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return Task.FromResult(GetChannelSourceHelper());
}

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

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

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

/// <inheritdoc/>
public void Dispose()
{
Expand Down
51 changes: 51 additions & 0 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 @@ -61,6 +62,22 @@ public interface IReadBinding : IBinding
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken);

/// <summary>
/// Gets a channel source for read operations while deprioritizing servers in the provided collection.
/// </summary>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
IChannelSourceHandle GetReadChannelSource(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken);
BorisDog marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets a channel source for read operations while deprioritizing servers in the provided collection.
/// </summary>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetReadChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken);
}

/// <summary>
Expand All @@ -75,6 +92,14 @@ public interface IWriteBinding : IBinding
/// <returns>A channel source.</returns>
IChannelSourceHandle GetWriteChannelSource(CancellationToken cancellationToken);

/// <summary>
/// Gets a channel source for write operations while deprioritizing servers in the provided collection.
/// </summary>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
IChannelSourceHandle GetWriteChannelSource(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken);

/// <summary>
/// Gets a channel source for write operations that may use a secondary.
/// </summary>
Expand All @@ -83,20 +108,46 @@ public interface IWriteBinding : IBinding
/// <returns>A channel source.</returns>
IChannelSourceHandle GetWriteChannelSource(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken);

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

/// <summary>
/// Gets a channel source for write operations.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(CancellationToken cancellationToken);

/// <summary>
/// Gets a channel source for write operations while deprioritizing servers in the provided collection.
/// </summary>
/// <param name="deprioritizedServers">The deprioritized servers.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken);

/// <summary>
/// Gets a channel source for write operations that may use a secondary.
/// </summary>
/// <param name="mayUseSecondary">The may use secondary criteria.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A channel source.</returns>
Task<IChannelSourceHandle> GetWriteChannelSourceAsync(IMayUseSecondaryCriteria mayUseSecondary, CancellationToken cancellationToken);

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

/// <summary>
Expand Down
18 changes: 15 additions & 3 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 @@ -76,6 +74,20 @@ public Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken ca
return _reference.Instance.GetReadChannelSourceAsync(cancellationToken);
}

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

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

/// <inheritdoc/>
public void Dispose()
{
Expand Down
21 changes: 17 additions & 4 deletions src/MongoDB.Driver.Core/Core/Bindings/ReadPreferenceBinding.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.Clusters;
Expand Down Expand Up @@ -67,16 +68,28 @@ public ICoreSessionHandle Session
/// <inheritdoc/>
public IChannelSourceHandle GetReadChannelSource(CancellationToken cancellationToken)
{
ThrowIfDisposed();
var server = _cluster.SelectServerAndPinIfNeeded(_session, _serverSelector, cancellationToken);
return GetChannelSourceHelper(server);
return GetReadChannelSource(null, cancellationToken);
}

/// <inheritdoc/>
public async Task<IChannelSourceHandle> GetReadChannelSourceAsync(CancellationToken cancellationToken)
{
return await GetReadChannelSourceAsync(null, cancellationToken).ConfigureAwait(false);
}

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

/// <inheritdoc />
public async Task<IChannelSourceHandle> GetReadChannelSourceAsync(IReadOnlyCollection<ServerDescription> deprioritizedServers, CancellationToken cancellationToken)
{
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