drbqs 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. data/.document +3 -0
  2. data/README.md +137 -128
  3. data/VERSION +1 -1
  4. data/docs/FormatExecute.md +119 -0
  5. data/docs/GettingStarted.md +242 -0
  6. data/drbqs.gemspec +36 -13
  7. data/example/command/server_def.rb +4 -5
  8. data/example/execute/execute.rb +41 -0
  9. data/example/execute/server.rb +14 -0
  10. data/example/execute/task.rb +0 -0
  11. data/example/mandelbrot/README.md +15 -0
  12. data/example/mandelbrot/execute.rb +10 -0
  13. data/example/mandelbrot/mandelbrot.rb +56 -0
  14. data/example/mandelbrot/server.rb +49 -0
  15. data/example/server/server.rb +3 -6
  16. data/example/simple/README.md +18 -0
  17. data/example/simple/execute.rb +11 -0
  18. data/example/simple/server.rb +8 -0
  19. data/example/simple/task.rb +11 -0
  20. data/example/sum/server_def.rb +1 -1
  21. data/example/sum2/execute_def.rb +21 -8
  22. data/example/sum2/server_def.rb +8 -7
  23. data/example/transfer/file.rb +42 -8
  24. data/example/transfer/server_def.rb +43 -9
  25. data/lib/drbqs.rb +1 -1
  26. data/lib/drbqs/command_line/command_execute.rb +3 -3
  27. data/lib/drbqs/command_line/command_line.rb +1 -1
  28. data/lib/drbqs/execute/execute_node.rb +50 -0
  29. data/lib/drbqs/execute/process_define.rb +102 -54
  30. data/lib/drbqs/execute/register.rb +241 -87
  31. data/lib/drbqs/execute/server_define.rb +69 -58
  32. data/lib/drbqs/ext/task.rb +2 -0
  33. data/lib/drbqs/ext/task/command_task.rb +43 -0
  34. data/lib/drbqs/manage/manage.rb +5 -4
  35. data/lib/drbqs/manage/ssh_shell.rb +2 -8
  36. data/lib/drbqs/node/connection.rb +1 -1
  37. data/lib/drbqs/node/node.rb +8 -14
  38. data/lib/drbqs/node/task_client.rb +1 -1
  39. data/lib/drbqs/server/history.rb +5 -1
  40. data/lib/drbqs/server/message.rb +7 -34
  41. data/lib/drbqs/server/queue.rb +14 -2
  42. data/lib/drbqs/server/server.rb +86 -43
  43. data/lib/drbqs/server/server_hook.rb +3 -0
  44. data/lib/drbqs/server/test/node.rb +1 -1
  45. data/lib/drbqs/server/test/prof.rb +50 -0
  46. data/lib/drbqs/server/test/server.rb +2 -2
  47. data/lib/drbqs/server/transfer_setting.rb +23 -11
  48. data/lib/drbqs/setting/base.rb +15 -0
  49. data/lib/drbqs/setting/data_container.rb +1 -1
  50. data/lib/drbqs/setting/execute.rb +3 -3
  51. data/lib/drbqs/setting/node.rb +1 -1
  52. data/lib/drbqs/setting/server.rb +2 -2
  53. data/lib/drbqs/task/registrar.rb +39 -0
  54. data/lib/drbqs/task/task.rb +139 -59
  55. data/lib/drbqs/task/task_generator.rb +93 -116
  56. data/lib/drbqs/utility/misc.rb +15 -10
  57. data/lib/drbqs/utility/temporary.rb +7 -2
  58. data/lib/drbqs/utility/transfer/transfer.rb +81 -0
  59. data/lib/drbqs/utility/transfer/transfer_client.rb +68 -69
  60. data/lib/drbqs/utility/transfer/transfer_client_connect.rb +83 -0
  61. data/lib/drbqs/utility/transfer/transfer_file_list.rb +40 -0
  62. data/spec/execute/def/execute1.rb +4 -4
  63. data/spec/execute/def/execute2.rb +24 -0
  64. data/spec/execute/process_define_spec.rb +43 -6
  65. data/spec/execute/register_spec.rb +403 -9
  66. data/spec/execute/server_define_spec.rb +1 -1
  67. data/spec/ext/task/command_task_spec.rb +16 -0
  68. data/spec/integration_test/01_basic_usage_spec.rb +1 -1
  69. data/spec/integration_test/02_use_generator_spec.rb +2 -2
  70. data/spec/integration_test/04_use_unix_domain_spec.rb +1 -1
  71. data/spec/integration_test/05_server_exit_signal_spec.rb +1 -1
  72. data/spec/integration_test/06_node_exit_after_task_spec.rb +4 -4
  73. data/spec/integration_test/08_shutdown_unused_nodes_spec.rb +2 -2
  74. data/spec/integration_test/09_server_process_data_spec.rb +1 -1
  75. data/spec/integration_test/definition/server01.rb +4 -5
  76. data/spec/integration_test/definition/server02.rb +2 -4
  77. data/spec/node/node_spec.rb +34 -0
  78. data/spec/server/message_spec.rb +1 -1
  79. data/spec/server/queue_spec.rb +34 -7
  80. data/spec/server/server_spec.rb +21 -9
  81. data/spec/server/transfer_setting_spec.rb +59 -24
  82. data/spec/setting/base_spec.rb +11 -0
  83. data/spec/setting/data_container_spec.rb +8 -0
  84. data/spec/spec_helper.rb +1 -7
  85. data/spec/task/registrar_spec.rb +34 -0
  86. data/spec/task/task_generator_spec.rb +15 -15
  87. data/spec/task/task_spec.rb +132 -23
  88. data/spec/utility/misc_spec.rb +2 -2
  89. data/spec/utility/transfer/transfer_client_connect_spec.rb +90 -0
  90. data/spec/utility/transfer/transfer_file_list_spec.rb +27 -0
  91. data/spec/{task/file_transfer_spec.rb → utility/transfer/transfer_spec.rb} +24 -24
  92. metadata +66 -45
  93. data/lib/drbqs/manage/execute_node.rb +0 -50
  94. data/lib/drbqs/server/prof.rb +0 -48
  95. data/lib/drbqs/task/command_task.rb +0 -43
  96. data/lib/drbqs/utility/transfer/file_transfer.rb +0 -73
File without changes
@@ -0,0 +1,15 @@
1
+ # Mandelbrot set
2
+
3
+ Calculate Mandelbrot set and save results to directory 'result_mandelbrot'.
4
+
5
+ ## Requirements
6
+
7
+ This program requires gunzip command.
8
+
9
+ ## Help
10
+
11
+ drbqs-server server.rb -h
12
+
13
+ ## Execute
14
+
15
+ drbqs-server server.rb --execute-node <process_number>
@@ -0,0 +1,10 @@
1
+ DIR = File.dirname(__FILE__)
2
+
3
+ server :local_server, "localhost" do |srv|
4
+ srv.load File.join(DIR, 'server.rb')
5
+ end
6
+
7
+ node :local_node do |nd|
8
+ nd.load File.join(DIR, 'mandelbrot.rb')
9
+ nd.process 2 # For dual core CPU
10
+ end
@@ -0,0 +1,56 @@
1
+ require 'complex'
2
+
3
+ class Mandelbrot
4
+ def initialize(iterate, threshold)
5
+ @iterate = iterate
6
+ @threshold = threshold
7
+ end
8
+
9
+ def map(z, c)
10
+ z * z + c
11
+ end
12
+
13
+ def iterate_map(z, c, &block)
14
+ z_old = z
15
+ @iterate.times do |i|
16
+ z_new = map(z_old, c)
17
+ z_old = z_new
18
+ yield(z_old) if block_given?
19
+ end
20
+ z_old
21
+ end
22
+
23
+ def diverge?(c)
24
+ iterate_map(Complex(0.0, 0.0), c) do |z|
25
+ if z.abs > @threshold
26
+ return true
27
+ end
28
+ end
29
+ false
30
+ end
31
+ end
32
+
33
+ class CalcMandelbrot
34
+ def initialize(mandelbrot)
35
+ @mandelbrot = mandelbrot
36
+ end
37
+
38
+ def calc(io, xrange, yrange, step_size)
39
+ xrange.step(step_size) do |x|
40
+ yrange.step(step_size) do |y|
41
+ c = Complex(x, y)
42
+ unless @mandelbrot.diverge?(c)
43
+ io.puts "#{c.real}\t#{c.imag}"
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def calc_save(basename, *args)
50
+ file = DRbQS::Temporary.file
51
+ open(file, 'w') do |f|
52
+ calc(f, *args)
53
+ end
54
+ DRbQS::Transfer.enqueue(file, compress: true, rename: basename) # Return basename.
55
+ end
56
+ end
@@ -0,0 +1,49 @@
1
+ require_relative 'mandelbrot.rb'
2
+
3
+ RESULT_DIR = 'result_mandelbrot'
4
+
5
+ DRbQS.option_parser("Calculate Mandelbrot set.") do |prs, opts|
6
+ prs.on('-s NUM', '--step NUM', Float, 'Set the step size.') do |v|
7
+ opts[:step] = v
8
+ end
9
+ prs.on('-l NUM', '--limit NUM', Float, 'Set the limit to search.') do |v|
10
+ opts[:limit] = v
11
+ end
12
+ prs.on('-i NUM', '--iterate NUM', Integer, 'Set the iterate number.') do |v|
13
+ opts[:iterate] = v
14
+ end
15
+ prs.on('-t NUM', '--threshold NUM', Float, 'Set the threshold value.') do |v|
16
+ opts[:threshold] = v
17
+ end
18
+ end
19
+
20
+ DRbQS.define_server(file_directory: RESULT_DIR) do |server, argv, opts|
21
+ step_size = opts[:step] || 0.1
22
+ limit = opts[:limit] || 2.0
23
+ iterate = opts[:iterate] || 1000
24
+ threshold = opts[:threshold] || 5
25
+
26
+ mandelbrot = Mandelbrot.new(iterate, threshold)
27
+ calc = CalcMandelbrot.new(mandelbrot)
28
+
29
+ ranges = [[-limit..0, -limit..0, step_size], [-limit..0, 0..limit, step_size],
30
+ [0..limit, -limit..0, step_size], [0..limit, 0..limit, step_size]]
31
+ ranges.each_with_index do |ranges, i|
32
+ args_ary = ["%02d.txt" % i] + ranges
33
+ note_str = "#{ranges[0].inspect} #{ranges[1].inspect}"
34
+ task = DRbQS::Task.new(calc, :calc_save, args: args_ary, note: note_str) do |srv, result|
35
+ DRbQS::Transfer.decompress(srv, result)
36
+ end
37
+ server.queue.add(task)
38
+ end
39
+ server.add_hook(:finish) do |srv|
40
+ puts "Save results to #{RESULT_DIR}"
41
+ result_path = File.join(RESULT_DIR, "result.txt")
42
+ Dir.glob(File.join(RESULT_DIR, "*.txt")).sort.each do |path|
43
+ open(result_path, 'a+') do |out|
44
+ out.print File.read(path)
45
+ end
46
+ FileUtils.rm(path)
47
+ end
48
+ end
49
+ end
@@ -1,12 +1,9 @@
1
- task_generator = DRbQS::TaskGenerator.new(:iterate => 3)
2
- task_generator.set do
1
+ server = DRbQS::Server.new(:port => 13501)
2
+ server.task_generator do |reg|
3
3
  5.times do |i|
