Class: Fairy::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/fairy/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

Returns a new instance of Node.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fairy/node.rb', line 20

def initialize
  @id = nil
  @addr = nil
  @logger = nil

  @processor_seq = -1
  @processor_seq_mutex = Mutex.new

  @processors = []
  @processors_mutex = Mutex.new
  @processors_cv = ConditionVariable.new

  @active_processors = {}
  @active_processors_mutex = Mutex.new
  @active_processors_cv = ConditionVariable.new
end

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



38
39
40
# File 'lib/fairy/node.rb', line 38

def addr
  @addr
end

#idObject

Returns the value of attribute id.



37
38
39
# File 'lib/fairy/node.rb', line 37

def id
  @id
end

#loggerObject (readonly)

Returns the value of attribute logger.



39
40
41
# File 'lib/fairy/node.rb', line 39

def logger
  @logger
end

#processorsObject (readonly)

Returns the value of attribute processors.



41
42
43
# File 'lib/fairy/node.rb', line 41

def processors
  @processors
end

Class Method Details

.start(master_host, master_port) ⇒ Object



182
183
184
185
# File 'lib/fairy/node.rb', line 182

def Node.start(master_host, master_port)
  node = Node.new
  node.start(master_host, master_port)
end

Instance Method Details

#create_processorObject



79
80
81
82
83
84
85
86
87
88
89
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
# File 'lib/fairy/node.rb', line 79

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



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fairy/node.rb', line 141

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_idObject



43
44
45
# File 'lib/fairy/node.rb', line 43

def log_id
  "Node[#{@id}]"
end

#processor_next_idObject



73
74
75
76
77
# File 'lib/fairy/node.rb', line 73

def processor_next_id
  @processor_seq_mutex.synchronize do
	@processor_seq += 1
  end
end

#register_processor(processor) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/fairy/node.rb', line 130

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


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fairy/node.rb', line 53

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}]") 

  @master.register_node(self)
end

#terminate_processor(processor) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fairy/node.rb', line 117

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_sObject



178
179
180
# File 'lib/fairy/node.rb', line 178

def to_s
  "#<#{self.class}: #{id}>"
end

#update_processor_status(processor, st) ⇒ Object

process status management



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/fairy/node.rb', line 158

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