Example #1
0
        // note: this method will run on a thread from the ThreadPool
        private void QueryNodeWorkItem(
            object parameters
            )
        {
            // this method has to work at a very low level because the connection pool isn't set up yet
            var args     = (QueryNodeParameters)parameters;
            var response = new QueryNodeResponse {
                Address = args.Address, EndPoint = args.EndPoint
            };

            try {
                var connection = new MongoConnection(null, args.EndPoint); // no connection pool
                try {
                    var isMasterCommand = new CommandDocument("ismaster", 1);
                    var isMasterResult  = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                    response.IsMasterResult   = isMasterResult;
                    response.Connection       = connection; // might become the first connection in the connection pool
                    response.IsPrimary        = isMasterResult.Response["ismaster", false].ToBoolean();
                    response.MaxDocumentSize  = isMasterResult.Response["maxBsonObjectSize", server.MaxDocumentSize].ToInt32();
                    response.MaxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, response.MaxDocumentSize + 1024); // derived from maxDocumentSize

                    if (server.Settings.ReplicaSetName != null)
                    {
                        var getStatusCommand = new CommandDocument("replSetGetStatus", 1);
                        var getStatusResult  = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, getStatusCommand);

                        var replicaSetName = getStatusResult.Response["set"].AsString;
                        if (replicaSetName != server.Settings.ReplicaSetName)
                        {
                            var message = string.Format("Host {0} belongs to a different replica set: {1}", args.EndPoint, replicaSetName);
                            throw new MongoConnectionException(message);
                        }
                    }
                } catch {
                    try { connection.Close(); } catch { } // ignore exceptions
                    throw;
                }
            } catch (Exception ex) {
                response.Exception = ex;
            }

            args.ResponseQueue.Enqueue(response);
        }
Example #2
0
        private List <MongoServerAddress> GetHostAddresses(
            QueryNodeResponse response
            )
        {
            if (!response.IsMasterResult.Response.Contains("hosts"))
            {
                var message = string.Format("Server is not a member of a replica set: {0}", response.Address);
                throw new MongoConnectionException(message);
            }

            var nodes = new List <MongoServerAddress>();

            foreach (BsonString host in response.IsMasterResult.Response["hosts"].AsBsonArray.Values)
            {
                var address = MongoServerAddress.Parse(host.Value);
                nodes.Add(address);
            }
            return(nodes);
        }
        private List<MongoServerAddress> GetHostAddresses(
            QueryNodeResponse response
        ) {
            if (!response.IsMasterResult.Response.Contains("hosts")) {
                var message = string.Format("Server is not a member of a replica set: {0}", response.Address);
                throw new MongoConnectionException(message);
            }

            var nodes = new List<MongoServerAddress>();
            foreach (BsonString host in response.IsMasterResult.Response["hosts"].AsBsonArray.Values) {
                var address = MongoServerAddress.Parse(host.Value);
                nodes.Add(address);
            }
            return nodes;
        }
        // note: this method will run on a thread from the ThreadPool
        private void QueryNodeWorkItem(
            object parameters
        ) {
            // this method has to work at a very low level because the connection pool isn't set up yet
            var args = (QueryNodeParameters) parameters;
            var response = new QueryNodeResponse { Address = args.Address, EndPoint = args.EndPoint };

            try {
                var connection = new MongoConnection(null, args.EndPoint); // no connection pool
                try {
                    var isMasterCommand = new CommandDocument("ismaster", 1);
                    var isMasterResult = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                    response.IsMasterResult = isMasterResult;
                    response.Connection = connection; // might become the first connection in the connection pool
                    response.IsPrimary = isMasterResult.Response["ismaster", false].ToBoolean();
                    response.MaxDocumentSize = isMasterResult.Response["maxBsonObjectSize", server.MaxDocumentSize].ToInt32();
                    response.MaxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, response.MaxDocumentSize + 1024); // derived from maxDocumentSize

                    if (server.Settings.ReplicaSetName != null) {
                        var getStatusCommand = new CommandDocument("replSetGetStatus", 1);
                        var getStatusResult = connection.RunCommand(server, "admin.$cmd", QueryFlags.SlaveOk, getStatusCommand);

                        var replicaSetName = getStatusResult.Response["set"].AsString;
                        if (replicaSetName != server.Settings.ReplicaSetName) {
                            var message = string.Format("Host {0} belongs to a different replica set: {1}", args.EndPoint, replicaSetName);
                            throw new MongoConnectionException(message);
                        }
                    }
                } catch {
                    try { connection.Close(); } catch { } // ignore exceptions
                    throw;
                }
            } catch (Exception ex) {
                response.Exception = ex;
            }

            args.ResponseQueue.Enqueue(response);
        }
        // note: this method will run on a thread from the ThreadPool
        private void QueryNodeWorkItem(
            object parameters
        )
        {
            // this method has to work at a very low level because the connection pool isn't set up yet
            var args = (QueryNodeParameters) parameters;
            var response = new QueryNodeResponse { Address = args.Address };

            try {
                var connection = new MongoConnection(null, args.Address); // no connection pool
                try {
                    var isMasterCommand = new BsonDocument("ismaster", 1);
                    var isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);

                    response.IsMasterResult = isMasterResult;
                    response.Connection = connection; // might become the first connection in the connection pool
                    response.IsPrimary = isMasterResult["ismaster", false].ToBoolean();

                    if (url.ReplicaSetName != null) {
                        var getStatusCommand = new BsonDocument("replSetGetStatus", 1);
                        var getStatusResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, getStatusCommand);

                        var replicaSetName = getStatusResult["set"].AsString;
                        if (replicaSetName != url.ReplicaSetName) {
                            var message = string.Format("Host {0} belongs to a different replica set: {1}", args.Address, replicaSetName);
                            throw new MongoConnectionException(message);
                        }
                    }
                } catch {
                    try { connection.Close(); } catch { } // ignore exceptions
                    throw;
                }
            } catch (Exception ex) {
                response.Exception = ex;
            }

            args.ResponseQueue.Enqueue(response);
        }