harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0021harbor download openthoughts/tasktrove-exp-rpt-curriculum-easy-curriculum-easy-0021Task: Implement a simplified allocation module for mapping App tasks to Servers with basic resource and anti-affinity constraints.
Overview
- You will create a small package at /app/allocation (or a single module at /app/allocation.py) that exposes a submodule allocator containing three classes: Server, App, and Allocator.
- The allocator should produce a deterministic, valid allocation of all Apps onto the provided Servers, subject to simple resource and anti-affinity rules.
- This easier version focuses on the core idea with a straightforward, easy-to-understand approach and minimal edge-case handling.
What to implement (simplified, easier version)
- Package/module layout
- Create /app/allocation with a module allocator.py.
- Expose the following classes from allocation.allocator:
- class Server
- class App
- class Allocator
- Class definitions (simplified signatures)
- Server
- __init__(self, cpu_capacity: int, mem_capacity: int, disk_capacity: int, name: str | None = None)
- Represents a server with fixed total resources.
- App
- __init__(self, cpu_req: int, mem_req: int, disk_req: int, name: str | None = None, antiAffinityLabels: list[str] | None = None)
- Stores per-app resource requirements and optional anti-affinity labels.
- If antiAffinityLabels is None, treat it as an empty set.
- Allocator
- __init__(self, servers: list[Server], apps: list[App])
- allocate(self) -> list[dict[str, object]]
- Returns a list of allocations, where each allocation is a dict:
{ "node": server_instance, "apps": [list_of_App_instances_allocated_to_that_server] }
- The servers in the output must be the same object references as provided to Allocator (no copies).
- The allocation must place every App from the input list onto one of the provided servers without exceeding:
- CPU, memory, and disk capacities of each server.
- Anti-affinity rule (simplified):
- Apps that share any antiAffinityLabels cannot be placed on the same server.
- If multiple apps share a label, they must reside on different servers.
- If it is impossible to allocate all Apps across the given Servers while respecting all constraints, return [].
- If allocation is possible, return any valid arrangement that places all Apps across servers. Tests may accept multiple valid outputs; aim for a deterministic approach.
Assumptions and behavior (deliberately simple)
- Deterministic placement strategy:
- Iterate through Apps in the given order.
- For each App, place it on the first server (in the given order) that has enough remaining resources (cpu, mem, disk) and on which no existing app on that server shares any antiAffinityLabels with this App.
- If no server can host the App, return [] (no partial allocations).
- Edge cases:
- If apps is empty, return [].
- If servers is empty, return [].
- Do not modify App or Server objects beyond recording their allocation; return references as provided.
Hints and approach guidance
- Maintain per-server remaining resources to check capacity quickly.
- Maintain a mapping on each server from anti-affinity label to the set of apps already placed with that label, or simply a set of labels present on the server.
- When evaluating a server for an App:
- Check resource sufficiency.
- Check that the App’s antiAffinityLabels do not intersect with labels already present on that server.
- The tests will expect a valid allocation or an empty list if impossible. For simple cases, a straightforward first-fit strategy usually suffices.
Skeleton to start (starter code)
- You may begin from a minimal skeleton like below and fill in the logic:
# File: /app/allocation/allocator.py
class Server:
def __init__(self, cpu_capacity: int, mem_capacity: int, disk_capacity: int, name: str | None = None):
...
class App:
def __init__(self, cpu_req: int, mem_req: int, disk_req: int, name: str | None = None, antiAffinityLabels: list[str] | None = None):
...
class Allocator:
def __init__(self, servers: list[Server], apps: list[App]):
...
def allocate(self) -> list[dict[str, object]]:
...
Notes
- The implementation should be self-contained and rely only on Python standard library.
- The Output should be the allocation structure described above, using the same Server and App instances that were provided to Allocator.
Placement
- Place the final implementation in /app/allocation/ as allocation/allocator.py, ensuring the module can be imported as allocation.allocator from tests or other code.