-
Notifications
You must be signed in to change notification settings - Fork 248
/
0091.yml
606 lines (575 loc) · 21.6 KB
/
0091.yml
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
version: 91
description: Migrate existing azure queue messages into new tables in a reversible way
migrationScript: 0091-migration.sql
downgradeScript: 0091-downgrade.sql
methods:
# existing azure_queue methods are being modified to use new tables
# this is to allow smooth transition during migration, as there will be
# background processes and api requests that will still use the old methods
azure_queue_get:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
As data is migrated to new table we return empty set here.
mode: write
serviceName: queue
args: queue_name text, visible timestamp, count integer
returns: table (message_id uuid, message_text text, pop_receipt uuid)
body: |-
begin
return query
select
null::uuid as message_id,
null::text as message_text,
null::uuid as pop_receipt
limit 0;
end
azure_queue_count:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
This function switches between queue names to get the count from needed table,
although in practice this was only used for pending tasks.
mode: read
serviceName: queue
args: queue_name text
returns: integer
body: |-
declare
result integer;
begin
IF queue_name = 'claim-queue' THEN
SELECT count(*) into result FROM queue_claimed_tasks WHERE expires > now();
ELSIF queue_name = 'resolved-queue' THEN
SELECT count(*) into result FROM queue_resolved_tasks WHERE expires > now();
ELSIF queue_name = 'deadline-queue' THEN
SELECT count(*) into result FROM queue_task_deadlines WHERE expires > now();
ELSE
SELECT count(*) into result FROM queue_pending_tasks
WHERE queue_name_compat = queue_name
AND expires > now();
END IF;
RETURN result;
end
azure_queue_delete:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
This will delete record if it exists in one of the new tables.
mode: write
serviceName: queue
args: queue_name text, message_id uuid, pop_receipt uuid
returns: void
body: |-
begin
IF queue_name = 'claim-queue' THEN
DELETE FROM queue_claimed_tasks
WHERE queue_claimed_tasks.message_id_compat = message_id
AND queue_claimed_tasks.pop_receipt = pop_receipt;
ELSIF queue_name = 'resolved-queue' THEN
DELETE FROM queue_resolved_tasks
WHERE queue_resolved_tasks.message_id_compat = message_id
AND queue_resolved_tasks.pop_receipt = pop_receipt;
ELSIF queue_name = 'deadline-queue' THEN
DELETE FROM queue_task_deadlines
WHERE queue_task_deadlines.message_id_compat = message_id
AND queue_task_deadlines.pop_receipt = pop_receipt;
ELSE
DELETE FROM queue_pending_tasks
WHERE queue_pending_tasks.message_id_compat = message_id
AND queue_pending_tasks.queue_name_compat = queue_name
AND queue_pending_tasks.pop_receipt = pop_receipt;
END IF;
end
azure_queue_update:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
This will update record if it exists in one of the new tables with one caveat:
only `visible` column would be updated as `message_text` was immutable in the old table.
mode: write
serviceName: queue
args: queue_name text, message_text text, message_id uuid, pop_receipt uuid, visible timestamp
returns: void
body: |-
begin
IF queue_name = 'claim-queue' THEN
UPDATE queue_claimed_tasks
SET queue_claimed_tasks.visible = visible
WHERE queue_claimed_tasks.message_id_compat = message_id
AND queue_claimed_tasks.pop_receipt = pop_receipt;
ELSIF queue_name = 'resolved-queue' THEN
UPDATE queue_resolved_tasks
SET queue_resolved_tasks.visible = visible
WHERE queue_resolved_tasks.message_id_compat = message_id
AND queue_resolved_tasks.pop_receipt = pop_receipt;
ELSIF queue_name = 'deadline-queue' THEN
UPDATE queue_task_deadlines
SET queue_task_deadlines.visible = visible
WHERE queue_task_deadlines.message_id_compat = message_id
AND queue_task_deadlines.pop_receipt = pop_receipt;
ELSE
UPDATE queue_pending_tasks
SET queue_pending_tasks.visible = visible
WHERE queue_pending_tasks.message_id_compat = message_id
AND queue_pending_tasks.queue_name_compat = queue_name
AND queue_pending_tasks.pop_receipt = pop_receipt;
END IF;
end
azure_queue_put_extra:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
Messages would be routed to the correct table based on the queue_name.
mode: write
serviceName: queue
args: queue_name text, message_text text, visible timestamp, expires timestamp, task_queue_id text, priority int
returns: void
body: |-
begin
IF queue_name = 'claim-queue' THEN
INSERT INTO queue_claimed_tasks
(task_id, run_id, task_queue_id, worker_group, worker_id, claimed, taken_until, visible, message_id_compat)
VALUES (
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskId',
CAST(convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'runId' AS INTEGER),
COALESCE(azure_queue_put_extra.task_queue_id, ''), -- this might also be null
'', -- worker_group was not present in old format
'', -- worker_id was not present in old format
now(), -- claimed
CAST(convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'takenUntil' AS timestamp with time zone),
azure_queue_put_extra.visible, -- visible initially same as taken_until
public.gen_random_uuid()
);
ELSIF queue_name = 'resolved-queue' THEN
INSERT INTO queue_resolved_tasks
(task_id, task_group_id, scheduler_id, resolution, resolved, visible, message_id_compat)
VALUES (
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskId',
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskGroupId',
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'schedulerId',
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'resolution',
now(), -- resolved
azure_queue_put_extra.visible,
public.gen_random_uuid()
);
ELSIF queue_name = 'deadline-queue' THEN
INSERT INTO queue_task_deadlines
(task_group_id, task_id, scheduler_id, created, deadline, visible, message_id_compat)
VALUES (
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskGroupId',
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskId',
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'schedulerId',
now(), -- created
CAST(convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'deadline' AS timestamp with time zone),
azure_queue_put_extra.visible,
public.gen_random_uuid()
);
ELSE
INSERT INTO queue_pending_tasks
(task_queue_id, priority, task_id, run_id, hint_id, inserted, expires, visible, queue_name_compat, message_id_compat)
VALUES
(
azure_queue_put_extra.task_queue_id,
azure_queue_put_extra.priority,
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'taskId',
cast(convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'runId' AS INTEGER),
convert_from(decode(azure_queue_put_extra.message_text, 'base64'), 'utf-8')::jsonb->>'hintId',
now(),
azure_queue_put_extra.expires,
azure_queue_put_extra.visible,
-- for backward compatibility with azure_queue_messages
azure_queue_put_extra.queue_name,
public.gen_random_uuid()
);
END IF;
execute 'notify ' || quote_ident(queue_name);
end
azure_queue_delete_expired:
description: |
This is a modified version of the original method that is used during migration.
We want to prevent data loss and let running processes still use existing functions.
We will not do anything here to avoid data loss.
mode: write
serviceName: queue
args: ''
returns: void
body: |-
begin
-- all good, nothing to see here
end
queue_pending_tasks_get:
description: |
Get up to `count` tasks for the pending tasks from the given taskQueueId.
Tasks are locked and will temporarily become invisible for the `visible` period.
mode: write
serviceName: queue
args: task_queue_id_in text, visible_in timestamptz, count integer
returns: table (task_id text, run_id integer, hint_id text, pop_receipt uuid)
body: |-
begin
return query
with updated as (
update queue_pending_tasks q
set pop_receipt = public.gen_random_uuid(),
visible = visible_in
where
q.task_id in (
select q2.task_id
from queue_pending_tasks q2
where q2.task_queue_id = task_queue_id_in
and q2.visible <= now()
and q2.expires > now()
order by q2.priority desc, q2.inserted asc
for update skip locked
limit count
)
returning q.priority, q.inserted, q.task_id, q.run_id, q.hint_id, q.pop_receipt
)
select u.task_id, u.run_id, u.hint_id, u.pop_receipt
from updated as u
order by u.priority desc, u.inserted asc;
end
queue_pending_tasks_put:
description: |
Put the task into the pending queue.
When record already exists, we update the priority, run_id, hint_id and expiration.
This also sends a notification to the `task_pending` channel with the `task_queue_id` as its payload.
mode: write
serviceName: queue
args: task_queue_id_in text, priority_in integer, task_id_in text, run_id_in integer, hint_id_in text, expires_in timestamp, queue_name_compat_in text
returns: void
body: |-
begin
INSERT INTO queue_pending_tasks
(task_queue_id, priority, task_id, run_id, hint_id, inserted, expires, visible, queue_name_compat, message_id_compat)
VALUES (
task_queue_id_in,
priority_in,
task_id_in,
run_id_in,
hint_id_in,
now(),
expires_in,
now(),
queue_name_compat_in, -- these are temporary columns to provide backward compatibility with azure_queue_messages
public.gen_random_uuid()
)
ON CONFLICT (task_id, run_id) DO UPDATE
SET
expires = greatest(coalesce(expires_in, queue_pending_tasks.expires), queue_pending_tasks.expires),
priority = priority_in,
hint_id = hint_id_in
WHERE
queue_pending_tasks.task_queue_id = task_queue_id_in
AND queue_pending_tasks.task_id = task_id_in
AND queue_pending_tasks.run_id = run_id_in
AND queue_pending_tasks.pop_receipt is null;
-- notify listeners that there is a new task in the queue
EXECUTE 'NOTIFY task_pending, ' || quote_literal(task_queue_id_in) || ';';
end
queue_pending_tasks_release:
description: |
Release task back to the queue to be picked up by another worker.
mode: write
serviceName: queue
args: task_id_in text, pop_receipt_in uuid
returns: void
body: |-
begin
UPDATE queue_pending_tasks
SET visible = now()
WHERE task_id = task_id_in AND pop_receipt = pop_receipt_in;
end
queue_pending_tasks_delete:
description: |
Delete single pending task from the queue.
mode: write
serviceName: queue
args: task_id_in text, pop_receipt_in uuid
returns: void
body: |-
begin
DELETE FROM queue_pending_tasks
WHERE task_id = task_id_in AND pop_receipt = pop_receipt_in;
end
queue_pending_tasks_delete_expired:
description: |
Delete all expired tasks from pending queue.
mode: write
serviceName: queue
args: ''
returns: void
body: |-
begin
DELETE FROM queue_pending_tasks
WHERE expires <= now();
end
queue_pending_tasks_count:
description: |
Count the number of pending tasks for given task queue.
mode: read
serviceName: queue
args: task_queue_id_in text
returns: integer
body: |-
begin
return (
select count(*)
from queue_pending_tasks
where task_queue_id = task_queue_id_in
and expires > now()
);
end
# deadline queue
queue_task_deadline_put:
description: |
Track task deadline upon task creation. This would stay until task
deadline to see if it was ever scheduled or resolved.
mode: write
serviceName: queue
args: task_group_id_in text, task_id_in text, scheduler_id_in text, deadline_in timestamptz, visible timestamptz
returns: void
body: |-
begin
insert into queue_task_deadlines (
task_group_id,
task_id,
scheduler_id,
created,
deadline,
visible,
message_id_compat
)
values (
task_group_id_in,
task_id_in,
scheduler_id_in,
now(),
deadline_in,
visible,
public.gen_random_uuid()
);
end
queue_task_deadline_get:
description: |
Get up to `count` tasks from the deadline queue.
mode: write
serviceName: queue
args: visible_in timestamptz, count integer
returns: table (task_id text, task_group_id text, scheduler_id text, deadline timestamptz, pop_receipt uuid)
body: |-
begin
return query
with updated as (
update queue_task_deadlines q
set pop_receipt = public.gen_random_uuid(),
visible = visible_in
where
q.task_id in (
select q2.task_id
from queue_task_deadlines q2
where q2.visible <= now()
order by q2.deadline
for update skip locked
limit count
)
returning q.task_id, q.task_group_id, q.scheduler_id, q.deadline, q.pop_receipt
)
select u.task_id, u.task_group_id, u.scheduler_id, u.deadline, u.pop_receipt
from updated as u
order by u.deadline;
end
queue_task_deadline_delete:
description: |
Delete single deadline task.
mode: write
serviceName: queue
args: task_id_in text, pop_receipt_in uuid
returns: void
body: |-
begin
delete from queue_task_deadlines
where task_id = task_id_in
and pop_receipt = pop_receipt_in;
end
queue_task_deadline_resolved:
description: |
Once the task gets resolved it is no longer relevant for the deadline queue.
Since resolved task triggered dependency resolver, its dependencies were already
updated by DependencyTracker.
We can safely delete given run from the deadline queue.
mode: write
serviceName: queue
args: task_id_in text
returns: void
body: |-
begin
delete from queue_task_deadlines
where task_id = task_id_in;
end
# claim queue
queue_claimed_task_put:
description: |
Track when task was claimed and when it should be reclaimed.
It is possible to have multiple records for a given taskId+runId combination.
mode: write
serviceName: queue
args: task_id_in text, run_id_in integer, taken_until_in timestamptz, task_queue_id_in text, worker_group_in text, worker_id_in text
returns: void
body: |-
begin
INSERT INTO queue_claimed_tasks (
task_id,
run_id,
task_queue_id,
worker_group,
worker_id,
claimed,
taken_until,
visible,
message_id_compat
)
VALUES (
task_id_in,
run_id_in,
task_queue_id_in,
worker_group_in,
worker_id_in,
now(),
taken_until_in,
taken_until_in, -- visible initially same as taken_until
public.gen_random_uuid()
);
end
queue_claimed_task_get:
description: |
Get up to `count` tasks from the claimed queue.
mode: write
serviceName: queue
args: visible_in timestamptz, count integer
returns: table (task_id text, run_id integer, taken_until timestamptz, pop_receipt uuid)
body: |-
begin
return query
with updated as (
update queue_claimed_tasks q
set pop_receipt = public.gen_random_uuid(),
visible = visible_in
where
q.task_id in (
select q2.task_id
from queue_claimed_tasks q2
where q2.visible <= now()
order by q2.taken_until
for update skip locked
limit count
)
returning q.task_id, q.run_id, q.taken_until, q.pop_receipt
)
select u.task_id, u.run_id, u.taken_until, u.pop_receipt
from updated as u
order by u.taken_until;
end
queue_claimed_task_delete:
description: |
Delete single claimed task from the queue.
mode: write
serviceName: queue
args: task_id_in text, pop_receipt_in uuid
returns: void
body: |-
begin
delete from queue_claimed_tasks
where task_id = task_id_in
and pop_receipt = pop_receipt_in;
end
queue_claimed_task_resolved:
description: |
Once the task gets resolved it is no longer relevant for the claim queue, since it cannot expire anymore.
We can safely delete given run from the claim queue.
mode: write
serviceName: queue
args: task_id_in text, run_id_in integer
returns: void
body: |-
begin
delete from queue_claimed_tasks
where task_id = task_id_in
and run_id = run_id_in;
end
# resolved queue
queue_resolved_task_put:
description: |
Track when task was resolved.
This is a short-lived record that is used by dependency resolver to update dependencies.
Notification is sent to `task_resolved` channel with the `task_id` as its payload.
mode: write
serviceName: queue
args: task_group_id_in text, task_id_in text, scheduler_id_in text, resolution_in text
returns: void
body: |-
begin
insert into queue_resolved_tasks (
task_group_id,
task_id,
scheduler_id,
resolution,
resolved,
visible,
message_id_compat
)
values (
task_group_id_in,
task_id_in,
scheduler_id_in,
resolution_in,
now(),
now(),
public.gen_random_uuid()
);
-- notify listeners that task was resolved
EXECUTE 'NOTIFY task_resolved';
end
queue_resolved_task_get:
description: |
Get up to `count` tasks from the resolved queue.
mode: write
serviceName: queue
args: visible_in timestamptz, count integer
returns: table (task_id text, task_group_id text, scheduler_id text, resolution text, pop_receipt uuid)
body: |-
begin
return query
with updated as (
update queue_resolved_tasks q
set pop_receipt = public.gen_random_uuid(),
visible = visible_in
where
q.task_id in (
select q2.task_id
from queue_resolved_tasks q2
where q2.visible <= now()
order by q2.visible
for update skip locked
limit count
)
returning q.task_id, q.task_group_id, q.scheduler_id, q.resolution, q.visible, q.pop_receipt
)
select u.task_id, u.task_group_id, u.scheduler_id, u.resolution, u.pop_receipt
from updated as u
order by u.visible;
end
queue_resolved_task_delete:
description: |
Delete single task from claimed queue.
mode: write
serviceName: queue
args: task_id_in text, pop_receipt_in uuid
returns: void
body: |-
begin
delete from queue_resolved_tasks
where task_id = task_id_in
and pop_receipt = pop_receipt_in;
end