fairy 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. data/LICENSE +674 -0
  2. data/Makefile +116 -0
  3. data/README +15 -0
  4. data/bin/fairy +582 -0
  5. data/bin/fairy-cat +74 -0
  6. data/bin/fairy-cp +128 -0
  7. data/bin/fairy-rm +122 -0
  8. data/bin/subcmd/controller +41 -0
  9. data/bin/subcmd/inspector +81 -0
  10. data/bin/subcmd/master +43 -0
  11. data/bin/subcmd/node +47 -0
  12. data/bin/subcmd/processor +54 -0
  13. data/doc/programming-interface.html +240 -0
  14. data/doc/programming-interface.rd +300 -0
  15. data/etc/fairy.conf.tmpl +118 -0
  16. data/ext/simple_hash/extconf.rb +4 -0
  17. data/ext/simple_hash/simple_hash.c +42 -0
  18. data/fairy.gemspec +60 -0
  19. data/lib/fairy/client/addins.rb +20 -0
  20. data/lib/fairy/client/barrier.rb +29 -0
  21. data/lib/fairy/client/basic-group-by.rb +52 -0
  22. data/lib/fairy/client/cat.rb +41 -0
  23. data/lib/fairy/client/direct-product.rb +51 -0
  24. data/lib/fairy/client/equijoin.rb +79 -0
  25. data/lib/fairy/client/exec.rb +54 -0
  26. data/lib/fairy/client/filter.rb +62 -0
  27. data/lib/fairy/client/find.rb +35 -0
  28. data/lib/fairy/client/group-by.rb +194 -0
  29. data/lib/fairy/client/here.rb +84 -0
  30. data/lib/fairy/client/inject.rb +70 -0
  31. data/lib/fairy/client/input-file.rb +53 -0
  32. data/lib/fairy/client/input-iota.rb +49 -0
  33. data/lib/fairy/client/input-local-file.rb +188 -0
  34. data/lib/fairy/client/input-varray.rb +30 -0
  35. data/lib/fairy/client/input.rb +42 -0
  36. data/lib/fairy/client/io-filter.rb +26 -0
  37. data/lib/fairy/client/junction.rb +31 -0
  38. data/lib/fairy/client/map.rb +34 -0
  39. data/lib/fairy/client/merge-group-by.rb +71 -0
  40. data/lib/fairy/client/output-file.rb +64 -0
  41. data/lib/fairy/client/output-local-file.rb +60 -0
  42. data/lib/fairy/client/output-null.rb +47 -0
  43. data/lib/fairy/client/output-varray.rb +50 -0
  44. data/lib/fairy/client/output.rb +29 -0
  45. data/lib/fairy/client/roma-put.rb +62 -0
  46. data/lib/fairy/client/roma.rb +156 -0
  47. data/lib/fairy/client/seg-join.rb +61 -0
  48. data/lib/fairy/client/seg-map.rb +78 -0
  49. data/lib/fairy/client/seg-shuffle.rb +35 -0
  50. data/lib/fairy/client/seg-split.rb +27 -0
  51. data/lib/fairy/client/seg-zip.rb +60 -0
  52. data/lib/fairy/client/select.rb +38 -0
  53. data/lib/fairy/client/sort.rb +48 -0
  54. data/lib/fairy/client/sort18.rb +56 -0
  55. data/lib/fairy/client/sort19.rb +61 -0
  56. data/lib/fairy/client/there.rb +47 -0
  57. data/lib/fairy/client/top_n_into_roma.rb +34 -0
  58. data/lib/fairy/client/wc.rb +92 -0
  59. data/lib/fairy/controller.rb +1103 -0
  60. data/lib/fairy/logger.rb +107 -0
  61. data/lib/fairy/master/addins.rb +20 -0
  62. data/lib/fairy/master/atom.rb +17 -0
  63. data/lib/fairy/master/c-barrier.rb +283 -0
  64. data/lib/fairy/master/c-basic-group-by.rb +250 -0
  65. data/lib/fairy/master/c-cat.rb +159 -0
  66. data/lib/fairy/master/c-direct-product.rb +203 -0
  67. data/lib/fairy/master/c-exec.rb +68 -0
  68. data/lib/fairy/master/c-filter.rb +422 -0
  69. data/lib/fairy/master/c-find.rb +138 -0
  70. data/lib/fairy/master/c-group-by.rb +64 -0
  71. data/lib/fairy/master/c-here.rb +80 -0
  72. data/lib/fairy/master/c-inject.rb +119 -0
  73. data/lib/fairy/master/c-input-file.rb +46 -0
  74. data/lib/fairy/master/c-input-iota.rb +66 -0
  75. data/lib/fairy/master/c-input-local-file.rb +117 -0
  76. data/lib/fairy/master/c-input-varray.rb +53 -0
  77. data/lib/fairy/master/c-input.rb +24 -0
  78. data/lib/fairy/master/c-inputtable.rb +31 -0
  79. data/lib/fairy/master/c-inputtable18.rb +36 -0
  80. data/lib/fairy/master/c-inputtable19.rb +35 -0
  81. data/lib/fairy/master/c-io-filter.rb +28 -0
  82. data/lib/fairy/master/c-junction.rb +54 -0
  83. data/lib/fairy/master/c-map.rb +27 -0
  84. data/lib/fairy/master/c-merge-group-by.rb +241 -0
  85. data/lib/fairy/master/c-output-file.rb +84 -0
  86. data/lib/fairy/master/c-output-local-file.rb +19 -0
  87. data/lib/fairy/master/c-output-null.rb +45 -0
  88. data/lib/fairy/master/c-output-varray.rb +57 -0
  89. data/lib/fairy/master/c-output.rb +20 -0
  90. data/lib/fairy/master/c-seg-join.rb +141 -0
  91. data/lib/fairy/master/c-seg-map.rb +26 -0
  92. data/lib/fairy/master/c-seg-shuffle.rb +87 -0
  93. data/lib/fairy/master/c-seg-split.rb +110 -0
  94. data/lib/fairy/master/c-seg-zip.rb +132 -0
  95. data/lib/fairy/master/c-select.rb +27 -0
  96. data/lib/fairy/master/c-sort.rb +108 -0
  97. data/lib/fairy/master/c-there.rb +57 -0
  98. data/lib/fairy/master/c-wc.rb +232 -0
  99. data/lib/fairy/master/job-interpriter.rb +19 -0
  100. data/lib/fairy/master/scheduler.rb +24 -0
  101. data/lib/fairy/master.rb +329 -0
  102. data/lib/fairy/node/addins.rb +19 -0
  103. data/lib/fairy/node/p-barrier.rb +95 -0
  104. data/lib/fairy/node/p-basic-group-by.rb +252 -0
  105. data/lib/fairy/node/p-direct-product.rb +153 -0
  106. data/lib/fairy/node/p-exec.rb +30 -0
  107. data/lib/fairy/node/p-filter.rb +363 -0
  108. data/lib/fairy/node/p-find.rb +111 -0
  109. data/lib/fairy/node/p-group-by.rb +1534 -0
  110. data/lib/fairy/node/p-here.rb +21 -0
  111. data/lib/fairy/node/p-identity.rb +24 -0
  112. data/lib/fairy/node/p-inject.rb +127 -0
  113. data/lib/fairy/node/p-input-file.rb +108 -0
  114. data/lib/fairy/node/p-input-iota.rb +39 -0
  115. data/lib/fairy/node/p-input-local-file.rb +61 -0
  116. data/lib/fairy/node/p-input-varray.rb +26 -0
  117. data/lib/fairy/node/p-io-filter.rb +28 -0
  118. data/lib/fairy/node/p-map.rb +40 -0
  119. data/lib/fairy/node/p-merger-group-by.rb +48 -0
  120. data/lib/fairy/node/p-output-file.rb +104 -0
  121. data/lib/fairy/node/p-output-local-file.rb +14 -0
  122. data/lib/fairy/node/p-output-null.rb +32 -0
  123. data/lib/fairy/node/p-output-varray.rb +41 -0
  124. data/lib/fairy/node/p-seg-join.rb +82 -0
  125. data/lib/fairy/node/p-seg-map.rb +34 -0
  126. data/lib/fairy/node/p-seg-split.rb +61 -0
  127. data/lib/fairy/node/p-seg-zip.rb +79 -0
  128. data/lib/fairy/node/p-select.rb +40 -0
  129. data/lib/fairy/node/p-single-exportable.rb +90 -0
  130. data/lib/fairy/node/p-sort.rb +195 -0
  131. data/lib/fairy/node/p-task.rb +113 -0
  132. data/lib/fairy/node/p-there.rb +44 -0
  133. data/lib/fairy/node/p-wc.rb +266 -0
  134. data/lib/fairy/node.rb +187 -0
  135. data/lib/fairy/processor.rb +510 -0
  136. data/lib/fairy/share/base-app.rb +114 -0
  137. data/lib/fairy/share/block-source.rb +234 -0
  138. data/lib/fairy/share/conf.rb +396 -0
  139. data/lib/fairy/share/debug.rb +21 -0
  140. data/lib/fairy/share/encoding.rb +17 -0
  141. data/lib/fairy/share/fast-tempfile.rb +93 -0
  142. data/lib/fairy/share/file-place.rb +176 -0
  143. data/lib/fairy/share/hash-1.rb +20 -0
  144. data/lib/fairy/share/hash-md5.rb +28 -0
  145. data/lib/fairy/share/hash-murmur.rb +69 -0
  146. data/lib/fairy/share/hash-rb18.rb +20 -0
  147. data/lib/fairy/share/hash-simple-hash.rb +28 -0
  148. data/lib/fairy/share/inspector.rb +16 -0
  149. data/lib/fairy/share/lc/exceptions.rb +82 -0
  150. data/lib/fairy/share/lc/ja/exceptions.rb +81 -0
  151. data/lib/fairy/share/locale.rb +17 -0
  152. data/lib/fairy/share/log.rb +215 -0
  153. data/lib/fairy/share/pool-dictionary.rb +53 -0
  154. data/lib/fairy/share/port-marshaled-queue.rb +347 -0
  155. data/lib/fairy/share/port.rb +1697 -0
  156. data/lib/fairy/share/reference.rb +45 -0
  157. data/lib/fairy/share/stdout.rb +56 -0
  158. data/lib/fairy/share/tr.rb +16 -0
  159. data/lib/fairy/share/varray.rb +147 -0
  160. data/lib/fairy/share/vfile.rb +183 -0
  161. data/lib/fairy/version.rb +8 -0
  162. data/lib/fairy.rb +206 -0
  163. data/sample/grep.rb +46 -0
  164. data/sample/ping.rb +19 -0
  165. data/sample/sort.rb +102 -0
  166. data/sample/wordcount.rb +61 -0
  167. data/spec/README +12 -0
  168. data/spec/fairy1_spec.rb +31 -0
  169. data/spec/fairy2_spec.rb +42 -0
  170. data/spec/fairy3_spec.rb +126 -0
  171. data/spec/fairy4_spec.rb +63 -0
  172. data/spec/fairy5_spec.rb +45 -0
  173. data/spec/fairy6_spec.rb +52 -0
  174. data/spec/fairy7_spec.rb +58 -0
  175. data/spec/fairy8_spec.rb +48 -0
  176. data/spec/mkdat.rb +148 -0
  177. data/spec/run_all.sh +65 -0
  178. data/test/testc.rb +7111 -0
  179. data/tools/cap_recipe/Capfile +144 -0
  180. data/tools/cap_recipe/cluster.yml.sample +14 -0
  181. data/tools/fairy_perf_graph.rb +444 -0
  182. data/tools/git-tag +44 -0
  183. data/tools/log-analysis.rb +62 -0
  184. data/tools/svn-ls-diff +38 -0
  185. data/tools/svn-tags +37 -0
  186. metadata +298 -0
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (C) 2007-2010 Rakuten, Inc.
4
+ #
5
+
6
+ module Fairy
7
+ class Scheduler
8
+
9
+ def initialize(controller)
10
+ # 取りあえず
11
+ @controller = controller
12
+ #@processors = [Processor.new]
13
+ end
14
+
15
+
16
+ def schedule(atom)
17
+ # 取りあえず.
18
+ # ここで, atomにはノード指定がすでにあるとしている.
19
+ # 指定されたノードにスケジュールする.
20
+ @processors[0].exec atom
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,329 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (C) 2007-2010 Rakuten, Inc.
4
+ #
5
+
6
+ require "thread"
7
+ require "resolv"
8
+ require "ipaddr"
9
+
10
+ require "deep-connect"
11
+ #DeepConnect::Organizer.immutable_classes.push Array
12
+
13
+ require "fairy/version"
14
+ require "fairy/share/conf"
15
+ require "fairy/logger"
16
+
17
+ module Fairy
18
+
19
+ class Master
20
+ def initialize
21
+
22
+ # @clients = {}
23
+ # @clients_mutex = Mutex.new
24
+ # @clients_cv = ConditionVariable.new
25
+
26
+ @controller_seq = -1
27
+ @controller_seq_mutex = Mutex.new
28
+
29
+ @controllers = {}
30
+ @controllers_mutex = Mutex.new
31
+ @controllers_cv = ConditionVariable.new
32
+
33
+ # @clientds2controller = {}
34
+
35
+ @nodes = {}
36
+ @nodes_mutex = Mutex.new
37
+ @node_seq = -1
38
+
39
+ @no_of_processors = {}
40
+ @no_of_processors_mutex = Mutex.new
41
+
42
+ @no_of_active_processors = {}
43
+ @no_of_active_processors_mutex = Mutex.new
44
+ @no_of_active_processors_cv = ConditionVariable.new
45
+
46
+ end
47
+
48
+ attr_reader :controllers
49
+ attr_reader :nodes
50
+
51
+ attr_reader :logger
52
+
53
+ def log_id
54
+ "Master"
55
+ end
56
+
57
+ def start(service)
58
+ @deepconnect = DeepConnect.start(service)
59
+ @deepconnect.export("Master", self)
60
+
61
+ require "fairy/share/inspector"
62
+ @deepconnect.export("Inspector", Inspector.new(self))
63
+
64
+
65
+ require "fairy/share/log"
66
+ @logger = Logger.new
67
+ Log.logger = @logger
68
+ Log.type = "[M]"
69
+
70
+ @deepconnect.when_disconnected do |deepspace, opts|
71
+ when_disconnected(deepspace, opts)
72
+ end
73
+
74
+ Log.info(self, "Master Service Start")
75
+ Log::info(self, "\tfairy version: #{Version}")
76
+ Log::info(self, "\t[Powerd By #{RUBY_DESCRIPTION}]")
77
+ end
78
+
79
+ def when_disconnected(deepspace, opts)
80
+ Log::debug self, "MASTER: disconnected: Start termination"
81
+ # @controllers_mutex.synchronize do
82
+ # if c = @controllers.find{|c| c.deep_space == deepspace}
83
+ # when_disconnected_controller(c, deepspace, opts)
84
+ # end
85
+ # end
86
+
87
+ # node
88
+
89
+ @nodes_mutex.synchronize do
90
+ if addr_node= @nodes.find{|addr, node| node.deep_space == deepspace}
91
+ Log::info self, "MASTER: disconnected NODE: start termination"
92
+ when_disconnected_node(addr_node[0], addr_node[1], opts)
93
+ end
94
+ end
95
+ end
96
+
97
+ # Controller 関連メソッド
98
+
99
+ def controller_next_id
100
+ @controller_seq_mutex.synchronize do
101
+ @controller_seq += 1
102
+ end
103
+ end
104
+
105
+ def assgin_controller
106
+
107
+ # @clients_mutex.synchronize do
108
+ # @clients[fairy.deep_space] = fairy
109
+ # end
110
+
111
+ Log::debug(self, "Assgin Controller")
112
+ @controllers_mutex.synchronize do
113
+ controller_id = controller_next_id
114
+ MasterAPP.start_subcommand(CONF.RUBY_BIN,
115
+ CONF.CONTROLLER_BIN,
116
+ "--master", @deepconnect.local_id.to_s,
117
+ "--id", controller_id.to_s)
118
+ begin
119
+ timeout(CONF.SUBCMD_EXEC_TIMEOUT) do
120
+ while !@controllers[controller_id]
121
+ @controllers_cv.wait(@controllers_mutex)
122
+ end
123
+ end
124
+ rescue Timeout::Error
125
+ Log::fatal(self, "Can't exec Controller")
126
+ ERR::Fail ERR::CantExecSubcmd, "controller"
127
+ end
128
+
129
+ # @clientds2controller[fairy.deep_space] = @controllers[controller_id]
130
+ Log::debug(self, "Assgin Controller: Assgined")
131
+ @controllers[controller_id]
132
+ end
133
+ end
134
+
135
+ def register_controller(controller)
136
+ @controllers_mutex.synchronize do
137
+ @controllers[controller.id] = controller
138
+ @controllers_cv.broadcast
139
+ end
140
+ end
141
+
142
+ def terminate_controller(controller)
143
+ @controllers_mutex.synchronize do
144
+ @controllers.delete(controller.id)
145
+ @controllers_cv.broadcast
146
+ end
147
+
148
+ begin
149
+ controller.terminate
150
+ Process.wait
151
+ rescue
152
+ p $!, $@
153
+ end
154
+ end
155
+
156
+ #
157
+ def set_no_of_processors(node, no)
158
+ @no_of_processors_mutex.synchronize do
159
+ @no_of_processors[node] = no
160
+ end
161
+ end
162
+
163
+ #
164
+ def set_no_of_active_processors(node, no)
165
+ @no_of_active_processors_mutex.synchronize do
166
+ Log::debug(self, "CHANGE ACTIVE PROCESSORS: #{node}->#{no}")
167
+ @no_of_active_processors[node] = no
168
+ @no_of_active_processors_cv.broadcast
169
+ end
170
+ end
171
+
172
+ def node_in_reisured(host)
173
+ Log::debug(self, "NODE IN LAISURED S:")
174
+ node = node(host)
175
+
176
+ return nil unless node
177
+
178
+ @no_of_active_processors_mutex.synchronize do
179
+ while @no_of_active_processors[node] > CONF.MASTER_MAX_ACTIVE_PROCESSORS
180
+ Log::debug(self, "NODE IN LAISURED 1: WAITING")
181
+ @no_of_active_processors_cv.wait(@no_of_active_processors_mutex)
182
+ Log::debug(self, "NODE IN LAISURED 2: WAITING END")
183
+ end
184
+ Log::debug(self, "NODE IN LAISURED E:")
185
+ node
186
+ end
187
+ end
188
+
189
+ def node_in_reisured_without_block(host)
190
+ node = node(host)
191
+ @no_of_active_processors_mutex.synchronize do
192
+ if @no_of_active_processors[node] > CONF.MASTER_MAX_ACTIVE_PROCESSORS
193
+ return false
194
+ end
195
+ end
196
+ node
197
+ end
198
+
199
+ def leisured_node(blocking = true)
200
+ Log::debug(self, "LAISURED NODE S:")
201
+ @no_of_active_processors_mutex.synchronize do
202
+ loop do
203
+ min_node = nil
204
+ min_no_processor = nil
205
+ for uuid, node in @nodes.dup
206
+ no = @no_of_active_processors[node]
207
+ if !min_no_processor or min_no_processor > no
208
+ min_no_processor = no
209
+ min_node = node
210
+ end
211
+ end
212
+ if min_no_processor <= CONF.MASTER_MAX_ACTIVE_PROCESSORS
213
+ Log::debug(self, "LAISURED NODE E:")
214
+ return min_node
215
+ end
216
+ if blocking
217
+ Log::debug(self, "LAISURED NODE 1 WAITING:")
218
+ @no_of_active_processors_cv.wait(@no_of_active_processors_mutex)
219
+ Log::debug(self, "LAISURED NODE 2 WAITING END:")
220
+ else
221
+ Log::debug(self, "LAISURED NODE E:")
222
+ return nil
223
+ end
224
+ end
225
+ end
226
+ end
227
+
228
+ def leisured_node_except_nodes(except_nodes = [], blocking = true)
229
+ Log::debug(self, "LAISURED NODE S:")
230
+ @no_of_active_processors_mutex.synchronize do
231
+ loop do
232
+ min_node = nil
233
+ min_no_processor = nil
234
+ for uuid, node in @nodes.dup
235
+ next if except_nodes.include?(node)
236
+
237
+ no = @no_of_active_processors[node]
238
+ if !min_no_processor or min_no_processor > no
239
+ min_no_processor = no
240
+ min_node = node
241
+ end
242
+ end
243
+ if min_node && min_no_processor <= CONF.MASTER_MAX_ACTIVE_PROCESSORS
244
+ Log::debug(self, "LAISURED NODE E:")
245
+ return min_node
246
+ end
247
+ if blocking
248
+ Log::debug(self, "LAISURED NODE 1 WAITING:")
249
+ @no_of_active_processors_cv.wait(@no_of_active_processors_mutex)
250
+ Log::debug(self, "LAISURED NODE 2 WAITING END:")
251
+ else
252
+ return nil
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ def unlimited_leisured_node
259
+ min_node = nil
260
+ min_no_processor = nil
261
+ for uuid, node in @nodes.dup
262
+ # no = nil
263
+ # @no_of_processors_mutex.synchronize do
264
+ no = @no_of_active_processors[node]
265
+ # end
266
+ if !min_no_processor or min_no_processor > no
267
+ min_no_processor = no
268
+ min_node = node
269
+ end
270
+ end
271
+ min_node
272
+ end
273
+
274
+ # Node 関連メソッド
275
+ def register_node(node)
276
+ @nodes_mutex.synchronize do
277
+ @node_seq += 1
278
+ @no_of_processors[node] = 0
279
+ @no_of_active_processors[node] = 0
280
+
281
+ addr = node.deep_space.peer_uuid[0]
282
+ @nodes[addr] = node
283
+ Log::info self, "Node added: #{addr}->#{node}##{@node_seq}"
284
+ node.id = @node_seq
285
+ node.addr = addr
286
+ end
287
+ end
288
+
289
+ # IPv4(ipv6map) または IPv6アドレスか?
290
+ IPADDR_REGEXP = /(::ffff:)?([0-9]+\.){3}[0-9]+|[0-9a-f]+:([0-9a-f]*:)[0-9a-f]*/
291
+
292
+ def node(host)
293
+ #puts "HOST: #{host}"
294
+ unless IPADDR_REGEXP =~ host
295
+ Resolv.each_address(host) do |addr|
296
+ ipaddr = IPAddr.new(addr)
297
+ # ipaddr = ipaddr.ipv4_mapped if ipaddr.ipv4?
298
+ ipaddr = ipaddr.native
299
+ host = ipaddr.to_s
300
+
301
+ @nodes_mutex.synchronize do
302
+ if n = @nodes[host]
303
+ return n
304
+ end
305
+ end
306
+ end
307
+
308
+ return nil
309
+ end
310
+
311
+ node = nil
312
+ @nodes_mutex.synchronize do
313
+ node = @nodes[host]
314
+ end
315
+ node
316
+ end
317
+
318
+ def when_disconnected_node(addr, node, opts)
319
+ # addr = deep_space.peer_uuid[0]
320
+ Log::info(self, "NODE: disconnected(#{addr})")
321
+ @nodes.delete(addr)
322
+ end
323
+
324
+ def Master.start(service)
325
+ master = Master.new
326
+ master.start(service)
327
+ end
328
+ end
329
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (C) 2007-2010 Rakuten, Inc.
4
+ #
5
+
6
+ njob_dir = File.dirname(__FILE__)
7
+ subdir = File.basename(File.dirname(njob_dir))
8
+ njob_name = File.basename(njob_dir)
9
+ for njob in Dir.glob("#{njob_dir}/*.rb")
10
+ base = File.basename(njob)
11
+ case base
12
+ when /18.rb$/
13
+ next if RUBY_VERSION >= "1.9.0"
14
+ when /19.rb$/
15
+ next unless RUBY_VERSION >= "1.9.0"
16
+ end
17
+ require [subdir, njob_name, base].join("/")
18
+ end
19
+
@@ -0,0 +1,95 @@
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright (C) 2007-2010 Rakuten, Inc.
4
+ #
5
+
6
+ require "fairy/node/p-io-filter"
7
+ require "fairy/node/p-single-exportable"
8
+
9
+ module Fairy
10
+ class PBarrierMemoryBuffer<PSingleExportFilter
11
+ Processor.def_export self
12
+
13
+ ST_ALL_IMPORTED = :ST_ALL_IMPORTED
14
+
15
+ def initialize(id, ntask, bjob, opts=nil)
16
+ # @export = Export.new()
17
+ super
18
+
19
+ @queuing_policy = CONF.BARRIER_MEMORY_BUFFERING_POLICY
20
+ @queue = eval("#{@queuing_policy[:queuing_class]}").new(@queuing_policy)
21
+ end
22
+
23
+ # def input=(input)
24
+ # unless @import
25
+ # @import = Import.new(Queue.new)
26
+ # @import.no=input.no
27
+ # @import.add_key(input.key)
28
+ # @import.set_log_callback do |n|
29
+ # Log::verbose(self, "IMPORT POP: #{n}")
30
+ # end
31
+
32
+ # start
33
+ # end
34
+ # self
35
+ # end
36
+
37
+ def input=(input)
38
+ super
39
+ start_buffering
40
+ end
41
+
42
+ def start_buffering
43
+ Log::info self, "START BUFFERING: #{self.class}"
44
+
45
+ start_watch_status
46
+
47
+ @main_thread = Thread.start {
48
+ begin
49
+ self.status = ST_ACTIVATE
50
+ if @begin_block_source
51
+ bsource = BSource.new(@begin_block_source, @context, self)
52
+ bsource.evaluate
53
+ end
54
+ begin
55
+ basic_start{}
56
+ ensure
57
+ if @end_block_source
58
+ bsource = BSource.new(@end_block_source, @context, self)
59
+ bsource.evaluate
60
+ end
61
+
62
+ @main_thread = nil
63
+ Log::info self, "FINISH BUFFERING: #{self.class}"
64
+ end
65
+ rescue Exception
66
+ Log::error_exception(self)
67
+ handle_exception($!)
68
+ raise
69
+ end
70
+ }
71
+ end
72
+
73
+ def basic_start(&block)
74
+ Log::debug(self, "START")
75
+
76
+ begin
77
+ @input.each{|e| @queue.push e}
78
+ ensure
79
+ @queue.push :END_OF_STREAM
80
+ self.status = ST_ALL_IMPORTED
81
+ end
82
+ end
83
+
84
+
85
+ def basic_each(&block)
86
+ @bjob.wait_export
87
+
88
+ while (e = @queue.pop) != END_OF_STREAM
89
+ block.call e
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ end