Beispiel #1
0
        // constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="address">The address.</param>
        internal MongoServerInstance(MongoServerSettings settings, MongoServerAddress address)
        {
            _settings     = settings;
            _address      = address;
            _sequentialId = Interlocked.Increment(ref __nextSequentialId);
            _state        = MongoServerState.Disconnected;
            _serverInfo   = new ServerInformation
            {
                MaxDocumentSize  = MongoDefaults.MaxDocumentSize,
                MaxMessageLength = MongoDefaults.MaxMessageLength,
                InstanceType     = MongoServerInstanceType.Unknown
            };
            _connectionPool          = new MongoConnectionPool(this);
            _pingTimeAggregator      = new PingTimeAggregator(5);
            _permanentlyDisconnected = false;
            // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);

            _stateVerificationAcquireConnectionOptions = new MongoConnectionPool.AcquireConnectionOptions
            {
                OkToAvoidWaitingByCreatingNewConnection = false,
                OkToExceedMaxConnectionPoolSize         = true,
                OkToExceedWaitQueueSize = true,
                WaitQueueTimeout        = TimeSpan.FromSeconds(2)
            };
        }
        internal void Connect(
            bool slaveOk
            )
        {
            if (state != MongoServerState.Disconnected)
            {
                var message = string.Format("MongoServerInstance.Connect called when state is: {0}", state);
                throw new InvalidOperationException(message);
            }

            State            = MongoServerState.Connecting;
            connectException = null;
            try {
                endPoint = address.ToIPEndPoint(server.Settings.AddressFamily);

                var connectionPool = new MongoConnectionPool(this);
                try {
                    var connection = connectionPool.AcquireConnection(null);
                    try {
                        try {
                            var isMasterCommand = new CommandDocument("ismaster", 1);
                            isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);
                        } catch (MongoCommandException ex) {
                            isMasterResult = ex.CommandResult;
                            throw;
                        }

                        isPrimary   = isMasterResult.Response["ismaster", false].ToBoolean();
                        isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                        isPassive   = isMasterResult.Response["passive", false].ToBoolean();
                        isArbiter   = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                        if (!isPrimary && !slaveOk)
                        {
                            throw new MongoConnectionException("Server is not a primary and SlaveOk is false");
                        }

                        maxDocumentSize  = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                        maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize
                    } finally {
                        connectionPool.ReleaseConnection(connection);
                    }
                } catch {
                    connectionPool.Close();
                    throw;
                }

                State = MongoServerState.Connected;
                this.connectionPool = connectionPool;
            } catch (Exception ex) {
                State            = MongoServerState.Disconnected;
                connectException = ex;
                throw;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Connects to the server. Normally there is no need to call this method as
        /// the driver will connect to the server automatically when needed.
        /// </summary>
        /// <param name="timeout">How long to wait before timing out.</param>
        public virtual void Connect(
            TimeSpan timeout
            )
        {
            lock (serverLock) {
                if (state != MongoServerState.Connected)
                {
                    state = MongoServerState.Connecting;
                    try {
                        switch (settings.ConnectionMode)
                        {
                        case ConnectionMode.Direct:
                            var directConnector = new DirectConnector(this);
                            directConnector.Connect(timeout);
                            primaryConnectionPool    = new MongoConnectionPool(this, directConnector.Connection);
                            secondaryConnectionPools = null;
                            replicaSet       = null;
                            maxDocumentSize  = directConnector.MaxDocumentSize;
                            maxMessageLength = directConnector.MaxMessageLength;
                            break;

                        case ConnectionMode.ReplicaSet:
                            var replicaSetConnector = new ReplicaSetConnector(this);
                            replicaSetConnector.Connect(timeout);
                            primaryConnectionPool = new MongoConnectionPool(this, replicaSetConnector.PrimaryConnection);
                            if (settings.SlaveOk && replicaSetConnector.SecondaryConnections.Count > 0)
                            {
                                secondaryConnectionPools = new List <MongoConnectionPool>();
                                foreach (var connection in replicaSetConnector.SecondaryConnections)
                                {
                                    var secondaryConnectionPool = new MongoConnectionPool(this, connection);
                                    secondaryConnectionPools.Add(secondaryConnectionPool);
                                }
                            }
                            else
                            {
                                secondaryConnectionPools = null;
                            }
                            replicaSet       = replicaSetConnector.ReplicaSet;
                            maxDocumentSize  = replicaSetConnector.MaxDocumentSize;
                            maxMessageLength = replicaSetConnector.MaxMessageLength;
                            break;

                        default:
                            throw new MongoInternalException("Invalid ConnectionMode");
                        }
                        state = MongoServerState.Connected;
                    } catch {
                        state = MongoServerState.Disconnected;
                        throw;
                    }
                }
            }
        }
Beispiel #4
0
        private MongoServerState _state; // always use property to set value so event gets raised

        // constructors
        internal MongoServerInstance(MongoServer server, MongoServerAddress address)
        {
            _server           = server;
            _address          = address;
            _sequentialId     = Interlocked.Increment(ref __nextSequentialId);
            _maxDocumentSize  = MongoDefaults.MaxDocumentSize;
            _maxMessageLength = MongoDefaults.MaxMessageLength;
            _state            = MongoServerState.Disconnected;
            _connectionPool   = new MongoConnectionPool(this);
            // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
        }
 internal void Disconnect()
 {
     if (state != MongoServerState.Disconnected)
     {
         try {
             connectionPool.Close();
             connectionPool = null;
         } finally {
             State = MongoServerState.Disconnected;
         }
     }
 }
 internal void Disconnect()
 {
     if (state != MongoServerState.Disconnected)
     {
         try {
             // if we fail during Connect the connectionPool field will still be null
             if (connectionPool != null)
             {
                 connectionPool.Close();
                 connectionPool = null;
             }
         } finally {
             State = MongoServerState.Disconnected;
         }
     }
 }
 // constructors
 /// <summary>
 /// Initializes a new instance of the <see cref="MongoServerInstance"/> class.
 /// </summary>
 /// <param name="server">The server.</param>
 /// <param name="address">The address.</param>
 internal MongoServerInstance(MongoServer server, MongoServerAddress address)
 {
     _server       = server;
     _address      = address;
     _sequentialId = Interlocked.Increment(ref __nextSequentialId);
     _state        = MongoServerState.Disconnected;
     _serverInfo   = new ServerInformation
     {
         MaxDocumentSize  = MongoDefaults.MaxDocumentSize,
         MaxMessageLength = MongoDefaults.MaxMessageLength,
         InstanceType     = MongoServerInstanceType.Unknown
     };
     _connectionPool          = new MongoConnectionPool(this);
     _pingTimeAggregator      = new PingTimeAggregator(5);
     _permanentlyDisconnected = false;
     // Console.WriteLine("MongoServerInstance[{0}]: {1}", sequentialId, address);
 }
Beispiel #8
0
 /// <summary>
 /// Disconnects from the server. Normally there is no need to call this method so
 /// you should be sure to have a good reason to call it.
 /// </summary>
 public virtual void Disconnect()
 {
     // normally called from a connection when there is a SocketException
     // but anyone can call it if they want to close all sockets to the server
     lock (serverLock) {
         if (state == MongoServerState.Connected)
         {
             primaryConnectionPool.Close();
             primaryConnectionPool = null;
             if (secondaryConnectionPools != null)
             {
                 foreach (var secondaryConnectionPool in secondaryConnectionPools)
                 {
                     secondaryConnectionPool.Close();
                 }
                 secondaryConnectionPools = null;
             }
             state = MongoServerState.Disconnected;
         }
     }
 }