コード例 #1
0
        private bool IsServerValidForCluster(ClusterType clusterType, ClusterConnectionMode connectionMode, ServerType serverType)
        {
            switch (clusterType)
            {
            case ClusterType.ReplicaSet:
                return(serverType.IsReplicaSetMember());

            case ClusterType.Sharded:
                return(serverType == ServerType.ShardRouter);

            case ClusterType.Standalone:
                return(serverType == ServerType.Standalone);

            case ClusterType.Unknown:
                switch (connectionMode)
                {
                case ClusterConnectionMode.Automatic:
                case ClusterConnectionMode.Direct:
                    return(true);

                default:
                    throw new MongoInternalException("Unexpected connection mode.");
                }

            default:
                throw new MongoInternalException("Unexpected cluster type.");
            }
        }
コード例 #2
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="ClusterSettings"/> class.
        /// </summary>
        /// <param name="connectionMode">The connection mode.</param>
        /// <param name="connectionModeSwitch">The connection mode switch.</param>
        /// <param name="directConnection">The directConnection.</param>
        /// <param name="endPoints">The end points.</param>
        /// <param name="kmsProviders">The kms providers.</param>
        /// <param name="localThreshold">The local threshold.</param>
        /// <param name="maxServerSelectionWaitQueueSize">Maximum size of the server selection wait queue.</param>
        /// <param name="replicaSetName">Name of the replica set.</param>
        /// <param name="serverSelectionTimeout">The server selection timeout.</param>
        /// <param name="preServerSelector">The pre server selector.</param>
        /// <param name="postServerSelector">The post server selector.</param>
        /// <param name="schemaMap">The schema map.</param>
        /// <param name="scheme">The connection string scheme.</param>
        public ClusterSettings(
#pragma warning disable CS0618 // Type or member is obsolete
            Optional <ClusterConnectionMode> connectionMode      = default(Optional <ClusterConnectionMode>),
            Optional <ConnectionModeSwitch> connectionModeSwitch = default,
#pragma warning restore CS0618 // Type or member is obsolete
            Optional <bool?> directConnection            = default,
            Optional <IEnumerable <EndPoint> > endPoints = default(Optional <IEnumerable <EndPoint> >),
            Optional <IReadOnlyDictionary <string, IReadOnlyDictionary <string, object> > > kmsProviders = default(Optional <IReadOnlyDictionary <string, IReadOnlyDictionary <string, object> > >),
            Optional <TimeSpan> localThreshold             = default,
            Optional <int> maxServerSelectionWaitQueueSize = default(Optional <int>),
            Optional <string> replicaSetName              = default(Optional <string>),
            Optional <TimeSpan> serverSelectionTimeout    = default(Optional <TimeSpan>),
            Optional <IServerSelector> preServerSelector  = default(Optional <IServerSelector>),
            Optional <IServerSelector> postServerSelector = default(Optional <IServerSelector>),
            Optional <IReadOnlyDictionary <string, BsonDocument> > schemaMap = default(Optional <IReadOnlyDictionary <string, BsonDocument> >),
            Optional <ConnectionStringScheme> scheme = default(Optional <ConnectionStringScheme>))
        {
#pragma warning disable CS0618 // Type or member is obsolete
            _connectionMode       = connectionMode.WithDefault(ClusterConnectionMode.Automatic);
            _connectionModeSwitch = connectionModeSwitch.WithDefault(ConnectionModeSwitch.NotSet);
#pragma warning restore CS0618 // Type or member is obsolete
            _directConnection = directConnection.WithDefault(null);
            _endPoints        = Ensure.IsNotNull(endPoints.WithDefault(__defaultEndPoints), "endPoints").ToList();
            _kmsProviders     = kmsProviders.WithDefault(null);
            _localThreshold   = Ensure.IsGreaterThanOrEqualToZero(localThreshold.WithDefault(TimeSpan.FromMilliseconds(15)), "localThreshold");
            _maxServerSelectionWaitQueueSize = Ensure.IsGreaterThanOrEqualToZero(maxServerSelectionWaitQueueSize.WithDefault(500), "maxServerSelectionWaitQueueSize");
            _replicaSetName         = replicaSetName.WithDefault(null);
            _serverSelectionTimeout = Ensure.IsGreaterThanOrEqualToZero(serverSelectionTimeout.WithDefault(TimeSpan.FromSeconds(30)), "serverSelectionTimeout");
            _preServerSelector      = preServerSelector.WithDefault(null);
            _postServerSelector     = postServerSelector.WithDefault(null);
            _scheme    = scheme.WithDefault(ConnectionStringScheme.MongoDB);
            _schemaMap = schemaMap.WithDefault(null);

            ClusterConnectionModeHelper.EnsureConnectionModeValuesAreValid(_connectionMode, _connectionModeSwitch, _directConnection);
        }
コード例 #3
0
        private bool IsServerValidForCluster(ClusterType clusterType, ClusterConnectionMode connectionMode, ServerType serverType)
        {
            switch (clusterType)
            {
            case ClusterType.Standalone:
                return(serverType == ServerType.Standalone);

            case ClusterType.ReplicaSet:
                return(serverType.IsReplicaSetMember());

            case ClusterType.Sharded:
                return(serverType == ServerType.ShardRouter);

            case ClusterType.Unknown:
                switch (connectionMode)
                {
                case ClusterConnectionMode.Automatic:
                    if (serverType == ServerType.Standalone)
                    {
                        return(Settings.Scheme == ConnectionStringScheme.MongoDBPlusSrv);        // Standalone is only valid in MultiServerCluster when using MongoDBPlusSrv scheme
                    }
                    return(serverType.IsReplicaSetMember() || serverType == ServerType.ShardRouter);

                default:
                    throw new MongoInternalException("Unexpected connection mode.");
                }

            default:
                throw new MongoInternalException("Unexpected cluster type.");
            }
        }
コード例 #4
0
        public ServerTests()
        {
            _clusterId = new ClusterId();
            _endPoint  = new DnsEndPoint("localhost", 27017);

            _clusterClock          = new Mock <IClusterClock>().Object;
            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _mockConnectionPool    = new Mock <IConnectionPool>();
            _mockConnectionPool.Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>())).Returns(new Mock <IConnectionHandle>().Object);
            _mockConnectionPool.Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Mock <IConnectionHandle>().Object));
            _mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();
            _mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint))
            .Returns(_mockConnectionPool.Object);

            _mockServerMonitor = new Mock <IServerMonitor>();
            _mockServerMonitor.Setup(m => m.Description).Returns(new ServerDescription(new ServerId(_clusterId, _endPoint), _endPoint));
            _mockServerMonitorFactory = new Mock <IServerMonitorFactory>();
            _mockServerMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), _endPoint)).Returns(_mockServerMonitor.Object);

            _capturedEvents = new EventCapturer();
            _settings       = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject = new Server(_clusterId, _clusterClock, _clusterConnectionMode, _settings, _endPoint, _mockConnectionPoolFactory.Object, _mockServerMonitorFactory.Object, _capturedEvents);
        }
コード例 #5
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ClusterSettings"/> class.
 /// </summary>
 /// <param name="connectionMode">The connection mode.</param>
 /// <param name="endPoints">The end points.</param>
 /// <param name="kmsProviders">The kms providers.</param>
 /// <param name="localThreshold">The local threshold.</param>
 /// <param name="maxServerSelectionWaitQueueSize">Maximum size of the server selection wait queue.</param>
 /// <param name="replicaSetName">Name of the replica set.</param>
 /// <param name="serverSelectionTimeout">The server selection timeout.</param>
 /// <param name="preServerSelector">The pre server selector.</param>
 /// <param name="postServerSelector">The post server selector.</param>
 /// <param name="schemaMap">The schema map.</param>
 /// <param name="scheme">The connection string scheme.</param>
 public ClusterSettings(
     Optional <ClusterConnectionMode> connectionMode = default(Optional <ClusterConnectionMode>),
     Optional <IEnumerable <EndPoint> > endPoints    = default(Optional <IEnumerable <EndPoint> >),
     Optional <IReadOnlyDictionary <string, IReadOnlyDictionary <string, object> > > kmsProviders = default(Optional <IReadOnlyDictionary <string, IReadOnlyDictionary <string, object> > >),
     Optional <TimeSpan> localThreshold             = default,
     Optional <int> maxServerSelectionWaitQueueSize = default(Optional <int>),
     Optional <string> replicaSetName              = default(Optional <string>),
     Optional <TimeSpan> serverSelectionTimeout    = default(Optional <TimeSpan>),
     Optional <IServerSelector> preServerSelector  = default(Optional <IServerSelector>),
     Optional <IServerSelector> postServerSelector = default(Optional <IServerSelector>),
     Optional <IReadOnlyDictionary <string, BsonDocument> > schemaMap = default(Optional <IReadOnlyDictionary <string, BsonDocument> >),
     Optional <ConnectionStringScheme> scheme = default(Optional <ConnectionStringScheme>))
 {
     _connectionMode = connectionMode.WithDefault(ClusterConnectionMode.Automatic);
     _endPoints      = Ensure.IsNotNull(endPoints.WithDefault(__defaultEndPoints), "endPoints").ToList();
     _kmsProviders   = kmsProviders.WithDefault(null);
     _localThreshold = Ensure.IsGreaterThanOrEqualToZero(localThreshold.WithDefault(TimeSpan.FromMilliseconds(15)), "localThreshold");
     _maxServerSelectionWaitQueueSize = Ensure.IsGreaterThanOrEqualToZero(maxServerSelectionWaitQueueSize.WithDefault(500), "maxServerSelectionWaitQueueSize");
     _replicaSetName         = replicaSetName.WithDefault(null);
     _serverSelectionTimeout = Ensure.IsGreaterThanOrEqualToZero(serverSelectionTimeout.WithDefault(TimeSpan.FromSeconds(30)), "serverSelectionTimeout");
     _preServerSelector      = preServerSelector.WithDefault(null);
     _postServerSelector     = postServerSelector.WithDefault(null);
     _scheme    = scheme.WithDefault(ConnectionStringScheme.MongoDB);
     _schemaMap = schemaMap.WithDefault(null);
 }
コード例 #6
0
        public DefaultServer(
            ClusterId clusterId,
            IClusterClock clusterClock,
#pragma warning disable CS0618 // Type or member is obsolete
            ClusterConnectionMode clusterConnectionMode,
            ConnectionModeSwitch connectionModeSwitch,
#pragma warning restore CS0618 // Type or member is obsolete
            bool?directConnection,
            ServerSettings settings,
            EndPoint endPoint,
            IConnectionPoolFactory connectionPoolFactory,
            IServerMonitorFactory monitorFactory,
            IEventSubscriber eventSubscriber,
            ServerApi serverApi)
            : base(
                clusterId,
                clusterClock,
                clusterConnectionMode,
                connectionModeSwitch,
                directConnection,
                settings,
                endPoint,
                connectionPoolFactory,
                eventSubscriber,
                serverApi)
        {
            _monitor         = Ensure.IsNotNull(monitorFactory, nameof(monitorFactory)).Create(ServerId, endPoint);
            _baseDescription = _currentDescription = new ServerDescription(ServerId, endPoint, reasonChanged: "ServerInitialDescription", heartbeatInterval: settings.HeartbeatInterval);
        }
コード例 #7
0
 // methods
 public ClusterSettings WithConnectionMode(ClusterConnectionMode value)
 {
     return((_connectionMode == value) ? this : new Builder(this)
     {
         _connectionMode = value
     }.Build());
 }
コード例 #8
0
        public ServerTests()
        {
            _clusterId = new ClusterId();
            _endPoint  = new DnsEndPoint("localhost", 27017);

            _clusterClock = new Mock <IClusterClock>().Object;
#pragma warning disable CS0618 // Type or member is obsolete
            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _connectionModeSwitch  = ConnectionModeSwitch.UseConnectionMode;
#pragma warning restore CS0618 // Type or member is obsolete
            _directConnection   = null;
            _mockConnectionPool = new Mock <IConnectionPool>();
            _mockConnectionPool.Setup(p => p.AcquireConnection(It.IsAny <CancellationToken>())).Returns(new Mock <IConnectionHandle>().Object);
            _mockConnectionPool.Setup(p => p.AcquireConnectionAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(new Mock <IConnectionHandle>().Object));
            _mockConnectionPoolFactory = new Mock <IConnectionPoolFactory>();
            _mockConnectionPoolFactory
            .Setup(f => f.CreateConnectionPool(It.IsAny <ServerId>(), _endPoint))
            .Returns(_mockConnectionPool.Object);

            _mockServerMonitor        = new Mock <IServerMonitor>();
            _mockServerMonitorFactory = new Mock <IServerMonitorFactory>();
            _mockServerMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), _endPoint)).Returns(_mockServerMonitor.Object);

            _capturedEvents = new EventCapturer();
            _serverApi      = new ServerApi(ServerApiVersion.V1, true, true);
            _settings       = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject      = new DefaultServer(_clusterId, _clusterClock, _clusterConnectionMode, _connectionModeSwitch, _directConnection, _settings, _endPoint, _mockConnectionPoolFactory.Object, _mockServerMonitorFactory.Object, _capturedEvents, _serverApi);
            _connectionId = new ConnectionId(_subject.ServerId);
        }
コード例 #9
0
#pragma warning disable CS0618 // Type or member is obsolete
        public static void EnsureConnectionModeValuesAreValid(ClusterConnectionMode connectionMode, ConnectionModeSwitch connectionModeSwitch, bool?directConnection)
        {
            switch (connectionModeSwitch)
            {
            case ConnectionModeSwitch.NotSet:
                if (connectionMode != default)
                {
                    throw new ArgumentException($"When connectionModeSwitch is NotSet connectionMode must have the default value.", nameof(connectionMode));
                }
                if (directConnection != default)
                {
                    throw new ArgumentException($"When connectionModeSwitch is NotSet directConnection must have the default value.", nameof(directConnection));
                }
                break;

            case ConnectionModeSwitch.UseConnectionMode:
                if (directConnection != default)
                {
                    throw new ArgumentException($"When connectionModeSwitch is UseConnectionMode directConnection must have the default value.", nameof(directConnection));
                }
                break;

            case ConnectionModeSwitch.UseDirectConnection:
                if (connectionMode != default)
                {
                    throw new ArgumentException($"When connectionModeSwitch is UseDirectConnection connectionMode must have the default value.", nameof(connectionMode));
                }
                break;

            default:
                throw new ArgumentException($"Invalid connectionMode: {connectionMode}.", nameof(connectionMode));
            }
        }
コード例 #10
0
 // constructors
 public ServerFactory(ClusterConnectionMode clusterConnectionMode, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IConnectionFactory heartbeatConnectionFactory, IEventSubscriber eventSubscriber)
 {
     _clusterConnectionMode = clusterConnectionMode;
     _settings = Ensure.IsNotNull(settings, "settings");
     _connectionPoolFactory      = Ensure.IsNotNull(connectionPoolFactory, "connectionPoolFactory");
     _heartbeatConnectionFactory = Ensure.IsNotNull(heartbeatConnectionFactory, "heartbeatConnectionFactory");
     _eventSubscriber            = Ensure.IsNotNull(eventSubscriber, "eventSubscriber");
 }
コード例 #11
0
 // constructors
 public ServerFactory(ClusterConnectionMode clusterConnectionMode, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IConnectionFactory heartbeatConnectionFactory, IEventSubscriber eventSubscriber)
 {
     _clusterConnectionMode = clusterConnectionMode;
     _settings = Ensure.IsNotNull(settings, "settings");
     _connectionPoolFactory = Ensure.IsNotNull(connectionPoolFactory, "connectionPoolFactory");
     _heartbeatConnectionFactory = Ensure.IsNotNull(heartbeatConnectionFactory, "heartbeatConnectionFactory");
     _eventSubscriber = Ensure.IsNotNull(eventSubscriber, "eventSubscriber");
 }
コード例 #12
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ServerFactory"/> class.
 /// </summary>
 /// <param name="clusterConnectionMode">The cluster connection mode.</param>
 /// <param name="settings">The settings.</param>
 /// <param name="connectionPoolFactory">The connection pool factory.</param>
 /// <param name="heartbeatConnectionFactory">The heartbeat connection factory.</param>
 /// <param name="listener">The listener.</param>
 public ServerFactory(ClusterConnectionMode clusterConnectionMode, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IConnectionFactory heartbeatConnectionFactory, IServerListener listener)
 {
     _clusterConnectionMode = clusterConnectionMode;
     _settings = Ensure.IsNotNull(settings, "settings");
     _connectionPoolFactory      = Ensure.IsNotNull(connectionPoolFactory, "connectionPoolFactory");
     _heartbeatConnectionFactory = Ensure.IsNotNull(heartbeatConnectionFactory, "heartbeatConnectionFactory");
     _listener = listener;
 }
コード例 #13
0
 // constructors
 public ServerFactory(ClusterConnectionMode clusterConnectionMode, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IServerMonitorFactory serverMonitoryFactory, IEventSubscriber eventSubscriber)
 {
     _clusterConnectionMode = clusterConnectionMode;
     _settings = Ensure.IsNotNull(settings, nameof(settings));
     _connectionPoolFactory = Ensure.IsNotNull(connectionPoolFactory, nameof(connectionPoolFactory));
     _serverMonitorFactory = Ensure.IsNotNull(serverMonitoryFactory, nameof(serverMonitoryFactory));
     _eventSubscriber = Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
 }
コード例 #14
0
 // static methods
 internal static ClusterDescription CreateInitial(ClusterId clusterId, ClusterConnectionMode connectionMode)
 {
     return(new ClusterDescription(
                clusterId,
                connectionMode,
                ClusterType.Unknown,
                Enumerable.Empty <ServerDescription>()));
 }
コード例 #15
0
 public ClusterDescription(
     ClusterId clusterId,
     ClusterConnectionMode connectionMode,
     ClusterType type,
     IEnumerable <ServerDescription> servers)
     : this(clusterId, connectionMode, dnsMonitorException : null, type, servers)
 {
 }
コード例 #16
0
 // constructors
 public ServerFactory(ClusterConnectionMode clusterConnectionMode, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IServerMonitorFactory serverMonitoryFactory, IEventSubscriber eventSubscriber)
 {
     _clusterConnectionMode = clusterConnectionMode;
     _settings = Ensure.IsNotNull(settings, nameof(settings));
     _connectionPoolFactory = Ensure.IsNotNull(connectionPoolFactory, nameof(connectionPoolFactory));
     _serverMonitorFactory  = Ensure.IsNotNull(serverMonitoryFactory, nameof(serverMonitoryFactory));
     _eventSubscriber       = Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
 }
コード例 #17
0
 internal ClusterSettings(
     ClusterConnectionMode connectionMode,
     IReadOnlyList <EndPoint> endPoints,
     string replicaSetName)
 {
     _connectionMode = connectionMode;
     _endPoints      = endPoints;
     _replicaSetName = replicaSetName;
 }
コード例 #18
0
 internal ClusterSettings(
     ClusterConnectionMode connectionMode,
     IReadOnlyList<EndPoint> endPoints,
     string replicaSetName)
 {
     _connectionMode = connectionMode;
     _endPoints = endPoints;
     _replicaSetName = replicaSetName;
 }
コード例 #19
0
        public void Constructor_should_throw_if_cluster_connection_mode_is_not_supported(ClusterConnectionMode mode)
        {
            var settings = new ClusterSettings(
                endPoints: new[] { new DnsEndPoint("localhost", 27017) },
                connectionMode: mode);
            Action act = () => new MultiServerCluster(settings, _serverFactory, _capturedEvents);

            act.ShouldThrow<ArgumentException>();
        }
コード例 #20
0
        public void Description_should_return_correct_description_when_not_initialized(ClusterConnectionMode connectionMode, ClusterType clusterType)
        {
            var subject = CreateSubject(connectionMode);
            var description = subject.Description;

            description.Servers.Should().BeEmpty();
            description.State.Should().Be(ClusterState.Disconnected);
            description.Type.Should().Be(clusterType);
        }
コード例 #21
0
 public void Setup()
 {
     _clusterId = new ClusterId();
     _clusterConnectionMode = ClusterConnectionMode.Standalone;
     _connectionPoolFactory = Substitute.For<IConnectionPoolFactory>();
     _endPoint = new DnsEndPoint("localhost", 27017);
     _heartbeatConnectionFactory = Substitute.For<IConnectionFactory>();
     _eventSubscriber = Substitute.For<IEventSubscriber>();
     _settings = new ServerSettings();
 }
コード例 #22
0
 public void Setup()
 {
     _clusterId             = new ClusterId();
     _clusterConnectionMode = ClusterConnectionMode.Standalone;
     _connectionPoolFactory = Substitute.For <IConnectionPoolFactory>();
     _endPoint = new DnsEndPoint("localhost", 27017);
     _heartbeatConnectionFactory = Substitute.For <IConnectionFactory>();
     _listener = Substitute.For <IServerListener>();
     _settings = new ServerSettings();
 }
コード例 #23
0
 // private methods
 private ClusterDescription CreateClusterDescription(
     ClusterId clusterId = null,
     ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic,
     ClusterType type = ClusterType.Unknown,
     IEnumerable <ServerDescription> servers = null)
 {
     clusterId = clusterId ?? new ClusterId(1);
     servers   = servers ?? new ServerDescription[0];
     return(new ClusterDescription(clusterId, connectionMode, type, servers));
 }
コード例 #24
0
 public ServerFactoryTests()
 {
     _clusterId = new ClusterId();
     _clusterConnectionMode = ClusterConnectionMode.Standalone;
     _connectionPoolFactory = new Mock<IConnectionPoolFactory>().Object;
     _endPoint = new DnsEndPoint("localhost", 27017);
     _serverMonitorFactory = new Mock<IServerMonitorFactory>().Object;
     _eventSubscriber = new Mock<IEventSubscriber>().Object;
     _settings = new ServerSettings();
 }
 public ServerFactoryTests()
 {
     _clusterId             = new ClusterId();
     _clusterConnectionMode = ClusterConnectionMode.Standalone;
     _connectionPoolFactory = new Mock <IConnectionPoolFactory>().Object;
     _endPoint             = new DnsEndPoint("localhost", 27017);
     _serverMonitorFactory = new Mock <IServerMonitorFactory>().Object;
     _eventSubscriber      = new Mock <IEventSubscriber>().Object;
     _settings             = new ServerSettings();
 }
コード例 #26
0
 public void Setup()
 {
     _clusterId             = new ClusterId();
     _clusterConnectionMode = ClusterConnectionMode.Standalone;
     _connectionPoolFactory = Substitute.For <IConnectionPoolFactory>();
     _endPoint             = new DnsEndPoint("localhost", 27017);
     _serverMonitorFactory = Substitute.For <IServerMonitorFactory>();
     _eventSubscriber      = Substitute.For <IEventSubscriber>();
     _settings             = new ServerSettings();
 }
コード例 #27
0
        private StubCluster CreateSubject(ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic, TimeSpan?serverSelectionTimeout = null)
        {
            _settings = _settings.With(connectionMode: connectionMode);
            if (serverSelectionTimeout != null)
            {
                _settings = _settings.With(serverSelectionTimeout: serverSelectionTimeout.Value);
            }

            return(new StubCluster(_settings, _mockServerFactory.Object, _capturedEvents));
        }
コード例 #28
0
        // private methods
#pragma warning disable CS0618 // Type or member is obsolete
        private StubCluster CreateSubject(ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic, ConnectionModeSwitch connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode, TimeSpan?serverSelectionTimeout = null)
#pragma warning restore CS0618 // Type or member is obsolete
        {
            _settings = _settings.With(connectionMode: connectionMode, connectionModeSwitch: connectionModeSwitch);
            if (serverSelectionTimeout != null)
            {
                _settings = _settings.With(serverSelectionTimeout: serverSelectionTimeout.Value);
            }

            return(new StubCluster(_settings, _mockServerFactory.Object, _capturedEvents));
        }
コード例 #29
0
        // internal static methods
#pragma warning disable CS0618 // Type or member is obsolete
        internal static ClusterDescription CreateInitial(ClusterId clusterId, ClusterConnectionMode connectionMode, ConnectionModeSwitch connectionModeSwitch, bool?directConnection)
        {
            return(new ClusterDescription(
                       clusterId,
                       connectionMode,
                       connectionModeSwitch,
                       directConnection,
                       dnsMonitorException: null,
                       type: ClusterType.Unknown,
                       servers: Enumerable.Empty <ServerDescription>()));
        }
コード例 #30
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ClusterDescription" /> class.
 /// </summary>
 /// <param name="clusterId">The cluster identifier.</param>
 /// <param name="connectionMode">The connection mode.</param>
 /// <param name="type">The type.</param>
 /// <param name="servers">The servers.</param>
 public ClusterDescription(
     ClusterId clusterId,
     ClusterConnectionMode connectionMode,
     ClusterType type,
     IEnumerable <ServerDescription> servers)
 {
     _clusterId      = Ensure.IsNotNull(clusterId, nameof(clusterId));
     _connectionMode = connectionMode;
     _type           = type;
     _servers        = (servers ?? new ServerDescription[0]).OrderBy(n => n.EndPoint, new ToStringComparer <EndPoint>()).ToList();
 }
コード例 #31
0
        // constructors
#pragma warning disable CS0618 // Type or member is obsolete
        public ServerFactory(ClusterConnectionMode clusterConnectionMode, ConnectionModeSwitch connectionModeSwitch, bool?directConnection, ServerSettings settings, IConnectionPoolFactory connectionPoolFactory, IServerMonitorFactory serverMonitoryFactory, IEventSubscriber eventSubscriber)
#pragma warning restore CS0618 // Type or member is obsolete
        {
            ClusterConnectionModeHelper.EnsureConnectionModeValuesAreValid(clusterConnectionMode, connectionModeSwitch, directConnection);

            _clusterConnectionMode = clusterConnectionMode;
            _connectionModeSwitch  = connectionModeSwitch;
            _directConnection      = directConnection;
            _settings = Ensure.IsNotNull(settings, nameof(settings));
            _connectionPoolFactory = Ensure.IsNotNull(connectionPoolFactory, nameof(connectionPoolFactory));
            _serverMonitorFactory  = Ensure.IsNotNull(serverMonitoryFactory, nameof(serverMonitoryFactory));
            _eventSubscriber       = Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
        }
コード例 #32
0
 // constructors
 public ClusterSettings(
     Optional <ClusterConnectionMode> connectionMode = default(Optional <ClusterConnectionMode>),
     Optional <IEnumerable <EndPoint> > endPoints    = default(Optional <IEnumerable <EndPoint> >),
     Optional <int> maxServerSelectionWaitQueueSize  = default(Optional <int>),
     Optional <string> replicaSetName           = default(Optional <string>),
     Optional <TimeSpan> serverSelectionTimeout = default(Optional <TimeSpan>))
 {
     _connectionMode = connectionMode.WithDefault(ClusterConnectionMode.Automatic);
     _endPoints      = Ensure.IsNotNull(endPoints.WithDefault(__defaultEndPoints), "endPoints").ToList();
     _maxServerSelectionWaitQueueSize = Ensure.IsGreaterThanOrEqualToZero(maxServerSelectionWaitQueueSize.WithDefault(500), "maxServerSelectionWaitQueueSize");
     _replicaSetName         = replicaSetName.WithDefault(null);
     _serverSelectionTimeout = Ensure.IsInfiniteOrGreaterThanOrEqualToZero(serverSelectionTimeout.WithDefault(TimeSpan.FromSeconds(30)), "serverSelectionTimeout");
 }
コード例 #33
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClusterDescription" /> class.
 /// </summary>
 /// <param name="clusterId">The cluster identifier.</param>
 /// <param name="connectionMode">The connection mode.</param>
 /// <param name="dnsMonitorException">The last DNS monitor exception (null if there was none).</param>
 /// <param name="type">The type.</param>
 /// <param name="servers">The servers.</param>
 public ClusterDescription(
     ClusterId clusterId,
     ClusterConnectionMode connectionMode,
     Exception dnsMonitorException,
     ClusterType type,
     IEnumerable <ServerDescription> servers)
 {
     _clusterId           = Ensure.IsNotNull(clusterId, nameof(clusterId));
     _connectionMode      = connectionMode;
     _dnsMonitorException = dnsMonitorException; // can be null
     _type    = type;
     _servers = (servers ?? new ServerDescription[0]).OrderBy(n => n.EndPoint, new ToStringComparer <EndPoint>()).ToList();
     _logicalSessionTimeout = CalculateLogicalSessionTimeout(_connectionMode, _servers);
 }
コード例 #34
0
        // constructors
        public ClusterableServer(ClusterId clusterId, ClusterConnectionMode clusterConnectionMode, ServerSettings settings, EndPoint endPoint, IConnectionPoolFactory connectionPoolFactory, IConnectionFactory heartbeatConnectionFactory, IServerListener listener)
        {
            Ensure.IsNotNull(clusterId, "clusterId");
            _clusterConnectionMode = clusterConnectionMode;
            _settings = Ensure.IsNotNull(settings, "settings");;
            _endPoint = Ensure.IsNotNull(endPoint, "endPoint");
            Ensure.IsNotNull(connectionPoolFactory, "connectionPoolFactory");
            _heartbeatConnectionFactory = Ensure.IsNotNull(heartbeatConnectionFactory, "heartbeatConnectionFactory");
            _listener = listener;

            _serverId        = new ServerId(clusterId, endPoint);
            _baseDescription = _currentDescription = new ServerDescription(_serverId, endPoint);
            _connectionPool  = connectionPoolFactory.CreateConnectionPool(_serverId, endPoint);
            _state           = new InterlockedInt32(State.Initial);
        }
コード例 #35
0
        // private methods
        private ClusterDescription CreateClusterDescription(
            ClusterId clusterId = null,
#pragma warning disable CS0618 // Type or member is obsolete
            ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic,
#pragma warning restore CS0618 // Type or member is obsolete
            ClusterType type = ClusterType.Unknown,
            IEnumerable <ServerDescription> servers = null)
        {
            clusterId = clusterId ?? new ClusterId(1);
            servers   = servers ?? new ServerDescription[0];
#pragma warning disable CS0618 // Type or member is obsolete
            return(new ClusterDescription(clusterId, connectionMode, type, servers));

#pragma warning restore CS0618 // Type or member is obsolete
        }
コード例 #36
0
        public void Should_remove_server_of_the_wrong_type(ClusterConnectionMode connectionMode, ServerType wrongType)
        {
            _settings = _settings
                        .WithEndPoints(new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint })
                        .WithConnectionMode(connectionMode);

            var subject = CreateSubject();

            subject.Initialize();

            PublishDescription(_secondEndPoint, wrongType);

            var description = subject.Description;

            description.Servers.Should().BeEquivalentTo(GetDescriptions(_firstEndPoint, _thirdEndPoint));
        }
コード例 #37
0
        public void Description_should_not_contain_any_servers_if_the_provided_server_is_not_of_the_required_type(ClusterConnectionMode connectionMode, ServerType serverType)
        {
            var server = Substitute.For<IClusterableServer>();
            _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(server);

            _settings = _settings.WithConnectionMode(connectionMode);

            var subject = CreateSubject();
            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, serverType: serverType);

            server.DescriptionChanged += Raise.EventWith(new object(), new ServerDescriptionChangedEventArgs(connected, connected));

            subject.Description.Servers.Should().BeEmpty();
        }
コード例 #38
0
        public ServerFactoryTests()
        {
            _clusterId             = new ClusterId();
            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _connectionPoolFactory = new Mock <IConnectionPoolFactory>().Object;
            _endPoint = new DnsEndPoint("localhost", 27017);
            var mockServerMonitor = new Mock <IServerMonitor>();

            mockServerMonitor.Setup(m => m.Description).Returns(new ServerDescription(new ServerId(_clusterId, _endPoint), _endPoint));
            var mockServerMonitorFactory = new Mock <IServerMonitorFactory>();

            mockServerMonitorFactory.Setup(f => f.Create(It.IsAny <ServerId>(), _endPoint)).Returns(mockServerMonitor.Object);
            _serverMonitorFactory = mockServerMonitorFactory.Object;
            _eventSubscriber      = new Mock <IEventSubscriber>().Object;
            _settings             = new ServerSettings();
        }
コード例 #39
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="ClusterSettings"/> class.
 /// </summary>
 /// <param name="connectionMode">The connection mode.</param>
 /// <param name="endPoints">The end points.</param>
 /// <param name="maxServerSelectionWaitQueueSize">Maximum size of the server selection wait queue.</param>
 /// <param name="replicaSetName">Name of the replica set.</param>
 /// <param name="serverSelectionTimeout">The server selection timeout.</param>
 /// <param name="preServerSelector">The pre server selector.</param>
 /// <param name="postServerSelector">The post server selector.</param>
 public ClusterSettings(
     Optional<ClusterConnectionMode> connectionMode = default(Optional<ClusterConnectionMode>),
     Optional<IEnumerable<EndPoint>> endPoints = default(Optional<IEnumerable<EndPoint>>),
     Optional<int> maxServerSelectionWaitQueueSize = default(Optional<int>),
     Optional<string> replicaSetName = default(Optional<string>),
     Optional<TimeSpan> serverSelectionTimeout = default(Optional<TimeSpan>),
     Optional<IServerSelector> preServerSelector = default(Optional<IServerSelector>),
     Optional<IServerSelector> postServerSelector = default(Optional<IServerSelector>))
 {
     _connectionMode = connectionMode.WithDefault(ClusterConnectionMode.Automatic);
     _endPoints = Ensure.IsNotNull(endPoints.WithDefault(__defaultEndPoints), "endPoints").ToList();
     _maxServerSelectionWaitQueueSize = Ensure.IsGreaterThanOrEqualToZero(maxServerSelectionWaitQueueSize.WithDefault(500), "maxServerSelectionWaitQueueSize");
     _replicaSetName = replicaSetName.WithDefault(null);
     _serverSelectionTimeout = Ensure.IsGreaterThanOrEqualToZero(serverSelectionTimeout.WithDefault(TimeSpan.FromSeconds(30)), "serverSelectionTimeout");
     _preServerSelector = preServerSelector.WithDefault(null);
     _postServerSelector = postServerSelector.WithDefault(null);
 }
コード例 #40
0
        public void Setup()
        {
            _clusterId = new ClusterId();
            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _connectionPool = Substitute.For<IConnectionPool>();
            _connectionPoolFactory = Substitute.For<IConnectionPoolFactory>();
            _connectionPoolFactory.CreateConnectionPool(null, null)
                .ReturnsForAnyArgs(_connectionPool);

            _endPoint = new DnsEndPoint("localhost", 27017);
            _heartbeatConnection = new MockConnection();
            _heartbeatConnectionFactory = Substitute.For<IConnectionFactory>();
            _heartbeatConnectionFactory.CreateConnection(null, null)
                .ReturnsForAnyArgs(_heartbeatConnection);

            _capturedEvents = new EventCapturer();
            _settings = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject = new ClusterableServer(_clusterId, _clusterConnectionMode, _settings, _endPoint, _connectionPoolFactory, _heartbeatConnectionFactory, _capturedEvents);
        }
コード例 #41
0
ファイル: ServerTests.cs プロジェクト: RavenZZ/MDRelation
        public ServerTests()
        {
            _clusterId = new ClusterId();
            _endPoint = new DnsEndPoint("localhost", 27017);

            _clusterConnectionMode = ClusterConnectionMode.Standalone;
            _mockConnectionPool = new Mock<IConnectionPool>();
            _mockConnectionPool.Setup(p => p.AcquireConnection(It.IsAny<CancellationToken>())).Returns(new Mock<IConnectionHandle>().Object);
            _mockConnectionPool.Setup(p => p.AcquireConnectionAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(new Mock<IConnectionHandle>().Object));
            _mockConnectionPoolFactory = new Mock<IConnectionPoolFactory>();
            _mockConnectionPoolFactory
                .Setup(f => f.CreateConnectionPool(It.IsAny<ServerId>(), _endPoint))
                .Returns(_mockConnectionPool.Object);

            _mockServerMonitor = new Mock<IServerMonitor>();
            _mockServerMonitorFactory = new Mock<IServerMonitorFactory>();
            _mockServerMonitorFactory.Setup(f => f.Create(It.IsAny<ServerId>(), _endPoint)).Returns(_mockServerMonitor.Object);

            _capturedEvents = new EventCapturer();
            _settings = new ServerSettings(heartbeatInterval: Timeout.InfiniteTimeSpan);

            _subject = new Server(_clusterId, _clusterConnectionMode, _settings, _endPoint, _mockConnectionPoolFactory.Object, _mockServerMonitorFactory.Object, _capturedEvents);
        }
コード例 #42
0
        public void Should_hide_a_seedlist_server_of_the_wrong_type(ClusterConnectionMode connectionMode, ServerType wrongType)
        {
            _settings = _settings.With(
                endPoints: new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint },
                connectionMode: connectionMode);

            var subject = CreateSubject();
            subject.Initialize();
            _capturedEvents.Clear();

            PublishDescription(subject, _secondEndPoint, wrongType);

            var description = subject.Description;
            description.Servers.Should().BeEquivalentTo(GetDescriptions(_firstEndPoint, _thirdEndPoint));

            _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
コード例 #43
0
 // methods
 public ClusterSettings WithConnectionMode(ClusterConnectionMode value)
 {
     return (_connectionMode == value) ? this : new Builder(this) { _connectionMode = value }.Build();
 }
コード例 #44
0
 // constructors
 public Builder(ClusterSettings other)
 {
     _connectionMode = other._connectionMode;
     _endPoints = other._endPoints;
     _replicaSetName = other._replicaSetName;
 }
コード例 #45
0
        private StubCluster CreateSubject(ClusterConnectionMode connectionMode = ClusterConnectionMode.Unspecified)
        {
            _settings = _settings.WithConnectionMode(connectionMode);

            return new StubCluster(_settings, _serverFactory, _clusterListener);
        }
コード例 #46
0
        private bool IsServerValidForCluster(ClusterType clusterType, ClusterConnectionMode connectionMode, ServerType serverType)
        {
            switch (clusterType)
            {
                case ClusterType.ReplicaSet:
                    return serverType.IsReplicaSetMember();

                case ClusterType.Sharded:
                    return serverType == ServerType.ShardRouter;

                case ClusterType.Standalone:
                    return serverType == ServerType.Standalone;

                case ClusterType.Unknown:
                    switch (connectionMode)
                    {
                        case ClusterConnectionMode.Automatic:
                        case ClusterConnectionMode.Direct:
                            return true;

                        default:
                            throw new MongoInternalException("Unexpected connection mode.");
                    }

                default:
                    throw new MongoInternalException("Unexpected cluster type.");
            }
        }
コード例 #47
0
        public void Description_should_not_contain_any_servers_if_the_provided_server_is_not_of_the_required_type(ClusterConnectionMode connectionMode, ServerType serverType)
        {
            _settings = _settings.With(connectionMode: connectionMode);

            var subject = CreateSubject();
            subject.Initialize();
            _capturedEvents.Clear();

            PublishDescription(_endPoint, serverType);

            subject.Description.Servers.Should().BeEmpty();
            _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
コード例 #48
0
        public void When_connect_is_specified(string connectionString, ClusterConnectionMode connect)
        {
            var subject = new ConnectionString(connectionString);

            subject.Connect.Should().Be(connect);
        }
コード例 #49
0
        public void Should_remove_server_of_the_wrong_type(ClusterConnectionMode connectionMode, ServerType wrongType)
        {
            _settings = _settings
                .WithEndPoints(new[] { _firstEndPoint, _secondEndPoint, _thirdEndPoint })
                .WithConnectionMode(connectionMode);

            var subject = CreateSubject();
            subject.Initialize();

            PublishDescription(_secondEndPoint, wrongType);

            var description = subject.Description;
            description.Servers.Should().BeEquivalentTo(GetDescriptions(_firstEndPoint, _thirdEndPoint));
        }
コード例 #50
0
ファイル: ClusterTests.cs プロジェクト: RavenZZ/MDRelation
        private StubCluster CreateSubject(ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic, TimeSpan? serverSelectionTimeout = null)
        {
            _settings = _settings.With(connectionMode: connectionMode);
            if (serverSelectionTimeout != null)
            {
                _settings = _settings.With(serverSelectionTimeout: serverSelectionTimeout.Value);
            }

            return new StubCluster(_settings, _mockServerFactory.Object, _capturedEvents);
        }
コード例 #51
0
        private StubCluster CreateSubject(ClusterConnectionMode connectionMode = ClusterConnectionMode.Automatic)
        {
            _settings = _settings.With(connectionMode: connectionMode);

            return new StubCluster(_settings, _serverFactory, _capturedEvents);
        }