There were six parts in total, and the problem statement was so long it read like a paper. I only got through part 2.
The task: implement a resource coordinator that generates and executes changes based on a target configuration and the actual resource state.
desired: logical address -> ResourceSpec, describing the resource configuration we want to have.
state: logical address -> ResourceBinding, where a binding contains resource_id and attributes.
client.read(resource_id): queries the actual remote resource, and returns a RemoteResource or "does not exist".
Change: describes an action to execute, with fields such as address, kind, resource_id, attributes, etc. kind is an enum with CREATE, UPDATE, DELETE and so on.
Part 1: generate a CREATE plan
The input is desired, state and client, and the output is a list[Change] sorted by address.
desired: Desired = {
"search_api": ResourceSpec(
attributes={"replicas": 3},
),
}
state: State = {
"search_api": ResourceBinding(
resource_id="r-123",
attributes={"replicas": 1},
),
}
In this example, I take the resource_id from state, client.read returns that the RemoteResource does not exist, so the Change is to create this address with these attributes. If the address is not in state either, just append the change directly, no need to call the client. For part 1 you can assume the only change is CREATE, and you do not need to consider UPDATE.
Part 2: execute the creation and save the binding
This one is about create and the persistent store's put:
- Create the remote resource and get the creation result.
- Save the binding of the logical address and the new resource to the state store, supporting insert or replace.
- A change only counts as complete once the resource exists and its binding has been saved.
- If a client call throws an exception, the exception needs to be propagated to the caller.
I did not finish this part. The interface is roughly like this:
class ResourceClient(Protocol):
def read(self, resource_id: str) -> RemoteResource | None:
...
def create(self, attributes: Attributes) -> RemoteResource:
...
class StateStore(Protocol):
def put(
self,
address: str,
binding: ResourceBinding,
) -> None:
...
Reading the problem made me so angry, and the interviewer did not want to help at all. I just moved on.
Discussion
Loading comments…