Skip to content

API request routing

Most Shaken Fist API handlers can run on any API node. A few cannot: they need to be on the node which holds the instance, the upload or the network, because the work reads or writes something that only exists there -- a libvirt domain, a partially written file, a network namespace.

There are two mechanisms for that, and they are not equivalent.

The operation queue

The replacement. The handler enqueues a cluster operation keyed to the node which must do the work, answers 202 with an operation handle, and the client polls the handle until the operation completes. Network dispatcher describes it in full.

Nothing is held open while the work happens, the answer does not depend on which node received the call, and the operation is visible, retryable and deferrable like any other.

The redirect decorators

The thing being replaced. A decorator in shakenfist/external_api/base.py compares the object's node against this node, and if they differ makes a synchronous HTTP request to the other node's gunicorn, returning its response verbatim.

That holds a gunicorn worker on both nodes for the duration of the remote work, and makes the handler's correctness depend on which node is running it. Routing network intents through the operation queue instead is what reduced redirect_to_network_node to its single remaining call site.

What still uses them

Verified on develop at 8a5a9bb50e. The counts go stale -- the previous inventory, taken four months earlier, said 13 for the first row and it is 12 now -- so re-derive them with:

grep -rn '@api_base.redirect' shakenfist/external_api/
Decorator Sites Where
redirect_instance_request 12 external_api/instance.py (11), external_api/snapshot.py (1)
redirect_upload_request 2 external_api/upload.py
redirect_to_network_node 1 external_api/network.py, the ping endpoint

redirect_to_eventlog_node is gone; no definition remains.

Why it is not a mechanical sweep

The sites fall into four groups, and only one of them can move today.

Power operations -- InstanceRebootSoftEndpoint, InstanceRebootHardEndpoint, InstancePowerOffEndpoint, InstancePowerOnEndpoint, InstancePauseEndpoint, InstanceUnpauseEndpoint. These are mutations of node-local state, which is the shape the operation queue already serves. They are the six that could move, but moving them changes what the endpoint answers: today a successful power operation answers 200 with a null body, and behind a queue handle it would answer 202 and the caller would have to poll. That answer is deliberate -- see Power states for what each operation now reports and when it refuses -- so changing it is a public API change and needs to be decided as one, not slipped in with a routing change.

Reads of live node state -- InstanceConsoleDataEndpoint.get, InstanceVDIConsoleHelperEndpoint.get, InstanceScreenshotEndpoint.get. A read has nowhere to put its answer. An operation handle carries a state, not a payload, so until cluster operations can return output these cannot move. This is the same gap that kept the network ping endpoint on its decorator, and it is the prerequisite for most of this work rather than a property of any one endpoint.

Mutations whose answer is a payload -- InstanceConsoleDataEndpoint.delete, InstanceConsoleKeysEndpoint.post, InstanceSnapshotEndpoint.post. Each writes node-local state and then answers with something the caller needs (the snapshot's artifact and blob uuids, in the last case). They need the same operation output as the reads above, or an answer that is genuinely just an acknowledgement.

Streamed request bodies -- UploadDataEndpoint.post, UploadTruncateEndpoint.post. A queue handle cannot carry a body, so these need a different answer entirely: something closer to a signed direct-to-node upload than to an operation.

So the ordering is: cluster operation output first, then the reads and the payload-answering mutations; the power endpoints whenever the API change is wanted; the uploads separately and last.

One thing worth fixing on its own

The two decorators disagree about how a node is named. redirect_instance_request compares placement['node'] against config.NODE_UUID, and redirect_upload_request compares u.node against config.NODE_NAME, the FQDN. That is the same FQDN-versus-UUID split as #5045, and it belongs with that cleanup rather than with a routing change.

📝 Report an issue with this page