Design a fault-tolerant work queue whose tasks may be reserved for processing, completed, failed, retried, or moved to a dead-letter queue (DLQ).
Reserving a task temporarily assigns it for processing for a fixed lease duration. While the reservation is valid, the task is not available to be reserved again. If the task is not completed or explicitly failed before the lease expires, the reservation expires, and the task is retried or moved to the dead-letter queue according to its remaining attempts.
Every task begins in the ready state in the order given by taskIds. A completed or dead-lettered task never becomes ready again.
Implement the WorkQueue class:
-
WorkQueue(int[] taskIds, int leaseDuration, int maxAttempts)Initializes the queue.taskIdscontains the initial tasks in ready-queue order.leaseDurationis the amount of logical time for which each successful reservation remains valid.maxAttemptsis the maximum number of times a task may be reserved, including its first reservation.
-
int[] reserve(int now)Reserves the task at the front of the ready queue for processing, wherenowis the current logical time. Return[taskId, reservationToken], or an empty array if no task is ready.- Each successful reservation receives a new positive
reservationToken, beginning at1and increasing by1across the entire queue. - Reserving a task consumes one attempt.
- The reservation expires at time
now + leaseDurationand is valid only while the current logical time is strictly less than that expiration time.
- Each successful reservation receives a new positive
-
boolean complete(int taskId, int reservationToken, int now)Marks a task as completed. Returntrueonly ifreservationTokenidentifies the task's current reservation and that reservation is still valid at timenow. Otherwise, returnfalsewithout changing the task. -
boolean fail(int taskId, int reservationToken, int now)Explicitly fails the task's current reservation. Returntrueonly ifreservationTokenidentifies the task's current reservation and that reservation is still valid at timenow.- If the task has attempts remaining, append it to the back of the ready queue.
- Otherwise, move it to the dead-letter queue.
- Return
falsewithout changing the task if the reservation is invalid, stale, or expired.
-
int[] getDeadLetterTaskIds(int now)Returns the dead-lettered task IDs in the order they entered the dead-letter queue, without removing them.
Before performing its own operation, every method that receives now must first process all reservations expiring at or before now. If multiple reservations expire, process them by increasing expiration time, breaking ties by increasing reservation token.
An expired reservation follows the same retry or dead-letter rule as an explicitly failed reservation. There is no background timeout worker, so expirations are processed only when one of these methods is called. Once a reservation expires, its token becomes stale and can no longer be used to complete or fail the task.
Constraints:
- 1 ≤
taskIds.length≤ 104. taskIdscontains distinct integers from 0 through 109.- 1 ≤
leaseDuration≤ 106. - 1 ≤
maxAttempts≤ 100. - 0 ≤
now≤ 108, andnowvalues are nondecreasing across method calls. - At most 105 method calls are made on one queue instance.
Example:
Input: ["WorkQueue", "reserve", "reserve", "complete", "reserve", "complete", "fail", "getDeadLetterTaskIds", "reserve"] [[[10, 20], 3, 2], [0], [0], [20, 2, 1], [3], [10, 1, 3], [10, 3, 4], [4], [4]]
Output: [null, [10, 1], [20, 2], true, [10, 3], false, true, [10], []]
Explanation:
WorkQueue queue = new WorkQueue(new int[]{10, 20}, 3, 2);// Initializes two ready tasks with a lease duration of 3 and at most 2 attempts.queue.reserve(0);// Returns [10, 1]. Task 10 receives the first reservation.queue.reserve(0);// Returns [20, 2]. Task 20 receives the second reservation.queue.complete(20, 2, 1);// Returns true. Task 20 completes before its lease expires.queue.reserve(3);// Returns [10, 3]. Task 10's first reservation expires at time 3, so it becomes ready for its second attempt.queue.complete(10, 1, 3);// Returns false, because token 1 is stale.queue.fail(10, 3, 4);// Returns true. This exhausts Task 10's attempts and moves it to the dead-letter queue.queue.getDeadLetterTaskIds(4);// Returns [10].queue.reserve(4);// Returns [], because no task is ready.
