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 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fd34bc0
CSHARP-3757: Redirect read/write retries to other mongos if possible
adelinowona Apr 10, 2024
52539e9
Add selectServer unit tests
adelinowona Apr 10, 2024
cd47033
Adds functionality for passing deprioritized servers in retries
adelinowona Apr 10, 2024
464b498
add new prose tests
adelinowona Apr 3, 2024
5efce80
Fix failing evergreen tests
adelinowona Apr 11, 2024
d123f07
change events capturing method for prose tests
adelinowona Apr 12, 2024
dd82180
fix failing retryable prose tests on evergreen
adelinowona Apr 15, 2024
bef0c37
refactor deprioritization mechanism
adelinowona Apr 22, 2024
49995fd
Incorporate review feedback
adelinowona Apr 23, 2024
3e6d403
remove unnecessary white space
adelinowona Apr 23, 2024
1e88dc4
Update api docs and remove overloads
adelinowona Apr 23, 2024
7394726
Revert "Update api docs and remove overloads"
adelinowona Apr 29, 2024
2f34978
Revert "remove unnecessary white space"
adelinowona Apr 29, 2024
83eda80
Revert "Incorporate review feedback"
adelinowona Apr 29, 2024
883d0a7
Use ServerSelector Pattern
adelinowona May 22, 2024
a6a1569
Cleanup
adelinowona May 22, 2024
8de4da2
resolve review comments
adelinowona May 24, 2024
42e8ba2
Avoid using async void
adelinowona May 28, 2024
9a7182a
Avoid double awaiting
adelinowona May 28, 2024
b50bc52
Fix spacing
adelinowona May 28, 2024
507cda1
ensure deprioritized servers collection is not empty as well
adelinowona May 28, 2024
b519cd4
Add PriorityServerSelector tests
adelinowona May 28, 2024
f723d93
Fix failing evergreen tests
adelinowona Jun 5, 2024
48dad1d
review comment
adelinowona Jun 5, 2024
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