-
Notifications
You must be signed in to change notification settings - Fork 35
feat(orchestration): admission queue with deferred pickup (#441) #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nizar-lahlali
wants to merge
5
commits into
main
Choose a base branch
from
feat/441-admission-queue-deferred-pickup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74d3f57
feat(orchestration): admission queue with deferred pickup (#441)
20c24c4
Merge branch 'main' into feat/441-admission-queue-deferred-pickup
0e0b37a
Merge remote-tracking branch 'origin/main' into feat/441-admission-qu…
98c86a3
Merge branch 'main' into feat/441-admission-queue-deferred-pickup
isadeks fb9c232
Update cdk/src/handlers/get-task.ts
isadeks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.