Skip to content

Commit

Permalink
Add origin readiness endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Kalpakchiev committed Nov 8, 2024
1 parent 6c87f36 commit f78735a
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
5 changes: 0 additions & 5 deletions core/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ import (
"strings"
)

const (
// DigestEmptyTar is the sha256 digest of an empty tar file.
DigestEmptyTar = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
)

// DigestList is a list of digests.
type DigestList []Digest

Expand Down
12 changes: 11 additions & 1 deletion origin/blobserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ func (s *Server) Handler() http.Handler {
// Public endpoints:

r.Get("/health", handler.Wrap(s.healthCheckHandler))

r.Get("/readiness", handler.Wrap(s.readinessCheckHandler))

r.Get("/blobs/{digest}/locations", handler.Wrap(s.getLocationsHandler))

r.Post("/namespace/{namespace}/blobs/{digest}/uploads", handler.Wrap(s.startClusterUploadHandler))
Expand Down Expand Up @@ -179,6 +180,15 @@ func (s *Server) healthCheckHandler(w http.ResponseWriter, r *http.Request) erro
return nil
}

func (s *Server) readinessCheckHandler(w http.ResponseWriter, r *http.Request) error {
isReady, err := s.backends.IsReady()
if isReady {
fmt.Fprintln(w, "OK")
return nil
}
return handler.Errorf("not ready to serve traffic: %s", err).Status(http.StatusServiceUnavailable)
}

// statHandler returns blob info if it exists.
func (s *Server) statHandler(w http.ResponseWriter, r *http.Request) error {
checkLocal, err := strconv.ParseBool(httputil.GetQueryArg(r, "local", "false"))
Expand Down
67 changes: 61 additions & 6 deletions origin/blobserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -53,6 +53,61 @@ func TestHealth(t *testing.T) {
require.Equal("OK\n", string(b))
}

func TestReadiness(t *testing.T) {
const isReadyNamespace = "isReadyNamespace"
const isReadyName = "38a03d499119bc417b8a6a016f2cb4540b9f9cc0c13e4da42a73867120d3e908"
for _, tc := range []struct {
mockStatErr error
expectedRes string
status int
}{
{
mockStatErr: nil,
expectedRes: "OK",
status: http.StatusOK,
},
{
mockStatErr: backenderrors.ErrBlobNotFound,
expectedRes: "OK",
status: http.StatusOK,
},
{
mockStatErr: errors.New("backend error"),
expectedRes: "",
status: http.StatusServiceUnavailable,
},
} {
require := require.New(t)

cp := newTestClientProvider()

s := newTestServer(t, master1, hashRingMaxReplica(), cp)
defer s.cleanup()

backendClient := s.backendClient(isReadyNamespace, true)
mockStat := &core.BlobInfo{}
if tc.mockStatErr != nil {
mockStat = nil
}
backendClient.EXPECT().Stat(isReadyNamespace, isReadyName).Return(mockStat, tc.mockStatErr)

resp, err := httputil.Get(
fmt.Sprintf("http://%s/readiness", s.addr))

if tc.status == http.StatusOK {
defer resp.Body.Close()
require.Equal(tc.status, resp.StatusCode)
require.NoError(err)
b, _ := ioutil.ReadAll(resp.Body)
require.Equal("OK\n", string(b))
} else {
require.True(httputil.IsStatus(err, tc.status))
require.Error(err)
require.Nil(resp)
}
}
}

func TestStatHandlerLocalNotFound(t *testing.T) {
require := require.New(t)

Expand Down Expand Up @@ -117,7 +172,7 @@ func TestStatHandlerNotFound(t *testing.T) {
d := core.DigestFixture()
namespace := core.TagFixture()

backendClient := s.backendClient(namespace)
backendClient := s.backendClient(namespace, false)

backendClient.EXPECT().Stat(namespace, d.Hex()).Return(nil, backenderrors.ErrBlobNotFound)

Expand Down Expand Up @@ -192,7 +247,7 @@ func TestDownloadBlobNotFound(t *testing.T) {
d := core.DigestFixture()
namespace := core.TagFixture()

backendClient := s.backendClient(namespace)
backendClient := s.backendClient(namespace, false)
backendClient.EXPECT().Stat(namespace, d.Hex()).Return(nil, backenderrors.ErrBlobNotFound)

err := cp.Provide(master1).DownloadBlob(namespace, d, ioutil.Discard)
Expand Down Expand Up @@ -280,7 +335,7 @@ func TestGetMetaInfoDownloadsBlobAndReplicates(t *testing.T) {

blob := computeBlobForHosts(ring, s1.host, s2.host)

backendClient := s1.backendClient(namespace)
backendClient := s1.backendClient(namespace, false)
backendClient.EXPECT().Stat(namespace,
blob.Digest.Hex()).Return(core.NewBlobInfo(int64(len(blob.Content))), nil).AnyTimes()
backendClient.EXPECT().Download(namespace, blob.Digest.Hex(), mockutil.MatchWriter(blob.Content)).Return(nil)
Expand Down Expand Up @@ -317,7 +372,7 @@ func TestGetMetaInfoBlobNotFound(t *testing.T) {
d := core.DigestFixture()
namespace := core.TagFixture()

backendClient := s.backendClient(namespace)
backendClient := s.backendClient(namespace, false)
backendClient.EXPECT().Stat(namespace, d.Hex()).Return(nil, backenderrors.ErrBlobNotFound)

mi, err := cp.Provide(master1).GetMetaInfo(namespace, d)
Expand Down Expand Up @@ -582,7 +637,7 @@ func TestReplicateToRemoteWhenBlobInStorageBackend(t *testing.T) {
blob := core.NewBlobFixture()
namespace := core.TagFixture()

backendClient := s.backendClient(namespace)
backendClient := s.backendClient(namespace, false)
backendClient.EXPECT().Stat(namespace,
blob.Digest.Hex()).Return(core.NewBlobInfo(int64(len(blob.Content))), nil).AnyTimes()
backendClient.EXPECT().Download(namespace, blob.Digest.Hex(), mockutil.MatchWriter(blob.Content)).Return(nil)
Expand Down
6 changes: 3 additions & 3 deletions origin/blobserver/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -158,9 +158,9 @@ func newTestServer(
}
}

func (s *testServer) backendClient(namespace string) *mockbackend.MockClient {
func (s *testServer) backendClient(namespace string, mustReady bool) *mockbackend.MockClient {
client := mockbackend.NewMockClient(s.ctrl)
if err := s.backendManager.Register(namespace, client, false); err != nil {
if err := s.backendManager.Register(namespace, client, mustReady); err != nil {
panic(err)
}
return client
Expand Down

0 comments on commit f78735a

Please sign in to comment.