4
- create_add_task(i, :to_s)
4
+ reg.create_add(i, :to_s)
5
5
  end
6
6
  end
7
-
8
- server = DRbQS::Server.new(:port => 13501, :finish_exit => true)
9
- server.add_task_generator(task_generator)
10
7
  server.set_initialization_task(DRbQS::Task.new(Kernel, :puts, ['hook: initialize']))
11
8
  server.set_finalization_task(DRbQS::Task.new(Kernel, :puts, ['hook: finalize']))
12
9
  server.set_signal_trap
@@ -0,0 +1,18 @@
1
+ # Simple example
2
+
3
+ Creating one task that calculates sum of three numbers
4
+ and outputing the result.
5
+
6
+ ## drbqs-server and drbqs-node
7
+
8
+ On first terminal,
9
+
10
+ drbqs-server server.rb
11
+
12
+ and then on second terminal
13
+
14
+ drbqs-node druby://:13500 -l task.rb
15
+
16
+ ## drbqs-execute
17
+
18
+ drbqs-execute execute.rb
@@ -0,0 +1,11 @@
1
+ DIR = File.dirname(__FILE__)
2
+
3
+ default port: 12345
4
+
5
+ server :local, "localhost" do |srv|
6
+ srv.load File.join(DIR, 'server.rb')
7
+ end
8
+
9
+ node :local do |nd|
10
+ nd.load File.join(DIR, 'task.rb')
11
+ end
@@ -0,0 +1,8 @@
1
+ require_relative 'task.rb'
2
+
3
+ DRbQS.define_server do |server, argv, opts|
4
+ task = DRbQS::Task.new(Sum.new(10, 20, 2), :calc) do |srv, result|
5
+ puts "Result is #{result}"
6
+ end
7
+ server.queue.add(task)
8
+ end
@@ -0,0 +1,11 @@
1
+ class Sum
2
+ def initialize(a, b, c)
3
+ @a = a
4
+ @b = b
5
+ @c = c
6
+ end
7
+
8
+ def calc
9
+ @a + @b + @c
10
+ end
11
+ end
@@ -17,7 +17,7 @@ DRbQS.define_server(:check_alive => 5) do |server, argv, opts|
17
17
  end_num = (argv[1] || 50).to_i
