riak-client 0.9.8 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. data/.gitignore +32 -0
  2. data/Gemfile +17 -11
  3. data/Guardfile +14 -0
  4. data/Rakefile +18 -44
  5. data/erl_src/riak_kv_test_backend.beam +0 -0
  6. data/erl_src/riak_kv_test_backend.erl +461 -128
  7. data/erl_src/riak_search_test_backend.beam +0 -0
  8. data/erl_src/riak_search_test_backend.erl +175 -0
  9. data/lib/active_support/cache/riak_store.rb +0 -13
  10. data/lib/riak.rb +11 -16
  11. data/lib/riak/bucket.rb +59 -41
  12. data/lib/riak/cache_store.rb +1 -14
  13. data/lib/riak/client.rb +145 -73
  14. data/lib/riak/client/beefcake/messages.rb +36 -31
  15. data/lib/riak/client/beefcake/object_methods.rb +27 -19
  16. data/lib/riak/client/beefcake_protobuffs_backend.rb +27 -33
  17. data/lib/riak/client/excon_backend.rb +0 -13
  18. data/lib/riak/client/http_backend.rb +95 -60
  19. data/lib/riak/client/http_backend/configuration.rb +144 -19
  20. data/lib/riak/client/http_backend/key_streamer.rb +1 -14
  21. data/lib/riak/client/http_backend/object_methods.rb +16 -16
  22. data/lib/riak/client/http_backend/request_headers.rb +0 -13
  23. data/lib/riak/client/http_backend/transport_methods.rb +26 -56
  24. data/lib/riak/client/net_http_backend.rb +11 -13
  25. data/lib/riak/client/protobuffs_backend.rb +21 -19
  26. data/lib/riak/client/pump.rb +1 -15
  27. data/lib/riak/client/search.rb +85 -0
  28. data/lib/riak/cluster.rb +151 -0
  29. data/lib/riak/core_ext.rb +1 -0
  30. data/lib/riak/core_ext/deep_dup.rb +13 -0
  31. data/lib/riak/core_ext/json.rb +15 -0
  32. data/lib/riak/core_ext/stringify_keys.rb +1 -1
  33. data/lib/riak/core_ext/symbolize_keys.rb +1 -1
  34. data/lib/riak/encoding.rb +6 -0
  35. data/lib/riak/failed_request.rb +2 -15
  36. data/lib/riak/i18n.rb +0 -13
  37. data/lib/riak/json.rb +19 -8
  38. data/lib/riak/link.rb +18 -20
  39. data/lib/riak/locale/en.yml +13 -16
  40. data/lib/riak/map_reduce.rb +40 -20
  41. data/lib/riak/map_reduce/filter_builder.rb +14 -18
  42. data/lib/riak/map_reduce/phase.rb +0 -13
  43. data/lib/riak/map_reduce_error.rb +0 -13
  44. data/lib/riak/node.rb +38 -0
  45. data/lib/riak/node/configuration.rb +286 -0
  46. data/lib/riak/node/console.rb +139 -0
  47. data/lib/riak/node/control.rb +207 -0
  48. data/lib/riak/node/defaults.rb +70 -0
  49. data/lib/riak/node/generation.rb +99 -0
  50. data/lib/riak/node/log.rb +34 -0
  51. data/lib/riak/node/version.rb +37 -0
  52. data/lib/riak/robject.rb +45 -41
  53. data/lib/riak/search.rb +2 -161
  54. data/lib/riak/serializers.rb +74 -0
  55. data/lib/riak/stamp.rb +77 -0
  56. data/lib/riak/test_server.rb +56 -220
  57. data/lib/riak/util/escape.rb +58 -17
  58. data/lib/riak/util/headers.rb +2 -15
  59. data/lib/riak/util/multipart.rb +0 -13
  60. data/lib/riak/util/multipart/stream_parser.rb +0 -13
  61. data/lib/riak/util/tcp_socket_extensions.rb +1 -14
  62. data/lib/riak/util/translation.rb +0 -13
  63. data/lib/riak/version.rb +3 -0
  64. data/lib/riak/walk_spec.rb +0 -13
  65. data/riak-client.gemspec +27 -47
  66. data/spec/fixtures/multipart-with-marked-tombstones.txt +17 -0
  67. data/spec/fixtures/multipart-with-unmarked-tombstone.txt +16 -0
  68. data/spec/integration/riak/cache_store_spec.rb +2 -40
  69. data/spec/integration/riak/cluster_spec.rb +88 -0
  70. data/spec/integration/riak/http_backends_spec.rb +6 -30
  71. data/spec/integration/riak/node_spec.rb +184 -0
  72. data/spec/integration/riak/protobuffs_backends_spec.rb +2 -26
  73. data/spec/integration/riak/test_server_spec.rb +31 -167
  74. data/spec/riak/beefcake_protobuffs_backend_spec.rb +5 -4
  75. data/spec/riak/bucket_spec.rb +26 -36
  76. data/spec/riak/client_spec.rb +44 -38
  77. data/spec/riak/escape_spec.rb +56 -30
  78. data/spec/riak/excon_backend_spec.rb +4 -17
  79. data/spec/riak/headers_spec.rb +1 -14
  80. data/spec/riak/http_backend/configuration_spec.rb +211 -34
  81. data/spec/riak/http_backend/object_methods_spec.rb +52 -18
  82. data/spec/riak/http_backend/transport_methods_spec.rb +5 -38
  83. data/spec/riak/http_backend_spec.rb +84 -78
  84. data/spec/riak/link_spec.rb +19 -18
  85. data/spec/riak/map_reduce/filter_builder_spec.rb +1 -14
  86. data/spec/riak/map_reduce/phase_spec.rb +1 -14
  87. data/spec/riak/map_reduce_spec.rb +141 -43
  88. data/spec/riak/multipart_spec.rb +1 -14
  89. data/spec/riak/net_http_backend_spec.rb +2 -15
  90. data/spec/riak/robject_spec.rb +129 -97
  91. data/spec/riak/search_spec.rb +45 -62
  92. data/spec/riak/serializers_spec.rb +93 -0
  93. data/spec/riak/stamp_spec.rb +54 -0
  94. data/spec/riak/stream_parser_spec.rb +3 -16
  95. data/spec/riak/walk_spec_spec.rb +1 -14
  96. data/spec/spec_helper.rb +22 -27
  97. data/spec/support/http_backend_implementation_examples.rb +49 -79
  98. data/spec/support/integration_setup.rb +10 -0
  99. data/spec/support/mock_server.rb +0 -14
  100. data/spec/support/mocks.rb +0 -13
  101. data/spec/support/test_server.rb +30 -0
  102. data/spec/support/test_server.yml.example +14 -2
  103. data/spec/support/unified_backend_examples.rb +36 -27
  104. metadata +100 -31
  105. data/lib/riak/client/curb_backend.rb +0 -89
  106. data/spec/riak/curb_backend_spec.rb +0 -76
