Skip to content

Commit

Permalink
Merge pull request #133 from XenitAB/feature/peer-readiness
Browse files Browse the repository at this point in the history
Use routing table size for readiness check
  • Loading branch information
phillebaba authored Jul 4, 2023
2 parents 02d419e + f219e00 commit e85c374
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#117](https://github.com/XenitAB/spegel/pull/117) Update Containerd client to 1.7.
- [#126](https://github.com/XenitAB/spegel/pull/126) Refactor registry implementation to not require separate handler.
- [#132](https://github.com/XenitAB/spegel/pull/132) Extend tests to validate single node and mirror fallback.
- [#133](https://github.com/XenitAB/spegel/pull/133) Use routing table size for readiness check.

### Deprecated

Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ e2e: docker-build
exit 1
fi

# Verify that Spegel has never restarted
RESTART_COUNT=$$(kubectl --kubeconfig $$KIND_KUBECONFIG --namespace spegel get pods -o=jsonpath='{.items[*].status.containerStatuses[0].restartCount}')
if [[ $$RESTART_COUNT != "0" ]]
then
echo "Spegel should not have restarted during tests."
exit 1
fi

# Delete cluster
kind delete cluster

Expand Down
12 changes: 11 additions & 1 deletion internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func (r *Registry) Server(addr string, log logr.Logger) *http.Server {
}

func (r *Registry) readyHandler(c *gin.Context) {
ok, err := r.router.HasMirrors()
if err != nil {
//nolint:errcheck // ignore
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if !ok {
c.Status(http.StatusInternalServerError)
return

}
c.Status(http.StatusOK)
}

Expand Down Expand Up @@ -139,7 +150,6 @@ func (r *Registry) registryHandler(c *gin.Context) {
c.Status(http.StatusNotFound)
}

// TODO: Explore if it is worth returning early if router is not populated.
func (r *Registry) handleMirror(c *gin.Context, key string) {
c.Set("handler", "mirror")

Expand Down
4 changes: 4 additions & 0 deletions internal/routing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (m *MockRouter) Close() error {
return nil
}

func (m *MockRouter) HasMirrors() (bool, error) {
return true, nil
}

func (m *MockRouter) Resolve(ctx context.Context, key string, allowSelf bool, count int) (<-chan string, error) {
peerCh := make(chan string, count)
peers, ok := m.resolver[key]
Expand Down
18 changes: 18 additions & 0 deletions internal/routing/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import (
)

type P2PRouter struct {
b Bootstrapper
host host.Host
kdht *dht.IpfsDHT
rd *routing.RoutingDiscovery
registryPort string
}
Expand Down Expand Up @@ -89,7 +91,9 @@ func NewP2PRouter(ctx context.Context, addr string, b Bootstrapper, registryPort
rd := routing.NewRoutingDiscovery(kdht)

return &P2PRouter{
b: b,
host: host,
kdht: kdht,
rd: rd,
registryPort: registryPort,
}, nil
Expand All @@ -99,6 +103,20 @@ func (r *P2PRouter) Close() error {
return r.host.Close()
}

func (r *P2PRouter) HasMirrors() (bool, error) {
addrInfo, err := r.b.GetAddress()
if err != nil {
return false, err
}
if addrInfo.ID == r.host.ID() {
return true, nil
}
if r.kdht.RoutingTable().Size() == 0 {
return false, nil
}
return true, nil
}

func (r *P2PRouter) Resolve(ctx context.Context, key string, allowSelf bool, count int) (<-chan string, error) {
log := logr.FromContextOrDiscard(ctx).WithValues("host", r.host.ID().Pretty(), "key", key)
c, err := createCid(key)
Expand Down
1 change: 1 addition & 0 deletions internal/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type Router interface {
Close() error
Resolve(ctx context.Context, key string, allowSelf bool, count int) (<-chan string, error)
Advertise(ctx context.Context, keys []string) error
HasMirrors() (bool, error)
}

0 comments on commit e85c374

Please sign in to comment.