18
18
  step_num = opts[:step] || 10
19
19
  start_num.step(end_num, step_num) do |i|
20
- task = DRbQS::Task.new(Sum.new(i - 10, i), :exec) do |srv, ret|
20
+ task = DRbQS::Task.new(Sum.new(i - 10, i), :exec, [], "#{i-10} to #{i}") do |srv, ret|
21
21
  puts "Receive: #{ret.inspect}"
22
22
  end
23
23
  server.queue.add(task)
@@ -2,26 +2,39 @@ default :server => :server_local, :port => 13789, :log => '/tmp/drbqs_execute'
2
2
 
3
3
  usage :message => "Calculate sum of numbers", :server => File.join(File.dirname(__FILE__), 'server_def.rb')
4
4
 
5
- register_server :server_local, "localhost" do |server|
5
+ server :server_template, :template => true do |server, ssh|
6
+ server.load 'server_def.rb'
7
+ end
8
+
9
+ server :server_local, "localhost" do |server|
6
10
  server.load File.expand_path(File.join(File.dirname(__FILE__), 'server_def.rb'))
7
11
  end
8
12
 
9
- register_server :server_ssh, "localhost" do |server, ssh|
10
- ssh.connect 'localhost'
11
- ssh.directory File.expand_path(File.dirname(__FILE__))
13
+ ssh_localhost = "#{ENV['USER']}@localhost"
14
+ current_dir = File.expand_path(File.dirname(__FILE__))
15
+
16
+ server :server_ssh, "localhost" do |server, ssh|
17
+ ssh.connect ssh_localhost
18
+ ssh.directory current_dir
12
19
  ssh.output "/tmp/drbqs_ssh/server"
13
20
  server.load 'server_def.rb'
14
21
  end
15
22
 
16
- register_node :node_local do |node|
23
+ node :node_template, template: true do |node|
24
+ node.load 'sum.rb'
25
+ end
26
+
27
+ node :node_local do |node|
17
28
  node.load File.expand_path(File.join(File.dirname(__FILE__), 'sum.rb'))
18
29
  node.process 2
19
30
  end
20
31
 
21
- register_node :node_ssh do |node, ssh|
22
- ssh.connect 'localhost'
23
- ssh.directory File.expand_path(File.dirname(__FILE__))
32
+ node :node_ssh do |node, ssh|
33
+ ssh.connect ssh_localhost
34
+ ssh.directory current_dir
24
35
  ssh.output "/tmp/drbqs_ssh/node"
