ndb 1.15.0 → 1.17.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.
data/lib/ndb/stream.rb CHANGED
@@ -21,8 +21,9 @@ module Ndb
21
21
  end
22
22
  end
23
23
 
24
- # The answer of Group#claim and Group#auto_claim. cursor is where the next
25
- # auto_claim starts ("0-0" when the scan is complete; nil for claim).
24
+ # The answer of Group#claim, Group#auto_claim and Group#claim_due. cursor
25
+ # is where the next scan starts ("0-0" when the scan is complete; nil for
26
+ # claim).
26
27
  # deleted holds the pending IDs whose entry is no longer in the stream.
27
28
  Claimed = Data.define(:cursor, :entries, :deleted)
28
29
 
@@ -36,6 +37,10 @@ module Ndb
36
37
  # The group reads every entry of the stream (0).
37
38
  START_ALL = "0"
38
39
 
40
+ # A blocking read waits this many seconds more than block before the
41
+ # client gives up on the answer.
42
+ BLOCK_MARGIN = 5
43
+
39
44
  attr_reader :client, :name
40
45
 
41
46
  # The time of an entry ID (its ms part), or nil for a bad ID.
@@ -44,7 +49,14 @@ module Ndb
44
49
 
45
50
  return nil if seq.empty? || !ms.match?(/\A\d+\z/)
46
51
 
47
- Time.at(ms.to_i / 1000, ms.to_i % 1000, :millisecond).utc
52
+ time_of_ms(ms.to_i)
53
+ end
54
+
55
+ # The Time of a Unix time in milliseconds, or nil for nil.
56
+ def self.time_of_ms(ms)
57
+ return nil if ms.nil?
58
+
59
+ Time.at(ms / 1000, ms % 1000, :millisecond).utc
48
60
  end
49
61
 
50
62
  def initialize(client, name)
@@ -110,6 +122,17 @@ module Ndb
110
122
  range_of(start, stop, count, -1)
111
123
  end
112
124
 
125
+ # Up to count entries with an ID greater than after, in ID order, without
126
+ # a group (XREAD). after is an ID ("0": from the start) or "$": only the
127
+ # entries appended after the request starts. block (seconds, a Float is
128
+ # fine) waits for an entry when there is none. To read on, give the last
129
+ # ID of the answer as after (not "$" again). Returns an Array of Entry.
130
+ def read(after: START_ALL, count: nil, block: nil, fields: nil)
131
+ body = { after: after, count: count, fields: fields }.compact
132
+
133
+ entries("_streams/read", body, **block_options(body, block))
134
+ end
135
+
113
136
  # Removes one entry by ID (XDEL). Returns 1, or 0 when it is not there.
114
137
  def delete(id)
115
138
  call("DELETE", "_doc/#{@client.escape_segment(id)}", nil).fetch("deleted")
@@ -132,6 +155,18 @@ module Ndb
132
155
  rows.map { |row| Entry.from(row) }
133
156
  end
134
157
 
158
+ # Puts block (seconds) in body as block_ms. Returns the options of the
159
+ # request: a read timeout long enough for the wait.
160
+ def block_options(body, block)
161
+ block_ms = block ? (block * 1000).round : 0
162
+
163
+ return {} unless block_ms.positive?
164
+
165
+ body[:block_ms] = block_ms
166
+
167
+ { timeout: [@client.timeout, block + BLOCK_MARGIN].max }
168
+ end
169
+
135
170
  private
136
171
 
137
172
  def append_body(body, id, trim, no_create)
@@ -160,9 +195,7 @@ module Ndb
160
195
  # One consumer group of a stream. Each entry goes to one consumer of the
161
196
  # group and stays pending until it is acked.
162
197
  class Group
163
- # A blocking read waits this many seconds more than block before the
164
- # client gives up on the answer.
165
- BLOCK_MARGIN = 5
198
+ BLOCK_MARGIN = Stream::BLOCK_MARGIN
166
199
 
167
200
  attr_reader :stream, :name
168
201
 
@@ -173,9 +206,13 @@ module Ndb
173
206
 
174
207
  # Makes the group (XGROUP CREATE ... MKSTREAM). start is Stream::START_NEW
175
208
  # ("$"), Stream::START_ALL ("0") or an ID. A group that exists raises
176
- # GroupExists.
177
- def create(start = Stream::START_NEW)
178
- @stream.call("PUT", path, { start: start })
209
+ # GroupExists. no_create: true raises UnknownCollection when the stream
210
+ # does not exist.
211
+ def create(start = Stream::START_NEW, no_create: false)
212
+ body = { start: start }
213
+ body[:no_create] = true if no_create
214
+
215
+ @stream.call("PUT", path, body)
179
216
  end
180
217
 
181
218
  # Makes the group, or does nothing when it exists.
@@ -221,12 +258,7 @@ module Ndb
221
258
  body[:pending] = true if pending
222
259
  body[:no_ack] = true if no_ack
223
260
 
224
- block_ms = block ? (block * 1000).round : 0
225
- body[:block_ms] = block_ms if block_ms.positive?
226
-
227
- timeout = block_ms.positive? ? [@stream.client.timeout, block + BLOCK_MARGIN].max : nil
228
-
229
- @stream.entries(path("read"), body, timeout: timeout)
261
+ @stream.entries(path("read"), body, **@stream.block_options(body, block))
230
262
  end
231
263
 
232
264
  # Ends the work on the entries. Returns how many were pending (XACK). An
@@ -239,32 +271,76 @@ module Ndb
239
271
  @stream.call("POST", path("ack"), { ids: ids }).fetch("acked")
240
272
  end
241
273
 
