Beispiel #1
0
            public IServer SelectServer()
            {
                lock (_cluster._descriptionLock)
                {
                    _descriptionChangedTask = _cluster._descriptionChangedTaskCompletionSource.Task;
                    _description            = _cluster._description;
                }

                if (!_serverSelectionWaitQueueEntered)
                {
                    var selectingServerEventHandler = _cluster._selectingServerEventHandler;
                    if (selectingServerEventHandler != null)
                    {
                        // this is our first time through...
                        selectingServerEventHandler(new ClusterSelectingServerEvent(
                                                        _description,
                                                        _selector,
                                                        EventContext.OperationId));
                    }
                }

                MongoIncompatibleDriverException.ThrowIfNotSupported(_description);

                var connectedServers = _description.Servers.Where(s => s.State == ServerState.Connected);
                var selectedServers  = _selector.SelectServers(_description, connectedServers).ToList();

                while (selectedServers.Count > 0)
                {
                    var server = selectedServers.Count == 1 ?
                                 selectedServers[0] :
                                 __randomServerSelector.SelectServers(_description, selectedServers).Single();

                    IClusterableServer selectedServer;
                    if (_cluster.TryGetServer(server.EndPoint, out selectedServer))
                    {
                        _stopwatch.Stop();
                        var selectedServerEventHandler = _cluster._selectedServerEventHandler;
                        if (selectedServerEventHandler != null)
                        {
                            selectedServerEventHandler(new ClusterSelectedServerEvent(
                                                           _description,
                                                           _selector,
                                                           server,
                                                           _stopwatch.Elapsed,
                                                           EventContext.OperationId));
                        }
                        return(selectedServer);
                    }

                    selectedServers.Remove(server);
                }

                return(null);
            }
Beispiel #2
0
            public IServer SelectServer()
            {
                lock (_cluster._descriptionLock)
                {
                    _descriptionChangedTask = _cluster._descriptionChangedTaskCompletionSource.Task;
                    _description            = _cluster._description;
                }

                if (!_serverSelectionWaitQueueEntered)
                {
                    var selectingServerEventHandler = _cluster._selectingServerEventHandler;
                    if (selectingServerEventHandler != null)
                    {
                        // this is our first time through...
                        selectingServerEventHandler(new ClusterSelectingServerEvent(
                                                        _description,
                                                        _selector,
                                                        EventContext.OperationId));
                    }
                }

                MongoIncompatibleDriverException.ThrowIfNotSupported(_description);

                _connectedServers.Clear();
                _connectedServerDescriptions.Clear();

                foreach (var description in _description.Servers)
                {
                    if (description.State == ServerState.Connected &&
                        _cluster.TryGetServer(description.EndPoint, out var server))
                    {
                        _connectedServers.Add(server);
                        _connectedServerDescriptions.Add(description);
                    }
                }

                var selectedServersDescriptions = _selector
                                                  .SelectServers(_description, _connectedServerDescriptions)
                                                  .ToList();

                IServer selectedServer = null;

                if (selectedServersDescriptions.Count > 0)
                {
                    var selectedServerDescription = selectedServersDescriptions.Count == 1
                        ? selectedServersDescriptions[0]
                        : __randomServerSelector.SelectServers(_description, selectedServersDescriptions).Single();

                    selectedServer = _connectedServers.FirstOrDefault(s => EndPointHelper.Equals(s.EndPoint, selectedServerDescription.EndPoint));
                }

                if (selectedServer != null)
                {
                    _stopwatch.Stop();

                    _cluster._selectedServerEventHandler?.Invoke(new ClusterSelectedServerEvent(
                                                                     _description,
                                                                     _selector,
                                                                     selectedServer.Description,
                                                                     _stopwatch.Elapsed,
                                                                     EventContext.OperationId));
                }

                return(selectedServer);
            }