openfaas / faasd

A lightweight & portable faas engine
https://store.openfaas.com/l/serverless-for-everyone-else
MIT License
2.97k stars 213 forks source link

Support request for faasd and Go #317

Closed DanielLee343 closed 1 year ago

DanielLee343 commented 1 year ago

Due diligence

My actions before raising this issue

I tried the similar code on bare metal for standard http request, without any problem. Also restarting faasd didn't help.

Why do you need this?

I want to test multi-function workloads behavior on faasd (e.g., image processing). Currently stuck on inter-function call.

Who is this for?

Academic research on serverless

Expected Behaviour

fn2 should get the http request and send back response to fn1

Current Behaviour

journalctl on faasd shows error with proxy request to: http://10.62.0.32:8080, Get "http://10.62.0.32:8080": EOF which means fn2 does not get the request from fn1. Also, when calling fn1, it makes its status from Ready to Not Ready, prompts shows Can't reach service for: fn1.

Are you a GitHub Sponsor (Yes/No?)

Check at: https://github.com/sponsors/openfaas

List All Possible Solutions and Workarounds

I wrote the similar code on bare metal to perform http post to another local endpoint, which did not throw any error.

Which Solution Do You Recommend?

Steps to Reproduce (for bugs)

  1. Generate template code of fn1 and fn2:
    faas-cli template store pull golang-http
    faas-cli new --lang golang-http fn1 --prefix lyuze
    faas-cli new --lang golang-http fn2 --append fn1.yml --prefix lyuze
    mv fn1.yml stack.yml
  2. To invoke fn2 from fn1 async, according to the doc, I make changes to fn1's handler.go:
    
    package function

import ( "fmt" "io/ioutil" "log" "net/http" "strings"

handler "github.com/openfaas/templates-sdk/go-http"

)

func Handle(req handler.Request) (handler.Response, error) { client := &http.Client{} var payload = strings.NewReader(payload) req, err := http.NewRequest("POST", "http://gateway.openfaas.svc.cluster.local:8080/async-function/fn2",payload) if err != nil { log.Fatal(err) return handler.Response{ Body: []byte("error 1"), StatusCode: http.StatusInternalServerError, }, err } resp, err := client.Do(req) if err != nil { log.Fatal(err) return handler.Response{ Body: []byte("error 2"), StatusCode: http.StatusInternalServerError, }, err } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) return handler.Response{ Body: []byte("error 3"), StatusCode: http.StatusInternalServerError, }, err } message := fmt.Sprintf("response: %s\n", bodyText) return handler.Response{ Body: []byte(message), StatusCode: http.StatusOK, }, err }

`handler.go` in fn2 remain still:

package function

import ( "fmt" "net/http"

handler "github.com/openfaas/templates-sdk/go-http"

)

func Handle(req handler.Request) (handler.Response, error) { var err error

message := fmt.Sprintf("Body: %s", string(req.Body))

return handler.Response{
    Body:       []byte(message),
    StatusCode: http.StatusOK,
}, err

}

3. Again, I need to comment out this line in template `Dockerfile` to prevent [build error](https://github.com/openfaas/faasd/issues/316#issuecomment-1405952984):

RUN test -z "$(gofmt -l $(find . -type f -name '.go' -not -path "./vendor/" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

4. `faas-cli up --parallel 2`
`journalctl` shows:

Jan 27 22:13:47 nu-ins-2 faasd[279975]: 2023/01/27 22:13:47 [Deploy] request: {"service":"fn2","image":"lyuze/fn2:latest","envProcess":"./handler","labels":{},"annotations":{}} Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 Image for: docker.io/lyuze/fn2:latest size: 9204111, took: 0.499744s Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 Container ID: fn2 Task ID fn2: Task PID: 389256 Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 fn2 has IP: 10.62.0.36. Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 [Update] request: {"service":"fn1","image":"lyuze/fn1:latest","envProcess":"./handler","labels":{},"annotations":{}} Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 [Update] service fn1 not found Jan 27 22:13:48 nu-ins-2 faasd[279975]: 2023/01/27 22:13:48 [Deploy] request: {"service":"fn1","image":"lyuze/fn1:latest","envProcess":"./handler","labels":{},"annotations":{}} Jan 27 22:13:50 nu-ins-2 faasd[279975]: 2023/01/27 22:13:50 Image for: docker.io/lyuze/fn1:latest size: 9398317, took: 2.061220s Jan 27 22:13:50 nu-ins-2 faasd[279975]: 2023/01/27 22:13:50 Container ID: fn1 Task ID fn1: Task PID: 389420 Jan 27 22:13:50 nu-ins-2 faasd[279975]: 2023/01/27 22:13:50 fn1 has IP: 10.62.0.37.

Ensure function status is OK:

cc@nu-ins-2:~/functions/test_fn$ faas-cli describe fn1 Name: fn1 Status: Ready Replicas: 1 Available Replicas: 1 Invocations: 4 Image: docker.io/lyuze/fn1:latest Function Process: ./handler URL: http://127.0.0.1:8080/function/fn1 Async URL: http://127.0.0.1:8080/async-function/fn1 Environment: mode: http prefix_logs: false upstream_url: http://127.0.0.1:8082

5. Error invoking fn1 (inside which will invoke fn2 async):

cc@nu-ins-2:~/functions/test_fn$ curl http://127.0.0.1:8080/function/fn1 Can't reach service for: fn1.

`journalctl` shows:

Jan 27 22:14:23 nu-ins-2 faasd[279975]: 2023/01/27 22:14:23 Resolve: "fn1" Jan 27 22:14:23 nu-ins-2 faasd[279975]: 2023/01/27 22:14:23 error with proxy request to: http://10.62.0.37:8080, Get "http://10.62.0.37:8080": EOF

Check fn1 status again and it shows not ready:

cc@nu-ins-2:~/functions/test_fn$ faas-cli describe fn1 Name: fn1 Status: Not Ready Replicas: 0 Available Replicas: 0 Invocations: 10 Image: docker.io/lyuze/fn1:latest Function Process: ./handler URL: http://127.0.0.1:8080/function/fn1 Async URL: http://127.0.0.1:8080/async-function/fn1 Environment: mode: http prefix_logs: false upstream_url: http://127.0.0.1:8082

## Your Environment

* OS and architecture: Ubuntu 20.04, x86-64

* Versions:

```sh
go version
go version go1.18 linux/amd64

containerd -version
containerd containerd.io 1.6.15 5b842e528e99d4d4c1686467debf2bd4b88ecd86

uname -a
Linux nu-ins-2 5.4.0-107-generic #121-Ubuntu SMP Thu Mar 24 16:04:27 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

faasd version
  __                     _ 
 / _| __ _  __ _ ___  __| |
| |_ / _` |/ _` / __|/ _` |
|  _| (_| | (_| \__ \ (_| |
|_|  \__,_|\__,_|___/\__,_|

faasd version: 0.16.8-rc1       commit: 4d6b6dfdc5ccc827742dea4dff506e242b67459b
alexellis commented 1 year ago

The address gateway.openfaas.svc.cluster.local is incorrect. This only works on Kubernetes.

You will not find that in the Ebook, are you sure that you have a copy?

The only reason I can think of for a function to go to not ready is because you have an unhandled exception and it crashed the function. See the notes in the troubleshooting chapter to get the logs from journalctl for specific functions. No need to copy the input commands here, but have a good look over it.

This is a really well supported use-case, and there's no reason it shouldn't work as described, but you need to get your code right.

Did you say you had a separate issue with vendoring? Do you want to raise an issue so we can support you separately, without crossing streams?

https://github.com/openfaas/golang-http-template

Alex

DanielLee343 commented 1 year ago

No, the gateway address is not from the ebook but here. The ebook does have a link in chapter 10 leading me there so I assumed it worked for faasd. Anyway, I'll look into nats-connector as alternative.

Regarding the Not Ready error, it's because I have a log.Fatal("2", err) inside error handling, which causes the function to exit with status 1. Removing this solves the problem.

alexellis commented 1 year ago

Glad you figured out what was going wrong with your function.

We'll get this closed. Feel free to attend our weekly call if you'd like to say hello or get more support.

https://docs.openfaas.com/community/

alexellis commented 1 year ago

/lock: support question resolved