Class: Mongo::Node

Inherits:
Object show all
Defined in:
lib/mongo/util/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, host_port) ⇒ Node

Returns a new instance of Node.



20
21
22
23
24
25
26
27
28
# File 'lib/mongo/util/node.rb', line 20

def initialize(client, host_port)
  @client = client
  @manager = @client.local_manager
  @host, @port = Support.normalize_seeds(host_port)
  @address = "#{@host}:#{@port}"
  @config = nil
  @socket = nil
  @node_mutex = Mutex.new
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def address
  @address
end

#clientObject

Returns the value of attribute client.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def client
  @client
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def host
  @host
end

#last_stateObject

Returns the value of attribute last_state.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def last_state
  @last_state
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def port
  @port
end

#socketObject

Returns the value of attribute socket.



18
19
20
# File 'lib/mongo/util/node.rb', line 18

def socket
  @socket
end

Instance Method Details

#=~(other) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/mongo/util/node.rb', line 35

def =~(other)
  if other.is_a?(String)
    h, p = Support.normalize_seeds(other)
    h == @host && p == @port
  else
    false
  end
end

#active?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
# File 'lib/mongo/util/node.rb', line 89

def active?
  begin
    result = @client['admin'].command({:ping => 1}, :socket => @socket)
  rescue OperationFailure, SocketError, SystemCallError, IOError
    return nil
  end
  result['ok'] == 1
end

#arbitersObject



145
146
147
148
149
150
# File 'lib/mongo/util/node.rb', line 145

def arbiters
  return [] unless config['arbiters']
  config['arbiters'].map do |arbiter|
    Support.normalize_seeds(arbiter)
  end
end

#closeObject

This should only be called within a mutex



77
78
79
80
81
82
83
# File 'lib/mongo/util/node.rb', line 77

def close
  if @socket && !@socket.closed?
    @socket.close
  end
  @socket = nil
  @config = nil
end

#configObject



48
49
50
51
52
# File 'lib/mongo/util/node.rb', line 48

def config
  connect unless connected?
  set_config unless @config || !connected?
  @config
end

#connectObject

Create a connection to the provided node, and, if successful, return the socket. Otherwise, return nil.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mongo/util/node.rb', line 61

def connect
  @node_mutex.synchronize do
    begin
      @socket = @client.socket_class.new(@host, @port,
                                         @client.op_timeout,
                                         @client.connect_timeout,
                                         @client.socket_opts)
    rescue ConnectionTimeoutError, OperationTimeout, ConnectionFailure, OperationFailure,
           SocketError, SystemCallError, IOError => ex
      @client.log(:debug, "Failed connection to #{host_string} with #{ex.class}, #{ex.message}.")
      close
    end
  end
end

#connected?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mongo/util/node.rb', line 85

def connected?
  @socket != nil && !@socket.closed?
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


30
31
32
# File 'lib/mongo/util/node.rb', line 30

def eql?(other)
  (other.is_a?(Node) && @address == other.address)
end

#hashObject



168
169
170
# File 'lib/mongo/util/node.rb', line 168

def hash
  address.hash
end

#healthy?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/mongo/util/node.rb', line 172

def healthy?
  connected? && config
end

#host_portObject



164
165
166
# File 'lib/mongo/util/node.rb', line 164

def host_port
  [@host, @port]
end

#host_stringObject



44
45
46
# File 'lib/mongo/util/node.rb', line 44

def host_string
  address
end

#inspectObject



54
55
56
# File 'lib/mongo/util/node.rb', line 54

def inspect
  "<Mongo::Node:0x#{self.object_id.to_s(16)} @host=#{@host} @port=#{@port}>"
end

#max_bson_sizeObject



176
177
178
# File 'lib/mongo/util/node.rb', line 176

def max_bson_size
  @max_bson_size || DEFAULT_MAX_BSON_SIZE
end

#max_message_sizeObject



180
181
182
# File 'lib/mongo/util/node.rb', line 180

def max_message_size
  @max_message_size || max_bson_size * MESSAGE_SIZE_FACTOR
end

#node_listObject

Return a list of replica set nodes from the config. Note: this excludes arbiters.



137
138
139
140
141
142
143
# File 'lib/mongo/util/node.rb', line 137

def node_list
  nodes = []
  nodes += config['hosts'] if config['hosts']
  nodes += config['passives'] if config['passives']
  nodes += ["#{@host}:#{@port}"] if @client.mongos?
  nodes
end

#primary?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/mongo/util/node.rb', line 152

def primary?
  config['ismaster'] == true || config['ismaster'] == 1
end

#secondary?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/mongo/util/node.rb', line 156

def secondary?
  config['secondary'] == true || config['secondary'] == 1
end

#set_configObject

Get the configuration for the provided node as returned by the ismaster command. Additionally, check that the replica set name matches with the name provided.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mongo/util/node.rb', line 101

def set_config
  @node_mutex.synchronize do
    begin
      if @config
        @last_state = @config['ismaster'] ? :primary : :other
      end

      if @client.connect_timeout
        Timeout::timeout(@client.connect_timeout, OperationTimeout) do
          @config = @client['admin'].command({:ismaster => 1}, :socket => @socket)
        end
      else
        @config = @client['admin'].command({:ismaster => 1}, :socket => @socket)
      end

      update_max_sizes

      if @config['msg']
        @client.log(:warn, "#{config['msg']}")
      end

      unless @client.mongos?
        check_set_membership(@config)
        check_set_name(@config)
      end
    rescue ConnectionFailure, OperationFailure, OperationTimeout, SocketError, SystemCallError, IOError => ex
      @client.log(:warn, "Attempted connection to node #{host_string} raised " +
                          "#{ex.class}: #{ex.message}")
      # Socket may already be nil from issuing command
      close
    end
  end
end

#tagsObject



160
161
162
# File 'lib/mongo/util/node.rb', line 160

def tags
  config['tags'] || {}
end