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.



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

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

.anchored_port_mapObject (readonly)

Returns the value of attribute anchored_port_map.



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

def anchored_port_map
  @anchored_port_map
end

.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.



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

def finale
  @finale
end

.monitorsObject (readonly)

Returns the value of attribute monitors.



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

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.



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

def agent_id
  @agent_id
end

#anchorableObject (readonly)

Returns the value of attribute anchorable.



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

def anchorable
  @anchorable
end

#child_pidObject (readonly)

Returns the value of attribute child_pid.



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

def child_pid
  @child_pid
end

#child_threadObject (readonly)

Returns the value of attribute child_thread.



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

def child_thread
  @child_thread
end

#rudderObject (readonly)

Returns the value of attribute rudder.



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

def rudder
  @rudder
end

Class Method Details

.abort_allObject



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

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

.add(anchoroable) ⇒ Object



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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 147

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
    @anchored_port_map.each_key do |ch|
      no_close_io[ch] = ch
      if ports != ""
        ports +=","
      end
      ports += ch.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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 219

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



287
288
289
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 287

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

.init(num_agents, anchored_port_map) ⇒ Object

Class methods



139
140
141
142
143
144
145
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 139

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

.int_to_buffer(val) ⇒ Object



291
292
293
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 291

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

.joinObject



246
247
248
249
250
251
252
253
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 246

def self.join
  while !@monitors.empty?
    @monitors.values.each do |mon|
      mon.child_thread.join
      sleep 5
    end
  end
end


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

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

.reload_cert_allObject



254
255
256
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 254

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

.restart_allObject



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

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

  #@agent_count.times {add()}

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

.shutdown_allObject



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

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

Instance Method Details

#abortObject



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

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

#closeObject



118
119
120
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 118

def close()
  @rudder.close()
end

#on_read_catch_upObject



122
123
124
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 122

def on_read_catch_up()

end

#on_readableObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 78

def on_readable()
  begin
    res = IOUtil.read_int32(@communication_channel)
    if res == nil || res == GrandAgent::CMD_CLOSE
      close()
    else
      BayLog.debug("%s read OK: %d", self, res)
    end
  rescue IO::WaitReadable
    #BayLog.debug("%s no data", self)
  end
end


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

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

#reload_certObject



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

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

#req_catch_upObject



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

def req_catch_up()

end

#runObject



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
75
76
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 46

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



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

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



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

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

#startObject



129
130
131
132
133
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 129

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

#to_sObject



42
43
44
# File 'lib/baykit/bayserver/agent/monitor/grand_agent_monitor.rb', line 42

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