dalli 5.1.10 → 5.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -122
- data/README.md +20 -25
- data/lib/dalli/cache_result.rb +65 -0
- data/lib/dalli/client.rb +87 -61
- data/lib/dalli/compressor.rb +5 -53
- data/lib/dalli/key_manager.rb +2 -23
- data/lib/dalli/options.rb +1 -1
- data/lib/dalli/pipelined_getter.rb +21 -44
- data/lib/dalli/pipelined_setter.rb +8 -17
- data/lib/dalli/protocol/base.rb +26 -145
- data/lib/dalli/protocol/connection_manager.rb +13 -121
- data/lib/dalli/protocol/meta.rb +64 -127
- data/lib/dalli/protocol/request_formatter.rb +30 -34
- data/lib/dalli/protocol/response_buffer.rb +1 -4
- data/lib/dalli/protocol/response_processor.rb +106 -98
- data/lib/dalli/protocol/string_marshaller.rb +1 -3
- data/lib/dalli/protocol/value_compressor.rb +7 -36
- data/lib/dalli/protocol/value_marshaller.rb +0 -5
- data/lib/dalli/protocol/value_serializer.rb +1 -1
- data/lib/dalli/protocol.rb +9 -7
- data/lib/dalli/ring.rb +44 -17
- data/lib/dalli/socket.rb +9 -64
- data/lib/dalli/version.rb +1 -1
- data/lib/dalli.rb +1 -5
- metadata +4 -7
- data/lib/dalli/json_serializer.rb +0 -28
data/lib/dalli/compressor.rb
CHANGED
|
@@ -13,54 +13,8 @@ module Dalli
|
|
|
13
13
|
Zlib::Deflate.deflate(data)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# Compressed input is fed to zlib this many bytes at a time. MRI's zlib
|
|
19
|
-
# yields the output in small chunks; one that returns it instead still
|
|
20
|
-
# produces at most MAX_EXPANSION times this per step.
|
|
21
|
-
INFLATE_SLICE_BYTES = 64 * 1024
|
|
22
|
-
|
|
23
|
-
# max_bytes caps the decompressed size; inflating stops and raises as soon
|
|
24
|
-
# as the output passes it, so a small compressed value can't expand into
|
|
25
|
-
# gigabytes of memory.
|
|
26
|
-
def self.decompress(data, max_bytes: nil)
|
|
27
|
-
return Zlib::Inflate.inflate(data) unless may_exceed?(data, max_bytes)
|
|
28
|
-
|
|
29
|
-
inflate_within_limit(Zlib::Inflate.new, data, max_bytes)
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
# False when even the largest possible expansion stays within max_bytes,
|
|
33
|
-
# so the value can be inflated in one step
|
|
34
|
-
def self.may_exceed?(data, max_bytes)
|
|
35
|
-
max_bytes ? data.bytesize * MAX_EXPANSION > max_bytes : false
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def self.inflate_within_limit(inflater, data, max_bytes)
|
|
39
|
-
out = String.new(encoding: Encoding::BINARY)
|
|
40
|
-
0.step(data.bytesize - 1, INFLATE_SLICE_BYTES) do |pos|
|
|
41
|
-
inflate_slice(inflater, data.byteslice(pos, INFLATE_SLICE_BYTES), out, max_bytes)
|
|
42
|
-
# Anything after the end of the stream isn't part of the value, as
|
|
43
|
-
# with Zlib::Inflate.inflate; finish would return it as output
|
|
44
|
-
break if inflater.finished?
|
|
45
|
-
end
|
|
46
|
-
return out if inflater.finished?
|
|
47
|
-
|
|
48
|
-
append_within_limit(out, inflater.finish, max_bytes)
|
|
49
|
-
ensure
|
|
50
|
-
inflater.close
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
# Both yielded and returned output count toward the limit
|
|
54
|
-
def self.inflate_slice(inflater, slice, out, max_bytes)
|
|
55
|
-
rest = inflater.inflate(slice) { |chunk| append_within_limit(out, chunk, max_bytes) }
|
|
56
|
-
append_within_limit(out, rest, max_bytes)
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def self.append_within_limit(out, chunk, max_bytes)
|
|
60
|
-
out << chunk if chunk
|
|
61
|
-
return out if out.bytesize <= max_bytes
|
|
62
|
-
|
|
63
|
-
raise Dalli::UnmarshalError, "Decompressed value exceeds #{max_bytes} bytes (decompressed_max_bytes)"
|
|
16
|
+
def self.decompress(data)
|
|
17
|
+
Zlib::Inflate.inflate(data)
|
|
64
18
|
end
|
|
65
19
|
end
|
|
66
20
|
|
|
@@ -78,11 +32,9 @@ module Dalli
|
|
|
78
32
|
io.string
|
|
79
33
|
end
|
|
80
34
|
|
|
81
|
-
def self.decompress(data
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
# MAX_WBITS + 16 makes zlib read the gzip format (and check its CRC)
|
|
85
|
-
Compressor.inflate_within_limit(Zlib::Inflate.new(Zlib::MAX_WBITS + 16), data, max_bytes)
|
|
35
|
+
def self.decompress(data)
|
|
36
|
+
io = StringIO.new(data, 'rb')
|
|
37
|
+
Zlib::GzipReader.new(io).read
|
|
86
38
|
end
|
|
87
39
|
end
|
|
88
40
|
end
|
data/lib/dalli/key_manager.rb
CHANGED
|
@@ -58,7 +58,7 @@ module Dalli
|
|
|
58
58
|
raise ArgumentError, 'key cannot be blank' unless key&.length&.positive?
|
|
59
59
|
|
|
60
60
|
key = key_with_namespace(key)
|
|
61
|
-
|
|
61
|
+
key.length > MAX_KEY_LENGTH ? truncated_key(key) : key
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
##
|
|
@@ -122,28 +122,7 @@ module Dalli
|
|
|
122
122
|
##
|
|
123
123
|
def truncated_key(key)
|
|
124
124
|
digest = digest_class.hexdigest(key)
|
|
125
|
-
|
|
126
|
-
truncated = "#{prefix}#{TRUNCATED_KEY_SEPARATOR}#{digest}"
|
|
127
|
-
# A prefix with whitespace or non-ASCII characters sends the whole key
|
|
128
|
-
# base64-encoded, which can still be too long on the wire; shorten it.
|
|
129
|
-
while wire_length(truncated) > MAX_KEY_LENGTH && !prefix.empty?
|
|
130
|
-
prefix = prefix[0...-1]
|
|
131
|
-
truncated = "#{prefix}#{TRUNCATED_KEY_SEPARATOR}#{digest}"
|
|
132
|
-
end
|
|
133
|
-
return truncated if wire_length(truncated) <= MAX_KEY_LENGTH
|
|
134
|
-
|
|
135
|
-
# Only possible with a digest_class whose digests aren't short hex strings
|
|
136
|
-
raise ArgumentError, "#{digest_class} digests are too long to fit in a truncated key"
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
# memcached's key limit applies to the key as sent: in bytes, and after
|
|
140
|
-
# base64 encoding when the key needs it. A key can be within the limit in
|
|
141
|
-
# characters and still be rejected, and memcached's CLIENT_ERROR for it
|
|
142
|
-
# must never reach a pipelined get.
|
|
143
|
-
def wire_length(key)
|
|
144
|
-
return key.bytesize unless Dalli::Protocol::Meta::KeyRegularizer.required?(key)
|
|
145
|
-
|
|
146
|
-
((key.bytesize + 2) / 3) * 4
|
|
125
|
+
"#{key[0, prefix_length(digest)]}#{TRUNCATED_KEY_SEPARATOR}#{digest}"
|
|
147
126
|
end
|
|
148
127
|
|
|
149
128
|
def prefix_length(digest)
|
data/lib/dalli/options.rb
CHANGED
|
@@ -32,21 +32,15 @@ module Dalli
|
|
|
32
32
|
return {} if keys.empty?
|
|
33
33
|
|
|
34
34
|
@ring.lock do
|
|
35
|
-
#
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
@partial_results = {}
|
|
40
|
-
setup_requests(servers, keys, req_options, return_cas: return_cas)
|
|
41
|
-
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
35
|
+
# Stores partial results collected during interleaved send phase
|
|
36
|
+
@partial_results = {}
|
|
37
|
+
servers = setup_requests(keys, req_options, return_cas: return_cas)
|
|
38
|
+
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
42
39
|
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
# First yield any partial results collected during interleaved send
|
|
41
|
+
yield_partial_results(&block)
|
|
45
42
|
|
|
46
|
-
|
|
47
|
-
ensure
|
|
48
|
-
abort_unfinished(servers)
|
|
49
|
-
end
|
|
43
|
+
servers = fetch_responses(servers, start_time, @ring.socket_timeout, &block) until servers.empty?
|
|
50
44
|
end
|
|
51
45
|
rescue Dalli::RetryableNetworkError => e
|
|
52
46
|
Dalli.logger.debug { e.inspect }
|
|
@@ -99,9 +93,9 @@ module Dalli
|
|
|
99
93
|
|
|
100
94
|
# Sends each server's queries and its terminating noop before building
|
|
101
95
|
# the next server's, so memcached is already answering the first servers
|
|
102
|
-
# while the rest are prepared.
|
|
103
|
-
|
|
104
|
-
|
|
96
|
+
# while the rest are prepared. Returns the servers with a pending response.
|
|
97
|
+
def setup_requests(keys, req_options = nil, return_cas: true)
|
|
98
|
+
started = []
|
|
105
99
|
groups_for_keys(keys).each do |server, keys_for_server|
|
|
106
100
|
make_getkq_request(server, keys_for_server, req_options, return_cas: return_cas)
|
|
107
101
|
next unless server.connected?
|
|
@@ -114,6 +108,7 @@ module Dalli
|
|
|
114
108
|
rescue Dalli::DalliError
|
|
115
109
|
started.delete(server)
|
|
116
110
|
end
|
|
111
|
+
started
|
|
117
112
|
end
|
|
118
113
|
|
|
119
114
|
##
|
|
@@ -157,39 +152,29 @@ module Dalli
|
|
|
157
152
|
raise
|
|
158
153
|
end
|
|
159
154
|
|
|
160
|
-
# Swallows Dalli::NetworkError
|
|
161
|
-
# again.
|
|
155
|
+
# Swallows Dalli::NetworkError
|
|
162
156
|
def abort_without_timeout(servers)
|
|
163
157
|
servers.each(&:pipeline_abort)
|
|
164
|
-
servers.clear
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# When #process stops before reading every server's replies (the caller's
|
|
168
|
-
# block raises or breaks, a Timeout.timeout fires, a value can't be
|
|
169
|
-
# deserialized), the rest are still on the socket and some may be in the
|
|
170
|
-
# response buffer. Closing those connections keeps them from being read
|
|
171
|
-
# as a later request's replies. Aborts that #process already handled
|
|
172
|
-
# emptied servers.
|
|
173
|
-
def abort_unfinished(servers)
|
|
174
|
-
servers.each { |server| server.pipeline_abort(timed_out: false) }
|
|
175
|
-
servers.clear
|
|
176
158
|
end
|
|
177
159
|
|
|
178
|
-
# Reads the replies available, removing finished servers from servers
|
|
179
160
|
def fetch_responses(servers, start_time, timeout, &block)
|
|
180
161
|
# Remove any servers which are not connected
|
|
181
162
|
servers.select!(&:connected?)
|
|
182
|
-
return if servers.empty?
|
|
163
|
+
return [] if servers.empty?
|
|
183
164
|
|
|
184
165
|
time_left = remaining_time(start_time, timeout)
|
|
185
166
|
readable_servers = servers_with_response(servers, time_left)
|
|
186
|
-
|
|
167
|
+
if readable_servers.empty?
|
|
168
|
+
abort_with_timeout(servers)
|
|
169
|
+
return []
|
|
170
|
+
end
|
|
187
171
|
|
|
188
172
|
# Loop through the servers with responses, and
|
|
189
173
|
# delete any from our list that are finished
|
|
190
174
|
readable_servers.each do |server|
|
|
191
175
|
servers.delete(server) if process_server(server, &block)
|
|
192
176
|
end
|
|
177
|
+
servers
|
|
193
178
|
rescue NetworkError
|
|
194
179
|
# Abort and raise if we encountered a network error. This triggers
|
|
195
180
|
# a retry at the top level on RetryableNetworkError.
|
|
@@ -206,10 +191,12 @@ module Dalli
|
|
|
206
191
|
|
|
207
192
|
# Swallows Dalli::NetworkError
|
|
208
193
|
def abort_with_timeout(servers)
|
|
194
|
+
abort_without_timeout(servers)
|
|
209
195
|
servers.each do |server|
|
|
210
196
|
Dalli.logger.debug { "memcached at #{server.name} did not response within timeout" }
|
|
211
197
|
end
|
|
212
|
-
|
|
198
|
+
|
|
199
|
+
true # Required to simplify caller
|
|
213
200
|
end
|
|
214
201
|
|
|
215
202
|
# Processes responses from a server. Returns true if there are no
|
|
@@ -225,16 +212,6 @@ module Dalli
|
|
|
225
212
|
def servers_with_response(servers, timeout)
|
|
226
213
|
return [] if servers.empty?
|
|
227
214
|
|
|
228
|
-
# A bounded read can stop while OpenSSL still holds decrypted bytes.
|
|
229
|
-
# IO.select only sees the underlying socket, so process those bytes
|
|
230
|
-
# before waiting for another network event.
|
|
231
|
-
buffered = nil
|
|
232
|
-
servers.each do |server|
|
|
233
|
-
sock = server.sock
|
|
234
|
-
(buffered ||= []) << server if sock.respond_to?(:pending_data?) && sock.pending_data?
|
|
235
|
-
end
|
|
236
|
-
return buffered if buffered
|
|
237
|
-
|
|
238
215
|
sockets = servers.map(&:sock)
|
|
239
216
|
readable, = IO.select(sockets, nil, nil, timeout)
|
|
240
217
|
return [] if readable.nil?
|
|
@@ -18,9 +18,6 @@ module Dalli
|
|
|
18
18
|
# A transient network error is retried automatically. If a server remains
|
|
19
19
|
# unreachable after retrying, raises Dalli::NetworkError.
|
|
20
20
|
#
|
|
21
|
-
# A value that can't be marshalled or is over value_max_bytes is skipped;
|
|
22
|
-
# the other keys are still stored, then the first such error is raised.
|
|
23
|
-
#
|
|
24
21
|
# @param hash [Hash] key-value pairs to set
|
|
25
22
|
# @param ttl [Integer] time-to-live in seconds
|
|
26
23
|
# @param req_options [Hash] options passed to each set operation
|
|
@@ -29,12 +26,10 @@ module Dalli
|
|
|
29
26
|
def process(hash, ttl, req_options)
|
|
30
27
|
return if hash.empty?
|
|
31
28
|
|
|
32
|
-
@value_error = nil
|
|
33
29
|
@ring.lock do
|
|
34
30
|
servers = setup_requests(hash, ttl, req_options)
|
|
35
31
|
finish_requests(servers)
|
|
36
32
|
end
|
|
37
|
-
raise @value_error if @value_error
|
|
38
33
|
rescue Dalli::RetryableNetworkError => e
|
|
39
34
|
Dalli.logger.debug { e.inspect }
|
|
40
35
|
Dalli.logger.debug { 'retrying pipelined sets because of network error' }
|
|
@@ -43,12 +38,9 @@ module Dalli
|
|
|
43
38
|
|
|
44
39
|
private
|
|
45
40
|
|
|
46
|
-
# Values are looked up by each key as sent. The caller's key can't be
|
|
47
|
-
# rebuilt from it: a long key is digested, and a Symbol becomes a String.
|
|
48
41
|
def setup_requests(hash, ttl, req_options)
|
|
49
|
-
|
|
50
|
-
groups
|
|
51
|
-
make_set_requests(groups, pairs, ttl, req_options)
|
|
42
|
+
groups = groups_for_keys(hash.keys)
|
|
43
|
+
make_set_requests(groups, hash, ttl, req_options)
|
|
52
44
|
groups.keys
|
|
53
45
|
end
|
|
54
46
|
|
|
@@ -62,16 +54,14 @@ module Dalli
|
|
|
62
54
|
# RetryableNetworkError too -- since NetworkError < DalliError -- dropping
|
|
63
55
|
# this server's keys on a transient hiccup instead of retrying. Only a
|
|
64
56
|
# non-network DalliError should be swallowed here.
|
|
65
|
-
def make_set_requests(groups,
|
|
57
|
+
def make_set_requests(groups, hash, ttl, req_options)
|
|
66
58
|
groups.each do |server, keys_for_server|
|
|
67
59
|
keys_for_server.each do |key|
|
|
68
|
-
|
|
60
|
+
original_key = @key_manager.key_without_namespace(key)
|
|
61
|
+
value = hash[original_key]
|
|
62
|
+
server.request(:pipelined_set, key, value, ttl, req_options)
|
|
69
63
|
rescue Dalli::NetworkError
|
|
70
64
|
raise
|
|
71
|
-
rescue Dalli::MarshalError, Dalli::ValueOverMaxSize => e
|
|
72
|
-
# Nothing was written for this key, so the server's other keys are
|
|
73
|
-
# unaffected; raised by #process once the batch is complete
|
|
74
|
-
@value_error ||= e
|
|
75
65
|
rescue DalliError => e
|
|
76
66
|
Dalli.logger.debug { e.inspect }
|
|
77
67
|
Dalli.logger.debug { "unable to set key #{key} for server #{server.name}" }
|
|
@@ -94,7 +84,8 @@ module Dalli
|
|
|
94
84
|
end
|
|
95
85
|
|
|
96
86
|
def groups_for_keys(keys)
|
|
97
|
-
|
|
87
|
+
validated_keys = keys.map { |k| @key_manager.validate_key(k.to_s) }
|
|
88
|
+
groups = @ring.keys_grouped_by_server(validated_keys)
|
|
98
89
|
|
|
99
90
|
if (unfound_keys = groups.delete(nil))
|
|
100
91
|
Dalli.logger.debug do
|
data/lib/dalli/protocol/base.rb
CHANGED
|
@@ -18,7 +18,8 @@ module Dalli
|
|
|
18
18
|
|
|
19
19
|
def_delegators :@value_marshaller, :serializer, :compressor, :compression_min_size, :compress_by_default?
|
|
20
20
|
def_delegators :@connection_manager, :name, :hostname, :port, :close, :socket_timeout,
|
|
21
|
-
:socket_type, :up!, :down!, :write, :reconnect_down_server?, :raise_down_error
|
|
21
|
+
:socket_type, :up!, :down!, :write, :reconnect_down_server?, :raise_down_error,
|
|
22
|
+
:quiet_responses_pending?
|
|
22
23
|
|
|
23
24
|
# Delegated by hand rather than with def_delegators because they run on
|
|
24
25
|
# every request, and a plain method call is cheaper than a Forwardable one.
|
|
@@ -41,7 +42,7 @@ module Dalli
|
|
|
41
42
|
warn_uri_credentials(user_creds)
|
|
42
43
|
@options = client_options.merge(user_creds)
|
|
43
44
|
@raw_mode = client_options[:raw]
|
|
44
|
-
@
|
|
45
|
+
@defer_drain = client_options[:defer_drain] ? true : false
|
|
45
46
|
@value_marshaller = @raw_mode ? StringMarshaller.new(@options) : ValueMarshaller.new(@options)
|
|
46
47
|
@connection_manager = ConnectionManager.new(hostname, port, socket_type, @options)
|
|
47
48
|
end
|
|
@@ -52,14 +53,6 @@ module Dalli
|
|
|
52
53
|
@raw_mode
|
|
53
54
|
end
|
|
54
55
|
|
|
55
|
-
# True when values should come back as stored: the client is in raw mode,
|
|
56
|
-
# or this request passed raw: true. Retrieval paths use it to skip
|
|
57
|
-
# requesting flags, so a stored value is never deserialized or
|
|
58
|
-
# decompressed for a caller who asked for raw bytes.
|
|
59
|
-
def raw_request?(options)
|
|
60
|
-
raw_mode? || (options.is_a?(Hash) && options[:raw]) ? true : false
|
|
61
|
-
end
|
|
62
|
-
|
|
63
56
|
# Chokepoint method for error handling and ensuring liveness
|
|
64
57
|
def request(opkey, *args)
|
|
65
58
|
verify_state(opkey)
|
|
@@ -67,10 +60,10 @@ module Dalli
|
|
|
67
60
|
begin
|
|
68
61
|
request_completed = false
|
|
69
62
|
@connection_manager.start_request!
|
|
70
|
-
|
|
63
|
+
drain_deferred_quiet_responses(opkey)
|
|
71
64
|
response = send(opkey, *args)
|
|
72
|
-
# Quiet requests can leave replies on the socket; remember that so
|
|
73
|
-
#
|
|
65
|
+
# Quiet requests can leave replies on the socket; remember that so the
|
|
66
|
+
# end of the quiet block knows this server needs draining
|
|
74
67
|
@connection_manager.mark_quiet_responses_pending! if quiet? && opkey != :noop
|
|
75
68
|
|
|
76
69
|
# pipelined_get/pipelined_get_interleaved emit query but don't read the response(s)
|
|
@@ -78,14 +71,8 @@ module Dalli
|
|
|
78
71
|
|
|
79
72
|
request_completed = true
|
|
80
73
|
response
|
|
81
|
-
rescue Dalli::MarshalError
|
|
82
|
-
log_marshal_err(args.first, e)
|
|
83
|
-
# Raised only while the connection is clean: a value is marshalled
|
|
84
|
-
# before any of its request is written, and write_multi_req raises
|
|
85
|
-
# only after reading every reply. Closing the connection would
|
|
86
|
-
# discard earlier quiet writes still waiting in the write buffer.
|
|
87
|
-
@connection_manager.abort_request!
|
|
88
|
-
request_completed = true
|
|
74
|
+
rescue Dalli::MarshalError => e
|
|
75
|
+
log_marshal_err(args.first, e)
|
|
89
76
|
raise
|
|
90
77
|
rescue Dalli::DalliError
|
|
91
78
|
raise
|
|
@@ -145,7 +132,7 @@ module Dalli
|
|
|
145
132
|
verify_pipelined_state(:getkq)
|
|
146
133
|
write_noop
|
|
147
134
|
# Use ensure_ready instead of reset to preserve any data already buffered
|
|
148
|
-
#
|
|
135
|
+
# during interleaved pipelined get draining
|
|
149
136
|
response_buffer.ensure_ready
|
|
150
137
|
end
|
|
151
138
|
|
|
@@ -158,20 +145,10 @@ module Dalli
|
|
|
158
145
|
# avoiding intermediate Hash allocation. Returns nil.
|
|
159
146
|
# Without a block, returns a Hash of { key => [value, cas] }.
|
|
160
147
|
def pipeline_next_responses(&block)
|
|
161
|
-
return abandon_pipeline if @pipeline_out_of_step
|
|
162
|
-
|
|
163
148
|
reconnect_on_pipeline_complete!
|
|
164
149
|
values = nil
|
|
165
150
|
|
|
166
|
-
|
|
167
|
-
# this rescue, so an Errno or Timeout::Error it raises reaches the
|
|
168
|
-
# caller instead of being mistaken for a network failure (which closed
|
|
169
|
-
# the connection and retried the whole get_multi, yielding keys twice).
|
|
170
|
-
begin
|
|
171
|
-
response_buffer.read
|
|
172
|
-
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
173
|
-
@connection_manager.error_on_request!(e)
|
|
174
|
-
end
|
|
151
|
+
response_buffer.read
|
|
175
152
|
|
|
176
153
|
status, cas, key, value = response_buffer.process_single_getk_response
|
|
177
154
|
# status is not nil only if we have a full response to parse
|
|
@@ -181,16 +158,6 @@ module Dalli
|
|
|
181
158
|
# to the noop at the end of the pipeline
|
|
182
159
|
finish_pipeline && break if status && key.nil?
|
|
183
160
|
|
|
184
|
-
@pipeline_response_count += 1
|
|
185
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if @pipeline_response_count > @expected_response_count
|
|
186
|
-
|
|
187
|
-
# A key that wasn't requested means the replies are out of step
|
|
188
|
-
# (see expect_reply_keys): keep what was read so far, drop the rest
|
|
189
|
-
if unexpected_reply_key?(key)
|
|
190
|
-
abandon_pipeline
|
|
191
|
-
break
|
|
192
|
-
end
|
|
193
|
-
|
|
194
161
|
# If the status is ok and the key is not nil, then this is a
|
|
195
162
|
# getkq response with a value that we want to set in the response hash
|
|
196
163
|
unless key.nil?
|
|
@@ -202,40 +169,30 @@ module Dalli
|
|
|
202
169
|
end
|
|
203
170
|
end
|
|
204
171
|
|
|
205
|
-
# A request to this server from inside the block closes the
|
|
206
|
-
# connection, and with it the rest of this get_multi's replies
|
|
207
|
-
raise_pipeline_closed unless response_buffer.in_progress?
|
|
208
|
-
|
|
209
172
|
# Get the next response from the buffer
|
|
210
173
|
status, cas, key, value = response_buffer.process_single_getk_response
|
|
211
174
|
end
|
|
212
175
|
|
|
213
176
|
values || {}
|
|
177
|
+
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
178
|
+
@connection_manager.error_on_request!(e)
|
|
214
179
|
end
|
|
215
180
|
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
216
181
|
|
|
217
182
|
# Abort current pipelined get. Generally used to signal an external
|
|
218
|
-
# timeout during pipelined get
|
|
219
|
-
# reading all its replies (timed_out: false). The underlying socket is
|
|
183
|
+
# timeout during pipelined get. The underlying socket is
|
|
220
184
|
# disconnected, and the exception is swallowed.
|
|
221
185
|
#
|
|
222
186
|
# Returns nothing.
|
|
223
|
-
def pipeline_abort
|
|
187
|
+
def pipeline_abort
|
|
224
188
|
response_buffer.clear
|
|
225
|
-
@pipeline_out_of_step = false
|
|
226
|
-
@expected_reply_keys = nil
|
|
227
|
-
@expected_response_count = nil
|
|
228
|
-
@pipeline_response_count = nil
|
|
229
189
|
@connection_manager.abort_request!
|
|
230
190
|
return true unless connected?
|
|
231
191
|
|
|
232
|
-
# Closes the connection, which ensures that our connection
|
|
233
|
-
# clean state for future requests
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
# row would otherwise mark a healthy server down.
|
|
237
|
-
log_pipeline_abort(timed_out)
|
|
238
|
-
@connection_manager.close
|
|
192
|
+
# Closes the connection, which ensures that our connection
|
|
193
|
+
# is in a clean state for future requests
|
|
194
|
+
@connection_manager.error_on_request!('External timeout')
|
|
195
|
+
rescue NetworkError
|
|
239
196
|
true
|
|
240
197
|
end
|
|
241
198
|
|
|
@@ -266,14 +223,10 @@ module Dalli
|
|
|
266
223
|
ALLOWED_QUIET_OPS = %i[add replace set delete incr decr append prepend flush noop].freeze
|
|
267
224
|
private_constant :ALLOWED_QUIET_OPS
|
|
268
225
|
|
|
269
|
-
#
|
|
270
|
-
# non-quiet request, which reads a reply of its own.
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
# open, and would otherwise read the block's replies as its own. The
|
|
274
|
-
# noop also sends any quiet requests still buffered.
|
|
275
|
-
def drain_pending_quiet_responses(opkey)
|
|
276
|
-
return unless @connection_manager.quiet_responses_pending? && opkey != :noop && !quiet?
|
|
226
|
+
# With defer_drain, quiet blocks leave their replies unread. Read them
|
|
227
|
+
# before any non-quiet request, which reads a reply of its own.
|
|
228
|
+
def drain_deferred_quiet_responses(opkey)
|
|
229
|
+
return unless @defer_drain && !quiet? && opkey != :noop && @connection_manager.quiet_responses_pending?
|
|
277
230
|
|
|
278
231
|
noop
|
|
279
232
|
end
|
|
@@ -375,8 +328,6 @@ module Dalli
|
|
|
375
328
|
# Use clear (not reset) to keep pipeline_complete? = true, which is
|
|
376
329
|
# the expected state before pipeline_response_setup is called.
|
|
377
330
|
response_buffer.clear
|
|
378
|
-
response_buffer.raw = raw_request?(options)
|
|
379
|
-
expect_reply_keys(keys)
|
|
380
331
|
|
|
381
332
|
# The terminating noop is sent by pipeline_response_setup
|
|
382
333
|
write(quiet_get_requests(keys, options, return_cas: return_cas))
|
|
@@ -386,11 +337,8 @@ module Dalli
|
|
|
386
337
|
# This prevents socket buffer deadlock when sending many keys.
|
|
387
338
|
# Populates the provided results hash with any responses drained during send.
|
|
388
339
|
def pipelined_get_interleaved(keys, chunk_size, results, options = nil)
|
|
389
|
-
#
|
|
390
|
-
|
|
391
|
-
response_buffer.reset
|
|
392
|
-
response_buffer.raw = raw_request?(options)
|
|
393
|
-
expect_reply_keys(keys)
|
|
340
|
+
# Initialize the response buffer for draining during send phase
|
|
341
|
+
response_buffer.ensure_ready
|
|
394
342
|
|
|
395
343
|
keys.each_slice(chunk_size) do |chunk|
|
|
396
344
|
# Build and write this chunk of requests
|
|
@@ -409,7 +357,7 @@ module Dalli
|
|
|
409
357
|
# Non-blocking read and processing of any available pipeline responses.
|
|
410
358
|
# Used during interleaved pipelined gets to prevent buffer deadlock.
|
|
411
359
|
# Populates the provided results hash directly to avoid allocation overhead.
|
|
412
|
-
def drain_pipeline_responses(results)
|
|
360
|
+
def drain_pipeline_responses(results)
|
|
413
361
|
return unless connected?
|
|
414
362
|
|
|
415
363
|
# Non-blocking check if socket has data available
|
|
@@ -423,15 +371,6 @@ module Dalli
|
|
|
423
371
|
status, cas, key, value = response_buffer.process_single_getk_response
|
|
424
372
|
break if status.nil? # No complete response available
|
|
425
373
|
|
|
426
|
-
@pipeline_response_count += 1
|
|
427
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if @pipeline_response_count > @expected_response_count
|
|
428
|
-
|
|
429
|
-
# Handled by the next pipeline_next_responses, once sending is done
|
|
430
|
-
if unexpected_reply_key?(key)
|
|
431
|
-
@pipeline_out_of_step = true
|
|
432
|
-
break
|
|
433
|
-
end
|
|
434
|
-
|
|
435
374
|
results[key] = [value, cas] unless key.nil?
|
|
436
375
|
end
|
|
437
376
|
rescue SystemCallError, Dalli::NetworkError
|
|
@@ -439,50 +378,8 @@ module Dalli
|
|
|
439
378
|
nil
|
|
440
379
|
end
|
|
441
380
|
|
|
442
|
-
# Registered with the connection manager, which clears it whenever the
|
|
443
|
-
# connection closes, so a new connection never parses an old one's bytes
|
|
444
381
|
def response_buffer
|
|
445
|
-
@response_buffer ||= ResponseBuffer.new(@connection_manager, response_processor)
|
|
446
|
-
@connection_manager.response_buffer = buffer
|
|
447
|
-
end
|
|
448
|
-
end
|
|
449
|
-
|
|
450
|
-
# A multi-get only accepts replies for the keys it asked for. memcached
|
|
451
|
-
# names an item created by auto-create (ma or mg with N) for a
|
|
452
|
-
# base64-encoded key by its raw bytes, without the b flag; such a key
|
|
453
|
-
# can contain spaces or CR/LF, which split the reply header and make the
|
|
454
|
-
# replies after it parse as different ones. A key that wasn't requested
|
|
455
|
-
# is how that shows up.
|
|
456
|
-
# Only with the verify_reply_keys option, since building the set costs a
|
|
457
|
-
# multi-get about 15%. Dalli itself no longer creates such items (see
|
|
458
|
-
# Meta#decr_incr); the check guards against ones that older versions or
|
|
459
|
-
# other clients created, on memcached versions with the behavior.
|
|
460
|
-
def expect_reply_keys(keys)
|
|
461
|
-
@pipeline_out_of_step = false
|
|
462
|
-
@expected_response_count = keys.size
|
|
463
|
-
@pipeline_response_count = 0
|
|
464
|
-
@expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
|
|
465
|
-
end
|
|
466
|
-
|
|
467
|
-
def unexpected_reply_key?(key)
|
|
468
|
-
!key.nil? && @expected_reply_keys && !@expected_reply_keys.include?(reply_key_form(key))
|
|
469
|
-
end
|
|
470
|
-
|
|
471
|
-
# Keys compared as bytes; ASCII keys compare equal in any encoding as is
|
|
472
|
-
def reply_key_form(key)
|
|
473
|
-
key.ascii_only? ? key : key.b
|
|
474
|
-
end
|
|
475
|
-
|
|
476
|
-
# Keeps what was read, drops the rest of this server's replies, and
|
|
477
|
-
# closes the connection so nothing unread is left on it. Not a socket
|
|
478
|
-
# failure, so it doesn't count toward socket_max_failures; the keys not
|
|
479
|
-
# read are simply missing from the result.
|
|
480
|
-
def abandon_pipeline
|
|
481
|
-
Dalli.logger.warn { "#{name} replied to a get_multi with a key that wasn't requested; closing the connection" }
|
|
482
|
-
@pipeline_out_of_step = false
|
|
483
|
-
response_buffer.clear
|
|
484
|
-
@connection_manager.close
|
|
485
|
-
{}
|
|
382
|
+
@response_buffer ||= ResponseBuffer.new(@connection_manager, response_processor)
|
|
486
383
|
end
|
|
487
384
|
|
|
488
385
|
# Called after the noop response is received at the end of a set
|
|
@@ -498,23 +395,7 @@ module Dalli
|
|
|
498
395
|
@connection_manager.reconnect! 'pipelined get has completed' if pipeline_complete?
|
|
499
396
|
end
|
|
500
397
|
|
|
501
|
-
def raise_pipeline_closed
|
|
502
|
-
raise Dalli::DalliError, "The connection to #{name} closed while a get_multi was reading its replies " \
|
|
503
|
-
'(a request made from inside the get_multi block?)'
|
|
504
|
-
end
|
|
505
|
-
|
|
506
|
-
def log_pipeline_abort(timed_out)
|
|
507
|
-
if timed_out
|
|
508
|
-
Dalli.logger.warn { "#{name} didn't finish a get_multi within socket_timeout; closing the connection" }
|
|
509
|
-
else
|
|
510
|
-
Dalli.logger.debug { "#{name} get_multi stopped before reading all replies; closing the connection" }
|
|
511
|
-
end
|
|
512
|
-
end
|
|
513
|
-
|
|
514
|
-
# The single-server set_multi path passes its whole pairs hash as the
|
|
515
|
-
# first argument; log only its keys, never the values
|
|
516
398
|
def log_marshal_err(key, err)
|
|
517
|
-
key = key.keys.join("', '") if key.is_a?(Hash)
|
|
518
399
|
Dalli.logger.error "Marshalling error for key '#{key}': #{err.message}"
|
|
519
400
|
Dalli.logger.error 'You are trying to cache a Ruby object which cannot be serialized to memcached.'
|
|
520
401
|
end
|