riak-client 1.4.5 → 2.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +0 -1
- data/{LICENSE → LICENSE.md} +0 -0
- data/README.markdown +211 -66
- data/RELEASE_NOTES.md +22 -47
- data/Rakefile +45 -0
- data/lib/riak.rb +1 -1
- data/lib/riak/bucket.rb +2 -2
- data/lib/riak/client.rb +22 -195
- data/lib/riak/client/beefcake/crdt_loader.rb +127 -0
- data/lib/riak/client/beefcake/crdt_operator.rb +222 -0
- data/lib/riak/client/beefcake/footer +4 -0
- data/lib/riak/client/beefcake/header +6 -0
- data/lib/riak/client/beefcake/message_codes.rb +29 -0
- data/lib/riak/client/beefcake/message_overlay.rb +61 -0
- data/lib/riak/client/beefcake/messages.rb +733 -371
- data/lib/riak/client/beefcake/object_methods.rb +1 -1
- data/lib/riak/client/beefcake/protocol.rb +105 -0
- data/lib/riak/client/beefcake/socket.rb +243 -0
- data/lib/riak/client/beefcake_protobuffs_backend.rb +262 -122
- data/lib/riak/client/node.rb +4 -75
- data/lib/riak/client/protobuffs_backend.rb +6 -14
- data/lib/riak/client/search.rb +0 -64
- data/lib/riak/client/yokozuna.rb +52 -0
- data/lib/riak/counter.rb +1 -1
- data/lib/riak/crdt.rb +21 -0
- data/lib/riak/crdt/base.rb +97 -0
- data/lib/riak/crdt/batch_counter.rb +19 -0
- data/lib/riak/crdt/batch_map.rb +41 -0
- data/lib/riak/crdt/counter.rb +71 -0
- data/lib/riak/crdt/inner_counter.rb +74 -0
- data/lib/riak/crdt/inner_flag.rb +42 -0
- data/lib/riak/crdt/inner_map.rb +53 -0
- data/lib/riak/crdt/inner_register.rb +26 -0
- data/lib/riak/crdt/inner_set.rb +95 -0
- data/lib/riak/crdt/map.rb +88 -0
- data/lib/riak/crdt/operation.rb +19 -0
- data/lib/riak/crdt/set.rb +156 -0
- data/lib/riak/crdt/typed_collection.rb +131 -0
- data/lib/riak/errors/base.rb +9 -0
- data/lib/riak/errors/connection_error.rb +44 -0
- data/lib/riak/errors/crdt_error.rb +18 -0
- data/lib/riak/errors/failed_request.rb +56 -0
- data/lib/riak/errors/protobuffs_error.rb +11 -0
- data/lib/riak/i18n.rb +2 -0
- data/lib/riak/json.rb +1 -1
- data/lib/riak/locale/en.yml +26 -1
- data/lib/riak/locale/fr.yml +0 -1
- data/lib/riak/map_reduce.rb +1 -1
- data/lib/riak/map_reduce/results.rb +1 -1
- data/lib/riak/multiget.rb +1 -2
- data/lib/riak/rcontent.rb +8 -3
- data/lib/riak/robject.rb +2 -8
- data/lib/riak/secondary_index.rb +4 -4
- data/lib/riak/serializers.rb +1 -1
- data/lib/riak/util/escape.rb +3 -5
- data/lib/riak/version.rb +1 -1
- data/lib/riak/walk_spec.rb +7 -3
- data/riak-client.gemspec +10 -8
- data/spec/fixtures/bitcask.txt +25 -0
- data/spec/integration/riak/bucket_types_spec.rb +61 -0
- data/spec/integration/riak/counters_spec.rb +17 -32
- data/spec/integration/riak/crdt_spec.rb +181 -0
- data/spec/integration/riak/crdt_validation/map_spec.rb +63 -0
- data/spec/integration/riak/crdt_validation/set_spec.rb +122 -0
- data/spec/integration/riak/protobuffs_backends_spec.rb +9 -26
- data/spec/integration/riak/security_spec.rb +94 -0
- data/spec/integration/riak/threading_spec.rb +24 -67
- data/spec/integration/yokozuna/index_spec.rb +61 -0
- data/spec/integration/yokozuna/queries_spec.rb +116 -0
- data/spec/integration/yokozuna/schema_spec.rb +49 -0
- data/spec/riak/beefcake_protobuffs_backend/crdt_operator_spec.rb +222 -0
- data/spec/riak/beefcake_protobuffs_backend/object_methods_spec.rb +4 -4
- data/spec/riak/beefcake_protobuffs_backend/protocol_spec.rb +189 -0
- data/spec/riak/beefcake_protobuffs_backend/socket_spec.rb +151 -0
- data/spec/riak/beefcake_protobuffs_backend_spec.rb +68 -106
- data/spec/riak/bucket_spec.rb +81 -77
- data/spec/riak/client_spec.rb +43 -340
- data/spec/riak/core_ext/to_param_spec.rb +2 -2
- data/spec/riak/counter_spec.rb +20 -20
- data/spec/riak/crdt/counter_spec.rb +52 -0
- data/spec/riak/crdt/inner_counter_spec.rb +21 -0
- data/spec/riak/crdt/inner_flag_spec.rb +39 -0
- data/spec/riak/crdt/inner_map_spec.rb +47 -0
- data/spec/riak/crdt/inner_register_spec.rb +40 -0
- data/spec/riak/crdt/inner_set_spec.rb +33 -0
- data/spec/riak/crdt/map_spec.rb +77 -0
- data/spec/riak/crdt/set_spec.rb +58 -0
- data/spec/riak/crdt/shared_examples.rb +74 -0
- data/spec/riak/crdt/typed_collection_spec.rb +231 -0
- data/spec/riak/escape_spec.rb +33 -37
- data/spec/riak/feature_detection_spec.rb +45 -45
- data/spec/riak/index_collection_spec.rb +12 -12
- data/spec/riak/link_spec.rb +34 -34
- data/spec/riak/list_buckets_spec.rb +7 -7
- data/spec/riak/map_reduce/filter_builder_spec.rb +6 -6
- data/spec/riak/map_reduce/phase_spec.rb +35 -35
- data/spec/riak/map_reduce_spec.rb +89 -87
- data/spec/riak/multiget_spec.rb +20 -15
- data/spec/riak/node_spec.rb +5 -152
- data/spec/riak/robject_spec.rb +95 -108
- data/spec/riak/search_spec.rb +17 -139
- data/spec/riak/secondary_index_spec.rb +49 -49
- data/spec/riak/serializers_spec.rb +9 -9
- data/spec/riak/stamp_spec.rb +9 -9
- data/spec/riak/walk_spec_spec.rb +46 -46
- data/spec/spec_helper.rb +14 -22
- data/spec/support/certs/README.md +13 -0
- data/spec/support/certs/ca.crt +22 -0
- data/spec/support/certs/client.crt +95 -0
- data/spec/support/certs/client.key +27 -0
- data/spec/support/certs/empty_ca.crt +21 -0
- data/spec/support/certs/server.crl +13 -0
- data/spec/support/certs/server.crt +95 -0
- data/spec/support/certs/server.key +27 -0
- data/spec/support/integration_setup.rb +1 -1
- data/spec/support/search_corpus_setup.rb +29 -8
- data/spec/support/test_client.rb +46 -0
- data/spec/support/test_client.yml.example +10 -0
- data/spec/support/unified_backend_examples.rb +104 -83
- data/spec/support/version_filter.rb +2 -2
- data/spec/support/wait_until.rb +14 -0
- metadata +134 -132
- data/erl_src/riak_kv_test014_backend.beam +0 -0
- data/erl_src/riak_kv_test014_backend.erl +0 -189
- data/erl_src/riak_kv_test_backend.beam +0 -0
- data/erl_src/riak_kv_test_backend.erl +0 -731
- data/erl_src/riak_search_test_backend.beam +0 -0
- data/erl_src/riak_search_test_backend.erl +0 -175
- data/lib/riak/client/excon_backend.rb +0 -172
- data/lib/riak/client/http_backend.rb +0 -413
- data/lib/riak/client/http_backend/bucket_streamer.rb +0 -15
- data/lib/riak/client/http_backend/chunked_json_streamer.rb +0 -42
- data/lib/riak/client/http_backend/configuration.rb +0 -227
- data/lib/riak/client/http_backend/key_streamer.rb +0 -15
- data/lib/riak/client/http_backend/object_methods.rb +0 -114
- data/lib/riak/client/http_backend/request_headers.rb +0 -34
- data/lib/riak/client/http_backend/transport_methods.rb +0 -201
- data/lib/riak/client/instrumentation.rb +0 -25
- data/lib/riak/client/net_http_backend.rb +0 -82
- data/lib/riak/cluster.rb +0 -151
- data/lib/riak/failed_request.rb +0 -81
- data/lib/riak/instrumentation.rb +0 -6
- data/lib/riak/node.rb +0 -40
- data/lib/riak/node/configuration.rb +0 -304
- data/lib/riak/node/console.rb +0 -133
- data/lib/riak/node/control.rb +0 -207
- data/lib/riak/node/defaults.rb +0 -85
- data/lib/riak/node/generation.rb +0 -127
- data/lib/riak/node/log.rb +0 -34
- data/lib/riak/node/version.rb +0 -29
- data/lib/riak/search.rb +0 -3
- data/lib/riak/test_server.rb +0 -89
- data/lib/riak/util/headers.rb +0 -32
- data/lib/riak/util/multipart.rb +0 -52
- data/lib/riak/util/multipart/stream_parser.rb +0 -62
- data/spec/fixtures/munchausen.txt +0 -1033
- data/spec/integration/riak/cluster_spec.rb +0 -88
- data/spec/integration/riak/http_backends_spec.rb +0 -180
- data/spec/integration/riak/node_spec.rb +0 -170
- data/spec/integration/riak/test_server_spec.rb +0 -57
- data/spec/riak/excon_backend_spec.rb +0 -102
- data/spec/riak/headers_spec.rb +0 -21
- data/spec/riak/http_backend/configuration_spec.rb +0 -273
- data/spec/riak/http_backend/object_methods_spec.rb +0 -243
- data/spec/riak/http_backend/transport_methods_spec.rb +0 -97
- data/spec/riak/http_backend_spec.rb +0 -367
- data/spec/riak/instrumentation_spec.rb +0 -167
- data/spec/riak/multipart_spec.rb +0 -23
- data/spec/riak/net_http_backend_spec.rb +0 -15
- data/spec/riak/stream_parser_spec.rb +0 -53
- data/spec/support/drb_mock_server.rb +0 -39
- data/spec/support/http_backend_implementation_examples.rb +0 -253
- data/spec/support/mock_server.rb +0 -81
- data/spec/support/mocks.rb +0 -4
- data/spec/support/riak_test.rb +0 -77
- data/spec/support/sometimes.rb +0 -46
- data/spec/support/test_server.rb +0 -61
- data/spec/support/test_server.yml.example +0 -14
@@ -1,201 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'base64'
|
3
|
-
require 'uri'
|
4
|
-
require 'riak/client'
|
5
|
-
require 'riak/util/headers'
|
6
|
-
|
7
|
-
module Riak
|
8
|
-
class Client
|
9
|
-
class HTTPBackend
|
10
|
-
# Methods related to performing HTTP requests in a consistent
|
11
|
-
# fashion across multiple client libraries. HTTP/1.1 verbs are
|
12
|
-
# presented as methods.
|
13
|
-
module TransportMethods
|
14
|
-
# Performs a HEAD request to the specified resource on the Riak server.
|
15
|
-
# @param [Fixnum, Array] expect the expected HTTP response code(s) from Riak
|
16
|
-
# @param [String, Array<String,Hash>] resource a relative path or array of path segments and optional query params Hash that will be joined to the root URI
|
17
|
-
# @overload head(expect, *resource)
|
18
|
-
# @overload head(expect, *resource, headers)
|
19
|
-
# Send the request with custom headers
|
20
|
-
# @param [Hash] headers custom headers to send with the request
|
21
|
-
# @return [Hash] response data, containing only the :headers and :code keys
|
22
|
-
# @raise [FailedRequest] if the response code doesn't match the expected response
|
23
|
-
def head(expect, resource, headers={})
|
24
|
-
headers = default_headers.merge(headers)
|
25
|
-
perform(:head, resource, headers, expect)
|
26
|
-
end
|
27
|
-
|
28
|
-
# Performs a GET request to the specified resource on the Riak server.
|
29
|
-
# @param [Fixnum, Array] expect the expected HTTP response code(s) from Riak
|
30
|
-
# @param [String, Array<String,Hash>] resource a relative path or array of path segments and optional query params Hash that will be joined to the root URI
|
31
|
-
# @overload get(expect, *resource)
|
32
|
-
# @overload get(expect, *resource, headers)
|
33
|
-
# Send the request with custom headers
|
34
|
-
# @param [Hash] headers custom headers to send with the request
|
35
|
-
# @overload get(expect, *resource, headers={})
|
36
|
-
# Stream the response body through the supplied block
|
37
|
-
# @param [Hash] headers custom headers to send with the request
|
38
|
-
# @yield [chunk] yields successive chunks of the response body as strings
|
39
|
-
# @return [Hash] response data, containing only the :headers and :code keys
|
40
|
-
# @return [Hash] response data, containing :headers, :body, and :code keys
|
41
|
-
# @raise [FailedRequest] if the response code doesn't match the expected response
|
42
|
-
def get(expect, resource, headers={}, &block)
|
43
|
-
headers = default_headers.merge(headers)
|
44
|
-
perform(:get, resource, headers, expect, &block)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Performs a PUT request to the specified resource on the Riak server.
|
48
|
-
# @param [Fixnum, Array] expect the expected HTTP response code(s) from Riak
|
49
|
-
# @param [String, Array<String,Hash>] resource a relative path or array of path segments and optional query params Hash that will be joined to the root URI
|
50
|
-
# @param [String] body the request body to send to the server
|
51
|
-
# @overload put(expect, *resource, body)
|
52
|
-
# @overload put(expect, *resource, body, headers)
|
53
|
-
# Send the request with custom headers
|
54
|
-
# @param [Hash] headers custom headers to send with the request
|
55
|
-
# @overload put(expect, *resource, body, headers={})
|
56
|
-
# Stream the response body through the supplied block
|
57
|
-
# @param [Hash] headers custom headers to send with the request
|
58
|
-
# @yield [chunk] yields successive chunks of the response body as strings
|
59
|
-
# @return [Hash] response data, containing only the :headers and :code keys
|
60
|
-
# @return [Hash] response data, containing :headers, :code, and :body keys
|
61
|
-
# @raise [FailedRequest] if the response code doesn't match the expected response
|
62
|
-
def put(expect, resource, body, headers={}, &block)
|
63
|
-
headers = default_headers.merge(headers)
|
64
|
-
verify_body!(body)
|
65
|
-
perform(:put, resource, headers, expect, body, &block)
|
66
|
-
end
|
67
|
-
|
68
|
-
# Performs a POST request to the specified resource on the Riak server.
|
69
|
-
# @param [Fixnum, Array] expect the expected HTTP response code(s) from Riak
|
70
|
-
# @param [String, Array<String>] resource a relative path or array of path segments that will be joined to the root URI
|
71
|
-
# @param [String] body the request body to send to the server
|
72
|
-
# @overload post(expect, *resource, body)
|
73
|
-
# @overload post(expect, *resource, body, headers)
|
74
|
-
# Send the request with custom headers
|
75
|
-
# @param [Hash] headers custom headers to send with the request
|
76
|
-
# @overload post(expect, *resource, body, headers={})
|
77
|
-
# Stream the response body through the supplied block
|
78
|
-
# @param [Hash] headers custom headers to send with the request
|
79
|
-
# @yield [chunk] yields successive chunks of the response body as strings
|
80
|
-
# @return [Hash] response data, containing only the :headers and :code keys
|
81
|
-
# @return [Hash] response data, containing :headers, :code and :body keys
|
82
|
-
# @raise [FailedRequest] if the response code doesn't match the expected response
|
83
|
-
def post(expect, resource, body, headers={}, &block)
|
84
|
-
headers = default_headers.merge(headers)
|
85
|
-
verify_body!(body)
|
86
|
-
perform(:post, resource, headers, expect, body, &block)
|
87
|
-
end
|
88
|
-
|
89
|
-
# Performs a DELETE request to the specified resource on the Riak server.
|
90
|
-
# @param [Fixnum, Array] expect the expected HTTP response code(s) from Riak
|
91
|
-
# @param [String, Array<String,Hash>] resource a relative path or array of path segments and optional query params Hash that will be joined to the root URI
|
92
|
-
# @overload delete(expect, *resource)
|
93
|
-
# @overload delete(expect, *resource, headers)
|
94
|
-
# Send the request with custom headers
|
95
|
-
# @param [Hash] headers custom headers to send with the request
|
96
|
-
# @overload delete(expect, *resource, headers={})
|
97
|
-
# Stream the response body through the supplied block
|
98
|
-
# @param [Hash] headers custom headers to send with the request
|
99
|
-
# @yield [chunk] yields successive chunks of the response body as strings
|
100
|
-
# @return [Hash] response data, containing only the :headers and :code keys
|
101
|
-
# @return [Hash] response data, containing :headers, :code and :body keys
|
102
|
-
# @raise [FailedRequest] if the response code doesn't match the expected response
|
103
|
-
def delete(expect, resource, headers={}, &block)
|
104
|
-
headers = default_headers.merge(headers)
|
105
|
-
perform(:delete, resource, headers, expect, &block)
|
106
|
-
end
|
107
|
-
|
108
|
-
# Executes requests according to the underlying HTTP client library semantics.
|
109
|
-
# @abstract Subclasses must implement this internal method to perform HTTP requests
|
110
|
-
# according to the API of their HTTP libraries.
|
111
|
-
# @param [Symbol] method one of :head, :get, :post, :put, :delete
|
112
|
-
# @param [URI] uri the HTTP URI to request
|
113
|
-
# @param [Hash] headers headers to send along with the request
|
114
|
-
# @param [Fixnum, Array] expect the expected response code(s)
|
115
|
-
# @param [String, #read] body the PUT or POST request body
|
116
|
-
# @return [Hash] response data, containing :headers, :code and :body keys. Only :headers and :code should be present when the body is streamed or the method is :head.
|
117
|
-
# @yield [chunk] if the method is not :head, successive chunks of the response body will be yielded as strings
|
118
|
-
# @raise [NotImplementedError] if a subclass does not implement this method
|
119
|
-
def perform(method, uri, headers, expect, body=nil)
|
120
|
-
raise NotImplementedError
|
121
|
-
end
|
122
|
-
|
123
|
-
# Default header hash sent with every request, based on settings in the client
|
124
|
-
# @return [Hash] headers that will be merged with user-specified headers on every request
|
125
|
-
def default_headers
|
126
|
-
{
|
127
|
-
"Accept" => "multipart/mixed, application/json;q=0.7, */*;q=0.5",
|
128
|
-
"X-Riak-ClientId" => client_id
|
129
|
-
}.merge(basic_auth_header)
|
130
|
-
end
|
131
|
-
|
132
|
-
def client_id
|
133
|
-
value = @client.client_id
|
134
|
-
case value
|
135
|
-
when Integer
|
136
|
-
b64encode(value)
|
137
|
-
when String
|
138
|
-
value
|
139
|
-
end
|
140
|
-
end
|
141
|
-
|
142
|
-
def basic_auth_header
|
143
|
-
@node.basic_auth ? {"Authorization" => "Basic #{Base64::encode64(@node.basic_auth)}"} : {}
|
144
|
-
end
|
145
|
-
|
146
|
-
# @return [URI] The calculated root URI for the Riak HTTP endpoint
|
147
|
-
def root_uri
|
148
|
-
protocol = node.ssl_enabled? ? "https" : "http"
|
149
|
-
URI.parse("#{protocol}://#{node.host}:#{node.http_port}")
|
150
|
-
end
|
151
|
-
|
152
|
-
# Calculates an absolute URI from a relative path specification
|
153
|
-
# @param [Array<String,Hash>] segments a relative path or sequence of path segments and optional query params Hash that will be joined to the root URI
|
154
|
-
# @return [URI] an absolute URI for the resource
|
155
|
-
def path(*segments)
|
156
|
-
segments = segments.flatten
|
157
|
-
query = segments.extract_options!.to_param
|
158
|
-
root_uri.merge(segments.join("/").gsub(/\/+/, "/").sub(/^\//, '')).tap do |uri|
|
159
|
-
uri.query = query if query.present?
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
# Checks the expected response codes against the actual response code. Use internally when
|
164
|
-
# implementing {#perform}.
|
165
|
-
# @param [String, Fixnum, Array<String,Fixnum>] expected the expected response code(s)
|
166
|
-
# @param [String, Fixnum] actual the received response code
|
167
|
-
# @return [Boolean] whether the actual response code is acceptable given the expectations
|
168
|
-
def valid_response?(expected, actual)
|
169
|
-
Array(expected).map(&:to_i).include?(actual.to_i)
|
170
|
-
end
|
171
|
-
|
172
|
-
# Checks whether a combination of the HTTP method, response code, and block should
|
173
|
-
# result in returning the :body in the response hash. Use internally when implementing {#perform}.
|
174
|
-
# @param [Symbol] method the HTTP method
|
175
|
-
# @param [String, Fixnum] code the received response code
|
176
|
-
# @param [Boolean] has_block whether a streaming block was passed to {#perform}. Pass block_given? to this parameter.
|
177
|
-
# @return [Boolean] whether to return the body in the response hash
|
178
|
-
def return_body?(method, code, has_block)
|
179
|
-
method != :head && !valid_response?([204,205,304], code) && !has_block
|
180
|
-
end
|
181
|
-
|
182
|
-
# Checks whether the submitted body is valid. That is, it must
|
183
|
-
# be a String or respond to the 'read' method.
|
184
|
-
# @param [String, #read] body the body
|
185
|
-
# @raise [ArgumentError] if the body is of invalid type
|
186
|
-
def verify_body!(body)
|
187
|
-
raise ArgumentError, t('request_body_type') unless String === body || body.respond_to?(:read)
|
188
|
-
end
|
189
|
-
|
190
|
-
private
|
191
|
-
def response_headers
|
192
|
-
Thread.current[:response_headers] ||= Riak::Util::Headers.new
|
193
|
-
end
|
194
|
-
|
195
|
-
def b64encode(n)
|
196
|
-
Base64.encode64([n].pack("N")).chomp
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
class Riak::Client
|
2
|
-
include Instrumentable
|
3
|
-
|
4
|
-
client_payload = {protocol: :protocol, client_id: :client_id}
|
5
|
-
|
6
|
-
instrument_method :buckets, 'riak.list_buckets', client_payload
|
7
|
-
instrument_method :list_buckets, 'riak.list_buckets', client_payload
|
8
|
-
instrument_method :list_keys, 'riak.list_keys', client_payload
|
9
|
-
instrument_method :set_bucket_props, 'riak.set_bucket_props', client_payload
|
10
|
-
instrument_method :get_bucket_props, 'riak.get_bucket_props', client_payload
|
11
|
-
instrument_method :clear_bucket_props, 'riak.clear_bucket_props', client_payload
|
12
|
-
instrument_method :get_index, 'riak.get_index', client_payload
|
13
|
-
instrument_method :store_object, 'riak.store_object', client_payload
|
14
|
-
instrument_method :get_object, 'riak.get_object', client_payload
|
15
|
-
instrument_method :reload_object, 'riak.reload_object', client_payload
|
16
|
-
instrument_method :delete_object, 'riak.delete_object', client_payload
|
17
|
-
instrument_method :store_file, 'riak.store_file', client_payload
|
18
|
-
instrument_method :get_file, 'riak.get_file', client_payload
|
19
|
-
instrument_method :delete_file, 'riak.delete_file', client_payload
|
20
|
-
instrument_method :file_exists?, 'riak.file_exists', client_payload
|
21
|
-
instrument_method :file_exist?, 'riak.file_exists', client_payload
|
22
|
-
instrument_method :link_walk, 'riak.link_walk', client_payload
|
23
|
-
instrument_method :mapred, 'riak.map_reduce', client_payload
|
24
|
-
instrument_method :ping, 'riak.ping', client_payload
|
25
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require 'riak/client/http_backend'
|
2
|
-
require 'riak/failed_request'
|
3
|
-
|
4
|
-
module Riak
|
5
|
-
class Client
|
6
|
-
# Uses the Ruby standard library Net::HTTP to connect to Riak.
|
7
|
-
# Conforms to the Riak::Client::HTTPBackend interface.
|
8
|
-
class NetHTTPBackend < HTTPBackend
|
9
|
-
def self.configured?
|
10
|
-
begin
|
11
|
-
require 'net/http'
|
12
|
-
require 'openssl'
|
13
|
-
true
|
14
|
-
rescue LoadError, NameError
|
15
|
-
false
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Sets the read_timeout applied to Net::HTTP connections
|
20
|
-
# Increase this if you have very long request times.
|
21
|
-
def self.read_timeout=(timeout)
|
22
|
-
@read_timeout = timeout
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.read_timeout
|
26
|
-
@read_timeout ||= 4096
|
27
|
-
end
|
28
|
-
|
29
|
-
# Net::HTTP doesn't use persistent connections, so there's no
|
30
|
-
# work to do here.
|
31
|
-
def teardown; end
|
32
|
-
|
33
|
-
private
|
34
|
-
def perform(method, uri, headers, expect, data=nil) #:nodoc:
|
35
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
36
|
-
http.read_timeout = self.class.read_timeout
|
37
|
-
configure_ssl(http) if @node.ssl_enabled?
|
38
|
-
|
39
|
-
request = Net::HTTP.const_get(method.to_s.capitalize).new(uri.request_uri, headers)
|
40
|
-
if String === data
|
41
|
-
request.body = data
|
42
|
-
elsif data.respond_to?(:read)
|
43
|
-
case
|
44
|
-
when data.respond_to?(:stat) # IO#stat
|
45
|
-
request.content_length = data.stat.size
|
46
|
-
when data.respond_to?(:size) # Some IO-like objects
|
47
|
-
request.content_length = data.size
|
48
|
-
else
|
49
|
-
request['Transfer-Encoding'] = 'chunked'
|
50
|
-
end
|
51
|
-
request.body_stream = data
|
52
|
-
end
|
53
|
-
|
54
|
-
{}.tap do |result|
|
55
|
-
http.start # Fixes Ruby < 1.8.7p315
|
56
|
-
http.request(request) do |response|
|
57
|
-
unless valid_response?(expect, response.code)
|
58
|
-
raise Riak::HTTPFailedRequest.new(method, expect, response.code.to_i, response.to_hash, response.body)
|
59
|
-
end
|
60
|
-
|
61
|
-
result.merge!({:headers => response.to_hash, :code => response.code.to_i})
|
62
|
-
response.read_body {|chunk| yield chunk } if block_given?
|
63
|
-
if return_body?(method, response.code, block_given?)
|
64
|
-
result[:body] = response.body
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
def configure_ssl(http)
|
71
|
-
http.use_ssl = true
|
72
|
-
http.verify_mode = OpenSSL::SSL.const_get("VERIFY_#{@node.ssl_options[:verify_mode].upcase}")
|
73
|
-
if @node.ssl_options[:pem]
|
74
|
-
http.cert = OpenSSL::X509::Certificate.new(@node.ssl_options[:pem])
|
75
|
-
http.key = OpenSSL::PKey::RSA.new(@node.ssl_options[:pem], @node.ssl_options[:pem_password])
|
76
|
-
end
|
77
|
-
http.ca_file = @node.ssl_options[:ca_file] if @node.ssl_options[:ca_file]
|
78
|
-
http.ca_path = @node.ssl_options[:ca_path] if @node.ssl_options[:ca_path]
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
data/lib/riak/cluster.rb
DELETED
@@ -1,151 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require 'riak/node'
|
3
|
-
require 'riak/util/translation'
|
4
|
-
|
5
|
-
module Riak
|
6
|
-
# Generates and controls a cluster of {Riak::Node} instances for use
|
7
|
-
# in development or testing on a single machine.
|
8
|
-
class Cluster
|
9
|
-
include Util::Translation
|
10
|
-
# @return [Array<Node>] the member Nodes of this cluster
|
11
|
-
attr_reader :nodes
|
12
|
-
|
13
|
-
# @return [Hash] the cluster configuration
|
14
|
-
attr_reader :configuration
|
15
|
-
|
16
|
-
# @return [Pathname] the root directory of the cluster
|
17
|
-
attr_reader :root
|
18
|
-
|
19
|
-
# Creates a {Cluster} of {Node}s.
|
20
|
-
# @param [Hash] config the configuration for the cluster
|
21
|
-
# @option config [Fixnum] :count the number of nodes to create
|
22
|
-
# @option config [String] :source path to the Riak bin/ directory.
|
23
|
-
# See {Node#source}.
|
24
|
-
# @option config [String] :root path to where the nodes will be
|
25
|
-
# generated.
|
26
|
-
# @option config [Fixnum] :min_port the base port number from
|
27
|
-
# which nodes will claim IP ports for HTTP, PB, handoff.
|
28
|
-
def initialize(config={})
|
29
|
-
raise ArgumentError, t('source_and_root_required') unless config[:source] && config[:root]
|
30
|
-
@configuration = config
|
31
|
-
@count = config.delete(:count) || 4
|
32
|
-
@min_port = config.delete(:min_port) || 9000
|
33
|
-
@root = Pathname.new(config.delete(:root))
|
34
|
-
@nodes = []
|
35
|
-
cookie = "#{rand(100000).to_s}_#{rand(1000000).to_s}"
|
36
|
-
@count.times do |i|
|
37
|
-
nodes << Riak::Node.new(config.merge(:min_port => @min_port + (i * 3),
|
38
|
-
:root => @root + (i+1).to_s,
|
39
|
-
:cookie => cookie))
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# @return [true,false] whether the cluster has been created
|
44
|
-
def exist?
|
45
|
-
root.directory? && nodes.all? {|n| n.exist? }
|
46
|
-
end
|
47
|
-
|
48
|
-
# Generates all nodes in the cluster.
|
49
|
-
def create
|
50
|
-
unless exist?
|
51
|
-
root.mkpath unless root.exist?
|
52
|
-
nodes.each {|n| n.create }
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# Removes all nodes in the cluster and the root, and freezes the
|
57
|
-
# object.
|
58
|
-
def destroy
|
59
|
-
nodes.each {|n| n.destroy }
|
60
|
-
root.rmtree if root.exist?
|
61
|
-
freeze
|
62
|
-
end
|
63
|
-
|
64
|
-
# Removes and recreates the cluster.
|
65
|
-
def recreate
|
66
|
-
stop unless stopped?
|
67
|
-
root.rmtree if root.exist?
|
68
|
-
create
|
69
|
-
end
|
70
|
-
|
71
|
-
# Drops all data from the cluster without destroying the nodes.
|
72
|
-
def drop
|
73
|
-
nodes.each {|n| n.drop }
|
74
|
-
end
|
75
|
-
|
76
|
-
# Starts all nodes in the cluster.
|
77
|
-
def start
|
78
|
-
nodes.each {|n| n.start }
|
79
|
-
end
|
80
|
-
|
81
|
-
# Stops all nodes in the cluster.
|
82
|
-
def stop
|
83
|
-
nodes.each {|n| n.stop }
|
84
|
-
end
|
85
|
-
|
86
|
-
# Restarts all nodes in the cluster (without exiting the Erlang
|
87
|
-
# runtime)
|
88
|
-
def restart
|
89
|
-
nodes.each {|n| n.restart }
|
90
|
-
end
|
91
|
-
|
92
|
-
# Reboots all nodes in the cluster
|
93
|
-
def reboot
|
94
|
-
nodes.each {|n| n.reboot }
|
95
|
-
end
|
96
|
-
|
97
|
-
# Forces the cluster nodes to restart/reload their JavaScript VMs,
|
98
|
-
# effectively reloading any user-provided code.
|
99
|
-
def js_reload
|
100
|
-
nodes.each {|n| n.js_reload }
|
101
|
-
end
|
102
|
-
|
103
|
-
# Attaches to the console on all nodes, returning a list of
|
104
|
-
# {Riak::Node::Console} objects.
|
105
|
-
# @return [Array<Riak::Node::Console>] consoles for all running
|
106
|
-
# nodes, with nil for nodes that aren't running or otherwise
|
107
|
-
# fail to connect
|
108
|
-
def attach
|
109
|
-
nodes.map do |n|
|
110
|
-
begin
|
111
|
-
n.attach
|
112
|
-
rescue ArgumentError, SystemCallError
|
113
|
-
nil
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
# Executes the given block on each node against the node's
|
119
|
-
# console. You could use this to send Erlang commands to all nodes
|
120
|
-
# in the cluster.
|
121
|
-
# @yield [console] A block of commands to be run against the
|
122
|
-
# console
|
123
|
-
# @yieldparam [Riak::Node::Console] console A console manager for
|
124
|
-
# sending commands to the current node in the iteration
|
125
|
-
def with_console(&block)
|
126
|
-
nodes.each do |n|
|
127
|
-
n.with_console(&block)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
# Is the cluster started?
|
132
|
-
def started?
|
133
|
-
nodes.all? {|n| n.started? }
|
134
|
-
end
|
135
|
-
|
136
|
-
# Is the cluster stopped?
|
137
|
-
def stopped?
|
138
|
-
nodes.all? {|n| n.stopped? }
|
139
|
-
end
|
140
|
-
|
141
|
-
# Joins the nodes together into a cluster.
|
142
|
-
# @note This method relies on cluster membership changes present
|
143
|
-
# in the 1.0 series of Riak, and is NOT safe on 0.14 and
|
144
|
-
# earlier.
|
145
|
-
def join
|
146
|
-
claimant = nodes.first.name # Not really the claimant, just a
|
147
|
-
# node to join to
|
148
|
-
nodes[1..-1].each {|n| n.join(claimant) unless n.peers.include?(claimant) }
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|