Exemple #1
0
        private ClusterDescription CreateSubject(Exception dnsMonitorException)
        {
            var clusterId = new ClusterId(1);

#pragma warning disable CS0618 // Type or member is obsolete
            var  connectionModeSwitch = ConnectionModeSwitch.NotSet;
            var  connectionMode       = ClusterConnectionMode.Automatic;
            bool?directConnection     = null;
#pragma warning restore CS0618 // Type or member is obsolete
            var type    = ClusterType.ReplicaSet;
            var servers = new[] { __serverDescription1, __serverDescription2 };

            return(new ClusterDescription(clusterId, connectionMode, connectionModeSwitch, directConnection, dnsMonitorException, type, servers));
        }
        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);
        }
Exemple #3
0
        public void LogicalSessionTimeout_should_return_expected_result_with_0_servers()
        {
            var clusterId = new ClusterId(1);

#pragma warning disable CS0618 // Type or member is obsolete
            var connectionMode = ClusterConnectionMode.Automatic;
#pragma warning restore CS0618 // Type or member is obsolete
            var type    = ClusterType.ReplicaSet;
            var servers = new ServerDescription[0];
#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new ClusterDescription(clusterId, connectionMode, type, servers);
#pragma warning restore CS0618 // Type or member is obsolete

            var result = subject.LogicalSessionTimeout;

            result.Should().NotHaveValue();
        }
        private ClusterDescription CreateSubject(string notEqualField = null)
        {
            var clusterId = new ClusterId(1);
            var type      = ClusterType.ReplicaSet;
            var servers   = new[] { __serverDescription1, __serverDescription2 };

            switch (notEqualField)
            {
            case "ClusterId": clusterId = new ClusterId(2); break;

            case "Type": type = ClusterType.Unknown; break;

            case "Servers": servers = new[] { __serverDescription1 }; break;
            }

            return(new ClusterDescription(clusterId, type, servers));
        }
Exemple #5
0
        // 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);
        }
Exemple #6
0
#pragma warning disable CS0618 // Type or member is obsolete
        private ClusterDescription CreateSubject(string notEqualField = null)
        {
            var       clusterId            = new ClusterId(1);
            var       connectionModeSwitch = ConnectionModeSwitch.NotSet;
            var       connectionMode       = ClusterConnectionMode.Automatic;
            bool?     directConnection     = null;
            Exception dnsMonitorException  = null;
            var       type    = ClusterType.ReplicaSet;
            var       servers = new[] { __serverDescription1, __serverDescription2 };

            if (notEqualField != null)
            {
                switch (notEqualField)
                {
                case "ClusterId": clusterId = new ClusterId(2); break;

                case "ConnectionMode":
                {
                    connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode;
                    connectionMode       = ClusterConnectionMode.Standalone;
                }
                break;

                case "DirectConnection":
                {
                    connectionModeSwitch = ConnectionModeSwitch.UseDirectConnection;
                    directConnection     = true;
                }
                break;

                case "DnsMonitorException": dnsMonitorException = new Exception(); break;

                case "Type": type = ClusterType.Unknown; break;

                case "Servers": servers = new[] { __serverDescription1 }; break;

                default: throw new ArgumentException($"Unknown field name: \"{notEqualField}\".", nameof(notEqualField));
                }
            }

            return(new ClusterDescription(clusterId, connectionMode, connectionModeSwitch, directConnection, dnsMonitorException, type, servers));
        }
Exemple #7
0
        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();
        }
Exemple #8
0
#pragma warning disable CS0618 // Type or member is obsolete
        internal ClusterDescription(
            ClusterId clusterId,
            ClusterConnectionMode connectionMode,
            ConnectionModeSwitch connectionModeSwitch,
            bool?directConnection,
            Exception dnsMonitorException,
            ClusterType type,
            IEnumerable <ServerDescription> servers)
        {
            ClusterConnectionModeHelper.EnsureConnectionModeValuesAreValid(connectionMode, connectionModeSwitch, directConnection);

            _clusterId            = Ensure.IsNotNull(clusterId, nameof(clusterId));
            _connectionMode       = connectionMode;
            _connectionModeSwitch = connectionModeSwitch;
            _directConnection     = directConnection;
            _dnsMonitorException  = dnsMonitorException; // can be null
            _type    = type;
            _servers = (servers ?? new ServerDescription[0]).OrderBy(n => n.EndPoint, new ToStringComparer <EndPoint>()).ToList();
            _logicalSessionTimeout = CalculateLogicalSessionTimeout(this, _servers);
        }
Exemple #9
0
        // 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);
        }
Exemple #10
0
        public void LogicalSessionTimeout_should_return_expected_result_with_1_server(int?timeout1, int?expectedResultMinutes)
        {
            var clusterId      = new ClusterId(1);
            var connectionMode = ClusterConnectionMode.Automatic;
            var type           = ClusterType.ReplicaSet;
            var endPoint1      = new DnsEndPoint("localhost", 27017);
            var serverId1      = new ServerId(clusterId, endPoint1);
            var server1        = new ServerDescription(
                serverId1,
                endPoint1,
                state: ServerState.Connected,
                type: ServerType.ReplicaSetPrimary,
                logicalSessionTimeout: timeout1 == null ? (TimeSpan?)null : TimeSpan.FromMinutes(timeout1.Value));
            var servers        = new[] { server1 };
            var subject        = new ClusterDescription(clusterId, connectionMode, type, servers);
            var expectedResult = expectedResultMinutes == null ? (TimeSpan?)null : TimeSpan.FromMinutes(expectedResultMinutes.Value);

            var result = subject.LogicalSessionTimeout;

            result.Should().Be(expectedResult);
        }
Exemple #11
0
        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();
        }
Exemple #12
0
        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);
        }
Exemple #13
0
        public void ToString_should_return_string_representation()
        {
            var subject = new ClusterId(1);

            subject.ToString().Should().Be("1");
        }
Exemple #14
0
        public void Constructor_should_initialize_instance()
        {
            var subject = new ClusterId(1);

            subject.Value.Should().Be(1);
        }