-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathadmission-queue-pickup.ts
More file actions
160 lines (138 loc) · 6.1 KB
/
Copy pathadmission-queue-pickup.ts
File metadata and controls
160 lines (138 loc) · 6.1 KB
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
/**
* MIT No Attribution
*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as path from 'path';
import { Duration } from 'aws-cdk-lib';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda';
import * as lambda from 'aws-cdk-lib/aws-lambda-nodejs';
import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
/** Pickup Lambda timeout (minutes). */
const PICKUP_TIMEOUT_MINUTES = 5;
/** Pickup Lambda memory (MB). */
const PICKUP_MEMORY_MB = 256;
/**
* Default pickup schedule interval (minutes). Short — queue latency is
* user-visible wait time; 1 minute keeps the worst-case pickup delay
* bounded while a QUEUED-empty cycle is a single cheap GSI query.
*/
const DEFAULT_SCHEDULE_MINUTES = 1;
/** Default max queue age before the backstop fails the task (seconds; 24h). */
const DEFAULT_QUEUE_MAX_AGE_SECONDS = 86400;
/** Default task-record retention used for event TTL (days). */
const DEFAULT_TASK_RETENTION_DAYS = 90;
/**
* Properties for AdmissionQueuePickup construct.
*/
export interface AdmissionQueuePickupProps {
/** TaskTable (StatusIndex GSI powers the QUEUED FIFO query). */
readonly taskTable: dynamodb.ITable;
/** TaskEventsTable (handler writes queue_pickup / task_failed events). */
readonly taskEventsTable: dynamodb.ITable;
/** UserConcurrencyTable (read-only capacity check per user). */
readonly userConcurrencyTable: dynamodb.ITable;
/** ARN of the orchestrator Lambda alias to re-invoke on pickup. */
readonly orchestratorFunctionArn: string;
/**
* Maximum concurrent tasks per user — must match the orchestrator's
* value so the capacity pre-check agrees with `admissionControl`.
* @default 10
*/
readonly maxConcurrentTasksPerUser?: number;
/**
* How often to drain the queue.
* @default Duration.minutes(1)
*/
readonly schedule?: Duration;
/**
* Max time a task may sit QUEUED before the backstop fails it (seconds).
* @default 86400 (24 hours)
*/
readonly queueMaxAgeSeconds?: number;
/** Forwarded to the handler for event TTL. @default 90 */
readonly taskRetentionDays?: number;
}
/**
* Scheduled Lambda that drains the admission queue (#441).
*
* Tasks that hit the per-user concurrency cap are parked in QUEUED by the
* orchestrator instead of FAILED. This Lambda re-attempts admission in
* FIFO order (StatusIndex GSI, ascending ``created_at``) as slots free
* up: it flips QUEUED -> SUBMITTED and re-invokes the orchestrator, whose
* atomic `admissionControl` remains the single writer of the concurrency
* counter (a lost race simply re-queues the task, preserving position).
*/
export class AdmissionQueuePickup extends Construct {
public readonly fn: lambda.NodejsFunction;
constructor(scope: Construct, id: string, props: AdmissionQueuePickupProps) {
super(scope, id);
const handlersDir = path.join(__dirname, '..', 'handlers');
this.fn = new lambda.NodejsFunction(this, 'PickupFn', {
entry: path.join(handlersDir, 'reconcile-admission-queue.ts'),
handler: 'handler',
runtime: Runtime.NODEJS_24_X,
architecture: Architecture.ARM_64,
timeout: Duration.minutes(PICKUP_TIMEOUT_MINUTES),
memorySize: PICKUP_MEMORY_MB,
environment: {
TASK_TABLE_NAME: props.taskTable.tableName,
TASK_EVENTS_TABLE_NAME: props.taskEventsTable.tableName,
USER_CONCURRENCY_TABLE_NAME: props.userConcurrencyTable.tableName,
ORCHESTRATOR_FUNCTION_ARN: props.orchestratorFunctionArn,
MAX_CONCURRENT_TASKS_PER_USER: String(props.maxConcurrentTasksPerUser ?? 10),
QUEUE_MAX_AGE_SECONDS: String(props.queueMaxAgeSeconds ?? DEFAULT_QUEUE_MAX_AGE_SECONDS),
TASK_RETENTION_DAYS: String(props.taskRetentionDays ?? DEFAULT_TASK_RETENTION_DAYS),
},
bundling: {
externalModules: ['@aws-sdk/*'],
},
});
// TaskTable: StatusIndex query + conditional QUEUED->SUBMITTED /
// QUEUED->FAILED transitions.
props.taskTable.grantReadWriteData(this.fn);
// TaskEvents: queue_pickup / task_failed events.
props.taskEventsTable.grantWriteData(this.fn);
// Concurrency: READ-ONLY capacity pre-check — the orchestrator's
// admissionControl is the only writer of the counter.
props.userConcurrencyTable.grantReadData(this.fn);
// Re-invoke the orchestrator alias on pickup.
this.fn.addToRolePolicy(new iam.PolicyStatement({
actions: ['lambda:InvokeFunction'],
resources: [props.orchestratorFunctionArn],
}));
const schedule = props.schedule ?? Duration.minutes(DEFAULT_SCHEDULE_MINUTES);
const rule = new events.Rule(this, 'PickupSchedule', {
schedule: events.Schedule.rate(schedule),
});
rule.addTarget(new targets.LambdaFunction(this.fn));
NagSuppressions.addResourceSuppressions(this.fn, [
{
id: 'AwsSolutions-IAM4',
reason: 'AWSLambdaBasicExecutionRole is required for CloudWatch Logs access',
},
{
id: 'AwsSolutions-IAM5',
reason: 'DynamoDB index/* wildcards generated by CDK grantReadWriteData/grantReadData for StatusIndex query access',
},
], true);
}
}