@@ -0,0 +1,77 @@
1
+ require 'riak/client'
2
+ require 'riak/util/translation'
3
+ require 'thread'
4
+
5
+ module Riak
6
+ # Implements a client-side form of monotonically-increasing k-sorted
7
+ # unique identifiers. These are useful for key generation if your
8
+ # data is time-sequential and needs to be sorted by key, perhaps in
9
+ # Riak Search. Inspired by Twitter's Snowflake project.
10
+ class Stamp
11
+ attr_reader :client
12
+
13
+ CLIENT_ID_MASK = (1 << 10) - 1
14
+ SEQUENCE_MASK = (1 << 12) - 1
15
+ TIMESTAMP_MASK = (1 << 41) - 1
16
+ SEQUENCE_SHIFT = 10
17
+ TIMESTAMP_SHIFT = 22
18
+
19
+ # @param [Client] client a {Riak::Client} which will be used for
20
+ # the "worker ID" component of the stamp.
21
+ # @see Client#stamp
22
+ def initialize(client)
23
+ @client = client
24
+ @mutex = Mutex.new
25
+ @timestamp = time_gen
26
+ @sequence = 0
27
+ end
28
+
29
+ # Generates a k-sorted unique ID for use as a key or other
30
+ # disambiguation purposes.
31
+ def next
32
+ @mutex.synchronize do
33
+ now = time_gen
34
+ if @timestamp == now
35
+ @sequence = (@sequence + 1) & SEQUENCE_MASK
36
+ now = wait_for_next_ms(@timestamp) if @sequence == 0
37
+ else
38
+ @sequence = 0
39
+ end
40
+
41
+ raise BackwardsClockError.new(@timestamp - now) if now < @timestamp
42
+
43
+ @timestamp = now
44
+ @timestamp << TIMESTAMP_SHIFT | @sequence << SEQUENCE_SHIFT | client_id
45
+ end
46
+ end
47
+
48
+ private
49
+ def client_id
50
+ case id = @client.client_id
51
+ when Integer
52
+ id & CLIENT_ID_MASK
53
+ else
54
+ id.hash & CLIENT_ID_MASK
55
+ end
56
+ end
57
+
58
+ def time_gen
59
+ (Time.now.to_f * 1000).floor & TIMESTAMP_MASK
60
+ end
61
+
62
+ def wait_for_next_ms(start)
63
+ now = time_gen
64
+ now = time_gen while now <= start
65
+ now
66
+ end
67
+ end
68
+
69
+ # Raised when calling {Stamp#next} and NTP or some other external
70
+ # event has moved the system clock backwards.
71
+ class BackwardsClockError < StandardError
72
+ include Util::Translation
73
+ def initialize(delay)
74
+ super t('backwards_clock', :delay => delay)
75
+ end
76
+ end
77
+ end
@@ -1,239 +1,75 @@
1
- # Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
1
+ require 'riak/node'
14
2
 
15
- require 'tempfile'
16
- require 'expect'
17
- require 'open3'
18
- require 'riak/util/tcp_socket_extensions'
3
+ if ENV['DEBUG_RIAK_TEST_SERVER']
4
+ $expect_verbose = true
5
+ end
19
6
 
20
7
  module Riak
21
- class TestServer
22
- APP_CONFIG_DEFAULTS = {
23
- :riak_core => {
24
- :web_ip => "127.0.0.1",
25
- :web_port => 9000,
26
- :handoff_port => 9001,
27
- :ring_creation_size => 64
28
- },
29
- :riak_kv => {
30
- :storage_backend => :riak_kv_test_backend,
31
- :pb_ip => "127.0.0.1",
32
- :pb_port => 9002,
33
- :js_vm_count => 8,
34
- :js_max_vm_mem => 8,
35
- :js_thread_stack => 16,
36
- :riak_kv_stat => true,
37
- # Turn off map caching
38
- :map_cache_size => 0, # 0.14
39
- :vnode_cache_entries => 0 # 0.13
40
- },
41
- :luwak => {
42
- :enabled => false
43
- }
44
- }
45
- VM_ARGS_DEFAULTS = {
46
- "-name" => "riaktest#{rand(1000000).to_s}@127.0.0.1",
47
- "-setcookie" => "#{rand(1000000).to_s}_#{rand(1000000).to_s}",
48
- "+K" => true,
49
- "+A" => 64,
50
- "-smp" => "enable",
51
- "-env ERL_MAX_PORTS" => 4096,
52
- "-env ERL_FULLSWEEP_AFTER" => 10,
53
- "-pa" => File.expand_path("../../../erl_src", __FILE__)
54
- }
55
- DEFAULTS = {
56
- :app_config => APP_CONFIG_DEFAULTS,
57
- :vm_args => VM_ARGS_DEFAULTS,
58
- :temp_dir => File.join(Dir.tmpdir,'riaktest'),
59
- }
60
- attr_accessor :temp_dir, :app_config, :vm_args, :cin, :cout, :cerr, :cpid
61
-
62
- def initialize(options={})
63
- options = deep_merge(DEFAULTS.dup, options)
64
- @temp_dir = File.expand_path(options[:temp_dir])
65
- @bin_dir = File.expand_path(options[:bin_dir])
66
- options[:app_config][:riak_core][:ring_state_dir] ||= File.join(@temp_dir, "data", "ring")
67
- @app_config = options[:app_config]
68
- @vm_args = options[:vm_args]
69
- # For synchronizing start/stop/recycle
70
- @mutex = Mutex.new
71
- cleanup # Should prevent some errors related to unclean startup
72
- end
73
-
74
- # Sets up the proper scripts, configuration and directories for
75
- # the test server. Call at the top of your test suite (not in a
76
- # setup method).
77
- def prepare!
78
- unless @prepared
79
- create_temp_directories
80
- @riak_script = File.join(@temp_bin, 'riak')
81
- write_riak_script
82
- write_vm_args
83
- write_app_config
84
- @prepared = true
85
- end
8
+ # The TestServer is a special {Node} that uses in-memory storage
9
+ # engines that are easily cleared. This is helpful when running test
10
+ # suites that store and retrieve objects from Riak and expect a
11
+ # clean-slate at the beginning of each test. Like {Node}, creation
12
+ # is idempotent, so you can keep the server around between runs of
13
+ # your test suite.
14
+ class TestServer < Node
15
+ # Creates a TestServer node, using in-memory backends for KV and Search.
16
+ def initialize(configuration = {})
17
+ configuration[:env] ||= {}
18
+ configuration[:env][:riak_kv] ||= {}
19
+ (configuration[:env][:riak_kv][:add_paths] ||= []) << File.expand_path("../../../erl_src", __FILE__)
20
+ configuration[:env][:riak_kv][:storage_backend] = :riak_kv_test_backend
21
+ configuration[:env][:riak_search] ||= {}
22
+ configuration[:env][:riak_search][:search_backend] = :riak_search_test_backend
23
+ super configuration
24
+ end
25
+
26
+ # Overrides the default {Node#started?} to simply return true if the
27
+ # console is still attached.
28
+ def started?
29
+ open? || super
86
30
  end
