Class: Baykit::BayServer::Agent::Monitor::GrandAgentMonitor

Inherits:
Object
  • Object
show all
Includes:
Rudders, Util
Defined in:
lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agt_id, anchorable, rd, child_thread, child_pid) ⇒ GrandAgentMonitor

Returns a new instance of GrandAgentMonitor.



32
33
34
35
36
37
38
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 32

def initialize(agt_id, anchorable, rd, child_thread, child_pid)
  @agent_id = agt_id
  @anchorable = anchorable
  @rudder = rd
  @child_thread = child_thread
  @child_pid = child_pid
end

Class Attribute Details

.cur_idObject (readonly)

Returns the value of attribute cur_id.



16
17
18
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 16

def cur_id
  @cur_id
end

.finaleObject (readonly)

Returns the value of attribute finale.



18
19
20
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 18

def finale
  @finale
end

.monitorsObject (readonly)

Returns the value of attribute monitors.



17
18
19
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 17

def monitors
  @monitors
end

.num_agentsObject (readonly)

Returns the value of attribute num_agents.



15
16
17
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 15

def num_agents
  @num_agents
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



26
27
28
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 26

def agent_id
  @agent_id
end

#anchorableObject (readonly)

Returns the value of attribute anchorable.



27
28
29
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 27

def anchorable
  @anchorable
end

#child_pidObject (readonly)

Returns the value of attribute child_pid.



30
31
32
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 30

def child_pid
  @child_pid
end

#child_threadObject (readonly)

Returns the value of attribute child_thread.



29
30
31
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 29

def child_thread
  @child_thread
end

#rudderObject (readonly)

Returns the value of attribute rudder.



28
29
30
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 28

def rudder
  @rudder
end

Class Method Details

.abort_allObject



271
272
273
274
275
276
277
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 271

def self.abort_all()
  @finale = true
  @monitors.dup().values.each do |mon|
    mon.abort()
  end
  exit(1)
end

.add(anchoroable) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 131

def self.add(anchoroable)
  @cur_id = @cur_id + 1
  agt_id = @cur_id
  if agt_id > 100
    BayLog.error("Too many agents started")
    exit(1)
  end

  if BayServer.harbor.multi_core
    new_argv = BayServer.commandline_args.dup
    new_argv.insert(0, "ruby")
    new_argv << "-agentid=" + agt_id.to_s

    ports = ""

    no_close_io = {}  # Port list not to close on spawned
    BayServer.anchorable_port_map.each_key do |rd|
      no_close_io[rd.key()] = rd.key()
      if ports != ""
        ports +=","
      end
      ports += rd.key().fileno.to_s
    end
    new_argv << "-ports=" + ports

    server = TCPServer.open("localhost", 0)
    #BayLog.info("port=%d", server.local_address.ip_port)
    new_argv << "-monitor_port=" + server.local_address.ip_port.to_s

    if SysUtil.run_on_windows()
      child_pid = spawn(ENV, new_argv.join(" "))
    else
      child_pid = spawn(ENV, new_argv.join(" "), no_close_io)
    end

    BayLog.debug("Process spawned cmd=%s pid=%d", new_argv, child_pid)

    client_socket = server.accept()
    server.close()

  else

    if SysUtil::run_on_windows()
      pair = Socket.socketpair(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    else
      pair = Socket.socketpair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
    end

    client_socket = pair[0]
    GrandAgent.add(agt_id, anchoroable)

    # Agents run on single core (thread mode)
    child_thread = Thread.new() do
      agt = GrandAgent.get(agt_id)
      agt.add_command_receiver(IORudder.new(pair[1]))
      agt.start()
    end

  end

  mon =
    GrandAgentMonitor.new(
      agt_id,
      anchoroable,
      IORudder.new(client_socket),
      child_thread,
      child_pid)
  @monitors[agt_id] = mon
  mon.start

end

.agent_aborted(agt_id, pid, anchorable) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 203

def self.agent_aborted(agt_id, pid, anchorable)
  BayLog.info(BayMessage.get(:MSG_GRAND_AGENT_SHUTDOWN, agt_id))

  if pid != nil
    begin
      Process.kill("TERM", pid)
    rescue => e
      BayLog.debug_e(e, "Error on killing process")
    end
    Process.wait(pid)
  end
  @monitors.delete(agt_id)

  if not @finale
    if @monitors.length < @num_agents
      begin
        if !BayServer.harbor.multi_core
          GrandAgent.add(-1, @anchorable)
        end
        self.add(@anchorable)
      rescue => e
        BayLog.error_e(e)
      end
    end
  end
end

.buffer_to_int(buf) ⇒ Object



285
286
287
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 285

def self.buffer_to_int(buf)
  return buf.unpack("N").first
end

.init(num_agents) ⇒ Object

Class methods



124
125
126
127
128
129
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 124

def self.init(num_agents)
  @num_agents = num_agents
  @num_agents.times do
    add(true)
  end
end

.int_to_buffer(val) ⇒ Object



289
290
291
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 289

def self.int_to_buffer(val)
  return [val].pack("N")
end

.joinObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 230

def self.join
  while !@monitors.empty?
    err = nil
    @monitors.values.each do |mon|
      begin
        mon.child_thread.join
        sleep 5
      rescue Interrupt => e
        err = e
        BayLog.error("%s Interrupted! Kill child: %d", mon, mon.child_pid)
        begin
          Process.kill("INT", mon.child_pid)
        rescue SystemCallError => e
          BayLog.error("%s Failed to kill child: %s", mon, e)
        end
      end
    end
    if err
      raise err
    end
  end
end


279
280
281
282
283
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 279

def self.print_usage_all()
  @monitors.values.each do |mon|
    mon.print_usage()
  end
end

.reload_cert_allObject



252
253
254
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 252

def self.reload_cert_all()
  @monitors.values.each { |mon| mon.reload_cert() }
end

.restart_allObject



256
257
258
259
260
261
262
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 256

def self.restart_all()
  old_monitors = @monitors.dup()

  #@agent_count.times {add()}

  old_monitors.values.each { |mon| mon.shutdown() }
end

.shutdown_allObject



264
265
266
267
268
269
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 264

def self.shutdown_all()
  @finale = true
  @monitors.dup().values.each do |mon|
    mon.shutdown()
  end
end

Instance Method Details

#abortObject



81
82
83
84
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 81

def abort()
  BayLog.debug("%s Send abort command", self)
  send(GrandAgent::CMD_ABORT)
end

#closeObject



103
104
105
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 103

def close()
  @rudder.close()
end

#on_read_catch_upObject



107
108
109
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 107

def on_read_catch_up()

end


91
92
93
94
95
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 91

def print_usage()
  BayLog.debug("%s Send mem_usage command", self)
  send(GrandAgent::CMD_MEM_USAGE)
  sleep(0.5)  # Lazy implementation
end

#reload_certObject



86
87
88
89
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 86

def reload_cert()
  BayLog.debug("%s Send reload command", self)
  send(GrandAgent::CMD_RELOAD_CERT)
end

#req_catch_upObject



110
111
112
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 110

def req_catch_up()

end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 44

def run()
  begin
    while true do
      buf = " " * 4

      n = @rudder.read(buf, 4)
      if n == -1
        raise EOFError.new
      end
      if n < 4
        raise IOError.new("Cannot read int: nbytes=#{n}")
      end
      res = buffer_to_int(buf)
      if res == GrandAgent::CMD_CLOSE
        BayLog.debug("%s read Close", self)
        break
      elsif res == GrandAgent::CMD_CATCHUP
        on_read_catch_up
      else
        BayLog.debug("%s read OK: %d", self, res);
      end

    end
  rescue EOFError, IOError => e
    BayLog.fatal("%s Agent terminated", self)
  rescue Exception => e
    BayLog.fatal_e(e)
  end

  GrandAgentMonitor.agent_aborted @agent_id, @child_pid, @anchorable
end

#send(cmd) ⇒ Object



97
98
99
100
101
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 97

def send(cmd)
  BayLog.debug("%s send command %s rd=%s", self, cmd, @rudder)
  buf = GrandAgentMonitor.int_to_buffer(cmd)
  @rudder.write(buf)
end

#shutdownObject



76
77
78
79
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 76

def shutdown()
  BayLog.debug("%s send shutdown command", self)
  send(GrandAgent::CMD_SHUTDOWN)
end

#startObject



114
115
116
117
118
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 114

def start
  @child_thread = Thread.new do
    run()
  end
end

#to_sObject



40
41
42
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 40

def to_s()
  return "Monitor##{@agent_id}"
end