Skip to content

Commit

Permalink
Refactor readiness function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Kalpakchiev committed Nov 11, 2024
1 parent 6c87f36 commit b32b3e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
8 changes: 7 additions & 1 deletion lib/backend/constants.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 All @@ -14,6 +14,7 @@
package backend

import (
"github.com/uber/kraken/core"
"github.com/uber/kraken/utils/memsize"

"github.com/c2h5oh/datasize"
Expand All @@ -25,3 +26,8 @@ const (
DefaultConcurrency int = 10
DefaultListMaxKeys int = 250
)

var (
ReadinessCheckNamespace string = core.NamespaceFixture()
ReadinessCheckName string = core.DigestFixture().Hex()
)
18 changes: 8 additions & 10 deletions lib/backend/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ import (
var (
ErrNamespaceNotFound = errors.New("no matches for namespace")
)
const isReadyNamespace = "isReadyNamespace"
const isReadyName = "38a03d499119bc417b8a6a016f2cb4540b9f9cc0c13e4da42a73867120d3e908"

type backend struct {
regexp *regexp.Regexp
client Client
regexp *regexp.Regexp
client Client
mustReady bool
}

Expand All @@ -44,8 +42,8 @@ func newBackend(namespace string, c Client, mustReady bool) (*backend, error) {
return nil, fmt.Errorf("regexp: %s", err)
}
return &backend{
regexp: re,
client: c,
regexp: re,
client: c,
mustReady: mustReady,
}, nil
}
Expand Down Expand Up @@ -147,15 +145,15 @@ func (m *Manager) GetClient(namespace string) (Client, error) {

// IsReady returns whether the backends are ready (reachable).
// A backend must be explicitly configured as required for readiness to be checked.
func (m *Manager) IsReady() (bool, error) {
func (m *Manager) CheckReadiness() error {
for _, b := range m.backends {
if !b.mustReady {
continue
}
_, err := b.client.Stat(isReadyNamespace, isReadyName)
_, err := b.client.Stat(ReadinessCheckNamespace, ReadinessCheckName)
if err != nil && err != backenderrors.ErrBlobNotFound {
return false, fmt.Errorf("backend for namespace %s not ready: %s", b.regexp.String(), err)
return fmt.Errorf("backend for namespace '%s' not ready: %s", b.regexp.String(), err)
}
}
return true, nil
return nil
}
15 changes: 6 additions & 9 deletions lib/backend/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/uber-go/tally"
"github.com/uber/kraken/core"
"github.com/uber/kraken/lib/backend"
. "github.com/uber/kraken/lib/backend"
"github.com/uber/kraken/lib/backend/backenderrors"
"github.com/uber/kraken/lib/backend/namepath"
Expand Down Expand Up @@ -159,10 +160,7 @@ func TestManagerBandwidth(t *testing.T) {
checkBandwidth(5, 25)
}

func TestManagerIsReady(t *testing.T) {
const isReadyNamespace = "isReadyNamespace"
const isReadyName = "38a03d499119bc417b8a6a016f2cb4540b9f9cc0c13e4da42a73867120d3e908"

func TestManagerCheckReadiness(t *testing.T) {
n1 := "foo/*"
n2 := "bar/*"

Expand Down Expand Up @@ -191,7 +189,7 @@ func TestManagerIsReady(t *testing.T) {
mockStat1Err: nil,
mockStat2Err: errors.New("network error"),
expectedRes: false,
expectedErr: errors.New("backend for namespace bar/* not ready: network error"),
expectedErr: errors.New("backend for namespace 'bar/*' not ready: network error"),
},
{
name: "second required, only first fails",
Expand Down Expand Up @@ -224,14 +222,13 @@ func TestManagerIsReady(t *testing.T) {
mockStat2 = nil
}

c1.EXPECT().Stat(isReadyNamespace, isReadyName).Return(mockStat1, tc.mockStat1Err).AnyTimes()
c2.EXPECT().Stat(isReadyNamespace, isReadyName).Return(mockStat2, tc.mockStat2Err).AnyTimes()
c1.EXPECT().Stat(backend.ReadinessCheckNamespace, backend.ReadinessCheckName).Return(mockStat1, tc.mockStat1Err).AnyTimes()
c2.EXPECT().Stat(backend.ReadinessCheckNamespace, backend.ReadinessCheckName).Return(mockStat2, tc.mockStat2Err).AnyTimes()

require.NoError(m.Register(n1, c1, tc.mustReady1))
require.NoError(m.Register(n2, c2, tc.mustReady2))

res, err := m.IsReady()
require.Equal(tc.expectedRes, res)
err := m.CheckReadiness()
if tc.expectedErr == nil {
require.NoError(err)
} else {
Expand Down

0 comments on commit b32b3e1

Please sign in to comment.