Scheduler Components:
a. Task Queue: We need multiple queues for different priorities? Or a priority queue? But note: we want fairness among users, not just priorities.
We might use a multi-level feedback queue or a weighted fair queuing per user?
However, the requirement says "fairness" and "priorities". So we might want to combine both.
Idea:
- Group tasks by user. Within each user, tasks have their own priority (or FIFO?).
- Then we schedule between users with a fair share (maybe proportional to their priority? or just round-robin?).
- But then we also have the global priority of tasks? Let's define:
We can have:
- Each user has a weight (or priority) that determines their share of the cluster.
- Within a user's tasks, we schedule by task priority (or FIFO if same priority).
Alternatively, we can use a single priority queue but then high priority tasks from a user with a low weight might starve?
So we need to balance.
Another idea: Use a weighted fair queuing (WFQ) for users, and within each user, use priority scheduling.
Let's define:
- UserWeight[user] (could be static or dynamic based on SLA?).
- TaskPriority[task] (could be set by user, but we might have a maximum per user?).
How to combine? We can assign a virtual finish time for each task based on:
- The user's current virtual time (which advances as the user gets resources).
- The task's priority: we can adjust the weight within the user? For example, within the same user, higher priority tasks get a higher weight.
But note: the requirement says "fairness" and "priorities". So we want to prevent a user from flooding the system with high priority tasks.
Alternatively, we can have two levels:
Level 1: Fair share among users (using WFQ with user weights).
Level 2: Within a user, schedule by task priority (with preemption allowed).
But then if a user has a high weight, they get more resources, but within that share, we respect task priorities.
However, the requirement also says "preemption" across users? Or only within a user?
Let's assume preemption is allowed across users if the task has a higher global priority? But then we break fairness.
We need to define global priority:
GlobalPriority = f(UserWeight, TaskPriority)
How about:
GlobalPriority = (UserWeight * some_factor) + TaskPriority?
But then if UserWeight is 0 for a user, then even if they have high task priority, they get nothing? That might be acceptable.
Alternatively, we can have a two-tier system:
- First, we allocate resources to users according to their weight (using WFQ).
- Then, within each user's allocated share, we schedule tasks by their priority.
But then if a user with a low weight has a very high priority task, it might not get resources because the user's share is low.
The requirement says "fairness", so we must ensure that every user gets at least some minimum share?
We can have a minimum guarantee per user (maybe based on their weight) and then allow borrowing? That's getting complex.
Let's simplify:
We'll use a global priority queue that is ordered by:
priority = (UserWeight * MAX_TASK_PRIORITY) + (MAX_TASK_PRIORITY - TaskPriority)
So that higher user weight and higher task priority (lower number if priority 1 is highest) get a higher combined score.
But then a user with high weight and medium task priority might beat a user with low weight and high task priority.
Alternatively, we can use a weighted round-robin for users and then within the user's turn, schedule the highest priority task.
However, that might not be preemptive? We want preemption: if a high priority task arrives, it can preempt a lower priority task (even from another user) if it has a higher global priority?
Let's define global priority as a tuple: (UserWeight, TaskPriority) and compare lexicographically? Then user weight is the primary, task priority secondary.
But then if a user has weight 10 and task priority 5, and another user has weight 5 and task priority 1, then the first user's task will always be scheduled first?
That might be acceptable if we want to prioritize users with higher weight.
However, the requirement says "fairness", so we might want to avoid starvation of low weight users.
We can use aging: over time, we increase the priority of waiting tasks? Or we can use a deficit round-robin for users?
Given the complexity, let's assume we have a fixed set of user weights and task priorities, and we want to schedule by:
Sort tasks by:
primary: user weight (descending)
secondary: task priority (ascending, if 1 is highest)
But then if we have two users with same weight, we schedule by task priority.
However, this does not prevent starvation of a user with low weight if there are always tasks from high weight users.
We might need to implement a fair queuing mechanism for users, but then also allow high priority tasks to jump the queue?
Alternatively, we can use a weighted fair queuing for users, and within each user's queue, we use priority.
Steps:
We maintain:
- For each user, a priority queue of tasks (by task priority).
- A global WFQ scheduler that picks a user based on their weight and the amount of resources they have used recently.
How WFQ works: each user has a virtual finish time. We schedule the user with the smallest virtual finish time.
The virtual finish time is updated when the user is scheduled:
VF_new = max(VF_current, current_time) + (task_resource_requirements / user_weight)
But note: we are scheduling tasks, not users. So we schedule a task from the user with the smallest VF.
Then, when we schedule a task from user U, we update user U's VF by the resource usage of that task divided by user_weight[U].
This ensures that over time, each user gets a share proportional to their weight.
Within the user, we pick the highest priority task (lowest priority number) from that user's queue.
Preemption:
- We might be running a task from user A (with weight wA) and a new task arrives from user B (with weight wB) and with a higher priority (within user B) than the current task of user A?
But wait: the current task is from user A, and we are using WFQ. The new task from user B will increase user B's VF? Actually, the new task is added to user B's queue.
The WFQ scheduler will then compare VF of user A and user B. If user B's VF becomes less than user A's VF (because user A has been running for a while and user B just added a task?), then we might preempt?
Actually, WFQ does not preempt tasks. Once a task starts, it runs until completion (or until its lease expires?).
But we want preemption. So we need to allow preemption if a task from a user with a higher global priority (or higher WFQ priority?) arrives.
How to incorporate preemption in WFQ?
Alternatively, we can use a preemptive version: we can interrupt the current task if the next task in the global order (by some metric) has a higher priority.
Let's define a global priority for tasks:
We can compute for each task a virtual finish time based on:
VF_task = max(VF_user, current_time) + (task_resource_requirements / (user_weight * task_priority_weight?))
But then task_priority_weight? We want higher task priority to have a lower VF?
Alternatively, we can adjust the user's weight by the task priority?
This is getting messy.
Let's step back.
Requirement: fairness (among users) and priorities (within and across users?) and preemption.
We can do:
- Each user has a weight (for fair share).
- Each task has a priority (1=high, 10=low, for example).
- We want to schedule tasks in an order that respects both.
We can define a combined priority:
CombinedPriority = UserWeight * (MAX_PRIORITY - TaskPriority) [so that higher user weight and higher task priority (lower number) give higher combined]
Then we schedule by CombinedPriority descending.
But then a user with high weight and low priority task might beat a user with low weight and high priority task.
We can also set a minimum combined priority for each user? Or we can use a cap on the user's weight when combined?
Alternatively, we can use:
CombinedPriority = (UserWeight * MAX_PRIORITY) + (MAX_PRIORITY - TaskPriority)
Then the range of user weight is multiplied by MAX_PRIORITY, so user weight is the primary factor.
But then if we have two users with weights 1 and 2, and tasks with priorities 1 and 10:
User1, task priority 1: (1*10 + (10-1)) = 10+9=19
User2, task priority 10: (2*10 + (10-10)) = 20+0=20 -> higher, so User2's task is scheduled even though it's low priority?
That doesn't seem right.
How about:
CombinedPriority = UserWeight * (some_large_number) + (MAX_PRIORITY - TaskPriority)
Then the user weight dominates, but within the same user weight, task priority matters.
But then a user with weight 1 and task priority 1 (19) vs user with weight 2 and task priority 10 (2000+0) -> the second is much higher.
So if we set the large number to, say, 1000, then:
User1, task priority 1: 1000 + 9 = 1009
User2, task priority 10: 2000 + 0 = 2000
Then user2's task is scheduled even though it's the lowest priority? That might be acceptable if we want to weight users more.
But the requirement says "fairness" and "priorities". We might interpret that as:
- Fairness among users: each user gets a minimum share (based on weight).
- Within a user, we respect task priorities.
So maybe we don't combine them in a single number. Instead, we do:
Step 1: Select a user based on fair sharing (WFQ).
Step 2: From that user, pick the highest priority task (lowest number).
Then preemption:
- If a new task arrives from a user that is not the current user, we don't preempt because we are fair sharing.
- But if a new task arrives from the current user with higher priority than the currently running task (from the same user), then we can preempt the current task?
But wait: the current task is from user A, and we are running it because user A was selected by WFQ. Now a new task from user A arrives with higher priority.
We can preempt the current task (from user A) and run the new higher priority task (from user A). That is within the same user.
What about preemption across users?
- If we are running a task from user A (weight 1) and a task from user B (weight 2) arrives and user B is next in WFQ, we might want to preempt because user B has higher weight?
But then we break the fair sharing because user A hasn't used its share yet.
So maybe preemption is only allowed within the same user? Or we allow preemption if the new task's user has a higher weight and the current task's user has a lower weight?
But then we are not fair sharing.
Alternatively, we can allow preemption if the new task's combined priority (using the formula above) is higher than the current task's combined priority.
But then we are back to combining.
Given the complexity, let's assume:
- We have user weights and task priorities.
- We want to schedule in a way that:
a. Over a long period, each user gets a share proportional to their weight.
b. Within a user, higher priority tasks get scheduled before lower priority ones.
c. Preemption is allowed: if a task with a higher combined priority (defined as above) arrives, it can preempt the current task.
But note: if we allow preemption based on combined priority, then we might break the fair sharing because a burst of high priority tasks from a high weight user can preempt tasks from a low weight user even if the low weight user hasn't used its share.
So maybe we don't allow preemption across users? Only within the same user.
Let's decide:
- Preemption only within the same user: if a new task from user U arrives and has a higher priority than the currently running task from user U, then preempt.
But then what if the currently running task is from user U and we are about to finish it, and a task from user V (with higher weight) arrives? We don't preempt because it's a different user.
That seems to respect fair sharing.
However, the requirement says "preemption" without specifying scope. So we have to decide.
Given the lease mechanism: tasks have leases. We might not want to preempt a task that has a long lease? Or we might, if the lease is about to expire?
Let's define:
- Each task has a lease duration. When a task is scheduled, it gets a lease for that duration (or until it finishes).
- If the lease expires, the task must be rescheduled (or killed) and can be preempted by another task.
So preemption can happen when:
a. A higher priority task (from the same user) arrives and the current task is from the same user.
b. The lease of the current task expires and we decide not to renew.
But note: if the lease expires, we are going to preempt anyway (because the task doesn't have the right to run anymore). So that's not preemption by priority, but by lease.
So we have two reasons to preempt:
1. Lease expiration (the task is no longer allowed to run).
2. A higher priority task (from the same user) arrives.
But what about across users? We said we don't preempt across users for priority, but what if the lease of a task from user A expires and there's a task from user B waiting?
Then we can schedule user B's task because user A's task is gone. But that's not preemption, that's just scheduling after the lease expires.
So the preemption we are talking about is: while a task is running (and its lease hasn't expired), we might stop it because a higher priority task (from the same user) arrived.
Now, backpressure:
- If the system is overloaded (nodes are saturated), we might want to reject new tasks or slow down the submission rate.
- We can have a global load metric (e.g., average node utilization) and if it's above a threshold, we start rejecting tasks or ask submitters to slow down.
But note: we are distributed. So each node might have its own load, and the scheduler might be centralized or distributed.
Let's assume a centralized scheduler for simplicity, but we can extend to distributed.
Centralized Scheduler:
- Maintains a global view of nodes and tasks.
- Receives task submission requests.
- Makes scheduling decisions and dispatches tasks to nodes.
Steps for the scheduler:
1. When a task is submitted:
- Check backpressure: if the system is overloaded, reject or queue the task?
But if we queue, we might make the overload worse. So maybe we reject with a "backpressure" signal.
2. If accepted, add the task to the user's priority queue.
3. Run the scheduling loop (or trigger on events):
- For each node, check if there are running tasks that have lease expired -> preempt (stop) those tasks.
- Then, for each node with free resources, try to schedule a task.
How to schedule a task on a node?
- We have a set of nodes with available resources.
- We have a set of users, each with a weight and a priority queue of tasks.
We want to pick a user and then a task from that user.
Using WFQ for users:
We maintain for each user:
- virtual_finish_time (VFT)
- last_update_time (when we last updated VFT)
When we want to schedule a task (i.e., we have a node with available resources), we:
a. For each user, update their VFT:
VFT = max(VFT, current_time) [if we are using a virtual time that advances with real time?]
Actually, in WFQ, we update VFT when we schedule a task from that user.
b. Then, we pick the user with the smallest VFT.
c. From that user, pick the highest priority task (lowest task priority number) that fits the node's available resources.
d. If no task from that user fits, we try the next user? Or we mark that user as not having a fitting task and skip until next scheduling round?
e. Schedule the task on the node, and update the user's VFT:
VFT = max(VFT, current_time) + (task.resource_requirements / user_weight)
f. Set a lease for the task (either from the task's requested lease or a default).
4. Preemption within a user:
We are running a task from user U on a node.
A new task arrives for user U with higher priority (lower number) than the currently running task?
But note: the currently running task might have been scheduled a while ago and its priority might be lower than the new one.
We can preempt the current task if:
- The new task's priority is higher (lower number) than the current task's priority.
- And the current task's lease hasn't expired? But we are preempting before lease expiry.
How to decide? We can interrupt the current task and run the new one.
But note: the current task might have already run for some time. We might want to consider the remaining work?
We don't have that information. So we preempt based on priority.
Steps for preemption:
- When a new task arrives for user U, check if there is a running task from user U that has lower priority (higher number) than the new task.
- If yes, and the node where the lower priority task is running has enough resources for the new task (or we can preempt and then schedule the new task on the same node?), then we preempt the lower priority task.
But note: the new task might require more resources than the current task? Then we might need to preempt multiple tasks? Or we don't preempt if the node doesn't have enough resources for the new task?
Alternatively, we can preempt the current task and then try to schedule the new task on the same node. If the node doesn't have enough resources (because the current task was using some and the new task requires more), then we cannot schedule the new task?
Actually, when we preempt, we free the resources of the current task. Then we have more resources on that node. So the new task might fit.
But what if the new task requires more resources than the current task was using? Then after preempting, we free the current task's resources, so we have more. So it might fit.
So algorithm for preemption:
For a new task T from user U:
- Find all running tasks from user U that have lower priority (higher priority number) than T.
- Among these, find the one with the lowest priority (highest number) that, if preempted, would free enough resources to run T on the same node?
- But note: we might need to preempt multiple tasks to free enough resources?
This is complex.
Alternatively, we can preempt the lowest priority task from user U that is running on a node that has enough resources (after preempting that task) to run T.
But then we might preempt a task that is about to finish? We don't know.
Given the complexity, let's assume:
- We only preempt if there is exactly one running task from user U on a node and that task has lower priority than T, and the node has enough resources (after freeing that task) to run T.
But what if T requires more resources than that task? Then we need to preempt multiple tasks?
Maybe we don't allow preemption across nodes? We only preempt on the node where the new task can be scheduled?
Actually, we are preempting to free resources on a node so that we can run the new task on that same node? Or we preempt and then run the new task on any node?
Since we are preempting a task, we free resources on that node. Then we can schedule the new task on that node if it fits.
So:
For a new task T from user U:
- For each node that has a running task from user U with lower priority than T:
Let the running task be R.
If node.resources_available + R.resource_requirements >= T.resource_requirements:
Then preempt R and schedule T on that node.
- If no such node, then we don't preempt. We add T to the user's queue and wait for the next scheduling round.
But note: we might have multiple running tasks from user U on different nodes. We might preempt one that is on a node with enough free resources after preemption.
However, what if we preempt a task that is using a lot of resources and then T requires even more? Then we still cannot schedule T on that node.
So we need to check: after preempting R, the node must have at least T.resource_requirements available.
Alternatively, we can preempt multiple tasks from user U on the same node until we have enough resources?
This is getting too complex.
Let's simplify:
We allow preemption only if the new task T can be scheduled on the same node as the preempted task (after freeing the preempted task's resources) and the preempted task is the only running task from user U on that node?
Why only the only one? Because if there are multiple, we might preempt one and then still have other tasks from user U on that node, which might be of higher priority than T?
Actually, we are preempting a task with lower priority than T. If there are multiple tasks from user U on the same node, we might preempt the one with the lowest priority (highest number) that, when preempted, allows T to fit.
But then we are preempting a task from user U to run a higher priority task from the same user on the same node. That seems reasonable.
Algorithm for preemption:
For a new task T from user U:
For each node N that has at least one running task from user U:
Let running_tasks = tasks from user U on N.
Sort running_tasks by priority (highest number first, i.e., lowest priority).
For each task R in running_tasks (from lowest priority to highest):
If R.priority > T.priority (so R has lower priority than T) and
(N.available_resources + R.resource_requirements >= T.resource_requirements):
Preempt R.
Schedule T on N.
Break.
If we scheduled T, break.
If we didn't schedule T by preemption, then add T to user U's queue.
But note: we might preempt a task that is not the lowest priority? We are going in order of lowest priority first, so we preempt the lowest priority task that allows T to fit.
However, what if preempting a higher priority task (but still lower than T) would free more resources and allow T to fit, but we preempt a lower priority task that frees just enough?
We are preempting the first one that fits. That might be acceptable.
But what if we preempt a task that is about to finish? We don't know. We are being greedy.
Alternatively, we can preempt the task that frees the most resources? But then we might preempt a task that is almost done?
Given the lease mechanism, tasks have a lease. We might not want to preempt a task that has a long lease? Or we might, because the lease is about resource usage over time.
Let's assume we don't consider lease remaining time for preemption. We only consider priority.
But note: if a task has a lease that is about to expire, we might not want to preempt it because it will be killed soon anyway?
Actually, if the lease is about to expire, then we are going to kill it (or not renew) and then we can schedule T. So maybe we wait for lease expiry?
But then if the lease is long, we might wait a long time.
So we preempt based on priority, regardless of lease remaining.
Now, backpressure:
We maintain a global load metric, for example:
load = (sum of resources used by running tasks) / (total cluster resources)
If load > threshold (say 0.9), then we are in backpressure.
When in backpressure, we can:
- Reject new task submissions (return an error or ask to retry later).
- Or, we can still accept but put them in a pending queue and not schedule until load goes down.
But if we accept and queue, we might make the backpressure worse because we have more tasks waiting and eventually when load goes down we schedule them all at once?
So maybe we reject when in backpressure.
However, we might want to allow high priority tasks even in backpressure?
We can have:
- If load > high_threshold (e.g., 0.95), reject all.
- If load between medium and high, only accept tasks with priority above a certain level (e.g., priority 1 or 2).
But then we are mixing priorities with backpressure.
Alternatively, we can always accept tasks (because we have a queue) but when scheduling, we only schedule if the node has resources. And if the system is overloaded, tasks wait longer.
But then the queue might grow indefinitely.
So we need a way to limit the queue size per user or globally.
Given the complexity, let's assume:
- We have a global maximum queue length (for all pending tasks). If the queue is full, reject new tasks.
And we also have a backpressure mechanism based on load:
- If load > 0.9, we set a flag that we are in backpressure and we start rejecting tasks that are below a certain priority threshold?
- Or we reject all?
Let's do:
- If load > 0.9, we reject tasks with priority lower than 5 (if priority 1 is highest, then 5 is lower).
- If load > 0.95, we reject all.
But note: we are using user weights and task priorities. The priority threshold might be absolute? Or relative to the user?
We can have an absolute priority threshold for backpressure.
Summary of the design:
Centralized scheduler with:
Data structures:
- users: map from user_id to
{ weight: float,
queue: priority queue of tasks (by task priority, ascending)
vft: float (virtual finish time for WFQ)
}
- nodes: map from node_id to
{ resources: {cpu, memory, ...},
running_tasks: map from task_id to task (with lease expiry)
}
- tasks: map from task_id to
{ user_id, priority, resource_requirements, lease_duration, ... }
Scheduler loop (or event-driven):
Event types:
- Task submitted
- Task completed (or preempted)
- Lease expired (for a running task)
- Node resource change (maybe)
Steps:
On task submitted(T):
- Check backpressure (global load and queue size). If backpressure, reject.
- Else, add T to user U's queue (by priority).
- Trigger scheduling.
On scheduling (triggered by above events or periodically):
- For each node, check for lease expiry on running tasks. If expired, preempt (stop) the task and free resources. (Note: we might also have a background task that checks leases.)
- Then, while there are nodes with available resources and there are tasks in the user queues:
a. Select a user by WFQ:
For each user, update their vft if needed? Actually, we update vft only when we schedule a task from that user.
We pick the user with the smallest vft.
b. From that user's queue, pick the highest priority task (lowest priority number) that fits the node's available resources.
c. If found, schedule it on the node (assign node, set lease timer, update user's vft:
vft = max(vft, current_time) + (task.resource_requirements / user_weight)
d. If no task from that user fits, then we mark that user as not having a fitting task?
We can skip this user for now? But then we might starve other users?
Actually, in WFQ, if a user doesn't have a task that fits, we skip and go to the next user?
But then the user's vft doesn't update, so they might be scheduled again soon?
However, if a user has only large tasks that don't fit on any node, then they will never be scheduled?
So we need to consider that.
We can do:
For each node, we try to schedule a task from the user with the smallest vft that fits.
But then we are iterating over users for each node? That might be inefficient.
Alternatively, we can have a global list of users ordered by vft, and for each user, we try to schedule their highest priority task on any node that has enough resources.
But then we might schedule a user's task on a node that is not the best fit?
Given the complexity, let's assume we have a list of nodes with available resources, and we try to schedule one task per node in each scheduling round.
So:
For each node N with available resources:
- Find the user U with the smallest vft that has a task that fits N.
- Then pick the highest priority task from U that fits N.
But note: the same user might be selected for multiple nodes? That's okay.
However, updating vft: we update vft for user U for each task we schedule from them?
In WFQ, we update vft when we schedule a task from that user. So if we schedule two tasks from user U in the same scheduling round (for two nodes), we update vft twice?
That would be correct because we are serving two tasks from user U.
But note: the vft update formula uses the resource_requirements of the task. So if we schedule two tasks, we update twice.
However, we are in the same scheduling round (same current_time). So:
vft = max(old_vft, current_time) + (task1.resources / weight)
then for task2:
vft = max(updated_vft, current_time) + (task2.resources / weight)
That seems correct.
But note: if we are scheduling multiple tasks from the same user in the same round, we are increasing the vft by the sum of (resources/weight) for each task.
That is correct because the user's share is being used.
e. If we cannot find any user with a task that fits the node, then we leave the node idle.
On task preemption (by lease expiry or by higher priority task from same user):
- Stop the task on the node, free resources.
- If the task was preempted by lease expiry, we can remove it (or resubmit? depending on the lease policy).
- If preempted by a higher priority task from the same user, then we are going to schedule the higher priority task immediately?
Actually, in the preemption step above (when a new task arrives) we already schedule the higher priority task. So we don't need to do anything else.
On task completion:
- Free resources on the node.
- Trigger scheduling.
Preemption by higher priority task (same user) when a new task arrives:
When we add a new task T to user U's queue, we check:
For each node N that has a running task from user U with lower priority than T:
Let R be the running task on N from user U with the lowest priority (highest number) such that:
R.priority > T.priority and
(N.available_resources + R.resource_requirements >= T.resource_requirements)
If such R exists, then preempt R and schedule T on N.
But note: there might be multiple running tasks from user U on N. We want to preempt the one with the lowest priority (highest number) that, when preempted, allows T to fit.
We can do:
On node N, get all running tasks from user U.
Sort them by priority (descending, so lowest priority first).
For each R in that sorted list:
if R.priority > T.priority and
(N.available_resources + R.resource_requirements >= T.resource_requirements):
preempt R and schedule T on N.
break.
But note: what if preempting R doesn't free enough resources because T requires more than what R was using? Then we need to preempt more than one task?
We can do:
Let required = T.resource_requirements - N.available_resources
Find a set of tasks from user U on N with total resources >= required and with priorities lower than T, and then preempt the set that has the minimum total resources?
But that is a knapsack problem.
Given the complexity, we can limit preemption to one task per node?
Alternatively, we can preempt the lowest priority task from user U on N, and then check again if T fits?
If not, we preempt the next lowest priority task, until T fits or we run out of tasks from user U on N.
So:
tasks_to_preempt = []
current_available = N.available_resources
For each R in running_tasks_from_U_on_N (sorted by priority descending, i.e., lowest priority first):
if R.priority > T.priority:
tasks_to_preempt.append(R)
current_available += R.resource_requirements
if current_available >= T.resource_requirements:
break
If we have enough, then preempt all tasks in tasks_to_preempt and schedule T.
But note: we are preempting multiple tasks. That might be too disruptive.
Let's stick to preempting at most one task per node for now.
So we only preempt if there exists one task R such that after preempting R, T fits.
If not, we don't preempt.
This design is already complex. We might need to adjust.
However, note that the requirement says "distributed scheduler". We have assumed a centralized one.
For distributed, we might have multiple scheduler instances that coordinate? Or we can use a shared state (like a database) and have each scheduler make decisions based on that.
Given the time, we'll stick to centralized.
Let's write the algorithm for scheduling (the main loop) more formally:
while true:
for each node N:
if N has available resources:
// First, check for preemption due to new tasks?
// Actually, we handle preemption when a new task arrives, so we don't need to do it in the main loop?
// But what if a new task arrives and we don't check for preemption in the main loop?
// We already trigger scheduling when a new task arrives, and in that trigger we do the preemption check.
// So in the main loop, we only try to schedule new tasks from queues.
// Select user by WFQ:
user = null
min_vft = infinity
for each user U with non-empty queue:
// We don't update vft here because we haven't scheduled yet.
if U.vft < min_vft:
min_vft = U.vft
user = U
if user is not null:
// From user's queue, pick the highest priority task that fits N.
task = null
for each task T in user.queue (ordered by priority ascending):
if T.resource_requirements <= N.available_resources:
task = T
break
if task is not null:
schedule task on N
remove task from user.queue
update user.vft = max(user.vft, current_time) + (task.resource_requirements / user.weight)
set lease timer for task (task.lease_duration)
// Also, check for lease expirations?
// We can have a separate thread or timer for lease expirations.
But note: we are iterating over nodes and then over users. This might be inefficient.
Alternatively, we can have a priority queue of users by vft, and for each user, we try to schedule their highest priority task on any node that fits.
But then we might schedule a task on a node that is not the best fit?
Given the distributed nature, we might want to bin-pack? But that's another problem.
Given the time, we'll leave it as above.
Now, backpressure:
We maintain:
total_resources = sum of all nodes' resources.
used_resources = sum of running tasks' resource_requirements.
load = used_resources / total_resources
When load > 0.9, we set backpressure_flag = true.
When a task is submitted:
if backpressure_flag:
if load > 0.95:
reject task
else:
if task.priority > 5: // assuming 1 is highest, 10 is lowest, so 5 is medium?
reject task
else:
accept task
Also, we can have a queue length limit:
if global_pending_queue_length > MAX_QUEUE_LENGTH:
reject task
But note: we don't have a global pending queue? We have per-user queues. So we can compute:
total_pending = sum of lengths of all user queues.
If total_pending > MAX_QUEUE_LENGTH, then reject.
However, if we reject tasks, we might lose work. But in backpressure, we have to.
Alternatively, we can have a separate high-priority queue for high priority tasks even in backpressure?
But we already have per-user queues and we use WFQ. So if a user has high weight, they get more chance.
We might adjust the backpressure by user weight?
But let's keep it simple.
This is a basic design. There are many details to consider in a real system.
Let's write the key components in code-like pseudocode.
We'll define:
class Task:
id: string
user_id: string
priority: int // lower is higher priority
resources: ResourceRequirements // e.g., {cpu: 1, memory: 1024}
lease_duration: int // in seconds
class Node:
id: string
resources: ResourceRequirements
available: ResourceRequirements // initially same as resources
running_tasks: map[task_id] -> Task // and we might store the lease expiry time?
class User:
id: string
weight: float
queue: PriorityQueue of Task (by priority ascending)
vft: float
class Scheduler:
users: map[user_id] -> User
nodes: map[node_id] -> Node
tasks: map[task_id] -> Task // for all tasks (running and queued)
backpressure_threshold_high: float = 0.95
backpressure_threshold_medium: float = 0.9
max_pending_tasks: int = 10000
current_time: we can use a system clock or a virtual time? We'll use real time.
Methods:
submit_task(task):
if self.is_in_backpressure():
if self.load > self.backpressure_threshold_high:
reject(task)
else:
if task.priority > 5: // adjust as needed
reject(task)
else:
self.accept_task(task)
else:
self.accept_task(task)
accept_task(task):
user = self.get_or_create_user(task.user_id)
user.queue.push(task)
self.tasks[task.id] = task
self.trigger_scheduling()
trigger_scheduling():
// We can run this in a loop or call it on events.
// For simplicity, we run it in a loop.
schedule():
// Check lease expirations? We might have a separate timer for that.
// First, update load:
self.update_load()
// Then, for each node with available resources, try to schedule.
for node in self.nodes.values():
if node.available is not empty (at least one resource >0):
user = self.select_user_by_wfq()
if user is None:
continue
task = self.get_task_from_user_that_fits(user, node.available)
if task:
self.assign_task_to_node(task, node)
select_user_by_wfq():
// Return the user with the smallest vft that has at least one task that fits on any node?
// But we don't know which node? We are calling this per node?
// Actually, in the above schedule() we are iterating by node. So we need to select a user for this node.
// We want to select the user with the smallest vft that has a task that fits on this node?
// But note: the same user might be selected for multiple nodes.
// So for this node, we look at all users and pick the one with the smallest vft that has a task fitting this node.
candidate_user = None
min_vft = infinity
for user in self.users.values():
if user.queue is empty:
continue
// Check if user has any task that fits this node? We don't want to check all tasks?
// We can check the highest priority task of the user and see if it fits.
// But the highest priority task might not fit, but a lower priority one might?
// So we need to check the user's queue for any task that fits.
// We can do:
for task in user.queue: // but the queue is by priority, so we break when we find one that fits?
if task.resources <= node.available:
if user.vft < min_vft:
min_vft = user.vft
candidate_user = user
break // we only need one task from the user that fits
return candidate_user
get_task_from_user_that_fits(user, available_resources):
// The user's queue is a priority queue by task priority.
// We need to find the highest priority task (lowest priority number) that fits.
// We cannot just take the first because the first might not fit?
// But we are using a priority queue, so the first is the highest priority. If it doesn't fit, then we have to check the next?
// But then we break the priority order?
// We want to schedule the highest priority task that fits. So we might have to look through the queue until we find one that fits.
// However, if the highest priority task doesn't fit, then we might schedule a lower priority one?
// That breaks the user's internal priority order?
// But note: we are already respecting the user's queue by WFQ. So we are giving the user a chance to run a task.
// If the highest priority task doesn't fit, we might run a lower priority one? That might be acceptable.
// Alternatively, we can skip the user for this node and try the next user?
// Given the complexity, let's assume we take the first task in the user's queue (highest priority) and if it fits, we schedule it.
// If not, we skip the user for this node.
// But then if the user has a large task that doesn't fit, and a smaller task with lower priority that does fit, we would skip?
// We can do:
for task in user.queue: // ordered by priority ascending
if task.resources <= available_resources:
return task
return null
assign_task_to_node(task, node):
// Remove task from user's queue.
user = self.users[task.user_id]
user.queue.remove(task) // note: we need to remove by task id? The priority queue might not support removal by value easily.
// Update node:
node.available = node.available - task.resources
node.running_tasks[task.id] = task
// Update user's vft:
current_time = self.current_time()
user.vft = max(user.vft, current_time) + (task.resources.cpu / user.weight) // we are using only CPU for simplicity?
// Actually, we should use a vector of resources? But for simplicity, we can use a single scalar (like normalized resources).
// Set lease timer:
self.set_lease_timer(task.id, task.lease_duration)
on_lease_expired(task_id):
task = self.tasks[task_id]
node = self.find_node_running_task(task_id)
if node:
self.preempt_task(task, node, reason='lease_expired')
// Else, task might have already completed?
preempt_task(task, node, reason):
// Remove from node:
del node.running_tasks[task.id]
node.available = node.available + task.resources
// If reason is 'lease_expired', we might remove the task entirely? Or we can resubmit?
// Let's assume we remove the task (it's dead).
del self.tasks[task.id]
// If reason is 'higher_priority', then we are going to schedule a new task?
// Actually, when we preempt for a higher priority task, we already scheduled the new task.
// So we just free resources.
// Trigger scheduling because we have free resources now.
self.trigger_scheduling()
on_new_task_arrival(task):
// This is called when a new task is submitted and accepted.
// First, try to preempt lower priority tasks from the same user.
user = self.users[task.user_id]
for node in self.nodes.values():
// Get running tasks from user on this node.
running_tasks = [t for t in node.running_tasks.values() if t.user_id == user.id]
// Sort by priority descending (so lowest priority first)
running_tasks.sort(key=lambda t: t.priority, reverse=True)
available_after_preempt = node.available
tasks_to_preempt = []
for r in running_tasks:
if r.priority > task.priority: // r has lower priority than task
tasks_to_preempt.append(r)
available_after_preempt += r.resources
if available_after_preempt >= task.resources:
break
if available_after_preempt >= task.resources:
// Preempt all tasks in tasks_to_preempt
for r in tasks_to_preempt:
self.preempt_task(r, node, reason='higher_priority')
// Now node has enough resources, schedule the new task.
self.assign_task_to_node(task, node)
return
// If we didn't preempt, then add to user's queue.
user.queue.push(task)
self.tasks[task.id] = task
self.trigger_scheduling()
However, note: in on_new_task_arrival, we are iterating over all nodes and checking running tasks from the user.
We might preempt on multiple nodes? But we break after we preempt on one node?
We preempt on the first node that we can free enough resources?
But note: we might preempt on a node that is already running tasks from the same user, and then schedule the new task on that node.
This is acceptable.
This is a very basic design. There are many issues:
- The WFQ implementation is per scheduling decision per node. But WFQ is usually for a set of flows sharing a link. Here we have multiple nodes and multiple users.
- We are updating vft for a user every time we schedule a task from that user, but we are doing it per node. That might be okay because we are using the same current_time for all.
- We are not considering multiple resource types (CPU, memory) in the vft update? We used only CPU. We should use a normalized resource vector.
Given the complexity, we might need to use a scalar resource (like normalized to 1.0 per node).
Alternatively, we can use the dominant resource (like in dominant resource fairness) for the vft update?
But let's keep it simple and assume a single resource (CPU) for now.
Also, note: the WFQ algorithm we are using is:
vft = max(vft, current_time) + (task_cpu / user_weight)
But what if the node has multiple resources? We can use the dominant resource for the task and the node?
This design is a starting point.
We also haven't considered:
- Task migration? (moving tasks between nodes) - not in the requirements.
- Fault tolerance: if a node fails, we need to reschedule its tasks.
- Distributed state: if the scheduler is centralized, it's a single point of failure. We might need multiple schedulers with a consensus protocol.
But the requirement is to design, not implement. So we can stop here.
Let's summarize the design:
We have a centralized scheduler that:
- Maintains per-user priority queues and WFQ state (vft).
- Schedules by:
For each node with available resources, pick the user with the smallest vft that has a task fitting the node, then pick the highest priority task from that user.
- Preemption:
When a new task arrives for a user, we check if there is a node running a lower priority task from the same user that can be preempted to free enough resources for the new task.
If so, preempt that task (and run the new task on the same node).
- Leases:
Each task has a lease. When the lease expires, the task is preempted (and killed).
- Backpressure:
We monitor the cluster load (used resources / total resources).
If load > 0.9, we start rejecting tasks with priority > 5 (assuming 1 is highest, 10 is lowest).
If load > 0.95, we reject all tasks.
Also, if the total pending tasks (across all users) exceeds a threshold, we reject.
This design tries to balance fairness (via WFQ for users) and priorities (within user and for backpressure) and includes leases and preemption (within user).
However, note: preemption is only within the same user. This is to respect the fair sharing among users.
We can extend to cross-user preemption by using a global combined priority, but that might break fairness.
We leave that as a possible extension.
We hope this meets the requirements.</think>