1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- Atomically reserve an idempotency key and open one collective transfer.
--
-- KEYS[1]: operation hash
-- KEYS[2]: create-request idempotency key
-- KEYS[3]: group hash
-- KEYS[4]: group active-operations set
-- ARGV: operation_id, group_id, version_id, model_name, idempotency_key,
-- state, created_at_unix_ms, operation_key_prefix, idempotency_ttl_secs,
-- expected_epoch
--
-- Returns:
-- CREATED
-- EXISTING:<operation_id> another invocation already owns the key
-- COLLISION the generated operation ID already exists
-- NOGROUP no group exists for this membership
-- NOTREADY:<epoch>:<state> the group is not READY at the expected epoch
-- BUSY:<operation_id> another operation is still in flight at this epoch
--
-- The idempotency reservation is what makes an orchestrator retry safe: a
-- create that timed out client-side but committed server-side returns the
-- original operation instead of opening a second one against the same group.
--
-- The reservation key also carries a TTL: a worker that dies between this
-- call and the next epoch bump would otherwise leak the key forever, since
-- nothing but a terminal delete ever removes it. The active-operations set
-- is the fast path that lets an epoch bump find and abort this operation
-- before that TTL would matter.
local existing = redis.
if existing
if redis. == 1
local epoch = redis.
if not epoch
-- READY admission and epoch binding are one decision. The caller names the
-- READY epoch it checked; if the group has since moved on (or never got
-- there), nothing is written: no operation that would block the next create,
-- and no reservation that would replay this refusal's key.
local group_state = redis. or ''
if group_state ~= 'READY' or tonumber ~= tonumber
-- At most one nonterminal operation per group epoch. Two operations on one
-- epoch would ride the same lane communicators, and a split cohort (some
-- workers bound to each) would enter collectives its peers are not in. A
-- same-key replay was already answered above, so anything still in flight
-- here belongs to a different request.
local active = redis.
for i = 1, #active
redis.
redis.
redis.
return 'CREATED'