Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement graceful shutdown of HTTP server #15

Closed
wants to merge 1 commit into from
Closed

Implement graceful shutdown of HTTP server #15

wants to merge 1 commit into from

Conversation

burtonr
Copy link
Contributor

@burtonr burtonr commented Apr 17, 2018

When the of-watchdog reveives a SIGTERM, it should stop accepting
new requests. The current processes in progress should continue up to
the write_timeout property value.

This change is largely copied from the original faas watchdog
Tested with the 'hey' tool by invoking functions and forcing the scale
to 1 while monitoring the logs to verify the in-flight requests were
completed before shutting down the container

Signed-off-by: Burton Rheutan [email protected]

Description

Motivation and Context

How Has This Been Tested?

Tested using the hey testing tool

  • hey -n 500 -m POST -d "testing hey" -T text/plain http://localhost:8080/function/timer-test
  • docker service scale timer-test=1

Observed the service logs of the timer-test service:

timer-test.2.k41cue13b3dw@codefox    | 2018/04/17 13:22:31 SIGTERM received.. shutting down server
timer-test.5.pgjji7wntzt8@codefox    | 2018/04/17 13:22:31 SIGTERM received.. shutting down server
timer-test.4.tfr59k8c6ty3@codefox    | 2018/04/17 13:22:31 SIGTERM received.. shutting down server
timer-test.3.qj0x2wxml7l3@codefox    | 2018/04/17 13:22:31 SIGTERM received.. shutting down server
timer-test.5.pgjji7wntzt8@codefox    | 2018/04/17 13:22:33 Took 5.001429 secs
timer-test.3.qj0x2wxml7l3@codefox    | 2018/04/17 13:22:33 Took 5.003869 secs
timer-test.4.tfr59k8c6ty3@codefox    | 2018/04/17 13:22:33 Took 5.002544 secs

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@alexellis
Copy link
Member

cc @Templum

main.go Outdated
path, lockErr := lock()

if lockErr != nil {
log.Panicf("Cannot write %s. To disable lock-file set env suppress_lock=true.\n Error: %s.\n", path, lockErr.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this @burtonr ? I can't see any code that handles a suppress_lock environmental variable.

Copy link
Contributor Author

@burtonr burtonr Apr 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a relic of the copy from the original watchdog. I'll need to double check we can still disable the lock file here and test it. I'll need to update that panic message regardless


acceptingConnections = false

if err := s.Shutdown(context.Background()); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to make use of a context with timeout. ctx, _ := context.WithTimeout(context.Background(), 5*time.Second). As this can be used in the Shutdown, without the need of an additional construct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good @Templum

@burtonr
Copy link
Contributor Author

burtonr commented Apr 25, 2018

I added the suppress_lock env var, but have yet to test it. I'll get on that as soon as I can!
Also, I'll need to try out the context.WithTimeout() that @Templum suggested to see how that will work.

When the of-watchdog receives a SIGTERM, it should stop accepting
new requests. The current processes in progress should continue up to
the write_timeout property value.

A new environment variable was also ported over from the original faas
watchdog 'suppress_lock' that will not write a lock file to disk,
and not provide Swarm with healthchecks.

This change is largely copied from the original faas watchdog
Tested with the 'hey' tool by invoking functions and forcing the scale
to 1 while monitoring the logs to verify the in-flight requests were
completed before shutting down the container

Signed-off-by: Burton Rheutan <[email protected]>
@burtonr
Copy link
Contributor Author

burtonr commented Apr 25, 2018

I pushed one more commit up to update the README with the suppress_lock env var as "implemented"

Tested locally the same as before, but this time with the suppress_lock: true (also without the env var set after adding it to make sure I didn't break anything)

Verified that Swarm healthchecks were not available and the .lockfile was not present with the suppress_lock set to true

@burtonr
Copy link
Contributor Author

burtonr commented Apr 26, 2018

Fixed the tests by adding an additional request to the mockHttpServer to account for the additional call for ListFunctions.

Still a WIP though as I need to add additional tests around that piece of functionality

@alexellis
Copy link
Member

@burtonr thanks for porting my code across from the main watchdog. I can see a conflict now and I need to re-write this code a little bit in light of https://github.com/openfaas/faas/pull/873/files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement graceful shutdown from classic watchdog
3 participants