|
| 1 | +/* Copyright 2019–present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Threading; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using MongoDB.Driver.Core.Bindings; |
| 19 | +using MongoDB.Driver.Core.Clusters.ServerSelectors; |
| 20 | +using MongoDB.Driver.Core.Servers; |
| 21 | + |
| 22 | +namespace MongoDB.Driver.Core.Clusters |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// This class contains extension methods for ICluster used for server selection with sharded transactions. |
| 26 | + /// </summary> |
| 27 | + internal static class IClusterExtensions |
| 28 | + { |
| 29 | + public static IServer SelectServerAndPinIfNeeded( |
| 30 | + this ICluster cluster, |
| 31 | + ICoreSessionHandle session, |
| 32 | + IServerSelector selector, |
| 33 | + CancellationToken cancellationToken) |
| 34 | + { |
| 35 | + var pinnedServer = GetPinnedServerIfValid(cluster, session); |
| 36 | + if (pinnedServer != null) |
| 37 | + { |
| 38 | + return pinnedServer; |
| 39 | + } |
| 40 | + |
| 41 | + // Server selection also updates the cluster type, allowing us to to determine if the server |
| 42 | + // should be pinned. |
| 43 | + var server = cluster.SelectServer(selector, cancellationToken); |
| 44 | + PinServerIfNeeded(cluster, session, server); |
| 45 | + return server; |
| 46 | + } |
| 47 | + |
| 48 | + public static async Task<IServer> SelectServerAndPinIfNeededAsync( |
| 49 | + this ICluster cluster, |
| 50 | + ICoreSessionHandle session, |
| 51 | + IServerSelector selector, |
| 52 | + CancellationToken cancellationToken) |
| 53 | + { |
| 54 | + var pinnedServer = GetPinnedServerIfValid(cluster, session); |
| 55 | + if (pinnedServer != null) |
| 56 | + { |
| 57 | + return pinnedServer; |
| 58 | + } |
| 59 | + |
| 60 | + // Server selection also updates the cluster type, allowing us to to determine if the server |
| 61 | + // should be pinned. |
| 62 | + var server = await cluster.SelectServerAsync(selector, cancellationToken).ConfigureAwait(false); |
| 63 | + PinServerIfNeeded(cluster, session, server); |
| 64 | + |
| 65 | + return server; |
| 66 | + } |
| 67 | + |
| 68 | + private static void PinServerIfNeeded(ICluster cluster, ICoreSessionHandle session, IServer server) |
| 69 | + { |
| 70 | + if (cluster.Description.Type == ClusterType.Sharded && session.IsInTransaction) |
| 71 | + { |
| 72 | + session.PinnedServer = server; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static IServer GetPinnedServerIfValid(ICluster cluster, ICoreSessionHandle session) |
| 77 | + { |
| 78 | + if (cluster.Description.Type == ClusterType.Sharded |
| 79 | + && session.IsInTransaction |
| 80 | + && session.PinnedServer != null |
| 81 | + && session.CurrentTransaction.State != CoreTransactionState.Starting) |
| 82 | + { |
| 83 | + return session.PinnedServer; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + return null; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments