Exemplo n.º 1
0
        public override void Initialize()
        {
            base.Initialize();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (_openingEventHandler != null)
                {
                    _openingEventHandler(new ClusterOpeningEvent(ClusterId, Settings));
                }

                var stopwatch = Stopwatch.StartNew();
                _server = CreateServer(Settings.EndPoints[0]);
                var newClusterDescription = Description.WithServerDescription(_server.Description);
                if (_addingServerEventHandler != null)
                {
                    _addingServerEventHandler(new ClusterAddingServerEvent(ClusterId, _server.EndPoint));
                }
                _server.DescriptionChanged += ServerDescriptionChanged;
                stopwatch.Stop();

                if (_addedServerEventHandler != null)
                {
                    _addedServerEventHandler(new ClusterAddedServerEvent(_server.ServerId, stopwatch.Elapsed));
                }

                UpdateClusterDescription(newClusterDescription);

                _server.Initialize();

                if (_openedEventHandler != null)
                {
                    _openedEventHandler(new ClusterOpenedEvent(ClusterId, Settings, stopwatch.Elapsed));
                }
            }
        }
        public void Initialize()
        {
            ThrowIfDisposed();

            if (_state.TryChange(State.Initial, State.Open))
            {
                var stopwatch = Stopwatch.StartNew();
                _openingEventHandler?.Invoke(new ClusterOpeningEvent(ClusterId, Settings));

                if (_settings.KmsProviders != null || _settings.SchemaMap != null)
                {
                    _cryptClient = CryptClientCreator.CreateCryptClient(_settings.KmsProviders, _settings.SchemaMap);
                }

                var endPoint = _settings.EndPoints.Single();
                if (_settings.Scheme != ConnectionStringScheme.MongoDBPlusSrv)
                {
                    _server = _serverFactory.CreateServer(_clusterType, _clusterId, _clusterClock, endPoint);
                    InitializeServer(_server);
                }
                else
                {
                    // _server will be created after srv resolving
                    var dnsEndPoint      = (DnsEndPoint)endPoint;
                    var lookupDomainName = dnsEndPoint.Host;
                    var monitor          = _dnsMonitorFactory.CreateDnsMonitor(this, lookupDomainName, _dnsMonitorCancellationTokenSource.Token);
                    _dnsMonitorThread = monitor.Start();
                }

                _openedEventHandler?.Invoke(new ClusterOpenedEvent(ClusterId, Settings, stopwatch.Elapsed));
            }
        }
        public void GetChannel_should_set_operations_count_correctly(
            [Values(false, true)] bool async,
            [Values(0, 1, 2, 10)] int operationsCount)
        {
            IClusterableServer server = SetupServer(false, false);

            var channels = new List <IChannel>();

            for (int i = 0; i < operationsCount; i++)
            {
                if (async)
                {
                    channels.Add(server.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult());
                }
                else
                {
                    channels.Add(server.GetChannel(CancellationToken.None));
                }
            }

            server.OutstandingOperationsCount.Should().Be(operationsCount);

            foreach (var channel in channels)
            {
                channel.Dispose();
                server.OutstandingOperationsCount.Should().Be(--operationsCount);
            }
        }
Exemplo n.º 4
0
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     lock (_serversLock)
     {
         server = _servers.FirstOrDefault(s => EndPointHelper.Equals(s.EndPoint, endPoint));
         return(server != null);
     }
 }
Exemplo n.º 5
0
        private void VerifyServerGeneration(IClusterableServer actualServer, int poolGeneration, string phaseDescription)
        {
            switch (actualServer)
            {
            case Server server:
                server._connectionPool().Generation.Should().Be(poolGeneration, phaseDescription);
                break;

            default: throw new Exception("Verifying pool generation with mock servers is currently unsupported.");
            }
        }
Exemplo n.º 6
0
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     if (_server.EndPoint.Equals(endPoint))
     {
         server = _server;
         return(true);
     }
     else
     {
         server = null;
         return(false);
     }
 }
        // private method
        private void InitializeServer(IClusterableServer server)
        {
            ThrowIfDisposed();

            var newClusterDescription = _description
                                        .WithType(ClusterType.LoadBalanced)
                                        .WithServerDescription(server.Description)
                                        .WithDnsMonitorException(null);

            UpdateClusterDescription(newClusterDescription);

            server.DescriptionChanged += ServerDescriptionChangedHandler;
            server.Initialize();
        }
        void IDnsMonitoringCluster.ProcessDnsResults(List <DnsEndPoint> endPoints)
        {
            switch (endPoints.Count)
            {
            case < 1:
                throw new InvalidOperationException("No srv records were resolved.");

            case > 1:
                throw new InvalidOperationException("Load balanced mode cannot be used with multiple host names.");
            }

            var resolvedEndpoint = endPoints.Single();

            _server = _serverFactory.CreateServer(_clusterType, _clusterId, _clusterClock, resolvedEndpoint);
            InitializeServer(_server);
        }
Exemplo n.º 9
0
        public void GetChannel_should_not_increase_operations_count_on_exception(
            [Values(false, true)] bool async,
            [Values(false, true)] bool connectionOpenException)
        {
            IClusterableServer server = SetupServer(connectionOpenException, !connectionOpenException);

            _ = Record.Exception(() =>
            {
                if (async)
                {
                    server.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
                }
                else
                {
                    server.GetChannel(CancellationToken.None);
                }
            });

            server.OutstandingOperationsCount.Should().Be(0);
        }
Exemplo n.º 10
0
        private void VerifyServerPropertiesNotInServerDescription(IClusterableServer actualServer, BsonDocument expectedServer, string phaseDescription)
        {
            if (expectedServer.TryGetValue("pool", out var poolValue))
            {
                switch (poolValue)
                {
                case BsonDocument poolDocument:
                    if (poolDocument.Values.Count() == 1 &&
                        poolDocument.TryGetValue("generation", out var generationValue) &&
                        generationValue is BsonInt32 generation)
                    {
                        VerifyServerGeneration(actualServer, generation.Value, phaseDescription);
                        break;
                    }
                    throw new FormatException($"Invalid schema for pool.");

                default: throw new FormatException($"Invalid topologyVersion BSON type: {poolValue.BsonType}.");
                }
            }
        }
        public void ChannelFork_should_not_affect_operations_count([Values(false, true)] bool async)
        {
            IClusterableServer server = SetupServer(false, false);

            var channel = async ?
                          server.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult() :
                          server.GetChannel(CancellationToken.None);

            server.OutstandingOperationsCount.Should().Be(1);

            var forkedChannel = channel.Fork();

            server.OutstandingOperationsCount.Should().Be(1);

            forkedChannel.Dispose();
            server.OutstandingOperationsCount.Should().Be(1);

            channel.Dispose();
            server.OutstandingOperationsCount.Should().Be(0);
        }
Exemplo n.º 12
0
        public override void Initialize()
        {
            base.Initialize();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (Listener != null)
                {
                    Listener.ClusterBeforeOpening(ClusterId, Settings);
                    Listener.ClusterBeforeAddingServer(ClusterId, Settings.EndPoints[0]);
                }

                var stopwatch = Stopwatch.StartNew();
                _server = CreateServer(Settings.EndPoints[0]);
                _server.DescriptionChanged += ServerDescriptionChanged;
                _server.Initialize();
                stopwatch.Stop();

                if (Listener != null)
                {
                    Listener.ClusterAfterAddingServer(_server.ServerId, stopwatch.Elapsed);
                    Listener.ClusterAfterOpening(ClusterId, Settings, stopwatch.Elapsed);
                }
            }
        }
Exemplo n.º 13
0
 protected abstract bool TryGetServer(EndPoint endPoint, out IClusterableServer server);
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     lock (_serversLock)
     {
         server = _servers.FirstOrDefault(s => EndPointHelper.Equals(s.EndPoint, endPoint));
         return server != null;
     }
 }
Exemplo n.º 15
0
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     return(_servers.TryGetValue(endPoint, out server));
 }
Exemplo n.º 16
0
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     server = CreateServer(endPoint);
     return(server != null);
 }
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     if (EndPointHelper.Equals(_server.EndPoint, endPoint))
     {
         server = _server;
         return true;
     }
     else
     {
         server = null;
         return false;
     }
 }
        public override void Initialize()
        {
            base.Initialize();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (_openingEventHandler != null)
                {
                    _openingEventHandler(new ClusterOpeningEvent(ClusterId, Settings));
                }

                var stopwatch = Stopwatch.StartNew();
                _server = CreateServer(Settings.EndPoints[0]);
                var newClusterDescription = Description.WithServerDescription(_server.Description);
                if (_addingServerEventHandler != null)
                {
                    _addingServerEventHandler(new ClusterAddingServerEvent(ClusterId, _server.EndPoint));
                }
                _server.DescriptionChanged += ServerDescriptionChanged;
                stopwatch.Stop();

                if (_addedServerEventHandler != null)
                {
                    _addedServerEventHandler(new ClusterAddedServerEvent(_server.ServerId, stopwatch.Elapsed));
                }

                UpdateClusterDescription(newClusterDescription);

                _server.Initialize();

                if (_openedEventHandler != null)
                {
                    _openedEventHandler(new ClusterOpenedEvent(ClusterId, Settings, stopwatch.Elapsed));
                }
            }
        }
Exemplo n.º 19
0
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     return _servers.TryGetValue(endPoint, out server);
 }
        public override void Initialize()
        {
            base.Initialize();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (Listener != null)
                {
                    Listener.ClusterBeforeOpening(ClusterId, Settings);
                    Listener.ClusterBeforeAddingServer(ClusterId, Settings.EndPoints[0]);
                }

                var stopwatch = Stopwatch.StartNew();
                _server = CreateServer(Settings.EndPoints[0]);
                _server.DescriptionChanged += ServerDescriptionChanged;
                _server.Initialize();
                stopwatch.Stop();

                if (Listener != null)
                {
                    Listener.ClusterAfterAddingServer(_server.ServerId, stopwatch.Elapsed);
                    Listener.ClusterAfterOpening(ClusterId, Settings, stopwatch.Elapsed);
                }
            }
        }
 protected override bool TryGetServer(EndPoint endPoint, out IClusterableServer server)
 {
     server = CreateServer(endPoint);
     return server != null;
 }
Exemplo n.º 22
0
 protected abstract bool TryGetServer(EndPoint endPoint, out IClusterableServer server);
Exemplo n.º 23
0
 public static IClusterableServer InitializeServer(this LoadBalancedCluster cluster, IClusterableServer server) => (IClusterableServer)Reflector.Invoke(cluster, nameof(InitializeServer), server);