Skip to content

Commit

Permalink
feat: add reserveSizeWithinRadius to status protocol (#4585)
Browse files Browse the repository at this point in the history
  • Loading branch information
acha-bill authored Feb 21, 2024
1 parent e5e74a9 commit 598bf58
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 76 deletions.
2 changes: 2 additions & 0 deletions openapi/SwarmCommon.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ components:
type: integer
reserveSize:
type: integer
reserveSizeWithinRadius:
type: interger
pullsyncRate:
type: number
storageRadius:
Expand Down
43 changes: 23 additions & 20 deletions pkg/api/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ import (
)

type statusSnapshotResponse struct {
Peer string `json:"peer"`
Proximity uint8 `json:"proximity"`
BeeMode string `json:"beeMode"`
ReserveSize uint64 `json:"reserveSize"`
PullsyncRate float64 `json:"pullsyncRate"`
StorageRadius uint8 `json:"storageRadius"`
ConnectedPeers uint64 `json:"connectedPeers"`
NeighborhoodSize uint64 `json:"neighborhoodSize"`
RequestFailed bool `json:"requestFailed,omitempty"`
BatchCommitment uint64 `json:"batchCommitment"`
IsReachable bool `json:"isReachable"`
Peer string `json:"peer"`
Proximity uint8 `json:"proximity"`
BeeMode string `json:"beeMode"`
ReserveSize uint64 `json:"reserveSize"`
ReserveSizeWithinRadius uint64 `json:"reserveSizeWithinRadius"`
PullsyncRate float64 `json:"pullsyncRate"`
StorageRadius uint8 `json:"storageRadius"`
ConnectedPeers uint64 `json:"connectedPeers"`
NeighborhoodSize uint64 `json:"neighborhoodSize"`
RequestFailed bool `json:"requestFailed,omitempty"`
BatchCommitment uint64 `json:"batchCommitment"`
IsReachable bool `json:"isReachable"`
}

type statusResponse struct {
Expand Down Expand Up @@ -70,15 +71,16 @@ func (s *Service) statusGetHandler(w http.ResponseWriter, _ *http.Request) {
}

jsonhttp.OK(w, statusSnapshotResponse{
Peer: s.overlay.String(),
BeeMode: ss.BeeMode,
ReserveSize: ss.ReserveSize,
PullsyncRate: ss.PullsyncRate,
StorageRadius: uint8(ss.StorageRadius),
ConnectedPeers: ss.ConnectedPeers,
NeighborhoodSize: ss.NeighborhoodSize,
BatchCommitment: ss.BatchCommitment,
IsReachable: ss.IsReachable,
Peer: s.overlay.String(),
BeeMode: ss.BeeMode,
ReserveSize: ss.ReserveSize,
ReserveSizeWithinRadius: ss.ReserveSizeWithinRadius,
PullsyncRate: ss.PullsyncRate,
StorageRadius: uint8(ss.StorageRadius),
ConnectedPeers: ss.ConnectedPeers,
NeighborhoodSize: ss.NeighborhoodSize,
BatchCommitment: ss.BatchCommitment,
IsReachable: ss.IsReachable,
})
}

Expand Down Expand Up @@ -118,6 +120,7 @@ func (s *Service) statusGetPeersHandler(w http.ResponseWriter, r *http.Request)
} else {
snapshot.BeeMode = ss.BeeMode
snapshot.ReserveSize = ss.ReserveSize
snapshot.ReserveSizeWithinRadius = ss.ReserveSizeWithinRadius
snapshot.PullsyncRate = ss.PullsyncRate
snapshot.StorageRadius = uint8(ss.StorageRadius)
snapshot.ConnectedPeers = ss.ConnectedPeers
Expand Down
38 changes: 22 additions & 16 deletions pkg/api/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ func TestGetStatus(t *testing.T) {

mode := api.FullMode
ssr := api.StatusSnapshotResponse{
BeeMode: mode.String(),
ReserveSize: 128,
PullsyncRate: 64,
StorageRadius: 8,
ConnectedPeers: 0,
NeighborhoodSize: 0,
BatchCommitment: 1,
IsReachable: true,
BeeMode: mode.String(),
ReserveSize: 128,
ReserveSizeWithinRadius: 64,
PullsyncRate: 64,
StorageRadius: 8,
ConnectedPeers: 0,
NeighborhoodSize: 0,
BatchCommitment: 1,
IsReachable: true,
}

ssMock := &statusSnapshotMock{
syncRate: ssr.PullsyncRate,
reserveSize: int(ssr.ReserveSize),
storageRadius: ssr.StorageRadius,
commitment: ssr.BatchCommitment,
syncRate: ssr.PullsyncRate,
reserveSize: int(ssr.ReserveSize),
reserveSizeWithinRadius: ssr.ReserveSizeWithinRadius,
storageRadius: ssr.StorageRadius,
commitment: ssr.BatchCommitment,
}

statusSvc := status.NewService(
Expand Down Expand Up @@ -109,13 +111,17 @@ func (m *topologyPeersIterNoopMock) IsReachable() bool {
// - status.SyncReporter
// - postage.CommitmentGetter
type statusSnapshotMock struct {
syncRate float64
reserveSize int
storageRadius uint8
commitment uint64
syncRate float64
reserveSize int
reserveSizeWithinRadius uint64
storageRadius uint8
commitment uint64
}

func (m *statusSnapshotMock) SyncRate() float64 { return m.syncRate }
func (m *statusSnapshotMock) ReserveSize() int { return m.reserveSize }
func (m *statusSnapshotMock) StorageRadius() uint8 { return m.storageRadius }
func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.commitment, nil }
func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 {
return m.reserveSizeWithinRadius
}
90 changes: 63 additions & 27 deletions pkg/status/internal/pb/status.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/status/internal/pb/status.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ message Snapshot {
string BeeMode = 6;
uint64 BatchCommitment = 7;
bool IsReachable = 8;
uint64 ReserveSizeWithinRadius = 9;
}
30 changes: 17 additions & 13 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SyncReporter interface {
// Reserve defines the reserve storage related information required.
type Reserve interface {
ReserveSize() int
ReserveSizeWithinRadius() uint64
StorageRadius() uint8
}

Expand Down Expand Up @@ -79,16 +80,18 @@ func NewService(
// LocalSnapshot returns the current status snapshot of this node.
func (s *Service) LocalSnapshot() (*Snapshot, error) {
var (
storageRadius uint8
syncRate float64
reserveSize uint64
connectedPeers uint64
neighborhoodSize uint64
storageRadius uint8
syncRate float64
reserveSize uint64
reserveSizeWithinRadius uint64
connectedPeers uint64
neighborhoodSize uint64
)

if s.reserve != nil {
storageRadius = s.reserve.StorageRadius()
reserveSize = uint64(s.reserve.ReserveSize())
reserveSizeWithinRadius = s.reserve.ReserveSizeWithinRadius()
}

if s.sync != nil {
Expand All @@ -115,14 +118,15 @@ func (s *Service) LocalSnapshot() (*Snapshot, error) {
}

return &Snapshot{
BeeMode: s.beeMode,
ReserveSize: reserveSize,
PullsyncRate: syncRate,
StorageRadius: uint32(storageRadius),
ConnectedPeers: connectedPeers,
NeighborhoodSize: neighborhoodSize,
BatchCommitment: commitment,
IsReachable: s.topologyDriver.IsReachable(),
BeeMode: s.beeMode,
ReserveSize: reserveSize,
ReserveSizeWithinRadius: reserveSizeWithinRadius,
PullsyncRate: syncRate,
StorageRadius: uint32(storageRadius),
ConnectedPeers: connectedPeers,
NeighborhoodSize: neighborhoodSize,
BatchCommitment: commitment,
IsReachable: s.topologyDriver.IsReachable(),
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/status/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ func (m *statusSnapshotMock) SyncRate() float64 { return m.Snapshot.Pu
func (m *statusSnapshotMock) ReserveSize() int { return int(m.Snapshot.ReserveSize) }
func (m *statusSnapshotMock) StorageRadius() uint8 { return uint8(m.Snapshot.StorageRadius) }
func (m *statusSnapshotMock) Commitment() (uint64, error) { return m.Snapshot.BatchCommitment, nil }
func (m *statusSnapshotMock) ReserveSizeWithinRadius() uint64 {
return m.Snapshot.ReserveSizeWithinRadius
}
6 changes: 6 additions & 0 deletions pkg/storer/reserve.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func reserveUpdateBatchLockKey(batchID []byte) string {
}

var errMaxRadius = errors.New("max radius reached")
var reserveSizeWithinRadius atomic.Uint64

type Syncer interface {
// Number of active historical syncing jobs.
Expand Down Expand Up @@ -137,6 +138,7 @@ func (db *DB) reserveSizeWithinRadiusWorker(ctx context.Context) {
if err != nil {
db.logger.Error(err, "reserve count within radius")
}
reserveSizeWithinRadius.Store(uint64(count))

for batch := range evictBatches {
db.logger.Debug("reserve size worker, invalid batch id", "batch_id", hex.EncodeToString([]byte(batch)))
Expand Down Expand Up @@ -474,6 +476,10 @@ func (db *DB) ReserveSize() int {
return db.reserve.Size()
}

func (db *DB) ReserveSizeWithinRadius() uint64 {
return reserveSizeWithinRadius.Load()
}

func (db *DB) IsWithinStorageRadius(addr swarm.Address) bool {
if db.reserve == nil {
return false
Expand Down

0 comments on commit 598bf58

Please sign in to comment.