Faasm HTTP API

Most interactions with Faasm can be performed through the CLI,which itself uses the Faasm HTTP API.

You should only need to interact with the HTTP API directly when performingbenchmarking or integrating with another system.

Note that the API currently has no authentication (as this is still a researchproject).

Endpoints

The Faasm API is split into two parts which correspond to two endpoints:

  • upload - uploading state and functions
  • invoke - function invocation and function status

Generally upload is on port 8002, and invoke on port 8080. The specifics ofthe endpoints themselves will depend on the deployment of Faasm. Locally theseare available on localhost, whereas in a Knative deploymentthese will be provided by the Kubernetes infrastructure.

In some deployments the upload server will also act as a file server for theFaasm runtime instances.

Upload

The upload API is REST-ish and a list of interactions with cURL are shown below.

Functions

Functions all have a user and a name.

  1. # Upload a C/C++ function file
  2. curl -X PUT <host>:8002/f/<user>/<name> -T <wasm_file>
  3.  
  4. # Download a C/C++ function file
  5. curl -X GET <host>:8002/f/<user>/<name> -o <out_file>
  6.  
  7. # Upload a Python function file
  8. curl -X PUT <host>:8002/p/<user>/<name> -T <python_file>
  9.  
  10. # Download a Python function file
  11. curl -X GET <host>:8002/p/<user>/<name> -o <out_file>

State

State values all have a user and a key.

These keys are then accessible to functions at runtime.

  1. # Upload state data directly
  2. curl -X PUT <host>:8002/s/<user>/<key> -d "some state"
  3.  
  4. # Upload state from a file
  5. curl -X PUT <host>:8002/s/<user>/<key> -T <state_file>
  6.  
  7. # Download a state value
  8. curl -X GET <host>:8002/s/<user>/<key> -o <out_file>

Shared files

Files can be made accessible to functions through the virtual filesystem.

  1. # Upload a file (specifying the path at which functions can access the file as a header)
  2. curl -X PUT -H "FilePath: <access_path>" <host>:8002/file -T <file>
  3.  
  4. # Download a file
  5. curl -X GET <host>:8002/file -o <out_file>

Invoke

The invoke API is handled with JSON messages over HTTP.

Functions can be invoked synchronously while the client awaits the result, orasynchronously when a call ID is returned which can be used to pollthe function's status.

JSON Format

All Faasm invoke API calls use the same JSON message format. Possiblefields are as follows:

FieldTypeDescriptionUse
userstringUser associated with the functionInvoking
functionstringThe function nameInvoking
asyncboolFlag for asynchronous callsInvoking
input_databoolInput data for the function callInvoking
statusboolFlag to indicate a status callPolling
idintThe call ID, used for async callsPolling
pythonboolFlag to indicate a Python functionPython
py_userstringPython user (user must be python)Python
py_funcstringPython function (function must be py_func)Python
mpiboolFlag to indicate an MPI callMPI
mpi_world_sizeintHow big to make the MPI worldMPI

Example - synchronous invocation

An example of synchronously invoking a function from Python is shown below:

  1. import requests
  2.  
  3. endpoint = "http://localhost:8080"
  4.  
  5. json_data = {
  6. "user": "demo",
  7. "function": "echo",
  8. "input_data": "Hello API",
  9. }
  10.  
  11. requests.post(endpoint, json=json_data)

The response will contain the function output as well as any captured stdout.

Example - asynchronous invocation

An example of asynchronously invoking a function from Python is shown below:

  1. import requests
  2. from time import sleep
  3.  
  4. endpoint = "http://localhost:8080"
  5.  
  6. invoke_json = {
  7. "user": "demo",
  8. "function": "echo",
  9. "input_data": "Hello API",
  10. "async": True
  11. }
  12.  
  13. res = requests.post(endpoint, json=invoke_json)
  14. call_id = res.content.decode()
  15. print("Async call {}".format(call_id))
  16.  
  17. status_json = {
  18. "id": int(call_id),
  19. "status": True,
  20. }
  21.  
  22. str_res = ""
  23. count = 0
  24. while not str_res.startswith("SUCCESS") and count < 5:
  25. res = requests.post(endpoint, json=status_json)
  26. str_res = res.content.decode()
  27. count+=1
  28.  
  29. sleep(0.2)
  30.  
  31. print(str_res)