25
36
  node.load 'sum.rb'
26
37
  node.process 2
27
38
  end
39
+
40
+ node :node_group, :group => [:node_local, :node_ssh]
@@ -12,13 +12,14 @@ DRbQS.option_parser do |opt, hash|
12
12
  end
13
13
  end
14
14
 
15
- DRbQS.define_server(:finish_exit => true) do |server, argv, opts|
16
- tgen = DRbQS::TaskGenerator.new(:start_num => (argv[0] || 10).to_i,
17
- :end_num => (argv[1] || 100).to_i,
18
- :step_num => opts[:step] || 10)
19
- tgen.set do
20
- @start_num.step(@end_num, @step_num) do |i|
21
- create_add_task(Sum.new(i - 10, i), :exec) do |srv, ret|
15
+ DRbQS.define_server do |server, argv, opts|
16
+ start_num = (argv[0] || 10).to_i,
17
+ end_num = (argv[1] || 100).to_i,
18
+ step_num = opts[:step] || 10
19
+
20
+ server.task_generator do |reg|
21
+ start_num.step(end_num, step_num) do |i|
22
+ reg.create_add(Sum.new(i - 10, i), :exec) do |srv, ret|
22
23
  puts "Receive: #{ret.inspect}"
23
24
  end
24
25
  end
@@ -10,13 +10,15 @@ class CreateFile
10
10
  end
11
11
 
12
12
  def create
13
- DRbQS::FileTransfer.enqueue(output_to_file)
14
- nil
13
+ path = output_to_file
14
+ DRbQS::Transfer.enqueue(path)
15
+ File.basename(path)
15
16
  end
16
17
 
17
18
  def create_compress
18
- DRbQS::FileTransfer.compress_enqueue(output_to_file)
19
- nil
19
+ path = output_to_file
20
+ DRbQS::Transfer.compress_enqueue(path)
21
+ File.basename(path)
20
22
  end
21
23
  end
22
24
 
@@ -33,12 +35,44 @@ class CreateDirectory
33
35
  end
34
36
 
35
37
  def create
36
- DRbQS::FileTransfer.enqueue(output_to_directory)
37
- nil
38
+ dir = output_to_directory
39
+ DRbQS::Transfer.enqueue(dir)
40
+ File.basename(dir)
38
41
  end
39
42
 
40
43
  def create_compress
41
- DRbQS::FileTransfer.compress_enqueue(output_to_directory)
42
- nil
44
+ dir = output_to_directory
45
+ DRbQS::Transfer.compress_enqueue(dir)
46
+ File.basename(dir)
47
+ end
48
+ end
49
+
50
+ class ReceiveFile
51
+ def initialize(file_list)
52
+ @file_list = file_list
53
+ end
54
+
55
+ def read_file
56
+ ret = ''
57
+ @file_list.path.each do |path|
58
+ if File.directory?(path)
59
+ raise "Receive directory, not file."
60
+ end
61
+ ret << path << "\t" << File.read(path).strip << "\n"
62
+ end
63
+ ret
64
+ end
65
+
66
+ def read_directory
67
+ ret = ''
68
+ @file_list.path.each do |dir|
69
+ unless File.directory?(dir)
70
+ raise "Receive file, not directory."
71
+ end
72
+ Dir.glob("#{dir}/**/*.txt").each do |path|
73
+ ret << path << "\t" << File.read(path).strip << "\n"
74
+ end
75
+ end
76
+ ret
43
77
  end
44
78
  end
@@ -5,15 +5,49 @@
5
5
 
6
6
  require_relative 'file.rb'
7
7
 
