Skip to content

Commit

Permalink
[PATCH] CORE-6348 add ability to depool vtgates via liveness check en…
Browse files Browse the repository at this point in the history
…dpoint (vitessio#3)

`/debug/liveness` endpoint now returns a 503 when `/etc/etsy/depool` is present on the filesystem

note: included "unit" test currently relies on `/etc/etsy` existing and being writable. run with `go test go/vt/servenv/*.go`

Signed-off-by: Mackenzie Starr <mstarr@etsy.com>
  • Loading branch information
mackenziestarr authored and adsr committed Jan 23, 2023
1 parent a914f40 commit 97db5bf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
6 changes: 5 additions & 1 deletion go/vt/servenv/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package servenv

import (
"net/http"
"os"
)

// This file registers a handler that immediately responds with HTTP 200 OK.
Expand All @@ -30,6 +31,9 @@ import (

func init() {
http.HandleFunc("/debug/liveness", func(rw http.ResponseWriter, r *http.Request) {
// Do nothing. Return success immediately.
if _, err := os.Stat("/etc/etsy/depool"); !os.IsNotExist(err) {
rw.WriteHeader(http.StatusServiceUnavailable)
return
}
})
}
43 changes: 42 additions & 1 deletion go/vt/servenv/liveness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLivenessHandler(t *testing.T) {
server := httptest.NewServer(nil)
defer server.Close()

resp, err := http.Get(server.URL + "/debug/liveness")
if err != nil {
t.Fatalf("http.Get: %v", err)
Expand All @@ -40,3 +42,42 @@ func TestLivenessHandler(t *testing.T) {
}
t.Logf("body: %q", body)
}

func TestLivenessHandlerDepool(t *testing.T) {
assert := assert.New(t)
server := httptest.NewServer(nil)
defer server.Close()

var resp, err = http.Get(server.URL + "/debug/liveness")
if err != nil {
t.Fatalf("http.Get: %v", err)
}
assert.Equal(
http.StatusOK,
resp.StatusCode,
"should return 200 response code when depool file is not present",
)

// requires
// ```
// sudo mkdir -p /etc/etsy
// sudo chmod 777 /etc/etsy
// ```
var path = "/etc/etsy/depool"
_, err = os.Create(path)
if err != nil {
t.Error(err)
}
defer os.Remove(path)

resp, err = http.Get(server.URL + "/debug/liveness")
if err != nil {
t.Fatalf("http.Get: %v", err)
}
assert.Equal(
http.StatusServiceUnavailable,
resp.StatusCode,
"should return 503 response code when depool file is present",
)
defer resp.Body.Close()
}

0 comments on commit 97db5bf

Please sign in to comment.