tuber 0.6.0 → 0.8.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 +25 -0
- data/README.md +120 -1
- data/lib/tuber/configuration.rb +28 -2
- data/lib/tuber/connection.rb +189 -19
- data/lib/tuber/errors.rb +57 -3
- data/lib/tuber/schedules.rb +288 -0
- data/lib/tuber/tube/record.rb +21 -10
- data/lib/tuber/version.rb +1 -1
- data/lib/tuber.rb +10 -1
- data/test/connection_test.rb +234 -1
- data/test/errors_test.rb +7 -0
- data/test/schedules_test.rb +382 -0
- data/test/test_helper.rb +18 -0
- metadata +4 -2
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'time'
|
|
4
|
+
|
|
5
|
+
class Tuber
|
|
6
|
+
# Recurring schedules held by a Tuber server (beanstalkd has none): entries
|
|
7
|
+
# the server puts into a tube at the times a spec names, so an app needs no
|
|
8
|
+
# scheduler process of its own. A fire is an ordinary put — workers reserve
|
|
9
|
+
# the job as they would any other.
|
|
10
|
+
#
|
|
11
|
+
# An app owns its entries under a name of its choosing, the *owner*, and
|
|
12
|
+
# replaces the whole set with one #sync: entries it no longer sends are
|
|
13
|
+
# removed, an edited one is updated in place, and an unchanged one keeps its
|
|
14
|
+
# next fire. Every replica may sync; an identical set changes nothing.
|
|
15
|
+
#
|
|
16
|
+
# @example
|
|
17
|
+
# tuber.schedules.sync("splat") do |s|
|
|
18
|
+
# s.add "data_retention", tube: "splat.maintenance",
|
|
19
|
+
# cron: "0 2 * * *", zone: "Australia/Melbourne",
|
|
20
|
+
# body: { class: "DataRetentionJob", args: [] }.to_json, con: "maintenance"
|
|
21
|
+
# s.add "monitor_evaluation", tube: "splat.checkins", every: 60,
|
|
22
|
+
# idp: "monitor_evaluation", body: { class: "MonitorEvaluationJob", args: [] }.to_json
|
|
23
|
+
# end
|
|
24
|
+
# # => #<struct Tuber::Schedules::SyncResult added=2, changed=0, removed=0, unchanged=0>
|
|
25
|
+
#
|
|
26
|
+
class Schedules
|
|
27
|
+
# What a committed sync did to the owner's entries.
|
|
28
|
+
#
|
|
29
|
+
# If the connection dropped and the sync was re-run, the counts describe
|
|
30
|
+
# the last attempt only: a drop after the first attempt's commit landed
|
|
31
|
+
# makes the re-run report every entry as unchanged.
|
|
32
|
+
SyncResult = Struct.new(:added, :changed, :removed, :unchanged)
|
|
33
|
+
|
|
34
|
+
# One staged entry: its name, the `schedule-put` arguments, and the
|
|
35
|
+
# serialized body.
|
|
36
|
+
Entry = Struct.new(:name, :line, :body)
|
|
37
|
+
|
|
38
|
+
# Owners, entry names and tubes follow the server's tube-name rules.
|
|
39
|
+
NAME = %r{\A[A-Za-z0-9+/;.$_()][A-Za-z0-9\-+/;.$_()]*\z}
|
|
40
|
+
|
|
41
|
+
# @!attribute client
|
|
42
|
+
# @return [Tuber] returns the client instance
|
|
43
|
+
attr_reader :client
|
|
44
|
+
|
|
45
|
+
# @param [Tuber] client The tuber client instance.
|
|
46
|
+
def initialize(client)
|
|
47
|
+
@client = client
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Replaces +owner+'s entries with the ones the block adds, in one step.
|
|
51
|
+
#
|
|
52
|
+
# Send the complete schedule every time: an entry left out is deleted.
|
|
53
|
+
#
|
|
54
|
+
# Sync when a new release starts — a release step, or once at process
|
|
55
|
+
# boot — not on reconnect. Tuber keeps schedules across its own restarts
|
|
56
|
+
# (with -b), and during a rolling deploy an old process re-syncing would
|
|
57
|
+
# put back what the new release removed.
|
|
58
|
+
#
|
|
59
|
+
# @param [String] owner The app's name for its set of entries
|
|
60
|
+
# @param [Boolean] allow_empty A sync with no entries removes all of the
|
|
61
|
+
# owner's entries. Refused unless this is true, since an app that loaded
|
|
62
|
+
# an empty config by mistake would otherwise wipe its schedule.
|
|
63
|
+
# @yieldparam [Tuber::Schedules::Builder] s Call +s.add+ once per entry
|
|
64
|
+
# @return [Tuber::Schedules::SyncResult]
|
|
65
|
+
# @raise [Tuber::ScheduleSyncError] The server refused an entry or the
|
|
66
|
+
# sync. Nothing changed unless the error's #applied? says otherwise.
|
|
67
|
+
# @raise [ArgumentError] An entry is malformed, or the set is empty
|
|
68
|
+
# without +allow_empty+. Nothing is sent.
|
|
69
|
+
# @raise [Tuber::NotConnected] The sync's own connection couldn't be
|
|
70
|
+
# made (see Tuber::Configuration#connect_retries)
|
|
71
|
+
# @api public
|
|
72
|
+
def sync(owner, allow_empty: false)
|
|
73
|
+
owner = Schedules.name!("owner", owner)
|
|
74
|
+
builder = Builder.new
|
|
75
|
+
yield builder if block_given?
|
|
76
|
+
if builder.entries.empty? && !allow_empty
|
|
77
|
+
raise ArgumentError, "refusing to sync no entries for #{owner.inspect}: that removes " \
|
|
78
|
+
"all of its schedules. Pass allow_empty: true if that's meant."
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A connection of its own: a blocking reserve on the client's connection
|
|
82
|
+
# can't hold the sync up, and an error partway, which can leave the
|
|
83
|
+
# server mid-sync with a reply unread, is cleaned up by closing it.
|
|
84
|
+
connection = Connection.new(client.connection.address)
|
|
85
|
+
begin
|
|
86
|
+
connection.schedule_sync(owner, builder.entries)
|
|
87
|
+
ensure
|
|
88
|
+
connection.close
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The entries the server holds, as [owner, name] pairs.
|
|
93
|
+
#
|
|
94
|
+
# @param [String, nil] owner Only this owner's entries
|
|
95
|
+
# @return [Array<Array(String, String)>]
|
|
96
|
+
# @example
|
|
97
|
+
# tuber.schedules.list("splat") # => [["splat", "data_retention"], ["splat", "monitor_evaluation"]]
|
|
98
|
+
# @api public
|
|
99
|
+
def list(owner = nil)
|
|
100
|
+
owner = Schedules.name!("owner", owner) if owner
|
|
101
|
+
yaml = client.connection.transmit_raw(owner ? "list-schedules #{owner}" : "list-schedules")
|
|
102
|
+
yaml.each_line.grep(/\A- /).map { |line| line.chomp[2..-1].split(' ', 2) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# One entry's definition, next three fires, last fire and counters.
|
|
106
|
+
#
|
|
107
|
+
# Times are Time objects at the entry's own zone offset, nil when there is
|
|
108
|
+
# none yet; counters are Integers. +buried_streak+ above 0 means fires are
|
|
109
|
+
# deduplicating against a buried job (through +idp:+) and queueing nothing.
|
|
110
|
+
#
|
|
111
|
+
# @param [String] owner The entry's owner
|
|
112
|
+
# @param [String] name The entry's name
|
|
113
|
+
# @return [Tuber::StatStruct, nil] nil if there is no such entry
|
|
114
|
+
# @example
|
|
115
|
+
# stats = tuber.schedules.stats("splat", "data_retention")
|
|
116
|
+
# stats.next_fire # => 2026-10-12 02:00:00 +1100
|
|
117
|
+
# stats["next-fire-2"] # => 2026-10-13 02:00:00 +1100
|
|
118
|
+
# stats.buried_streak # => 0
|
|
119
|
+
# @api public
|
|
120
|
+
def stats(owner, name)
|
|
121
|
+
owner = Schedules.name!("owner", owner)
|
|
122
|
+
name = Schedules.name!("name", name)
|
|
123
|
+
yaml = client.connection.transmit_raw("stats-schedule #{owner} #{name}")
|
|
124
|
+
stats = yaml.each_line.with_object({}) do |line, hash|
|
|
125
|
+
key, value = line.chomp.split(': ', 2)
|
|
126
|
+
hash[key] = Schedules.stat_value(value) if value
|
|
127
|
+
end
|
|
128
|
+
StatStruct.from_hash(stats)
|
|
129
|
+
rescue Tuber::NotFoundError
|
|
130
|
+
nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# String representation of the schedules collection.
|
|
134
|
+
def to_s
|
|
135
|
+
"#<Tuber::Schedules>"
|
|
136
|
+
end
|
|
137
|
+
alias :inspect :to_s
|
|
138
|
+
|
|
139
|
+
# Checks an owner, entry name or tube against tube-name rules.
|
|
140
|
+
#
|
|
141
|
+
# @return [String] The name, a Symbol converted
|
|
142
|
+
# @raise [ArgumentError] It breaks them
|
|
143
|
+
# @api private
|
|
144
|
+
def self.name!(what, value)
|
|
145
|
+
value = value.to_s if value.is_a?(Symbol)
|
|
146
|
+
return value if value.is_a?(String) && value.bytesize <= 200 && value.match?(NAME)
|
|
147
|
+
raise ArgumentError, "invalid schedule #{what} #{value.inspect}: up to 200 bytes of letters, " \
|
|
148
|
+
"digits and - + / ; . $ _ ( ), not starting with -"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Converts one stats-schedule value by how tuber writes it: a quoted value
|
|
152
|
+
# is a String, `""` meaning none (nil); bare digits are a counter; any
|
|
153
|
+
# other bare value is a time.
|
|
154
|
+
#
|
|
155
|
+
# @api private
|
|
156
|
+
def self.stat_value(value)
|
|
157
|
+
if value.start_with?('"')
|
|
158
|
+
string = value.delete_prefix('"').delete_suffix('"')
|
|
159
|
+
string.empty? ? nil : string
|
|
160
|
+
elsif value.match?(/\A\d+\z/)
|
|
161
|
+
value.to_i
|
|
162
|
+
else
|
|
163
|
+
begin
|
|
164
|
+
Time.iso8601(value)
|
|
165
|
+
rescue ArgumentError
|
|
166
|
+
value # a field newer than this gem; better raw than an error
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Collects the entries of one sync. Nothing is sent until the block ends,
|
|
172
|
+
# and nothing at all if an #add raises.
|
|
173
|
+
class Builder
|
|
174
|
+
# @!attribute entries
|
|
175
|
+
# @return [Array<Tuber::Schedules::Entry>] the entries added so far
|
|
176
|
+
attr_reader :entries
|
|
177
|
+
|
|
178
|
+
def initialize
|
|
179
|
+
@entries = []
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Adds one entry. Give exactly one of +cron:+, +every:+ or +spec:+.
|
|
183
|
+
#
|
|
184
|
+
# @param [String] name The entry's name, unique within the owner. Keep
|
|
185
|
+
# it stable: a renamed entry is a removal plus an addition.
|
|
186
|
+
# @param [String] tube The tube each fire puts into
|
|
187
|
+
# @param [String] body The job body each fire puts, passed through
|
|
188
|
+
# Tuber.configuration.job_serializer as for Tube#put
|
|
189
|
+
# @param [String] cron 5-field cron (`"0 2 * * *"`) or a macro
|
|
190
|
+
# (`"@daily"`), optionally followed by an IANA zone
|
|
191
|
+
# @param [String] zone IANA zone for +cron+ (default UTC)
|
|
192
|
+
# @param [Integer, String] every Interval, aligned to the Unix epoch:
|
|
193
|
+
# seconds, or `"10m"`, `"1h"`, `"1d"`
|
|
194
|
+
# @param [Integer, String] offset Shift for +every+, e.g. `"5m"`
|
|
195
|
+
# @param [String] spec The spec as tuber reads it, instead of the above
|
|
196
|
+
# @param [Integer] pri Priority; defaults as for a put
|
|
197
|
+
# @param [Integer] delay Delay of each fire's job; defaults as for a put
|
|
198
|
+
# @param [Integer] ttr TTR of each fire's job; defaults as for a put
|
|
199
|
+
# @param [String, Array] idp Idempotency key, or [key, ttl], as for a
|
|
200
|
+
# put. A fire is skipped while the previous fire's job is still in
|
|
201
|
+
# the tube — buried included, so a buried job stops the entry until
|
|
202
|
+
# it is kicked or deleted.
|
|
203
|
+
# @param [String, Array] con Concurrency key, or [key, limit], as for a put
|
|
204
|
+
# @param [String] grp Group key, as for a put
|
|
205
|
+
# @param [String] aft After-group key, as for a put
|
|
206
|
+
# @param [Symbol, Integer, false] catchup What a tick missed while
|
|
207
|
+
# tuber was down does: `:once` (the default) fires once for the gap;
|
|
208
|
+
# an Integer fires that once only if it is at most that many seconds
|
|
209
|
+
# stale; `:never`, `false` or 0 never fires it.
|
|
210
|
+
# @return [self]
|
|
211
|
+
# @raise [ArgumentError] Something tuber would refuse, or that would
|
|
212
|
+
# break the line
|
|
213
|
+
def add(name, tube:, body:, cron: nil, zone: nil, every: nil, offset: nil, spec: nil,
|
|
214
|
+
pri: nil, delay: nil, ttr: nil, idp: nil, con: nil, grp: nil, aft: nil, catchup: :once)
|
|
215
|
+
name = Schedules.name!("name", name)
|
|
216
|
+
tube = Schedules.name!("tube", tube)
|
|
217
|
+
raise ArgumentError, "schedule #{name.inspect} added twice" if @entries.any? { |e| e.name == name }
|
|
218
|
+
|
|
219
|
+
config = Tuber.configuration
|
|
220
|
+
serialized = config.job_serializer.call(body)
|
|
221
|
+
unless serialized.is_a?(String)
|
|
222
|
+
raise ArgumentError, "the body of schedule #{name.inspect} is a #{serialized.class}, not a String: " \
|
|
223
|
+
"serialize it, or set Tuber.configuration.job_serializer"
|
|
224
|
+
end
|
|
225
|
+
serialized = serialized.b
|
|
226
|
+
|
|
227
|
+
words = [name, tube,
|
|
228
|
+
Integer(pri || config.default_put_pri),
|
|
229
|
+
Integer(delay || config.default_put_delay),
|
|
230
|
+
Integer(ttr || config.default_put_ttr),
|
|
231
|
+
serialized.bytesize]
|
|
232
|
+
words.concat(tags(name, idp: idp, con: con, grp: grp, aft: aft, catchup: catchup))
|
|
233
|
+
words << spec_text(name, cron: cron, zone: zone, every: every, offset: offset, spec: spec)
|
|
234
|
+
@entries << Entry.new(name, words.join(' '), serialized)
|
|
235
|
+
self
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
private
|
|
239
|
+
|
|
240
|
+
def spec_text(name, cron:, zone:, every:, offset:, spec:)
|
|
241
|
+
given = { spec: spec, cron: cron, every: every }.compact
|
|
242
|
+
unless given.size == 1
|
|
243
|
+
raise ArgumentError, "schedule #{name.inspect} needs exactly one of cron:, every: or spec: " \
|
|
244
|
+
"(got #{given.empty? ? 'none' : given.keys.join(', ')})"
|
|
245
|
+
end
|
|
246
|
+
raise ArgumentError, "schedule #{name.inspect}: zone: goes with cron:" if zone && !cron
|
|
247
|
+
raise ArgumentError, "schedule #{name.inspect}: offset: goes with every:" if offset && !every
|
|
248
|
+
|
|
249
|
+
text = if every
|
|
250
|
+
["every #{duration(every)}", (offset && "offset #{duration(offset)}")].compact.join(' ')
|
|
251
|
+
elsif cron
|
|
252
|
+
[cron, zone].compact.join(' ')
|
|
253
|
+
else
|
|
254
|
+
spec
|
|
255
|
+
end
|
|
256
|
+
# Tags end at the first word without a colon, so a spec can't have one.
|
|
257
|
+
if !text.is_a?(String) || text.strip.empty? || text.match?(/[\r\n:]/)
|
|
258
|
+
raise ArgumentError, "invalid spec #{text.inspect} for schedule #{name.inspect}"
|
|
259
|
+
end
|
|
260
|
+
text.strip
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def duration(value)
|
|
264
|
+
value.is_a?(Integer) ? "#{value.to_i}s" : value.to_s
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def tags(name, catchup:, **options)
|
|
268
|
+
tags = Tube.tags(options)
|
|
269
|
+
case catchup
|
|
270
|
+
when :once, nil then nil
|
|
271
|
+
when :never, false then tags << "catchup:0"
|
|
272
|
+
when Integer
|
|
273
|
+
raise ArgumentError, "catchup: can't be negative (schedule #{name.inspect})" if catchup.negative?
|
|
274
|
+
tags << "catchup:#{catchup.to_i}"
|
|
275
|
+
else
|
|
276
|
+
raise ArgumentError, "invalid catchup: #{catchup.inspect} for schedule #{name.inspect} " \
|
|
277
|
+
"(:once, :never, or a number of seconds)"
|
|
278
|
+
end
|
|
279
|
+
tags.each do |tag|
|
|
280
|
+
next if tag.match?(/\A[a-z]+:\S+\z/)
|
|
281
|
+
raise ArgumentError, "invalid tag #{tag.inspect} for schedule #{name.inspect}: " \
|
|
282
|
+
"keys can't be empty or contain whitespace"
|
|
283
|
+
end
|
|
284
|
+
tags
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end # Schedules
|
|
288
|
+
end # Tuber
|
data/lib/tuber/tube/record.rb
CHANGED
|
@@ -62,21 +62,32 @@ class Tuber
|
|
|
62
62
|
|
|
63
63
|
cmd_options = "#{options[:pri]} #{options[:delay]} #{options[:ttr]} #{serialized_body.bytesize}"
|
|
64
64
|
|
|
65
|
-
tags =
|
|
66
|
-
if options[:idp]
|
|
67
|
-
tags << (options[:idp].is_a?(Array) ? "idp:#{options[:idp].join(':')}" : "idp:#{options[:idp]}")
|
|
68
|
-
end
|
|
69
|
-
if options[:con]
|
|
70
|
-
tags << (options[:con].is_a?(Array) ? "con:#{options[:con].join(':')}" : "con:#{options[:con]}")
|
|
71
|
-
end
|
|
72
|
-
tags << "grp:#{options[:grp]}" if options[:grp]
|
|
73
|
-
tags << "aft:#{options[:aft]}" if options[:aft]
|
|
74
|
-
|
|
65
|
+
tags = Tube.tags(options)
|
|
75
66
|
cmd_options = "#{cmd_options} #{tags.join(' ')}" if tags.any?
|
|
76
67
|
transmit("put #{cmd_options}\r\n#{serialized_body}")
|
|
77
68
|
end
|
|
78
69
|
end
|
|
79
70
|
|
|
71
|
+
# The tags a put carries for its :idp, :con, :grp and :aft options.
|
|
72
|
+
# Tuber::Schedules builds its entries' tags here too: every fire is a put
|
|
73
|
+
# carrying them.
|
|
74
|
+
#
|
|
75
|
+
# @param [Hash] options Put options, as for #put
|
|
76
|
+
# @return [Array<String>] e.g. ["idp:report:300", "con:api"]
|
|
77
|
+
# @api private
|
|
78
|
+
def self.tags(options)
|
|
79
|
+
tags = []
|
|
80
|
+
if options[:idp]
|
|
81
|
+
tags << (options[:idp].is_a?(Array) ? "idp:#{options[:idp].join(':')}" : "idp:#{options[:idp]}")
|
|
82
|
+
end
|
|
83
|
+
if options[:con]
|
|
84
|
+
tags << (options[:con].is_a?(Array) ? "con:#{options[:con].join(':')}" : "con:#{options[:con]}")
|
|
85
|
+
end
|
|
86
|
+
tags << "grp:#{options[:grp]}" if options[:grp]
|
|
87
|
+
tags << "aft:#{options[:aft]}" if options[:aft]
|
|
88
|
+
tags
|
|
89
|
+
end
|
|
90
|
+
|
|
80
91
|
# Peek at next job within this tube in given `state`.
|
|
81
92
|
#
|
|
82
93
|
# @param [String] state The job state to peek at (`ready`, `buried`, `delayed`)
|
data/lib/tuber/version.rb
CHANGED
data/lib/tuber.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'thread' unless defined?(Mutex)
|
|
4
4
|
|
|
5
|
-
%w(version configuration errors connection tube job stats).each do |f|
|
|
5
|
+
%w(version configuration errors connection tube job stats schedules).each do |f|
|
|
6
6
|
require "tuber/#{f}"
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -51,6 +51,15 @@ class Tuber
|
|
|
51
51
|
@stats ||= Stats.new(self)
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
# Returns Tuber::Schedules object for the server's recurring schedules
|
|
55
|
+
# (Tuber only).
|
|
56
|
+
#
|
|
57
|
+
# @return [Tuber::Schedules] schedules object
|
|
58
|
+
# @api public
|
|
59
|
+
def schedules
|
|
60
|
+
@schedules ||= Schedules.new(self)
|
|
61
|
+
end
|
|
62
|
+
|
|
54
63
|
# Puts the server into drain mode, rejecting new puts.
|
|
55
64
|
#
|
|
56
65
|
# @return [Hash] Response from beanstalkd
|
data/test/connection_test.rb
CHANGED
|
@@ -99,7 +99,11 @@ describe Tuber::Connection do
|
|
|
99
99
|
assert_same real_socket, bc.connection
|
|
100
100
|
end
|
|
101
101
|
|
|
102
|
-
it "should set SO_RCVTIMEO and SO_SNDTIMEO when read/write timeouts are configured" do
|
|
102
|
+
it "should set SO_RCVTIMEO and SO_SNDTIMEO when read/write timeouts are configured on Ruby < 3.2" do
|
|
103
|
+
# Ruby's sockets are non-blocking from 3.0 on and ignore these; from 3.2
|
|
104
|
+
# IO#timeout does the job (see 'for read and write timeouts').
|
|
105
|
+
skip("IO#timeout replaces SO_RCVTIMEO/SO_SNDTIMEO on Ruby >= 3.2") if Tuber::Connection::IO_TIMEOUTS
|
|
106
|
+
|
|
103
107
|
Tuber.configure do |config|
|
|
104
108
|
config.read_timeout = 3
|
|
105
109
|
config.write_timeout = 5
|
|
@@ -114,6 +118,215 @@ describe Tuber::Connection do
|
|
|
114
118
|
|
|
115
119
|
end # timeout configuration
|
|
116
120
|
|
|
121
|
+
# Settings changed in these blocks are put back by swapping in a copy of the
|
|
122
|
+
# configuration and restoring the original afterwards.
|
|
123
|
+
def swap_in_config_copy!
|
|
124
|
+
@saved_config = Tuber.configuration
|
|
125
|
+
Tuber.instance_variable_set(:@_configuration, @saved_config.dup)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def restore_config!
|
|
129
|
+
Tuber.instance_variable_set(:@_configuration, @saved_config) if @saved_config
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# A server that accepts connections and then never reads or answers: the
|
|
133
|
+
# client side of a peer that has vanished without a FIN, as far as any one
|
|
134
|
+
# read or write can tell. Returns its address.
|
|
135
|
+
def start_silent_server
|
|
136
|
+
@silent_server = TCPServer.new('127.0.0.1', 0)
|
|
137
|
+
@silent_peers = []
|
|
138
|
+
@silent_thread = Thread.new do
|
|
139
|
+
loop { @silent_peers << @silent_server.accept }
|
|
140
|
+
rescue IOError
|
|
141
|
+
# closed by stop_silent_server
|
|
142
|
+
end
|
|
143
|
+
"127.0.0.1:#{@silent_server.addr[1]}"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def stop_silent_server
|
|
147
|
+
return unless @silent_server
|
|
148
|
+
@silent_server.close rescue nil
|
|
149
|
+
@silent_thread.join(1)
|
|
150
|
+
@silent_peers.each { |s| s.close rescue nil }
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# The longest a test lets a call block before calling it a hang. Without the
|
|
154
|
+
# guard a read the client never gives up on would stall the whole suite.
|
|
155
|
+
def hang_guard
|
|
156
|
+
10
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def int_sockopt(socket, level, name)
|
|
160
|
+
socket.getsockopt(level, name).int
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
describe 'for TCP keepalive' do
|
|
164
|
+
before { swap_in_config_copy! }
|
|
165
|
+
|
|
166
|
+
after do
|
|
167
|
+
@bc.close if @bc
|
|
168
|
+
restore_config!
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "enables keepalive by default, so a vanished server is noticed in about a minute" do
|
|
172
|
+
@bc = Tuber::Connection.new(tuber_address)
|
|
173
|
+
sock = @bc.connection
|
|
174
|
+
|
|
175
|
+
assert sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool,
|
|
176
|
+
"expected SO_KEEPALIVE on a fresh connection"
|
|
177
|
+
if Socket.const_defined?(:TCP_KEEPIDLE)
|
|
178
|
+
assert_equal 30, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPIDLE)
|
|
179
|
+
end
|
|
180
|
+
if Socket.const_defined?(:TCP_KEEPINTVL)
|
|
181
|
+
assert_equal 10, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPINTVL)
|
|
182
|
+
end
|
|
183
|
+
if Socket.const_defined?(:TCP_KEEPCNT)
|
|
184
|
+
assert_equal 3, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPCNT)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it "uses the configured probe timings" do
|
|
189
|
+
skip "no TCP_KEEPIDLE on this platform" unless Socket.const_defined?(:TCP_KEEPIDLE)
|
|
190
|
+
Tuber.configure do |config|
|
|
191
|
+
config.keepalive_idle = 7
|
|
192
|
+
config.keepalive_interval = 2
|
|
193
|
+
config.keepalive_count = 5
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
@bc = Tuber::Connection.new(tuber_address)
|
|
197
|
+
sock = @bc.connection
|
|
198
|
+
|
|
199
|
+
assert_equal 7, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPIDLE)
|
|
200
|
+
assert_equal 2, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPINTVL)
|
|
201
|
+
assert_equal 5, int_sockopt(sock, Socket::IPPROTO_TCP, Socket::TCP_KEEPCNT)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it "keeps keepalive on the socket a reconnect builds" do
|
|
205
|
+
@bc = Tuber::Connection.new(tuber_address)
|
|
206
|
+
@bc.reconnect!
|
|
207
|
+
|
|
208
|
+
assert @bc.connection.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
it "leaves keepalive off when it is turned off" do
|
|
212
|
+
Tuber.configure { |config| config.keepalive = false }
|
|
213
|
+
|
|
214
|
+
@bc = Tuber::Connection.new(tuber_address)
|
|
215
|
+
|
|
216
|
+
refute @bc.connection.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool
|
|
217
|
+
end
|
|
218
|
+
end # TCP keepalive
|
|
219
|
+
|
|
220
|
+
describe 'for a dead peer noticed mid-command' do
|
|
221
|
+
# What a blocked read raises once the kernel gives up on the peer:
|
|
222
|
+
# Errno::ETIMEDOUT from failed keepalive probes, IO::TimeoutError from a
|
|
223
|
+
# read_timeout. Either way the socket is dead and must be replaced.
|
|
224
|
+
timeout_errors = [Errno::ETIMEDOUT]
|
|
225
|
+
timeout_errors << IO::TimeoutError if defined?(IO::TimeoutError)
|
|
226
|
+
|
|
227
|
+
before { @bc = Tuber::Connection.new(tuber_address) }
|
|
228
|
+
after { @bc.close rescue nil }
|
|
229
|
+
|
|
230
|
+
timeout_errors.each do |error_class|
|
|
231
|
+
it "reconnects and retransmits an idempotent command after #{error_class}" do
|
|
232
|
+
old_socket = @bc.connection
|
|
233
|
+
TCPSocket.any_instance.expects(:readline).times(2).
|
|
234
|
+
raises(error_class.new).then.
|
|
235
|
+
returns("USING foo\n")
|
|
236
|
+
|
|
237
|
+
res = @bc.transmit "use foo"
|
|
238
|
+
|
|
239
|
+
assert_equal 'USING', res[:status]
|
|
240
|
+
refute_same old_socket, @bc.connection, "expected a fresh socket after #{error_class}"
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
it "heals the connection but re-raises #{error_class} for a non-idempotent command" do
|
|
244
|
+
old_socket = @bc.connection
|
|
245
|
+
TCPSocket.any_instance.expects(:readline).times(1).raises(error_class.new)
|
|
246
|
+
|
|
247
|
+
assert_raises(error_class) { @bc.transmit "delete 56" }
|
|
248
|
+
refute_same old_socket, @bc.connection, "expected a fresh socket after #{error_class}"
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
end # dead peer
|
|
252
|
+
|
|
253
|
+
describe 'for read and write timeouts' do
|
|
254
|
+
before do
|
|
255
|
+
skip "IO#timeout= (and with it working read/write timeouts) needs Ruby >= 3.2" unless IO.method_defined?(:timeout=)
|
|
256
|
+
swap_in_config_copy!
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
after do
|
|
260
|
+
@bc&.close
|
|
261
|
+
stop_silent_server
|
|
262
|
+
restore_config!
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
it "read_timeout raises against a server that never answers" do
|
|
266
|
+
address = start_silent_server
|
|
267
|
+
Tuber.configure { |config| config.read_timeout = 0.3 }
|
|
268
|
+
@bc = Tuber::Connection.new(address)
|
|
269
|
+
|
|
270
|
+
started = Time.now
|
|
271
|
+
Timeout.timeout(hang_guard) do
|
|
272
|
+
assert_raises(IO::TimeoutError) { @bc.transmit 'stats' }
|
|
273
|
+
end
|
|
274
|
+
# One retransmit on a fresh connection, then give up: about two reads' worth.
|
|
275
|
+
assert_operator Time.now - started, :<, 3
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
it "write_timeout raises when the server stops reading" do
|
|
279
|
+
address = start_silent_server
|
|
280
|
+
Tuber.configure { |config| config.write_timeout = 0.3 }
|
|
281
|
+
@bc = Tuber::Connection.new(address)
|
|
282
|
+
body = 'x' * (64 * 1024 * 1024) # far past any socket buffer
|
|
283
|
+
|
|
284
|
+
Timeout.timeout(hang_guard) do
|
|
285
|
+
assert_raises(IO::TimeoutError) { @bc.transmit "put 0 0 100 #{body.bytesize}\r\n#{body}" }
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
it "lets reserve-with-timeout wait out its own timeout past read_timeout" do
|
|
290
|
+
Tuber.configure { |config| config.read_timeout = 0.3 }
|
|
291
|
+
client = Tuber.new(tuber_address)
|
|
292
|
+
@bc = client.connection
|
|
293
|
+
client.tubes.watch!("rt_empty_#{Process.pid}")
|
|
294
|
+
|
|
295
|
+
Timeout.timeout(hang_guard) do
|
|
296
|
+
assert_raises(Tuber::TimedOutError) { client.tubes.reserve(1) }
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
it "lets a plain reserve block past read_timeout until a job arrives" do
|
|
301
|
+
Tuber.configure { |config| config.read_timeout = 0.3 }
|
|
302
|
+
tube = "rt_plain_#{Process.pid}"
|
|
303
|
+
client = Tuber.new(tuber_address)
|
|
304
|
+
@bc = client.connection
|
|
305
|
+
client.tubes.watch!(tube)
|
|
306
|
+
producer = Tuber.new(tuber_address)
|
|
307
|
+
Thread.new { sleep 1; producer.tubes[tube].put("late") }
|
|
308
|
+
|
|
309
|
+
job = Timeout.timeout(hang_guard) { client.tubes.reserve }
|
|
310
|
+
|
|
311
|
+
assert_equal "late", job.body
|
|
312
|
+
job.delete
|
|
313
|
+
ensure
|
|
314
|
+
producer.close if producer
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
it "lets reserve_batch long-poll past read_timeout" do
|
|
318
|
+
require_tuber!
|
|
319
|
+
Tuber.configure { |config| config.read_timeout = 0.3 }
|
|
320
|
+
client = Tuber.new(tuber_address)
|
|
321
|
+
@bc = client.connection
|
|
322
|
+
client.tubes.watch!("rt_batch_#{Process.pid}")
|
|
323
|
+
|
|
324
|
+
jobs = Timeout.timeout(hang_guard) { client.tubes.reserve_batch(5, 1) }
|
|
325
|
+
|
|
326
|
+
assert_equal [], jobs
|
|
327
|
+
end
|
|
328
|
+
end # read and write timeouts
|
|
329
|
+
|
|
117
330
|
describe 'for #transmit' do
|
|
118
331
|
before do
|
|
119
332
|
@host = 'localhost'
|
|
@@ -624,4 +837,24 @@ describe Tuber::Connection do
|
|
|
624
837
|
assert_raises(Tuber::DrainingError) { @bc.transmit("put 0 0 100 4\r\ntest") }
|
|
625
838
|
end
|
|
626
839
|
end # drain and undrain
|
|
840
|
+
|
|
841
|
+
describe 'for a put refused with OUT_OF_STORAGE' do
|
|
842
|
+
# Tuber answers OUT_OF_STORAGE once a persistent server's disk budget
|
|
843
|
+
# (--max-storage-bytes) is spent. It must raise like OUT_OF_MEMORY does,
|
|
844
|
+
# not come back as a hash a caller would read as a successful put.
|
|
845
|
+
it "should raise" do
|
|
846
|
+
@bc = Tuber::Connection.new('localhost')
|
|
847
|
+
TCPSocket.any_instance.stubs(:write)
|
|
848
|
+
TCPSocket.any_instance.expects(:readline).returns("OUT_OF_STORAGE\r\n")
|
|
849
|
+
err = begin
|
|
850
|
+
@bc.transmit("put 0 0 100 4\r\ntest")
|
|
851
|
+
nil
|
|
852
|
+
rescue Tuber::UnexpectedResponse => e
|
|
853
|
+
e
|
|
854
|
+
end
|
|
855
|
+
refute_nil err, "OUT_OF_STORAGE came back as a result instead of raising"
|
|
856
|
+
assert_equal 'OUT_OF_STORAGE', err.status
|
|
857
|
+
assert_equal 'OutOfStorageError', err.class.name.split('::').last
|
|
858
|
+
end
|
|
859
|
+
end # OUT_OF_STORAGE
|
|
627
860
|
end # Tuber::Connection
|
data/test/errors_test.rb
CHANGED
|
@@ -32,4 +32,11 @@ describe "Tuber::Errors" do
|
|
|
32
32
|
assert_equal 'reserve 0', @klazz.cmd
|
|
33
33
|
assert_equal 'EXPECTED_CRLF', @klazz.status
|
|
34
34
|
end
|
|
35
|
+
|
|
36
|
+
it 'should raise proper exception for invalid status OUT_OF_STORAGE' do
|
|
37
|
+
@klazz = Tuber::UnexpectedResponse.from_status("OUT_OF_STORAGE", "put 0 0 120 4")
|
|
38
|
+
assert_kind_of(Tuber::UnexpectedResponse, @klazz)
|
|
39
|
+
assert_equal 'OutOfStorageError', @klazz.class.name.split('::').last
|
|
40
|
+
assert_equal 'OUT_OF_STORAGE', @klazz.status
|
|
41
|
+
end
|
|
35
42
|
end
|