87
31
 
88
- # Starts the test server if it is not already running, and waits
89
- # for it to respond to pings.
32
+ # Overrides the default {Node#start} to return early if the
33
+ # console is still attached. Otherwise, starts and immediately
34
+ # attaches the console.
90
35
  def start
91
- if @prepared && !@started
92
- @mutex.synchronize do
93
- @cin, @cout, @cerr, @cpid = Open3.popen3("#{@riak_script} console")
94
- @cin.puts
95
- @cin.flush
96
- wait_for_erlang_prompt
97
- @started = true
98
- end
36
+ unless open?
37
+ super
38
+ maybe_attach
99
39
  end
100
40
  end
101
41
 
102
- # Stops the test server if it is running.
42
+ # Overrides the default {Node#stop} to close the console before
43
+ # stopping the node.
103
44
  def stop
104
- if @started
105
- @mutex.synchronize do
106
- begin
107
- @cin.puts "init:stop()."
108
- @cin.flush
109
- rescue Errno::EPIPE
110
- ensure
111
- register_stop
112
- end
113
- end
114
- true
115
- end
116
- end
117
-
118
- # Whether the server has been started.
119
- def started?
120
- @started
121
- end
122
-
123
- # Causes the entire contents of the Riak in-memory backends to be
124
- # dumped by performing a soft restart.
125
- def recycle
126
- if @started
127
- @mutex.synchronize do
128
- begin
129
- if @app_config[:riak_kv][:storage_backend] == :riak_kv_test_backend
130
- @cin.puts "riak_kv_test_backend:reset()."
131
- @cin.flush
132
- wait_for_erlang_prompt
133
- else
134
- @cin.puts "init:restart()."
135
- @cin.flush
136
- wait_for_erlang_prompt
137
- wait_for_startup
138
- end
139
- rescue Errno::EPIPE
140
- warn "Broken pipe when recycling, is Riak alive?"
141
- register_stop
142
- return false
143
- end
144
- end
145
- true
146
- else
147
- start
148
- end
149
- end
150
-
151
- # Cleans up any files and directories generated by the test
152
- # server.
153
- def cleanup
154
- stop if @started
155
- FileUtils.rm_rf(@temp_dir)
156
- @prepared = false
157
- end
158
-
159
- private
160
- def create_temp_directories
161
- %w{bin etc log data pipe}.each do |dir|
162
- instance_variable_set("@temp_#{dir}", File.expand_path(File.join(@temp_dir, dir)))
163
- FileUtils.mkdir_p(instance_variable_get("@temp_#{dir}"))
164
- end
165
- end
166
-
167
- def write_riak_script
168
- File.open(@riak_script, 'wb') do |f|
169
- File.readlines(File.join(@bin_dir, 'riak')).each do |line|
170
- line.sub!(/(RUNNER_SCRIPT_DIR=)(.*)/, '\1' + @temp_bin)
171
- line.sub!(/(RUNNER_ETC_DIR=)(.*)/, '\1' + @temp_etc)
172
- line.sub!(/(RUNNER_USER=)(.*)/, '\1')
173
- line.sub!(/(RUNNER_LOG_DIR=)(.*)/, '\1' + @temp_log)
174
- line.sub!(/(PIPE_DIR=)(.*)/, '\1' + @temp_pipe)
175
- if line.strip == "RUNNER_BASE_DIR=${RUNNER_SCRIPT_DIR%/*}"
176
- line = "RUNNER_BASE_DIR=#{File.expand_path("..",@bin_dir)}\n"
177
- end
178
- f.write line
179
- end
180
- end
181
- FileUtils.chmod(0755,@riak_script)
182
- end
183
-
184
- def write_vm_args
185
- File.open(File.join(@temp_etc, 'vm.args'), 'wb') do |f|
186
- f.write @vm_args.map {|k,v| "#{k} #{v}" }.join("\n")
187
- end
188
- end
189
-
190
- def write_app_config
191
- File.open(File.join(@temp_etc, 'app.config'), 'wb') do |f|
192
- f.write to_erlang_config(@app_config) + '.'
45
+ @console.close if @console && !@console.frozen?
46
+ @console = nil
47
+ super
48
+ end
49
+
50
+ # Overrides the default {Node#drop} to simply clear the in-memory
51
+ # backends.
52
+ def drop
53
+ begin
54
+ maybe_attach
55
+ @console.command "riak_kv_test_backend:reset()."
56
+ @console.command "riak_search_test_backend:reset()."
57
+ rescue IOError
58
+ retry
193
59
  end