274
+ # Gives entries back to the group after delay seconds (a Float is
275
+ # fine), for a retry (ndb only). Returns how many it gave back: only the
276
+ # IDs pending for consumer count. The entry stays pending with no owner
277
+ # ("consumer" "") until its retry time, and its delivery count does not
278
+ # change. After the retry time, any consumer takes it with #claim_due
279
+ # (or #claim). delay 0 makes it due at once; the most is 30 days.
280
+ def nack(consumer, *ids, delay: 0)
281
+ ids = ids.flatten
282
+
283
+ return 0 if ids.empty?
284
+
285
+ body = { consumer: consumer, ids: ids, delay_ms: (delay * 1000).round }
286
+
287
+ @stream.call("POST", path("nack"), body).fetch("nacked")
288
+ end
289
+
242
290
  # The pending list in short (XPENDING): "count", "lowest", "highest",
243
- # "consumers" ([{"name", "count"}]).
291
+ # "consumers" ([{"name", "count"}]), and "waiting" (ndb only): the
292
+ # nacked entries whose retry time has not come.
244
293
  def pending
245
294
  @stream.call("POST", path("pending"), {})
246
295
  end
247
296
 
248
297
  # Pending entries in ID order (XPENDING with a range). Each one is a Hash
249
- # with "_id", "consumer", "idle_ms", "deliveries". idle: seconds; only
250
- # the entries idle for at least this long.
251
- def pending_list(start: "-", stop: "+", count: 100, consumer: nil, idle: nil)
252
- body = { start: start, end: stop, count: count, consumer: consumer }.compact
298
+ # with "_id", "consumer", "idle_ms", "deliveries" and "retry_at" (ndb
299
+ # only): the retry time (a Time) of a nacked entry, or nil. A nacked
300
+ # entry has "consumer" "". idle: seconds; only the entries idle for at
301
+ # least this long. due: true gives only the nacked entries whose retry
302
+ # time passed; false only the ones that still wait (ndb only).
303
+ def pending_list(start: "-", stop: "+", count: 100, consumer: nil, idle: nil, due: nil)
304
+ body = { start: start, end: stop, count: count, consumer: consumer, due: due }.compact
253
305
  body[:idle_ms] = (idle * 1000).round if idle && idle.positive?
254
306
 
255
- @stream.call("POST", path("pending"), body) || []
307
+ rows = @stream.call("POST", path("pending"), body) || []
308
+
309
+ rows.each { |row| row["retry_at"] = Stream.time_of_ms(row["retry_at"]) }
256
310
  end
257
311
 
258
312
  # Moves the entries idle for at least min_idle seconds to consumer
259
313
  # (XCLAIM). Each one gets one more delivery. Returns a Claimed.
260
- def claim(consumer, *ids, min_idle: 0)
261
- claim_with(consumer: consumer, min_idle_ms: (min_idle * 1000).round, ids: ids.flatten)
314
+ # idle: (seconds) sets the idle time of the entries; retry_count: sets
315
+ # their delivery count; force: true also takes an ID that is not pending.
316
+ # just_ids: true gives IDs (Strings) as entries, and the delivery count
317
+ # does not change.
318
+ def claim(consumer, *ids, min_idle: 0, idle: nil, retry_count: nil, force: false, just_ids: false)
319
+ body = { consumer: consumer, min_idle_ms: (min_idle * 1000).round, ids: ids.flatten }
320
+ body[:idle_ms] = (idle * 1000).round if idle
321
+ body[:retry_count] = retry_count if retry_count
322
+ body[:force] = true if force
323
+
324
+ claim_with(body, just_ids)
262
325
  end
263
326
 
264
327
  # Scans the pending list from cursor and moves up to count entries idle
265
- # for at least min_idle seconds to consumer (XAUTOCLAIM).
266
- def auto_claim(consumer, min_idle: 0, cursor: "0-0", count: nil)
267
- claim_with({ consumer: consumer, min_idle_ms: (min_idle * 1000).round, cursor: cursor, count: count }.compact)
328
+ # for at least min_idle seconds to consumer (XAUTOCLAIM). just_ids: as
329
+ # in #claim.
330
+ def auto_claim(consumer, min_idle: 0, cursor: "0-0", count: nil, just_ids: false)
331
+ body = { consumer: consumer, min_idle_ms: (min_idle * 1000).round, cursor: cursor, count: count }.compact
332
+
333
+ claim_with(body, just_ids)
334
+ end
335
+
336
+ # Scans the nacked entries from cursor and moves up to count of the ones
337
+ # whose retry time passed to consumer (ndb only). It looks only at the
338
+ # nacked entries, not at the entries in flight at live workers, so a
339
+ # worker can call it often. just_ids: as in #claim.
340
+ def claim_due(consumer, cursor: "0-0", count: nil, just_ids: false)
341
+ body = { consumer: consumer, due: true, cursor: cursor, count: count }.compact
342
+
343
+ claim_with(body, just_ids)
268
344
  end
269
345
 
270
346
  private
@@ -275,9 +351,11 @@ module Ndb
275
351
  segments.join("/")
276
352
  end
277
353
 
278
- def claim_with(body)
354
+ def claim_with(body, just_ids = false)
355
+ body[:just_ids] = true if just_ids
356
+
279
357
  out = @stream.call("POST", path("claim"), body)
280
- entries = (out["entries"] || []).map { |row| Entry.from(row) }
358
+ entries = (out["entries"] || []).map { |row| row.is_a?(Hash) ? Entry.from(row) : row }
281
359
 
282
360
  Claimed.new(cursor: out["cursor"], entries: entries, deleted: out["deleted"] || [])
283
361
  end