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

Merged
merged 24 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
27d3bce
address review comments
adelinowona Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
17 changes: 9 additions & 8 deletions src/MongoDB.Driver.Core/Core/Bindings/ChannelReadWriteBinding.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.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
Loading