flare-tools 0.1.4 → 0.4.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. data/.gemtest +0 -0
  2. data/Flare-tools.txt +0 -0
  3. data/History.txt +114 -2
  4. data/LICENSE +21 -0
  5. data/Manifest.txt +65 -8
  6. data/README.txt +356 -0
  7. data/Rakefile +90 -25
  8. data/Tutorial.txt +370 -0
  9. data/bin/flare-admin +6 -0
  10. data/bin/flare-argv0 +6 -0
  11. data/bin/flare-deploy +6 -0
  12. data/bin/flare-keychecker +6 -0
  13. data/bin/flare-part +6 -0
  14. data/bin/flare-ping +6 -0
  15. data/bin/flare-stats +4 -10
  16. data/bin/flare-zkadmin +6 -0
  17. data/lib/flare/net/connection.rb +98 -0
  18. data/lib/flare/test/cluster.rb +140 -0
  19. data/lib/flare/test/daemon.rb +144 -0
  20. data/lib/flare/test/node.rb +62 -0
  21. data/lib/flare/tools.rb +18 -16
  22. data/lib/flare/tools/cli.rb +32 -0
  23. data/lib/flare/tools/cli/activate.rb +106 -0
  24. data/lib/flare/tools/cli/balance.rb +83 -0
  25. data/lib/flare/tools/cli/cli_util.rb +77 -0
  26. data/lib/flare/tools/cli/deploy.rb +170 -0
  27. data/lib/flare/tools/cli/down.rb +85 -0
  28. data/lib/flare/tools/cli/dump.rb +219 -0
  29. data/lib/flare/tools/cli/dumpkey.rb +117 -0
  30. data/lib/flare/tools/cli/flare_admin.rb +81 -0
  31. data/lib/flare/tools/cli/flare_argv0.rb +60 -0
  32. data/lib/flare/tools/cli/flare_keychecker.rb +106 -0
  33. data/lib/flare/tools/cli/flare_zkadmin.rb +226 -0
  34. data/lib/flare/tools/cli/index.rb +54 -0
  35. data/lib/flare/tools/cli/list.rb +93 -0
  36. data/lib/flare/tools/cli/master.rb +143 -0
  37. data/lib/flare/tools/cli/part.rb +100 -0
  38. data/lib/flare/tools/cli/ping.rb +81 -0
  39. data/lib/flare/tools/cli/reconstruct.rb +164 -0
  40. data/lib/flare/tools/cli/remove.rb +119 -0
  41. data/lib/flare/tools/cli/restore.rb +180 -0
  42. data/lib/flare/tools/cli/slave.rb +125 -0
  43. data/lib/flare/tools/cli/stats.rb +229 -122
  44. data/lib/flare/tools/cli/sub_command.rb +73 -0
  45. data/lib/flare/tools/cli/summary.rb +97 -0
  46. data/lib/flare/tools/cli/threads.rb +78 -0
  47. data/lib/flare/tools/cli/verify.rb +202 -0
  48. data/lib/flare/tools/client.rb +267 -0
  49. data/lib/flare/tools/cluster.rb +319 -0
  50. data/lib/flare/tools/common.rb +196 -0
  51. data/lib/flare/tools/index_server.rb +51 -0
  52. data/lib/flare/tools/node.rb +162 -0
  53. data/lib/flare/tools/stats.rb +75 -0
  54. data/lib/flare/tools/zk_util.rb +28 -0
  55. data/lib/flare/util.rb +34 -0
  56. data/lib/flare/util/bwlimit.rb +132 -0
  57. data/lib/flare/util/command_line.rb +79 -0
  58. data/lib/flare/util/conf.rb +71 -0
  59. data/lib/flare/util/constant.rb +25 -0
  60. data/lib/flare/util/conversion.rb +26 -0
  61. data/lib/flare/util/default_logger.rb +52 -0
  62. data/lib/flare/util/exception.rb +19 -0
  63. data/lib/flare/util/filesystem.rb +30 -0
  64. data/lib/flare/util/flared_conf.rb +33 -0
  65. data/lib/flare/util/flarei_conf.rb +32 -0
  66. data/lib/flare/util/hash_function.rb +32 -0
  67. data/lib/flare/util/interruption.rb +70 -0
  68. data/lib/flare/util/key_resolver.rb +67 -0
  69. data/lib/flare/util/log4r_logger.rb +79 -0
  70. data/lib/flare/util/logger.rb +40 -0
  71. data/lib/flare/util/logging.rb +84 -0
  72. data/lib/flare/util/result.rb +53 -0
  73. data/test/test/experimental/cache_test.rb +113 -0
  74. data/test/test/experimental/key_distribution_test.rb +38 -0
  75. data/test/test/experimental/keychecker_test.rb +60 -0
  76. data/test/test/experimental/list_test.rb +108 -0
  77. data/test/test/extra/replication_test.rb +184 -0
  78. data/test/test/integration/cli_test.rb +348 -0
  79. data/test/test/integration/dump_expired_test.rb +103 -0
  80. data/test/test/integration/dump_test.rb +128 -0
  81. data/test/test/integration/index_server_test.rb +35 -0
  82. data/test/test/integration/node_test.rb +78 -0
  83. data/test/test/integration/partition_test.rb +235 -0
  84. data/test/test/integration/proxy_test.rb +54 -0
  85. data/test/test/integration/stats_test.rb +79 -0
  86. data/test/test/system/flare_admin_test.rb +191 -0
  87. data/test/test/unit/bwlimit_test.rb +52 -0
  88. data/test/test/unit/cluster_test.rb +96 -0
  89. data/test/test/unit/daemon_test.rb +30 -0
  90. data/test/test/unit/logger_test.rb +46 -0
  91. data/test/test/unit/tools_test.rb +25 -0
  92. data/test/test/unit/util_test.rb +70 -0
  93. metadata +239 -84
  94. data/README.rdoc +0 -83
  95. data/bin/flare-partition-setting +0 -12
  96. data/lib/flare/tools/cli/partition_setting.rb +0 -86
  97. data/lib/flare/tools/core.rb +0 -189
  98. data/lib/flare/tools/logger.rb +0 -31
  99. data/test/test_flare-tools.rb +0 -11
  100. data/test/test_helper.rb +0 -3
@@ -0,0 +1,28 @@
1
+
2
+ require 'zookeeper'
3
+
4
+ module Flare
5
+ module Tools
6
+ module ZkUtil
7
+ ZOK = 0
8
+
9
+ def clear_nodemap z, path
10
+ path_nodemap = "#{path}/index/nodemap"
11
+ xml = <<EOS
12
+ <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
13
+ <!DOCTYPE boost_serialization>
14
+ <boost_serialization signature="serialization::archive" version="4">
15
+ <node_map class_id="0" tracking_level="0" version="0">
16
+ <count>0</count>
17
+ <item_version>0</item_version>
18
+ </node_map>
19
+ <thread_type>16</thread_type>
20
+ </boost_serialization>
21
+ EOS
22
+ result = z.set(:path => path_nodemap, :data => xml)
23
+ rc = result[:rc]
24
+ raise "failed to clear nodemap (#{rc})" if rc != ZOK
25
+ end
26
+ end # module ZkUtil
27
+ end # module Tools
28
+ end # module Flare
data/lib/flare/util.rb ADDED
@@ -0,0 +1,34 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ #
7
+ module Flare
8
+
9
+ # == Description
10
+ # Flare::Util module is a moudle that includes utility classes for basic feature of flare-tools and other utilities.
11
+ # This module shouled be moved to a common package, but for now we distribute it with flare-tools.
12
+ #
13
+ # Constant:: a module which defines common constants widely used by codes related to Flare.
14
+ # Conversion:: a module which covers verious unit conversion functions.
15
+ # Logger:: a logging class.
16
+ # Logging:: a mix-in module for Logger.
17
+ # Result:: a result code handling mix-in module.
18
+ # Conf:: an abstract base class of FlaredConf and FlareiConf.
19
+ # FlareiConf:: a class for flarei.conf.
20
+ # FlaredConf:: a class for flared.conf.
21
+ # FileSystem:: a file system manipulation class.
22
+ #
23
+ module Util
24
+ autoload :Constant, 'flare/util/constant'
25
+ autoload :Conversion, 'flare/util/conversion'
26
+ autoload :Logging, 'flare/util/logging'
27
+ autoload :Result, 'flare/util/result'
28
+ autoload :Conf, 'flare/util/conf'
29
+ autoload :FlaredConf, 'flare/util/flared_conf'
30
+ autoload :FlareiConf, 'flare/util/flarei_conf'
31
+ autoload :FileSystem, 'flare/util/filesystem'
32
+ autoload :KeyResolver, 'flare/util/key_resolver'
33
+ end
34
+ end
@@ -0,0 +1,132 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ require "flare/util/logging"
7
+
8
+ #
9
+ module Flare
10
+ module Util
11
+
12
+ # == Description
13
+ #
14
+ class Bwlimit
15
+ include Flare::Util::Logging
16
+
17
+ DefaultBandwidth = 1024*1024 # 1Mbps
18
+ Ki = 1024
19
+ Mi = 1024*Ki
20
+ Gi = 1024*Mi
21
+ Bit = 1
22
+ Byte = 8
23
+
24
+ def initialize(bwlimit)
25
+ @limit = Bwlimit.bps(bwlimit)
26
+ @basetime = @starttime = Time.now
27
+ @duration = 1.0
28
+ @history = []
29
+ @minwait = 0.01
30
+ @speed = 0
31
+ @bytes = 0
32
+ @totalbytes = 0
33
+ @thresh = 1*Ki
34
+ end
35
+
36
+ def limit=(bwlimit)
37
+ @limit = Bwlimit.bps(bwlimit)
38
+ end
39
+
40
+ def limit
41
+ @limit
42
+ end
43
+
44
+ def bps
45
+ @limit
46
+ end
47
+
48
+ def reset
49
+ @basetime = @starttime = Time.now
50
+ end
51
+
52
+ def inc(bytes)
53
+ @bytes += bytes
54
+ @totalbytes += bytes
55
+ end
56
+
57
+ def time_to_wait(now = Time.now)
58
+ waitsec = 0
59
+ limit = @limit/Byte
60
+ allowed = (now-@basetime)*limit
61
+ if @bytes > allowed
62
+ waitsec = ((@bytes-allowed).to_f/limit)
63
+ end
64
+ unless waitsec > 0
65
+ waitsec = @minwait if waitsec < @minwait
66
+ end
67
+ waitsec
68
+ end
69
+
70
+ def pasttime(now = Time.now)
71
+ now-@basetime
72
+ end
73
+
74
+ def wait
75
+ waitsec = 0
76
+ if @limit > 0 && @bytes > @thresh
77
+ now = Time.now
78
+ sleep time_to_wait(now)
79
+ diff = pasttime(now)
80
+ if diff > @duration
81
+ @speed = @bytes*Byte/diff
82
+ debug "#{@speed} bps"
83
+ @bytes = 0
84
+ @basetime = now
85
+ @history << {:time => now-@starttime, :speed => @speed}
86
+ end
87
+ end
88
+ waitsec
89
+ end
90
+
91
+ def history
92
+ @history.dup
93
+ end
94
+
95
+ def speed
96
+ @speed
97
+ end
98
+
99
+ def totalbytes
100
+ @totalbytes
101
+ end
102
+
103
+ def self.bps(bw)
104
+ return 0 if bw.nil?
105
+ case bw
106
+ when /^(\d+)$/
107
+ $1.to_i*Bit
108
+ when /^(\d+)B$/
109
+ $1.to_i*Byte
110
+ when /^(\d+)k$/
111
+ $1.to_i*Ki*Bit
112
+ when /^(\d+)kB$/
113
+ $1.to_i*Ki*Byte
114
+ when /^(\d+)M$/
115
+ $1.to_i*Mi*Bit
116
+ when /^(\d+)MB$/
117
+ $1.to_i*Mi*Byte
118
+ when /^(\d+)G$/
119
+ $1.to_i*Gi*Bit
120
+ when /^(\d+)GB$/
121
+ $1.to_i*Gi*Byte
122
+ else
123
+ bw.to_i
124
+ end
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+ end
131
+
132
+
@@ -0,0 +1,79 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ require 'optparse'
7
+ require 'flare/util/logging'
8
+
9
+ #
10
+ module Flare
11
+ module Util
12
+
13
+ # == Description
14
+ # CommandLine is a mix-in module for the top level.
15
+ #
16
+ # require 'flare/util/command_line'
17
+ #
18
+ # option do |opt|
19
+ # ...
20
+ # end
21
+ #
22
+ # setup do |opt|
23
+ # ...
24
+ # end
25
+ #
26
+ # execute do |args|
27
+ # ...
28
+ # end
29
+ module CommandLine
30
+ @@option = OptionParser.new
31
+ S_OK = 0
32
+ S_NG = 1
33
+
34
+ def option(&block)
35
+ block.call(@@option)
36
+ end
37
+
38
+ def setup(&block)
39
+ block.call(@@option)
40
+ begin
41
+ @@option.parse!(ARGV)
42
+ rescue OptionParser::ParseError => err
43
+ puts err.message
44
+ puts @@option.to_s
45
+ exit S_NG
46
+ end
47
+ end
48
+
49
+ def execute(&block)
50
+ status = S_OK
51
+ if block
52
+ args = ARGV.dup
53
+ ARGV.clear
54
+ status = block.call(args)
55
+ end
56
+ status
57
+ rescue => e
58
+ level = 1
59
+ Logging.logger.error(e.to_s)
60
+ e.backtrace.each do |line|
61
+ Logging.logger.error(" %3s: %s" % [level, line])
62
+ level += 1
63
+ end
64
+ raise e if $DEBUG
65
+ S_NG
66
+ end
67
+
68
+ end
69
+ end
70
+ end
71
+
72
+ extend Flare::Util::CommandLine
73
+
74
+ option do |opt|
75
+ opt.on('-h', '--help', "show this message") {puts opt.help; exit 1}
76
+ opt.on( '--debug', "enable debug mode") {$DEBUG = true}
77
+ opt.on( '--warn', "turn on warnings") {$-w = true}
78
+ end
79
+
@@ -0,0 +1,71 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ #
7
+ module Flare
8
+ module Util
9
+
10
+ # == Description
11
+ # Conf class is the abstract base class of Flare's configuration file classes.
12
+ # You can write sub classes derived from this class as follows.
13
+ #
14
+ # class FlaredConf < Flare::Util::Conf
15
+ # DefaultConf = {
16
+ # 'server-name' => "localhost",
17
+ # 'server-port' => "12121",
18
+ # }
19
+ #
20
+ # def initialize(config)
21
+ # @config = DefaultConf.merge(config)
22
+ # end
23
+ # end
24
+ #
25
+ class Conf
26
+
27
+ # Initialize a Conf object with a hash object.
28
+ def initialize(config)
29
+ @config = {}
30
+ @config = config unless config.nil?
31
+ end
32
+
33
+ # Convert to String object in a configuration file format.
34
+ def to_s
35
+ conf = ""
36
+ @config.each do |k,v|
37
+ conf += "#{k} = #{v}\n" unless v.nil?
38
+ end
39
+ conf
40
+ end
41
+
42
+ # Iterate item and value pairs.
43
+ def each(&block)
44
+ @config.each do |k,v|
45
+ block.call(k, v) if block
46
+ end
47
+ end
48
+
49
+ # Returns "server-name" entry.
50
+ def server_name
51
+ @config['server-name']
52
+ end
53
+
54
+ # Returns "server-port" entry.
55
+ def server_port
56
+ @config['server-port']
57
+ end
58
+
59
+ # Returns node name in hostname:port style.
60
+ def hostname_port
61
+ "#{server_name}:#{server_port}"
62
+ end
63
+
64
+ # Returns "data-dir" entry.
65
+ def data_dir
66
+ @config['data-dir']
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ #
7
+ module Flare
8
+ module Util
9
+
10
+ # == Description
11
+ #
12
+ module Constant
13
+ # the default index server name
14
+ DefaultIndexServerName = '127.0.0.1'
15
+ # the default port number of flarei daemon
16
+ DefaultIndexServerPort = 12120
17
+ # the default port number of flared daemon
18
+ DefaultNodePort = 12121
19
+ # the default timeout of client connections (sec.)
20
+ DefaultTimeout = 10
21
+ # the default bandwidth limit (bps)
22
+ DefalutBwlimit = 0 # unlimited
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+ #
7
+ module Flare
8
+ module Util
9
+
10
+ #
11
+ # == Description
12
+ #
13
+ module Conversion
14
+ def short_desc_of_second(second)
15
+ minute = hour = day = nil
16
+ second, unit = second.to_i, "s"
17
+ minute, second, unit = (second/60), (second%60), "m" unless second < 60
18
+ hour, minute, unit = (minute/60), (minute%60), "h" unless minute.nil? || minute < 60
19
+ day, hour, unit = (hour/24), (hour%24), "d" unless hour.nil? || hour < 24
20
+ n = (day || hour || minute || second)
21
+ "#{n}#{unit}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,52 @@
1
+ # -*- coding: utf-8; -*-
2
+ # Authors:: Kiyoshi Ikehara <kiyoshi.ikehara@gree.net>
3
+ # Copyright:: Copyright (C) GREE, Inc. 2011.
4
+ # License:: MIT-style
5
+
6
+
7
+ require 'flare/util/logger'
8
+ require 'logger'
9
+
10
+ #
11
+ module Flare
12
+ module Util
13
+
14
+ # == Description
15
+ # Logger is a custom logging class.
16
+ class DefaultLogger < Flare::Util::Logger
17
+ def initialize(logger)
18
+ @logger = ::Logger.new(STDOUT)
19
+ @logger.level = ::Logger::WARN
20
+ end
21
+
22
+ def info(msg)
23
+ @logger.info(msg)
24
+ end
25
+
26
+ def warn(msg)
27
+ @logger.warn(msg)
28
+ end
29
+
30
+ def trace(msg)
31
+ @logger.debug(msg)
32
+ end
33
+
34
+ def error(msg)
35
+ @logger.info(msg)
36
+ end
37
+
38
+ def fatal(msg)
39
+ @logger.info(msg)
40
+ end
41
+
42
+ def debug(msg)
43
+ @logger.debug(msg)
44
+ end
45
+
46
+ def console?
47
+ true
48
+ end
49
+ end
50
+
51
+ end
52
+ end