8
- DRbQS.define_server(:finish_exit => true) do |server, argv, opts|
9
- tgen = DRbQS::TaskGenerator.new(:sleep_time => 2)
10
- tgen.set do
11
- create_add_task(CreateFile.new(1), :create)
12
- create_add_task(CreateFile.new(2), :create_compress)
13
- create_add_task(CreateDirectory.new(3), :create)
14
- create_add_task(CreateDirectory.new(4), :create_compress)
8
+ DRbQS.define_server do |server, argv, opts|
9
+ server_directory = '/tmp/drbqs_transfer_test/'
10
+ FileUtils.rm_r(server_directory) if File.exist?(server_directory)
11
+ server_directory2 = '/tmp/drbqs_transfer_test2/'
12
+ FileUtils.rm_r(server_directory2) if File.exist?(server_directory2)
13
+ FileUtils.mkdir_p(server_directory2)
14
+ test_file = File.join(server_directory2, 'file1.txt')
15
+ open(test_file, 'w') do |f|
16
+ f.puts 'ABC'
17
+ end
18
+ test_dir = File.join(server_directory2, 'test_dir')
19
+ test_dir2 = File.join(test_dir, 'abc', 'def')
20
+ FileUtils.mkdir_p(test_dir2)
21
+ open(File.join(test_dir2, 'file2.txt'), 'w') do |f|
22
+ f.puts 'hello'
23
+ end
24
+ open(File.join(test_dir2, 'file3.txt'), 'w') do |f|
25
+ f.puts 'world'
15
26
  end
16
- server.add_task_generator(tgen)
17
27
 
18
- server.set_file_transfer('/tmp/drbqs_transfer_test/')
28
+ server.task_generator do |reg|
29
+ create_add(CreateFile.new(1), :create) do |srv, result|
30
+ path = File.join(server_directory, result)
31
+ puts "#{path} exist? #{File.exist?(path).inspect}"
32
+ end
33
+ create_add(CreateFile.new(2), :create_compress) do |srv, result|
34
+ path = File.join(server_directory, result + '.gz')
35
+ puts "#{path} exist? #{File.exist?(path).inspect}"
36
+ end
37
+ create_add(CreateDirectory.new(3), :create) do |srv, result|
38
+ path = File.join(server_directory, result)
39
+ puts "#{path} exist? #{File.exist?(path).inspect}"
40
+ end
41
+ create_add(CreateDirectory.new(4), :create_compress) do |srv, result|
42
+ path = File.join(server_directory, result + '.tar.gz')
43
+ puts "#{path} exist? #{File.exist?(path).inspect}"
44
+ end
45
+ create_add(ReceiveFile.new(DRbQS::Transfer::FileList.new(test_file)), :read_file) do |srv, result|
46
+ puts result
47
+ end
48
+ create_add(ReceiveFile.new(DRbQS::Transfer::FileList.new(test_dir)), :read_directory) do |srv, result|
49
+ puts result
50
+ end
51
+ end
52
+ server.set_file_transfer(server_directory)
19
53
  end
data/lib/drbqs.rb CHANGED
@@ -20,5 +20,5 @@ module DRbQS
20
20
 
21
21
  ROOT_DEFAULT_PORT = 13500
22
22
 
23
- VERSION = '0.0.15'
23
+ VERSION = '0.0.16'
24
24
  end
@@ -15,9 +15,9 @@ HELP
15
15
  def parse_option(argv)
16
16
  args, server_args = split_arguments(argv)
17
17
  args = option_parser_base(args) do
18
- set(:port, '--port NUM', Integer, 'Set the port number.')
19
- set(:server, '--server STR', String, 'Set the key of server.')
20
- set(:node, '--node STR', String, 'Set the comma sparated key of nodes.')
18
+ set(:port, '-p NUM', '--port NUM', Integer, 'Set the port number.')
19
+ set(:server, '-s STR', '--server STR', String, 'Set the key of server.')
20
+ set(:node, '-n STR', '--node STR', String, 'Set the comma sparated key of nodes.')
21
21
  set(:no_server, '--no-server', 'Not execute server.')
22
22
  set(:no_node, '--no-node', 'Not execute node.')
23
23
  set(:information, '-i', '--information', 'Show information.')
@@ -1,6 +1,6 @@
1
1
  require 'drbqs'
2
2
  require 'drbqs/manage/manage'
3
- require 'drbqs/manage/execute_node'
3
+ require 'drbqs/execute/execute_node'
4
4
  require 'drbqs/command_line/argument'
5
5
  require 'drbqs/command_line/option_setting'
6
6
  require 'drbqs/command_line/command_base'