Serverless R functions with OpenFaas

We just deployed our first function service into OpenFaas.

Some useful tips:Logging and troubleshootingDocker SwarmYou can check the services deployed through the command:$ docker service lsCheck a specific function like this:docker service ps <function_name_here>Find the logs for a function like this:docker service logs <function_name_here>If your function has a lot of logs, then prefix this with –tail 20.

Use -f if you want to stream them.

MetricsPrometheus is also baked into the FaaS stack, which means you can checkout the various metrics on how your functions are being used as well as how long they’re taking to run.

You can view the Prometheus UI at http://localhost:9090TroubleshootingIf you run into any errors such as Can't reach service check the following:If it’s a multi-node cluster, you have to push your images to the Docker Hub or similar registry before deployingIf you’re still getting the error docker service logs –no-trunc=true hello-python will give a bit more info.

If the above didn’t help then Docker Swarm has a known issue around re-creating Swarm services, you can work around it by running the deploycommand a second time.

Step 5: Further improvement.

You may notice that it took 0.

536s for our service to get a response, which is unacceptable for production purpose.

This is due to that the “handler.

R” needs time to source our function code and load packages.

This static process is repeated for each data payload.

To address this issue, we need the server load the packages once, and hanging there ready for any function calls.

To serve this purpose we shall switch to another underdevelopment version “of-watchdog”.

In addition to compatible with “of-watchdog”, we rewrite our “new_handler.

R”#!/usr/bin/env RscriptsuppressMessages(library(jsonlite))suppressMessages(library(plumber)) source("/root/example.

R") # Source our example.

Rpr <- plumber$new() # Open a new http server with preloaded packagespr$handle("POST", "/", function(req, res){ # similar to handler.

Rdata_input=jsonlite::fromJSON(paste(req$postBody)) output=hello(as.


frame(data_input)) output })pr$run(port=8081) # server listens port 8081, which is exposed by of-watchdogModify “Dockfile” as well:FROM r-base:latestRUN R –vanilla -e 'install.


table","dplyr","microbenchmark","plumber"), repos="http://cran.


com")'ADD https://github.



0/of-watchdog /usr/bin/fwatchdogRUN chmod +x /usr/bin/fwatchdogWORKDIR /root/COPY example.

R .

COPY new_handler.

R .

ENV upstream_url="http://127.



1:8081"ENV mode="http"ENV combine_output=falseENV fprocess="Rscript new_handler.

R" HEALTHCHECK –interval=1s CMD [ -e /tmp/.

lock ] || exit 1 CMD ["fwatchdog"]Rebuild our docker image and deploy it:$docker build -t faas_r:advanced .

$faas-cli -action deploy -a -image=faas_r:advanced -name=hello_r_advanceTest it on UI again, it reduces from 0.

536s to 0.

08s this time!.We improve our performance seven times faster.

.. More details

Leave a Reply