// constructors protected Cluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber) { _settings = Ensure.IsNotNull(settings, nameof(settings)); _serverFactory = Ensure.IsNotNull(serverFactory, nameof(serverFactory)); Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber)); _state = new InterlockedInt32(State.Initial); _rapidHeartbeatTimerCallbackState = new InterlockedInt32(RapidHeartbeatTimerCallbackState.NotRunning); _clusterId = new ClusterId(); _description = CreateInitialDescription(); _descriptionChangedTaskCompletionSource = new TaskCompletionSource <bool>(); _latencyLimitingServerSelector = new LatencyLimitingServerSelector(settings.LocalThreshold); _rapidHeartbeatTimer = new Timer(RapidHeartbeatTimerCallback, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); eventSubscriber.TryGetEventHandler(out _descriptionChangedEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectedServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerFailedEventHandler); _serverSessionPool = new CoreServerSessionPool(this); ClusterDescription CreateInitialDescription() { #pragma warning disable CS0618 // Type or member is obsolete var connectionModeSwitch = _settings.ConnectionModeSwitch; var clusterConnectionMode = connectionModeSwitch == ConnectionModeSwitch.UseConnectionMode ? _settings.ConnectionMode : default; var directConnection = connectionModeSwitch == ConnectionModeSwitch.UseDirectConnection ? _settings.DirectConnection : default; return(ClusterDescription.CreateInitial(_clusterId, clusterConnectionMode, _settings.ConnectionModeSwitch, directConnection)); #pragma warning restore CS0618 // Type or member is obsolete } }
public void CreateInitial_should_return_initial_description() { var subject = ClusterDescription.CreateInitial(__clusterId, ClusterType.Standalone); subject.ClusterId.Should().Be(__clusterId); subject.Servers.Should().BeEmpty(); subject.State.Should().Be(ClusterState.Disconnected); subject.Type.Should().Be(ClusterType.Standalone); }
// constructors protected Cluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IClusterListener listener) { _settings = Ensure.IsNotNull(settings, "settings"); _serverFactory = Ensure.IsNotNull(serverFactory, "serverFactory"); _listener = listener; _state = new InterlockedInt32(State.Initial); _clusterId = new ClusterId(); _description = ClusterDescription.CreateInitial(_clusterId, _settings.ConnectionMode.ToClusterType()); _descriptionChangedTaskCompletionSource = new TaskCompletionSource <bool>(); }
public void CreateInitial_should_return_initial_description() { var subject = ClusterDescription.CreateInitial(__clusterId, ClusterConnectionMode.Standalone); subject.ClusterId.Should().Be(__clusterId); subject.Servers.Should().BeEmpty(); subject.State.Should().Be(ClusterState.Disconnected); subject.ConnectionMode.Should().Be(ClusterConnectionMode.Standalone); subject.Type.Should().Be(ClusterType.Unknown); subject.LogicalSessionTimeout.Should().NotHaveValue(); }
public LoadBalancedCluster( ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber, IDnsMonitorFactory dnsMonitorFactory) { #pragma warning disable CS0618 // Type or member is obsolete Ensure.That(settings.ConnectionModeSwitch != ConnectionModeSwitch.UseConnectionMode, $"{nameof(ConnectionModeSwitch.UseConnectionMode)} must not be used for a {nameof(LoadBalancedCluster)}."); if (settings.ConnectionModeSwitch == ConnectionModeSwitch.UseDirectConnection) { Ensure.That(!settings.DirectConnection.GetValueOrDefault(), $"DirectConnection mode is not supported for {nameof(LoadBalancedCluster)}."); } #pragma warning restore CS0618 // Type or member is obsolete Ensure.That(settings.LoadBalanced, $"Only Load balanced mode is supported for a {nameof(LoadBalancedCluster)}."); Ensure.IsEqualTo(settings.EndPoints.Count, 1, nameof(settings.EndPoints.Count)); Ensure.IsNull(settings.ReplicaSetName, nameof(settings.ReplicaSetName)); Ensure.That(settings.SrvMaxHosts == 0, "srvMaxHosts cannot be used with load balanced mode."); _clusterClock = new ClusterClock(); _clusterId = new ClusterId(); _dnsMonitorCancellationTokenSource = new CancellationTokenSource(); _dnsMonitorFactory = Ensure.IsNotNull(dnsMonitorFactory, nameof(dnsMonitorFactory)); _settings = Ensure.IsNotNull(settings, nameof(settings)); _serverFactory = Ensure.IsNotNull(serverFactory, nameof(serverFactory)); _serverReadyTaskCompletionSource = new TaskCompletionSource <bool>(); _serverSessionPool = new CoreServerSessionPool(this); _state = new InterlockedInt32(State.Initial); _description = ClusterDescription.CreateInitial( _clusterId, #pragma warning disable CS0618 // Type or member is obsolete ClusterConnectionMode.Automatic, ConnectionModeSwitch.UseConnectionMode, #pragma warning restore CS0618 // Type or member is obsolete null); _eventSubscriber = eventSubscriber; eventSubscriber.TryGetEventHandler(out _closingEventHandler); eventSubscriber.TryGetEventHandler(out _closedEventHandler); eventSubscriber.TryGetEventHandler(out _openingEventHandler); eventSubscriber.TryGetEventHandler(out _openedEventHandler); eventSubscriber.TryGetEventHandler(out _descriptionChangedEventHandler); }
// constructors protected Cluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber) { _settings = Ensure.IsNotNull(settings, "settings"); _serverFactory = Ensure.IsNotNull(serverFactory, "serverFactory"); Ensure.IsNotNull(eventSubscriber, "eventSubscriber"); _state = new InterlockedInt32(State.Initial); _clusterId = new ClusterId(); _description = ClusterDescription.CreateInitial(_clusterId, _settings.ConnectionMode); _descriptionChangedTaskCompletionSource = new TaskCompletionSource <bool>(); _rapidHeartbeatTimer = new Timer(RapidHeartbeatTimerCallback, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); eventSubscriber.TryGetEventHandler(out _descriptionChangedEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectedServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerFailedEventHandler); }
public void CreateInitial_should_return_initial_description(ConnectionModeSwitch connectionModeSwitch, ClusterConnectionMode connectionMode, bool?directConnection) { var subject = ClusterDescription.CreateInitial(__clusterId, connectionMode, connectionModeSwitch, directConnection); subject.ClusterId.Should().Be(__clusterId); subject.ConnectionModeSwitch.Should().Be(connectionModeSwitch); switch (connectionModeSwitch) { case ConnectionModeSwitch.UseConnectionMode: subject.ConnectionMode.Should().Be(connectionMode); break; case ConnectionModeSwitch.UseDirectConnection: subject.DirectConnection.Should().Be(directConnection); break; } subject.DnsMonitorException.Should().BeNull(); subject.LogicalSessionTimeout.Should().NotHaveValue(); subject.Servers.Should().BeEmpty(); subject.State.Should().Be(ClusterState.Disconnected); subject.Type.Should().Be(ClusterType.Unknown); }
public void IsCompatibleWithDriver_should_return_true_if_server_unknown(int[] wireRanges) { var clusterId = new ClusterId(1); var connectionMode = ClusterConnectionMode.Automatic; var subject = ClusterDescription.CreateInitial(clusterId, connectionMode); for (var i = 0; i < wireRanges.Length; i++) { var endPoint = new DnsEndPoint("localhost", i); var serverId = new ServerId(clusterId, endPoint); var wireRange = wireRanges[i]; var wireVersionRange = wireRange == 0 ? new Range <int>(0, 0) : wireRange == 1 ? new Range <int>(2, 6) : null; var server = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Unknown); subject = subject.WithServerDescription(server); } var result = subject.IsCompatibleWithDriver; result.Should().BeTrue(); }
// constructors protected Cluster(ClusterSettings settings, IClusterableServerFactory serverFactory, IEventSubscriber eventSubscriber) { _settings = Ensure.IsNotNull(settings, nameof(settings)); _serverFactory = Ensure.IsNotNull(serverFactory, nameof(serverFactory)); Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber)); _state = new InterlockedInt32(State.Initial); _clusterId = new ClusterId(); _description = ClusterDescription.CreateInitial(_clusterId, _settings.ConnectionMode); _descriptionChangedTaskCompletionSource = new TaskCompletionSource <bool>(); _latencyLimitingServerSelector = new LatencyLimitingServerSelector(settings.LocalThreshold); _rapidHeartbeatTimer = new Timer(RapidHeartbeatTimerCallback, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); eventSubscriber.TryGetEventHandler(out _descriptionChangedEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectedServerEventHandler); eventSubscriber.TryGetEventHandler(out _selectingServerFailedEventHandler); _serverSessionPool = new CoreServerSessionPool(this); }
public void IsCompatibleWithDriver_should_return_true_if_server_unknown(int[] wireRanges) { var clusterId = new ClusterId(1); #pragma warning disable CS0618 // Type or member is obsolete var connectionMode = ClusterConnectionMode.Automatic; var connectionModeSwitch = ConnectionModeSwitch.NotSet; var subject = ClusterDescription.CreateInitial(clusterId, connectionMode, connectionModeSwitch, directConnection: null); #pragma warning restore CS0618 // Type or member is obsolete for (var i = 0; i < wireRanges.Length; i++) { var endPoint = new DnsEndPoint("localhost", i); var serverId = new ServerId(clusterId, endPoint); var wireRange = wireRanges[i]; var wireVersionRange = wireRange == 0 ? new Range <int>(0, 0) : wireRange == 1 ? new Range <int>(6, 15) : null; var server = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Unknown); subject = subject.WithServerDescription(server); } var result = subject.IsCompatibleWithDriver; result.Should().BeTrue(); }
public void IsCompatibleWithDriver_should_return_expected_result(int[] wireRanges, bool expectedResult) { var clusterId = new ClusterId(1); #pragma warning disable CS0618 var connectionMode = ClusterConnectionMode.Automatic; var connectionModeSwitch = ConnectionModeSwitch.NotSet; var subject = ClusterDescription.CreateInitial(clusterId, connectionMode, connectionModeSwitch, directConnection: null); #pragma warning restore CS0618 for (var i = 0; i < wireRanges.Length; i++) { var endPoint = new DnsEndPoint("localhost", i); var serverId = new ServerId(clusterId, endPoint); var wireRange = wireRanges[i]; var wireVersionRange = wireRange == 0 ? new Range <int>(0, 0) : wireRange == 1 ? new Range <int>(6, 14) : null; var server = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Standalone); subject = subject.WithServerDescription(server); } var result = subject.IsCompatibleWithDriver; result.Should().Be(expectedResult); }