Class: Fairy::Node
- Inherits:
-
Object
- Object
- Fairy::Node
- Defined in:
- lib/fairy/node.rb
Instance Attribute Summary collapse
-
#addr ⇒ Object
Returns the value of attribute addr.
-
#id ⇒ Object
Returns the value of attribute id.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#processors ⇒ Object
readonly
Returns the value of attribute processors.
Class Method Summary collapse
Instance Method Summary collapse
- #create_processor ⇒ Object
- #deregister_processor(processor) ⇒ Object
-
#initialize ⇒ Node
constructor
A new instance of Node.
- #log_id ⇒ Object
- #processor_next_id ⇒ Object
- #register_processor(processor) ⇒ Object
-
#start(master_host, master_port, service = 0) ⇒ Object
def processors_dup @processors.synchronize do @processors.dup end end.
- #terminate_processor(processor) ⇒ Object
- #to_s ⇒ Object
-
#update_processor_status(processor, st) ⇒ Object
process status management.
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fairy/node.rb', line 24 def initialize @id = nil @addr = nil @logger = nil @processor_seq = -1 @processor_seq_mutex = Mutex.new @processors = [] @processors_mutex = Mutex.new @processors_cv = XThread::ConditionVariable.new @active_processors = {} @active_processors_mutex = Mutex.new @active_processors_cv = XThread::ConditionVariable.new end |
Instance Attribute Details
#addr ⇒ Object
Returns the value of attribute addr.
42 43 44 |
# File 'lib/fairy/node.rb', line 42 def addr @addr end |
#id ⇒ Object
Returns the value of attribute id.
41 42 43 |
# File 'lib/fairy/node.rb', line 41 def id @id end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
43 44 45 |
# File 'lib/fairy/node.rb', line 43 def logger @logger end |
#processors ⇒ Object (readonly)
Returns the value of attribute processors.
45 46 47 |
# File 'lib/fairy/node.rb', line 45 def processors @processors end |
Class Method Details
.start(master_host, master_port) ⇒ Object
193 194 195 196 |
# File 'lib/fairy/node.rb', line 193 def Node.start(master_host, master_port) node = Node.new node.start(master_host, master_port) end |
Instance Method Details
#create_processor ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 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 |
# File 'lib/fairy/node.rb', line 90 def create_processor proc = nil @processors_mutex.synchronize do processor_id = processor_next_id # Process.spawn("test/testn.rb", # "--controller", @deepconnect.local_id, # "--id", processor_id.to_s) # pid = Process.fork{ # Process.fork{ # exec(CONF.RUBY_BIN, CONF.PROCESSOR_BIN, # "--node", @deepconnect.local_id.to_s, # "--id", processor_id.to_s) # } # } # Process.wait pid pid = NodeAPP.start_subcommand2(CONF.RUBY_BIN, CONF.PROCESSOR_BIN, "--node", @deepconnect.local_id.to_s, "--id", processor_id.to_s) Process.wait pid begin timeout(CONF.SUBCMD_EXEC_TIMEOUT) do while !@processors[processor_id] @processors_cv.wait(@processors_mutex) end end rescue Timeout::Error Log::fatal(self, "Can't exec Processor") ERR::Fail ERR::CantExecSubcmd, "processor" end @master.set_no_of_processors(self, @processors.size) @processors[processor_id] end end |
#deregister_processor(processor) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/fairy/node.rb', line 152 def deregister_processor(processor) # @processors.synchronize do update_processor_status(processor, :ST_WAIT) @processors_mutex.synchronize do # ↓パフォーマンス悪い!! @processors.delete(processor.id) @master.set_no_of_processors(self, @processors.size) @processors_cv.broadcast end end |
#log_id ⇒ Object
47 48 49 |
# File 'lib/fairy/node.rb', line 47 def log_id "Node[#{@id}]" end |
#processor_next_id ⇒ Object
84 85 86 87 88 |
# File 'lib/fairy/node.rb', line 84 def processor_next_id @processor_seq_mutex.synchronize do @processor_seq += 1 end end |
#register_processor(processor) ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fairy/node.rb', line 141 def register_processor(processor) # @processors.synchronize do @processors_mutex.synchronize do # ↓パフォーマンス悪い!! @processors[processor.id] = processor processor.addr = @addr @processors_cv.broadcast end end |
#start(master_host, master_port, service = 0) ⇒ Object
def processors_dup
@processors.synchronize do
@processors.dup
end
end
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/fairy/node.rb', line 57 def start(master_host, master_port, service=0) @deepconnect = DeepConnect.start(service) @deepconnect.export("Node", self) require "fairy/share/inspector" @deepconnect.export("Inspector", Inspector.new(self)) require "fairy/share/log" @master_deepspace = @deepconnect.open_deepspace(master_host, master_port) @master = @master_deepspace.import("Master") @logger = @master.logger Log.type = "[N]" Log.logger = @logger Log.info(self, "Node Service Start") Log::info(self, "\tfairy version: #{Version}") Log::info(self, "\t[Powered BY #{RUBY_DESCRIPTION}]") begin require "fairy.so" Log::warn self, "\t Load fairy.so" rescue LoadError Log::warn self, "Can't load fairy.so. Can't use this feature" end @master.register_node(self) end |
#terminate_processor(processor) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fairy/node.rb', line 128 def terminate_processor(processor) deregister_processor(processor) begin Log::info(self, "terminate processor.") processor.terminate rescue Log::debug(self, "Exception Rised in termination processor.") Log::debug_exception(self) end # forkの仕組みが変わった. # Process.wait end |
#to_s ⇒ Object
189 190 191 |
# File 'lib/fairy/node.rb', line 189 def to_s "#<#{self.class}: #{id}>" end |
#update_processor_status(processor, st) ⇒ Object
process status management
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/fairy/node.rb', line 169 def update_processor_status(processor, st) Log::debug(self, "UPDATE_PROCESSOR_STATUS S: #{processor} #{st}") @active_processors_mutex.synchronize do case st when :ST_WAIT, :ST_SEMIACTIVATE, :ST_FINISH Log::debug(self, "UPDATE_PROCESSOR_STATUS: 2") if @active_processors.key?(processor) Log::debug(self, "UPDATE_PROCESSOR_STATUS: 3") @active_processors.delete(processor) @master.set_no_of_active_processors(self, @active_processors.size) end when :ST_ACTIVATE Log::debug(self, "UPDATE_PROCESSOR_STATUS: 4") @active_processors[processor] = processor @master.set_no_of_active_processors(self, @active_processors.size) end end Log::debug(self, "UPDATE_PROCESSOR_STATUS: E") end |