a999d9ea3f
New batch trigger system with larger payloads, streaming ingestion, larger batch sizes, and a fair processing system. This PR introduces a new `FairQueue` abstraction inspired by our own `RunQueue` that enables multi-tenant fair queueing with concurrency limits. The new `BatchQueue` is built on top of the `FairQueue`, and handles processing Batch triggers in a fair manner with per-environment concurrency limits defined per-org. Additionally, there is a global concurrency limit to prevent the BatchQueue system from creating too many runs too quickly, which can cause downstream issues. For this new BatchQueue system we have a completely new batch trigger creation and ingestion system. Previously this was a single endpoint with a single JSON body that defined details about the batch as well as all the items in the batch. We're introducing a two-phase batch trigger ingestion system. In the first phase, the BatchTaskRun record is created (and possibly rate limited). The second phase is another endpoint that accepts an NDJSON body with each line being a single item/run with payload and options. At ingestion time all items are added to a queue, in order, and then processed by the BatchQueue system. ## New batch trigger rate limits This PR implements a new batch trigger specific rate limit, configured on the `Organization.batchRateLimitConfig` column, and defaults using these environment variables: - `BATCH_RATE_LIMIT_REFILL_RATE` defaults to 10 - `BATCH_RATE_LIMIT_REFILL_INTERVAL` the duration interval, defaults to `"10s"` - `BATCH_RATE_LIMIT_MAX` defaults to 1200 This rate limiter is scoped to the environment ID and controls how many runs can be submitted via batch triggers per interval. The SDK handles the retrying side. ## Batch queue concurrency limits The new column `Organization.batchQueueConcurrencyConfig` now defines an org specific `processingConcurrency` value, with a backup of the env var `BATCH_CONCURRENCY_LIMIT_DEFAULT` which defaults to 10. This controls how many batch queue items are processed concurrently per environment. There is also a global rate limit for the batch queue set via the `BATCH_QUEUE_GLOBAL_RATE_LIMIT` which defaults to being disabled. If set, the entire batch queue system won't process more than `BATCH_QUEUE_GLOBAL_RATE_LIMIT` items per second. This allows controlling the maximum number of runs created per second via batch triggers. ## Batch trigger settings - `STREAMING_BATCH_MAX_ITEMS` controls the maximum number of items in a single batch - `STREAMING_BATCH_ITEM_MAXIMUM_SIZE` controls the maximum size of each item in a batch - `BATCH_CONCURRENCY_DEFAULT_CONCURRENCY` controls the default environment concurrency - `BATCH_QUEUE_DRR_QUANTUM` how many credits each environment gets each round for the DRR scheduler - `BATCH_QUEUE_MAX_DEFICIT` the maximum deficit for the DRR scheduler - `BATCH_QUEUE_CONSUMER_COUNT` how many queue consumers to run - `BATCH_QUEUE_CONSUMER_INTERVAL_MS` how frequently they poll for items in the queue ### Configuration Recommendations by Use Case **High-throughput priority (fairness acceptable at 0.98+):** ```env BATCH_QUEUE_DRR_QUANTUM=25 BATCH_QUEUE_MAX_DEFICIT=100 BATCH_QUEUE_CONSUMER_COUNT=10 BATCH_QUEUE_CONSUMER_INTERVAL_MS=50 BATCH_CONCURRENCY_DEFAULT_CONCURRENCY=25 ``` **Strict fairness priority (throughput can be lower):** ```env BATCH_QUEUE_DRR_QUANTUM=5 BATCH_QUEUE_MAX_DEFICIT=25 BATCH_QUEUE_CONSUMER_COUNT=3 BATCH_QUEUE_CONSUMER_INTERVAL_MS=100 BATCH_CONCURRENCY_DEFAULT_CONCURRENCY=5 ```
106 lines
4.3 KiB
TypeScript
106 lines
4.3 KiB
TypeScript
import { Logger } from "@trigger.dev/core/logger";
|
|
import { Worker as RedisWorker } from "@trigger.dev/redis-worker";
|
|
import { z } from "zod";
|
|
import { env } from "~/env.server";
|
|
import { RunEngineBatchTriggerService } from "~/runEngine/services/batchTrigger.server";
|
|
import { logger } from "~/services/logger.server";
|
|
import { singleton } from "~/utils/singleton";
|
|
import { BatchTriggerV3Service } from "./services/batchTriggerV3.server";
|
|
// Import engine to ensure it's initialized (which initializes BatchQueue for v2 batches)
|
|
import { engine } from "./runEngine.server";
|
|
|
|
/**
|
|
* Legacy batch trigger worker for processing v3 and run engine v1 batches.
|
|
*
|
|
* NOTE: Run Engine v2 batches (batchVersion: "runengine:v2") use the new BatchQueue
|
|
* system with Deficit Round Robin scheduling, which is encapsulated within the RunEngine.
|
|
* See runEngine.server.ts for the configuration.
|
|
*
|
|
* This worker is kept for backwards compatibility with:
|
|
* - v3 batches (batchVersion: "v3") - handled by BatchTriggerV3Service
|
|
* - Run Engine v1 batches (batchVersion: "runengine:v1") - handled by RunEngineBatchTriggerService
|
|
*/
|
|
function initializeWorker() {
|
|
// Ensure the engine (and its BatchQueue) is initialized
|
|
void engine;
|
|
const redisOptions = {
|
|
keyPrefix: "batch-trigger:worker:",
|
|
host: env.BATCH_TRIGGER_WORKER_REDIS_HOST,
|
|
port: env.BATCH_TRIGGER_WORKER_REDIS_PORT,
|
|
username: env.BATCH_TRIGGER_WORKER_REDIS_USERNAME,
|
|
password: env.BATCH_TRIGGER_WORKER_REDIS_PASSWORD,
|
|
enableAutoPipelining: true,
|
|
...(env.BATCH_TRIGGER_WORKER_REDIS_TLS_DISABLED === "true" ? {} : { tls: {} }),
|
|
};
|
|
|
|
logger.debug(
|
|
`👨🏭 Initializing batch trigger worker at host ${env.BATCH_TRIGGER_WORKER_REDIS_HOST}`
|
|
);
|
|
|
|
const worker = new RedisWorker({
|
|
name: "batch-trigger-worker",
|
|
redisOptions,
|
|
catalog: {
|
|
"v3.processBatchTaskRun": {
|
|
schema: z.object({
|
|
batchId: z.string(),
|
|
processingId: z.string(),
|
|
range: z.object({ start: z.number().int(), count: z.number().int() }),
|
|
attemptCount: z.number().int(),
|
|
strategy: z.enum(["sequential", "parallel"]),
|
|
}),
|
|
visibilityTimeoutMs: env.BATCH_TRIGGER_PROCESS_JOB_VISIBILITY_TIMEOUT_MS,
|
|
retry: {
|
|
maxAttempts: 5,
|
|
},
|
|
},
|
|
"runengine.processBatchTaskRun": {
|
|
schema: z.object({
|
|
batchId: z.string(),
|
|
processingId: z.string(),
|
|
range: z.object({ start: z.number().int(), count: z.number().int() }),
|
|
attemptCount: z.number().int(),
|
|
strategy: z.enum(["sequential", "parallel"]),
|
|
parentRunId: z.string().optional(),
|
|
resumeParentOnCompletion: z.boolean().optional(),
|
|
}),
|
|
visibilityTimeoutMs: env.BATCH_TRIGGER_PROCESS_JOB_VISIBILITY_TIMEOUT_MS,
|
|
retry: {
|
|
maxAttempts: 5,
|
|
},
|
|
},
|
|
},
|
|
concurrency: {
|
|
workers: env.BATCH_TRIGGER_WORKER_CONCURRENCY_WORKERS,
|
|
tasksPerWorker: env.BATCH_TRIGGER_WORKER_CONCURRENCY_TASKS_PER_WORKER,
|
|
limit: env.BATCH_TRIGGER_WORKER_CONCURRENCY_LIMIT,
|
|
},
|
|
pollIntervalMs: env.BATCH_TRIGGER_WORKER_POLL_INTERVAL,
|
|
immediatePollIntervalMs: env.BATCH_TRIGGER_WORKER_IMMEDIATE_POLL_INTERVAL,
|
|
shutdownTimeoutMs: env.BATCH_TRIGGER_WORKER_SHUTDOWN_TIMEOUT_MS,
|
|
logger: new Logger("BatchTriggerWorker", env.BATCH_TRIGGER_WORKER_LOG_LEVEL),
|
|
jobs: {
|
|
"v3.processBatchTaskRun": async ({ payload }) => {
|
|
const service = new BatchTriggerV3Service(payload.strategy);
|
|
await service.processBatchTaskRun(payload);
|
|
},
|
|
"runengine.processBatchTaskRun": async ({ payload }) => {
|
|
const service = new RunEngineBatchTriggerService(payload.strategy);
|
|
await service.processBatchTaskRun(payload);
|
|
},
|
|
},
|
|
});
|
|
|
|
if (env.BATCH_TRIGGER_WORKER_ENABLED === "true") {
|
|
logger.debug(
|
|
`👨🏭 Starting batch trigger worker at host ${env.BATCH_TRIGGER_WORKER_REDIS_HOST}, pollInterval = ${env.BATCH_TRIGGER_WORKER_POLL_INTERVAL}, immediatePollInterval = ${env.BATCH_TRIGGER_WORKER_IMMEDIATE_POLL_INTERVAL}, workers = ${env.BATCH_TRIGGER_WORKER_CONCURRENCY_WORKERS}, tasksPerWorker = ${env.BATCH_TRIGGER_WORKER_CONCURRENCY_TASKS_PER_WORKER}, concurrencyLimit = ${env.BATCH_TRIGGER_WORKER_CONCURRENCY_LIMIT}`
|
|
);
|
|
|
|
worker.start();
|
|
}
|
|
|
|
return worker;
|
|
}
|
|
|
|
export const batchTriggerWorker = singleton("batchTriggerWorker", initializeWorker);
|