194
60
  end
195
61
 
196
- def deep_merge(source, target)
197
- source.merge(target) do |key, old_val, new_val|
198
- if Hash === old_val && Hash === new_val
199
- deep_merge(old_val, new_val)
200
- else
201
- new_val
202
- end
62
+ protected
63
+ # Tries to reattach the console if it's closed
64
+ def maybe_attach
65
+ unless open?
66
+ @console.close if @console && !@console.frozen?
67
+ @console = attach
203
68
  end
204
69
  end
205
70
 
206
- def to_erlang_config(hash, depth = 1)
207
- padding = ' ' * depth
208
- parent_padding = ' ' * (depth-1)
209
- values = hash.map do |k,v|
210
- printable = case v
211
- when Hash
212
- to_erlang_config(v, depth+1)
213
- when String
214
- "\"#{v}\""
215
- else
216
- v.to_s
217
- end
218
- "{#{k}, #{printable}}"
219
- end.join(",\n#{padding}")
220
- "[\n#{padding}#{values}\n#{parent_padding}]"
221
- end
222
-
223
- def wait_for_startup
224
- TCPSocket.wait_for_service_with_timeout(:host => @app_config[:riak_core][:web_ip],
225
- :port => @app_config[:riak_core][:web_port], :timeout => 10)
226
- end
227
-
228
- def wait_for_erlang_prompt
229
- @cout.expect(/\(#{Regexp.escape(vm_args["-name"])}\)\d+>/)
230
- end
231
-
232
- def register_stop
233
- %w{@cin @cout @cerr}.each {|io| if instance_variable_get(io); instance_variable_get(io).close; instance_variable_set(io, nil) end }
234
- _cpid = @cpid; @cpid = nil
235
- at_exit { _cpid.join if _cpid && _cpid.alive? }
236
- @started = false
71
+ def open?
72
+ @console && @console.open?
237
73
  end
238
74
  end
239
75
  end
@@ -1,34 +1,75 @@
1
- # Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
1
  require 'cgi'
2
+ require 'uri'
15
3
 
16
4
  module Riak
5
+ class << self
6
+ # @see #escaper=
7
+ attr_reader :escaper
8
+
9
+ # Sets the class used for escaping URLs (buckets and keys) sent to
10
+ # Riak. Currently only supports URI and CGI, and defaults to URI.
11
+ # @param [Symbol,String,Class] esc A representation of which
12
+ # escaping class to use, either the Class itself or a String or
13
+ # Symbol name
14
+ # @see Riak::Util::Escape
15
+ def escaper=(esc)
16
+ case esc
17
+ when Symbol, String
18
+ @escaper = ::Object.const_get(esc.to_s.upcase.intern) if esc.to_s =~ /uri|cgi/i
19
+ when Class, Module
20
+ @escaper = esc
21
+ end
22
+ end
23
+
24
+ # In Riak 1.0+, buckets and keys are decoded internally before
25
+ # being stored. This increases compatibility with the Protocol
26
+ # Buffers transport and reduces inconsistency of link-walking
27
+ # vs. regular operations. If the node you are connecting to has
28
+ # set {http_url_encoding, on}, set this to true. Default is false.
29
+ # @return [true,false] Whether Riak decodes URL-encoded paths and headers
30
+ attr_accessor :url_decoding
31
+ end
32
+
33
+ self.escaper = URI
34
+ self.url_decoding = false
35
+
17
36
  module Util
18
37
  # Methods for escaping URL segments.
19
38
  module Escape
20
- # CGI-escapes bucket or key names that may contain slashes for use in URLs.
39
+ # Conditionally escapes buckets and keys depending on whether
40
+ # Riak is configured to decode them. This is used in situations
41
+ # where the bucket or key is not part of a URL, but would need
42
+ # to be escaped on Riak 0.14 and earlier so that the name
43
+ # matches.
44
+ # @param [String] bucket_or_key the bucket or key name
45
+ # @return [String] the escaped path segment
46
+ def maybe_escape(bucket_or_key)
47
+ Riak.url_decoding ? bucket_or_key : escape(bucket_or_key)
48
+ end
49
+
50
+ # Escapes bucket or key names that may contain slashes for use in URLs.
21
51
  # @param [String] bucket_or_key the bucket or key name
22
52
  # @return [String] the escaped path segment
23
53
  def escape(bucket_or_key)
24
- CGI.escape(bucket_or_key.to_s).gsub("+", "%20")
54
+ Riak.escaper.escape(bucket_or_key.to_s).gsub("+", "%20").gsub('/', "%2F")
55
+ end
56
+
57
+ # Conditionally unescapes buckets and keys depending on whether
58
+ # Riak is configured to decode them. This is used in situations
59
+ # where the bucket or key is not part of a URL, but would need
60
+ # to be escaped on Riak 0.14 and earlier so that the name
61
+ # matches.
62
+ # @param [String] bucket_or_key the escaped bucket or key name
63
+ # @return [String] the unescaped path segment
64
+ def maybe_unescape(bucket_or_key)
65
+ Riak.url_decoding ? bucket_or_key : unescape(bucket_or_key)
25
66
  end
26
67
 
27
- # CGI-unescapes bucket or key names
68
+ # Unescapes bucket or key names in URLs.
28
69
  # @param [String] bucket_or_key the bucket or key name
29
70
  # @return [String] the unescaped name
30
71
  def unescape(bucket_or_key)
31
- CGI.unescape(bucket_or_key.to_s)
72
+ Riak.escaper.unescape(bucket_or_key.to_s)
32
73
  end
33
74
  end
34
75
  end