dalli 5.2.9 → 5.3.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 +40 -12
- data/README.md +30 -2
- data/lib/dalli/client.rb +189 -78
- data/lib/dalli/compressor.rb +15 -52
- data/lib/dalli/key_grouping.rb +50 -0
- data/lib/dalli/key_manager.rb +30 -0
- data/lib/dalli/pipelined_deleter.rb +7 -16
- data/lib/dalli/pipelined_getter.rb +64 -68
- data/lib/dalli/pipelined_setter.rb +6 -15
- data/lib/dalli/protocol/base.rb +96 -45
- data/lib/dalli/protocol/connection_manager.rb +100 -15
- data/lib/dalli/protocol/key_regularizer.rb +5 -0
- data/lib/dalli/protocol/meta.rb +40 -22
- data/lib/dalli/protocol/request_formatter.rb +62 -33
- data/lib/dalli/protocol/response_processor.rb +54 -34
- data/lib/dalli/protocol/string_marshaller.rb +1 -3
- data/lib/dalli/protocol/ttl_sanitizer.rb +13 -4
- data/lib/dalli/protocol/value_marshaller.rb +0 -4
- data/lib/dalli/ring.rb +4 -0
- data/lib/dalli/servers_arg_normalizer.rb +19 -1
- data/lib/dalli/socket.rb +11 -24
- data/lib/dalli/version.rb +1 -1
- data/lib/dalli.rb +14 -1
- data/lib/rack/session/dalli.rb +13 -7
- metadata +3 -2
data/lib/dalli/protocol/base.rb
CHANGED
|
@@ -39,8 +39,9 @@ module Dalli
|
|
|
39
39
|
|
|
40
40
|
def initialize(attribs, client_options = {})
|
|
41
41
|
hostname, port, socket_type, @weight, user_creds = ServerConfigParser.parse(attribs)
|
|
42
|
+
# Credentials are unused since Dalli 5: warned about, and not kept
|
|
42
43
|
warn_uri_credentials(user_creds)
|
|
43
|
-
@options = client_options
|
|
44
|
+
@options = client_options
|
|
44
45
|
@raw_mode = client_options[:raw]
|
|
45
46
|
@defer_drain = client_options[:defer_drain] ? true : false
|
|
46
47
|
@verify_reply_keys = client_options[:verify_reply_keys] ? true : false
|
|
@@ -68,18 +69,25 @@ module Dalli
|
|
|
68
69
|
|
|
69
70
|
begin
|
|
70
71
|
request_completed = false
|
|
71
|
-
|
|
72
|
+
# Drained before the request starts, so a failure while draining
|
|
73
|
+
# isn't taken for one after this request was sent
|
|
72
74
|
drain_pending_quiet_responses(opkey)
|
|
75
|
+
@connection_manager.start_request!
|
|
73
76
|
response = send(opkey, *args)
|
|
74
77
|
finish_request(opkey)
|
|
75
78
|
request_completed = true
|
|
76
79
|
response
|
|
77
|
-
rescue Dalli::
|
|
80
|
+
rescue Dalli::RetryableNetworkError => e
|
|
81
|
+
raise_unless_resend_safe!(e, opkey, args)
|
|
82
|
+
raise
|
|
83
|
+
rescue Dalli::MarshalError, Dalli::UnmarshalError, Dalli::ValueOverMaxSize => e
|
|
78
84
|
log_marshal_err(args.first, e) if e.is_a?(Dalli::MarshalError)
|
|
79
85
|
# Raised only while the connection is clean: a value is marshalled
|
|
80
86
|
# before any of its request is written, and write_multi_req raises
|
|
81
|
-
# only after reading every reply.
|
|
82
|
-
#
|
|
87
|
+
# only after reading every reply. Likewise a value is unmarshalled
|
|
88
|
+
# only after its reply is read, and the multi-gets raise only after
|
|
89
|
+
# reading every reply. Closing the connection would discard earlier
|
|
90
|
+
# quiet writes still waiting in the write buffer.
|
|
83
91
|
@connection_manager.abort_request!
|
|
84
92
|
request_completed = true
|
|
85
93
|
raise
|
|
@@ -177,9 +185,6 @@ module Dalli
|
|
|
177
185
|
# to the noop at the end of the pipeline
|
|
178
186
|
finish_pipeline && break if status && key.nil?
|
|
179
187
|
|
|
180
|
-
@pipeline_response_count += 1
|
|
181
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if @pipeline_response_count > @expected_response_count
|
|
182
|
-
|
|
183
188
|
# A key that wasn't requested means the replies are out of step
|
|
184
189
|
# (see expect_reply_keys): keep what was read so far, drop the rest
|
|
185
190
|
if unexpected_reply_key?(key)
|
|
@@ -188,8 +193,9 @@ module Dalli
|
|
|
188
193
|
end
|
|
189
194
|
|
|
190
195
|
# If the status is ok and the key is not nil, then this is a
|
|
191
|
-
# getkq response with a value that we want to set in the response hash
|
|
192
|
-
|
|
196
|
+
# getkq response with a value that we want to set in the response hash.
|
|
197
|
+
# A false status is a reply to skip (see getk_response_from_buffer).
|
|
198
|
+
if status && key
|
|
193
199
|
if block
|
|
194
200
|
yield key, value, cas
|
|
195
201
|
else
|
|
@@ -208,33 +214,38 @@ module Dalli
|
|
|
208
214
|
|
|
209
215
|
values || {}
|
|
210
216
|
end
|
|
211
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
212
217
|
|
|
213
|
-
# Abort current pipelined get
|
|
214
|
-
#
|
|
215
|
-
#
|
|
218
|
+
# Abort current pipelined get, when the get_multi stopped before reading
|
|
219
|
+
# all its replies: the caller's block raised or broke, an external
|
|
220
|
+
# timeout fired, or another server failed. The underlying socket is
|
|
216
221
|
# disconnected, and the exception is swallowed.
|
|
217
222
|
#
|
|
218
223
|
# Returns nothing.
|
|
219
|
-
def pipeline_abort
|
|
220
|
-
|
|
221
|
-
@pipeline_out_of_step = false
|
|
222
|
-
@expected_reply_keys = nil
|
|
223
|
-
@expected_response_count = nil
|
|
224
|
-
@pipeline_response_count = nil
|
|
224
|
+
def pipeline_abort
|
|
225
|
+
clear_pipeline
|
|
225
226
|
@connection_manager.abort_request!
|
|
226
227
|
return true unless connected?
|
|
227
228
|
|
|
228
229
|
# Closes the connection, which ensures that our connection is in a
|
|
229
|
-
# clean state for future requests.
|
|
230
|
-
#
|
|
231
|
-
#
|
|
232
|
-
# row would otherwise mark a healthy server down.
|
|
233
|
-
log_pipeline_abort(timed_out)
|
|
230
|
+
# clean state for future requests. This server didn't fail, so it
|
|
231
|
+
# doesn't count toward socket_max_failures.
|
|
232
|
+
Dalli.logger.debug { "#{name} get_multi stopped before reading all replies; closing the connection" }
|
|
234
233
|
@connection_manager.close
|
|
235
234
|
true
|
|
236
235
|
end
|
|
237
236
|
|
|
237
|
+
# A pipelined get waited socket_timeout for this server to send
|
|
238
|
+
# anything. Counted as a failed read, like a get's read timeout: raises
|
|
239
|
+
# Dalli::RetryableNetworkError, or Dalli::NetworkError once the server
|
|
240
|
+
# reaches socket_max_failures and is marked down.
|
|
241
|
+
def pipeline_timed_out!
|
|
242
|
+
clear_pipeline
|
|
243
|
+
# Raised and rescued so that down! records it as the reason
|
|
244
|
+
raise IO::TimeoutError, "No reply to get_multi within #{@connection_manager.socket_timeout}s"
|
|
245
|
+
rescue IO::TimeoutError => e
|
|
246
|
+
@connection_manager.error_on_request!(e)
|
|
247
|
+
end
|
|
248
|
+
|
|
238
249
|
# Did the last call to #pipeline_response_setup complete successfully?
|
|
239
250
|
def pipeline_complete?
|
|
240
251
|
!response_buffer.in_progress?
|
|
@@ -249,14 +260,53 @@ module Dalli
|
|
|
249
260
|
|
|
250
261
|
private
|
|
251
262
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
private_constant :URI_CREDENTIAL_WARNING
|
|
255
|
-
|
|
263
|
+
# Dalli::Client strips credentials before a server is built, so this is
|
|
264
|
+
# only for a server built directly from a URI with them
|
|
256
265
|
def warn_uri_credentials(user_creds)
|
|
257
266
|
return if user_creds[:username].nil? && user_creds[:password].nil?
|
|
258
267
|
|
|
259
|
-
Dalli.logger.warn(URI_CREDENTIAL_WARNING)
|
|
268
|
+
Dalli.logger.warn(ServersArgNormalizer::URI_CREDENTIAL_WARNING)
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Requests that leave memcached in the same state, and get the same kind
|
|
272
|
+
# of reply, when it applies them twice. Only these are retried after a
|
|
273
|
+
# failure that came once some of the request was sent, since memcached
|
|
274
|
+
# may have applied it and only the reply was lost. A set, replace or
|
|
275
|
+
# delete is in this group only without a CAS (see #resend_safe?). :cas
|
|
276
|
+
# is the read behind get_cas.
|
|
277
|
+
RESEND_SAFE_OPS = %i[get gat touch cas version stats reset_stats noop flush
|
|
278
|
+
read_multi_req read_multi_with_metadata_req pipelined_get pipelined_get_interleaved
|
|
279
|
+
write_multi_req pipelined_set delete_multi_req pipelined_delete
|
|
280
|
+
finish_pipelined_delete].freeze
|
|
281
|
+
private_constant :RESEND_SAFE_OPS
|
|
282
|
+
|
|
283
|
+
# A request that may have been applied, and can't safely be sent again,
|
|
284
|
+
# fails with a NetworkError the callers don't retry. Sending it again
|
|
285
|
+
# would apply an incr, decr, append or prepend twice, or report an add
|
|
286
|
+
# or CAS write that succeeded as failed.
|
|
287
|
+
def raise_unless_resend_safe!(err, opkey, args)
|
|
288
|
+
return unless err.request_sent?
|
|
289
|
+
return if resend_safe?(opkey, args)
|
|
290
|
+
|
|
291
|
+
raise Dalli::NetworkError, "#{name}: #{opkey} failed after it was sent, so it may have been applied; " \
|
|
292
|
+
"not retrying it. #{err.message}"
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def resend_safe?(opkey, args)
|
|
296
|
+
case opkey
|
|
297
|
+
# A replace sent again stores the same value, as a set does
|
|
298
|
+
when :set, :replace then no_cas?(args[3])
|
|
299
|
+
when :delete then no_cas?(args[1])
|
|
300
|
+
# With N or R, only the first request wins the recache race; a resent
|
|
301
|
+
# one would report the race lost to the caller that won it
|
|
302
|
+
when :meta_get then !(args[1] && (args[1][:vivify_ttl] || args[1][:recache_ttl]))
|
|
303
|
+
else RESEND_SAFE_OPS.include?(opkey)
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# A CAS of 0 or nil means an unconditional write
|
|
308
|
+
def no_cas?(cas)
|
|
309
|
+
cas.nil? || cas.to_i.zero?
|
|
260
310
|
end
|
|
261
311
|
|
|
262
312
|
# Only a request whose reply was read resets the failure count. Otherwise
|
|
@@ -374,7 +424,7 @@ module Dalli
|
|
|
374
424
|
|
|
375
425
|
kwargs = {}
|
|
376
426
|
kwargs[:stale] = true if opts[:invalidate]
|
|
377
|
-
kwargs[:ttl] = TtlSanitizer.sanitize(
|
|
427
|
+
kwargs[:ttl] = TtlSanitizer.sanitize(opts[:tombstone_ttl]) if opts[:tombstone_ttl]
|
|
378
428
|
kwargs[:drop_value] = true if opts[:drop_value]
|
|
379
429
|
kwargs
|
|
380
430
|
end
|
|
@@ -394,6 +444,8 @@ module Dalli
|
|
|
394
444
|
# the expected state before pipeline_response_setup is called.
|
|
395
445
|
response_buffer.clear
|
|
396
446
|
response_buffer.raw = raw_request?(options)
|
|
447
|
+
# Drops an error held from a get_multi that stopped early
|
|
448
|
+
response_processor.take_unmarshal_error
|
|
397
449
|
expect_reply_keys(keys)
|
|
398
450
|
|
|
399
451
|
# The terminating noop is sent by pipeline_response_setup
|
|
@@ -408,6 +460,8 @@ module Dalli
|
|
|
408
460
|
# left by an earlier get_multi that stopped early belong to its replies.
|
|
409
461
|
response_buffer.reset
|
|
410
462
|
response_buffer.raw = raw_request?(options)
|
|
463
|
+
# Drops an error held from a get_multi that stopped early
|
|
464
|
+
response_processor.take_unmarshal_error
|
|
411
465
|
expect_reply_keys(keys)
|
|
412
466
|
|
|
413
467
|
keys.each_slice(chunk_size) do |chunk|
|
|
@@ -427,7 +481,7 @@ module Dalli
|
|
|
427
481
|
# Non-blocking read and processing of any available pipeline responses.
|
|
428
482
|
# Used during interleaved pipelined gets to prevent buffer deadlock.
|
|
429
483
|
# Populates the provided results hash directly to avoid allocation overhead.
|
|
430
|
-
def drain_pipeline_responses(results)
|
|
484
|
+
def drain_pipeline_responses(results)
|
|
431
485
|
return unless connected?
|
|
432
486
|
|
|
433
487
|
# Non-blocking check if socket has data available
|
|
@@ -441,16 +495,13 @@ module Dalli
|
|
|
441
495
|
status, cas, key, value = response_buffer.process_single_getk_response
|
|
442
496
|
break if status.nil? # No complete response available
|
|
443
497
|
|
|
444
|
-
@pipeline_response_count += 1
|
|
445
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if @pipeline_response_count > @expected_response_count
|
|
446
|
-
|
|
447
498
|
# Handled by the next pipeline_next_responses, once sending is done
|
|
448
499
|
if unexpected_reply_key?(key)
|
|
449
500
|
@pipeline_out_of_step = true
|
|
450
501
|
break
|
|
451
502
|
end
|
|
452
503
|
|
|
453
|
-
results[key] = [value, cas]
|
|
504
|
+
results[key] = [value, cas] if status && key
|
|
454
505
|
end
|
|
455
506
|
rescue SystemCallError, Dalli::NetworkError
|
|
456
507
|
# Ignore errors during drain - they'll be handled in fetch_responses
|
|
@@ -477,8 +528,6 @@ module Dalli
|
|
|
477
528
|
# other clients created, on memcached versions with the behavior.
|
|
478
529
|
def expect_reply_keys(keys)
|
|
479
530
|
@pipeline_out_of_step = false
|
|
480
|
-
@expected_response_count = keys.size
|
|
481
|
-
@pipeline_response_count = 0
|
|
482
531
|
@expected_reply_keys = (keys.to_set { |k| reply_key_form(k) } if @verify_reply_keys)
|
|
483
532
|
end
|
|
484
533
|
|
|
@@ -504,10 +553,14 @@ module Dalli
|
|
|
504
553
|
end
|
|
505
554
|
|
|
506
555
|
# Called after the noop response is received at the end of a set
|
|
507
|
-
# of pipelined gets
|
|
556
|
+
# of pipelined gets. A value that couldn't be deserialized is raised
|
|
557
|
+
# here, once all of this server's replies are read, so the connection
|
|
558
|
+
# is left in step.
|
|
508
559
|
def finish_pipeline
|
|
509
560
|
response_buffer.clear
|
|
510
561
|
@connection_manager.finish_request!
|
|
562
|
+
unmarshal_error = response_processor.take_unmarshal_error
|
|
563
|
+
raise unmarshal_error if unmarshal_error
|
|
511
564
|
|
|
512
565
|
true # to simplify response
|
|
513
566
|
end
|
|
@@ -521,12 +574,10 @@ module Dalli
|
|
|
521
574
|
'(a request made from inside the get_multi block?)'
|
|
522
575
|
end
|
|
523
576
|
|
|
524
|
-
def
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
Dalli.logger.debug { "#{name} get_multi stopped before reading all replies; closing the connection" }
|
|
529
|
-
end
|
|
577
|
+
def clear_pipeline
|
|
578
|
+
response_buffer.clear
|
|
579
|
+
@pipeline_out_of_step = false
|
|
580
|
+
@expected_reply_keys = nil
|
|
530
581
|
end
|
|
531
582
|
|
|
532
583
|
# The single-server set_multi path passes its whole pairs hash as the
|
|
@@ -21,7 +21,10 @@ module Dalli
|
|
|
21
21
|
# amount of time to sleep between retries when a failure occurs
|
|
22
22
|
socket_failure_delay: 0.1,
|
|
23
23
|
# Set keepalive
|
|
24
|
-
keepalive: true
|
|
24
|
+
keepalive: true,
|
|
25
|
+
# On JRuby and TruffleRuby, time out reads after socket_timeout (see
|
|
26
|
+
# ReadTimeout). CRuby always does.
|
|
27
|
+
enforce_read_timeout: false
|
|
25
28
|
}.freeze
|
|
26
29
|
|
|
27
30
|
attr_accessor :hostname, :port, :socket_type, :options
|
|
@@ -36,6 +39,7 @@ module Dalli
|
|
|
36
39
|
@socket_type = socket_type
|
|
37
40
|
@options = DEFAULTS.merge(client_options)
|
|
38
41
|
@request_in_progress = false
|
|
42
|
+
@request_sent = false
|
|
39
43
|
@quiet_responses_pending = false
|
|
40
44
|
@sock = nil
|
|
41
45
|
@pid = nil
|
|
@@ -45,6 +49,7 @@ module Dalli
|
|
|
45
49
|
|
|
46
50
|
@fail_count = 0
|
|
47
51
|
reset_down_info
|
|
52
|
+
extend ReadTimeout if @options[:enforce_read_timeout] && RUBY_ENGINE != 'ruby'
|
|
48
53
|
end
|
|
49
54
|
|
|
50
55
|
def name
|
|
@@ -64,6 +69,7 @@ module Dalli
|
|
|
64
69
|
@pid = Process.pid
|
|
65
70
|
@request_in_progress = false
|
|
66
71
|
@quiet_responses_pending = false
|
|
72
|
+
@last_used_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
|
67
73
|
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError, SocketError => e
|
|
68
74
|
# SocketError = DNS resolution failure; SSL_ERRORS = failed TLS handshake
|
|
69
75
|
error_on_request!(e)
|
|
@@ -109,9 +115,46 @@ module Dalli
|
|
|
109
115
|
@socket_timeout ||= @options[:socket_timeout]
|
|
110
116
|
end
|
|
111
117
|
|
|
118
|
+
# Before a request: closes a connection left mid-request, reconnects
|
|
119
|
+
# after a fork, and checks a connection that sat idle (see
|
|
120
|
+
# #close_if_closed_by_peer!). Idle time runs from the start of the last
|
|
121
|
+
# request, so the clock is read once per request; after a request that
|
|
122
|
+
# itself took over a second, the next just makes a check that finds
|
|
123
|
+
# nothing. Pending quiet replies are readable and expected, so the
|
|
124
|
+
# check, and the clock read, are skipped while there are any; in a quiet
|
|
125
|
+
# block that is every write after the first.
|
|
112
126
|
def confirm_ready!
|
|
113
127
|
close if request_in_progress?
|
|
114
128
|
reconnect_on_fork if fork_detected?
|
|
129
|
+
return if @sock.nil? || @quiet_responses_pending
|
|
130
|
+
|
|
131
|
+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
|
|
132
|
+
idle = now - @last_used_at
|
|
133
|
+
@last_used_at = now
|
|
134
|
+
close_if_closed_by_peer! if idle >= IDLE_CHECK_MS
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# How long, in milliseconds, a connection goes without a request before
|
|
138
|
+
# the next one first checks that the server hasn't closed it. Integer
|
|
139
|
+
# milliseconds keep Float arithmetic off the request path.
|
|
140
|
+
IDLE_CHECK_MS = 1000
|
|
141
|
+
|
|
142
|
+
# memcached's idle_timeout, a load balancer or a firewall can close a
|
|
143
|
+
# connection that sat unused. The next write still succeeds, into the
|
|
144
|
+
# kernel's buffer, and only the read fails, by which time the request
|
|
145
|
+
# counts as sent: one that isn't safe to send twice, such as an incr or
|
|
146
|
+
# an add, then raises instead of being retried. With no replies pending,
|
|
147
|
+
# an idle connection has nothing to read, so a readable one was closed
|
|
148
|
+
# by the other end (or holds bytes nothing asked for). Closing it here
|
|
149
|
+
# lets the request go out on a new connection. A connection dropped
|
|
150
|
+
# without the other end being told can't be detected this way.
|
|
151
|
+
def close_if_closed_by_peer!
|
|
152
|
+
return unless @sock.to_io.wait_readable(0)
|
|
153
|
+
|
|
154
|
+
Dalli.logger.debug { "#{name} closed the connection while it was idle; reconnecting" }
|
|
155
|
+
close
|
|
156
|
+
rescue IOError, SystemCallError
|
|
157
|
+
close
|
|
115
158
|
end
|
|
116
159
|
|
|
117
160
|
def confirm_in_progress!
|
|
@@ -176,12 +219,14 @@ module Dalli
|
|
|
176
219
|
raise '[Dalli] Request already in progress. This may be a bug in Dalli.' if @request_in_progress
|
|
177
220
|
|
|
178
221
|
@request_in_progress = true
|
|
222
|
+
@request_sent = false
|
|
179
223
|
end
|
|
180
224
|
|
|
181
225
|
def finish_request!
|
|
182
226
|
raise '[Dalli] No request in progress. This may be a bug in Dalli.' unless @request_in_progress
|
|
183
227
|
|
|
184
228
|
@request_in_progress = false
|
|
229
|
+
@request_sent = false
|
|
185
230
|
# A completed request is what proves the server healthy again, so the
|
|
186
231
|
# failure count resets here rather than on reconnect: a server that
|
|
187
232
|
# accepts connections but never answers would otherwise reset it on
|
|
@@ -196,10 +241,19 @@ module Dalli
|
|
|
196
241
|
raise '[Dalli] No request in progress. This may be a bug in Dalli.' unless @request_in_progress
|
|
197
242
|
|
|
198
243
|
@request_in_progress = false
|
|
244
|
+
@request_sent = false
|
|
199
245
|
end
|
|
200
246
|
|
|
201
247
|
def abort_request!
|
|
202
248
|
@request_in_progress = false
|
|
249
|
+
@request_sent = false
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# True once any of the request in progress has been handed to the
|
|
253
|
+
# socket. Writes made outside a request (the version check on connect,
|
|
254
|
+
# draining an earlier quiet block's replies) don't count.
|
|
255
|
+
def request_sent?
|
|
256
|
+
@request_sent
|
|
203
257
|
end
|
|
204
258
|
|
|
205
259
|
# The longest reply line Dalli reads, in bytes, CRLF included. A meta
|
|
@@ -213,7 +267,7 @@ module Dalli
|
|
|
213
267
|
|
|
214
268
|
def read_line
|
|
215
269
|
flush_write_buffer
|
|
216
|
-
data =
|
|
270
|
+
data = @sock.gets("\r\n", MAX_LINE_BYTES)
|
|
217
271
|
error_on_request!('EOF in read_line') if data.nil?
|
|
218
272
|
# Without its CRLF the line either ran past MAX_LINE_BYTES or was cut
|
|
219
273
|
# short by EOF; either way the connection is out of step
|
|
@@ -315,9 +369,11 @@ module Dalli
|
|
|
315
369
|
end
|
|
316
370
|
|
|
317
371
|
def reconnect!(message)
|
|
372
|
+
# Read before close, which ends the request
|
|
373
|
+
request_sent = @request_sent
|
|
318
374
|
close
|
|
319
375
|
sleep(options[:socket_failure_delay]) if options[:socket_failure_delay]
|
|
320
|
-
raise Dalli::RetryableNetworkError,
|
|
376
|
+
raise Dalli::RetryableNetworkError.new(message, request_sent: request_sent)
|
|
321
377
|
end
|
|
322
378
|
|
|
323
379
|
# Called on connect. Deliberately leaves @fail_count alone; see
|
|
@@ -378,23 +434,15 @@ module Dalli
|
|
|
378
434
|
|
|
379
435
|
private
|
|
380
436
|
|
|
381
|
-
# Ruby 3.3's default OpenSSL can block past IO#timeout= on the TCP
|
|
382
|
-
# socket. Bound the blocking TLS call itself; error_on_request! closes
|
|
383
|
-
# the connection after the Timeout::Error, so a partial reply or write
|
|
384
|
-
# is never reused.
|
|
385
|
-
def timed_tls_operation(&)
|
|
386
|
-
return yield unless @sock.is_a?(Dalli::Socket::SSLSocket)
|
|
387
|
-
|
|
388
|
-
Timeout.timeout(socket_timeout, &)
|
|
389
|
-
end
|
|
390
|
-
|
|
391
437
|
# One write per flush. A TLS socket sends each argument of
|
|
392
438
|
# write(*parts) as its own record and system call, so the parts are
|
|
393
439
|
# joined first.
|
|
394
440
|
def flush_write_buffer
|
|
395
441
|
return if @write_buffer.empty?
|
|
396
442
|
|
|
397
|
-
|
|
443
|
+
# Set before writing: a write that fails may still have sent some bytes
|
|
444
|
+
@request_sent = true if @request_in_progress
|
|
445
|
+
@sock.write(@write_buffer.size == 1 ? @write_buffer.first : joined_write_buffer)
|
|
398
446
|
discard_write_buffer
|
|
399
447
|
end
|
|
400
448
|
|
|
@@ -427,11 +475,48 @@ module Dalli
|
|
|
427
475
|
# error_on_request!, which closes the dirty socket for a retry on a fresh
|
|
428
476
|
# connection (and preserves the $ERROR_INFO context down! relies on).
|
|
429
477
|
def read_bytes(count)
|
|
430
|
-
buffer =
|
|
478
|
+
buffer = @sock.read(count)
|
|
431
479
|
return buffer if buffer && buffer.bytesize == count
|
|
432
480
|
|
|
433
481
|
raise EOFError, "EOF reading #{count} bytes; received #{buffer ? buffer.bytesize : 0}"
|
|
434
482
|
end
|
|
483
|
+
|
|
484
|
+
# CRuby enforces socket_timeout on reads through IO#timeout=. TruffleRuby
|
|
485
|
+
# has no IO#timeout=, and JRuby (as of 10.1) defines it but doesn't
|
|
486
|
+
# enforce it; both ignore the SO_RCVTIMEO fallback, so a read from a
|
|
487
|
+
# server that stopped answering blocks forever. With
|
|
488
|
+
# enforce_read_timeout, the first read after a request is sent waits for
|
|
489
|
+
# the socket to be readable, up to socket_timeout. It's opt-in because
|
|
490
|
+
# the wait costs about 8% of single-key throughput on TruffleRuby and
|
|
491
|
+
# 20-29% on JRuby. A server that stops partway through a reply line can
|
|
492
|
+
# still block. So can one that stops partway through a value on
|
|
493
|
+
# TruffleRuby; JRuby reads values without blocking.
|
|
494
|
+
#
|
|
495
|
+
# TLS connections are left as they were: OpenSSL buffers decrypted
|
|
496
|
+
# bytes that wait_readable can't see.
|
|
497
|
+
module ReadTimeout
|
|
498
|
+
def read_line
|
|
499
|
+
flush_write_buffer
|
|
500
|
+
if @awaiting_reply
|
|
501
|
+
@awaiting_reply = false
|
|
502
|
+
wait_readable! unless @sock.is_a?(::OpenSSL::SSL::SSLSocket)
|
|
503
|
+
end
|
|
504
|
+
super
|
|
505
|
+
rescue SystemCallError, *TIMEOUT_ERRORS, *SSL_ERRORS, EOFError => e
|
|
506
|
+
error_on_request!(e)
|
|
507
|
+
end
|
|
508
|
+
|
|
509
|
+
private
|
|
510
|
+
|
|
511
|
+
def flush_write_buffer
|
|
512
|
+
@awaiting_reply = true unless @write_buffer.empty?
|
|
513
|
+
super
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def wait_readable!
|
|
517
|
+
@sock.wait_readable(socket_timeout) or raise Timeout::Error, "IO timeout reading from #{name}"
|
|
518
|
+
end
|
|
519
|
+
end
|
|
435
520
|
end
|
|
436
521
|
end
|
|
437
522
|
end
|
|
@@ -44,9 +44,14 @@ module Dalli
|
|
|
44
44
|
[key].pack('m0')
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
+
# Decodes a key from a reply. One that isn't valid base64 means the
|
|
48
|
+
# reply is malformed, so it raises Dalli::DalliError like other bad
|
|
49
|
+
# replies, rather than unpack1's ArgumentError.
|
|
47
50
|
def decode(encoded_key)
|
|
48
51
|
strict_base64_decoded = encoded_key.unpack1('m0')
|
|
49
52
|
strict_base64_decoded.force_encoding(Encoding::UTF_8)
|
|
53
|
+
rescue ArgumentError
|
|
54
|
+
raise Dalli::DalliError, "Reply key #{encoded_key.inspect} is marked base64 but isn't valid base64"
|
|
50
55
|
end
|
|
51
56
|
end
|
|
52
57
|
end
|
data/lib/dalli/protocol/meta.rb
CHANGED
|
@@ -103,9 +103,14 @@ module Dalli
|
|
|
103
103
|
# - :hit_before - true/false if previously accessed (only if return_hit_status: true)
|
|
104
104
|
# - :last_access - seconds since last access (only if return_last_access: true)
|
|
105
105
|
def meta_get(key, options = {})
|
|
106
|
+
# vivify_ttl is how long the stub lives, so like any TTL it must be
|
|
107
|
+
# sanitized: memcached reads a value over 30 days as a Unix timestamp.
|
|
108
|
+
# recache_ttl is a threshold compared with the remaining TTL, not an
|
|
109
|
+
# expiry, so it's sent as given.
|
|
110
|
+
vivify_ttl = options[:vivify_ttl] && TtlSanitizer.sanitize(options[:vivify_ttl])
|
|
106
111
|
req = RequestFormatter.meta_get(
|
|
107
112
|
key: key, value: true, return_cas: true, skip_flags: raw_request?(options),
|
|
108
|
-
vivify_ttl:
|
|
113
|
+
vivify_ttl: vivify_ttl, recache_ttl: options[:recache_ttl],
|
|
109
114
|
return_hit_status: options[:return_hit_status],
|
|
110
115
|
return_last_access: options[:return_last_access],
|
|
111
116
|
return_ttl_remaining: options[:return_ttl_remaining],
|
|
@@ -157,7 +162,11 @@ module Dalli
|
|
|
157
162
|
|
|
158
163
|
def write_storage_req(mode, key, raw_value, ttl = nil, cas = nil, options = {}, quiet: quiet?)
|
|
159
164
|
(value, bitflags) = @value_marshaller.store(key, raw_value, options)
|
|
160
|
-
|
|
165
|
+
# An Integer TTL of up to 30 days, the usual case, is left as it is by
|
|
166
|
+
# TtlSanitizer, so the call is skipped
|
|
167
|
+
ttl = TtlSanitizer.sanitize(ttl) unless ttl.nil? ||
|
|
168
|
+
(ttl.is_a?(Integer) &&
|
|
169
|
+
ttl <= TtlSanitizer::MAX_ACCEPTABLE_EXPIRATION_INTERVAL)
|
|
161
170
|
# eql?(0): only Integer 0 means "no CAS" here; anything else takes the general path
|
|
162
171
|
req = if mode == :set && !quiet && (cas.nil? || cas.eql?(0)) && !routing_tokens?(options)
|
|
163
172
|
RequestFormatter.plain_meta_set(key, value.bytesize, bitflags, ttl)
|
|
@@ -194,8 +203,8 @@ module Dalli
|
|
|
194
203
|
|
|
195
204
|
# Ruby refuses to join Strings whose encodings clash, such as a raw
|
|
196
205
|
# UTF-16 value, or a routing token with non-ASCII characters next to a
|
|
197
|
-
# binary value. memcached stores bytes, so join the bytes then.
|
|
198
|
-
#
|
|
206
|
+
# binary value. memcached stores bytes, so join the bytes then. Joining
|
|
207
|
+
# the Strings first keeps the common case free of extra checks.
|
|
199
208
|
def request_with_value(req, value)
|
|
200
209
|
"#{req}#{value}#{TERMINATOR}"
|
|
201
210
|
rescue Encoding::CompatibilityError
|
|
@@ -229,7 +238,7 @@ module Dalli
|
|
|
229
238
|
|
|
230
239
|
def finish_pipelined_delete(sent)
|
|
231
240
|
write_noop
|
|
232
|
-
sent - response_processor.pipelined_delete_non_deletions
|
|
241
|
+
sent - response_processor.pipelined_delete_non_deletions
|
|
233
242
|
end
|
|
234
243
|
|
|
235
244
|
# Arithmetic Commands
|
|
@@ -363,19 +372,15 @@ module Dalli
|
|
|
363
372
|
def read_multi_metadata_responses(is_raw, keys)
|
|
364
373
|
expect_reply_keys(keys)
|
|
365
374
|
hash = {}
|
|
366
|
-
|
|
375
|
+
unmarshal_error = nil
|
|
367
376
|
while (line = @connection_manager.read_line)
|
|
368
377
|
break if line.start_with?('MN')
|
|
369
|
-
|
|
370
|
-
replies += 1
|
|
371
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if replies > keys.size
|
|
372
|
-
|
|
373
378
|
next unless line.start_with?('VA ')
|
|
374
379
|
|
|
375
380
|
tokens = line.chomp!(TERMINATOR).split
|
|
376
381
|
size = tokens[1].to_i
|
|
377
382
|
response_processor.check_value_size!(size)
|
|
378
|
-
value = @connection_manager.read(size + TERMINATOR.bytesize)
|
|
383
|
+
value = response_processor.strip_value_terminator!(@connection_manager.read(size + TERMINATOR.bytesize))
|
|
379
384
|
stale = response_processor.stale_from_tokens(tokens)
|
|
380
385
|
cas = response_processor.cas_from_tokens(tokens)
|
|
381
386
|
bitflags = is_raw ? 0 : response_processor.bitflags_from_tokens(tokens)
|
|
@@ -384,27 +389,39 @@ module Dalli
|
|
|
384
389
|
|
|
385
390
|
raise_unexpected_reply_key(key) if unexpected_reply_key?(key)
|
|
386
391
|
|
|
387
|
-
|
|
392
|
+
begin
|
|
393
|
+
hash[key] = { value: @value_marshaller.retrieve(value, bitflags), cas: cas, stale: stale, miss: false }
|
|
394
|
+
rescue Dalli::UnmarshalError => e
|
|
395
|
+
# Raised once the rest of the replies are read; see read_multi_get_responses
|
|
396
|
+
unmarshal_error ||= e
|
|
397
|
+
end
|
|
388
398
|
end
|
|
399
|
+
raise unmarshal_error if unmarshal_error
|
|
400
|
+
|
|
389
401
|
hash
|
|
390
402
|
end
|
|
391
403
|
|
|
404
|
+
# A value that can't be deserialized raises Dalli::UnmarshalError, as
|
|
405
|
+
# get does, but only after the rest of the replies are read, so the
|
|
406
|
+
# connection is left in step. The first such error is raised.
|
|
392
407
|
def read_multi_get_responses(is_raw, keys)
|
|
393
408
|
expect_reply_keys(keys)
|
|
394
409
|
hash = {}
|
|
395
|
-
|
|
410
|
+
unmarshal_error = nil
|
|
396
411
|
while (line = @connection_manager.read_line)
|
|
397
412
|
break if line.start_with?('MN')
|
|
398
|
-
|
|
399
|
-
replies += 1
|
|
400
|
-
raise Dalli::DalliError, 'Too many get_multi replies' if replies > keys.size
|
|
401
|
-
|
|
402
413
|
next unless line.start_with?('VA ')
|
|
403
414
|
|
|
404
|
-
|
|
405
|
-
|
|
415
|
+
begin
|
|
416
|
+
key, value = parse_multi_get_value(line, is_raw)
|
|
417
|
+
rescue Dalli::UnmarshalError => e
|
|
418
|
+
unmarshal_error ||= e
|
|
419
|
+
next
|
|
420
|
+
end
|
|
406
421
|
hash[key] = value if key
|
|
407
422
|
end
|
|
423
|
+
raise unmarshal_error if unmarshal_error
|
|
424
|
+
|
|
408
425
|
hash
|
|
409
426
|
end
|
|
410
427
|
|
|
@@ -421,10 +438,11 @@ module Dalli
|
|
|
421
438
|
processor = response_processor
|
|
422
439
|
size = processor.size_from_va_line(line)
|
|
423
440
|
processor.check_value_size!(size)
|
|
424
|
-
value = @connection_manager.read(size + TERMINATOR.bytesize)
|
|
441
|
+
value = processor.strip_value_terminator!(@connection_manager.read(size + TERMINATOR.bytesize))
|
|
425
442
|
key = processor.key_from_va_line(line)
|
|
426
443
|
return unless key
|
|
427
444
|
|
|
445
|
+
raise_unexpected_reply_key(key) if unexpected_reply_key?(key)
|
|
428
446
|
bitflags = is_raw ? 0 : processor.bitflags_from_va_line(line)
|
|
429
447
|
[key, @value_marshaller.retrieve(value, bitflags)]
|
|
430
448
|
end
|
|
@@ -449,7 +467,7 @@ module Dalli
|
|
|
449
467
|
buffer = RequestFormatter.multi_meta_set(entries, ttl: ttl, **routing_token_kwargs(req_options))
|
|
450
468
|
flushed_write(buffer)
|
|
451
469
|
buffer.clear
|
|
452
|
-
response_processor.consume_all_responses_until_mn
|
|
470
|
+
response_processor.consume_all_responses_until_mn
|
|
453
471
|
end
|
|
454
472
|
raise value_error if value_error
|
|
455
473
|
end
|
|
@@ -461,7 +479,7 @@ module Dalli
|
|
|
461
479
|
**routing_token_kwargs(req_options))
|
|
462
480
|
flushed_write(buffer)
|
|
463
481
|
buffer.clear
|
|
464
|
-
keys.size - response_processor.pipelined_delete_non_deletions
|
|
482
|
+
keys.size - response_processor.pipelined_delete_non_deletions
|
|
465
483
|
end
|
|
466
484
|
|
|
467
485
|
require_relative 'key_regularizer'
|