Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func createRedistributionAgentService(
postageContract,
stakingContract,
mockstorer.NewReserve(),
func() bool { return true },
func(uint8) bool { return true },
time.Millisecond*10,
blocksPerRound,
blocksPerPhase,
Expand Down
19 changes: 10 additions & 9 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/hashicorp/go-multierror"
ma "github.com/multiformats/go-multiaddr"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/sha3"
"golang.org/x/net/idna"
"golang.org/x/sync/errgroup"

bee "github.com/ethersphere/bee/v2"
"github.com/ethersphere/bee/v2/pkg/accesscontrol"
"github.com/ethersphere/bee/v2/pkg/accounting"
Expand Down Expand Up @@ -81,12 +88,6 @@ import (
"github.com/ethersphere/bee/v2/pkg/util/ioutil"
"github.com/ethersphere/bee/v2/pkg/util/nbhdutil"
"github.com/ethersphere/bee/v2/pkg/util/syncutil"
"github.com/hashicorp/go-multierror"
ma "github.com/multiformats/go-multiaddr"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/sha3"
"golang.org/x/net/idna"
"golang.org/x/sync/errgroup"
)

// LoggerName is the tree path name of the logger for this package.
Expand Down Expand Up @@ -1323,10 +1324,10 @@ func NewBee(

redistributionContract := redistribution.New(swarmAddress, overlayEthAddress, logger, transactionService, redistributionContractAddress, abiutil.MustParseABI(chainCfg.RedistributionABI), contractGasLimit)

isFullySynced := func() bool {
isReserveSynced := func(depth uint8) bool {
reserveThreshold := reserveCapacity * 5 / 10
logger.Debug("Sync status check evaluated", "stabilized", detector.IsStabilized())
return localStore.ReserveSize() >= reserveThreshold && pullerService.SyncRate() == 0 && detector.IsStabilized()
return localStore.ReserveSizeWithinRadius() >= uint64(reserveThreshold) && pullerService.IsReserveSynced(depth) && detector.IsStabilized()
}

agent, err = storageincentives.New(
Expand All @@ -1337,7 +1338,7 @@ func NewBee(
postageStampContractService,
stakingContract,
localStore,
isFullySynced,
isReserveSynced,
o.BlockTime,
storageincentives.DefaultBlocksPerRound,
storageincentives.DefaultBlocksPerPhase,
Expand Down
2 changes: 1 addition & 1 deletion pkg/puller/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (p *Puller) IsSyncing(addr swarm.Address) bool {
return ok
}

func (p *Puller) IsBinSyncing(addr swarm.Address, bin uint8) bool {
func (p *Puller) IsPeerBinSyncing(addr swarm.Address, bin uint8) bool {
p.syncPeersMtx.Lock()
defer p.syncPeersMtx.Unlock()
if peer, ok := p.syncPeers[addr.ByteString()]; ok {
Expand Down
48 changes: 44 additions & 4 deletions pkg/puller/mock/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,48 @@ package mock

import "context"

type mockSyncer struct{ rate float64 }
type Option func(*mockSyncer)

func NewMockRateReporter(r float64) *mockSyncer { return &mockSyncer{r} }
func (m *mockSyncer) SyncRate() float64 { return m.rate }
func (m *mockSyncer) Start(context.Context) {}
type mockSyncer struct {
rate float64
isReserveSyncedFunc func(depth uint8) bool
isBinSyncingFunc func(bin uint8) bool
}

func WithReserveSynced(f func(depth uint8) bool) Option {
return func(m *mockSyncer) {
m.isReserveSyncedFunc = f
}
}

func WithBinSyncing(f func(bin uint8) bool) Option {
return func(m *mockSyncer) {
m.isBinSyncingFunc = f
}
}

func NewMockRateReporter(r float64, opts ...Option) *mockSyncer {
m := &mockSyncer{rate: r}
for _, opt := range opts {
opt(m)
}
return m
}

func (m *mockSyncer) SyncRate() float64 { return m.rate }

func (m *mockSyncer) IsReserveSynced(depth uint8) bool {
if m.isReserveSyncedFunc != nil {
return m.isReserveSyncedFunc(depth)
}
return true
}

func (m *mockSyncer) IsBinSyncing(bin uint8) bool {
if m.isBinSyncingFunc != nil {
return m.isBinSyncingFunc(bin)
}
return false
}

func (m *mockSyncer) Start(context.Context) {}
39 changes: 37 additions & 2 deletions pkg/puller/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"sync"
"time"

ratelimit "golang.org/x/time/rate"

"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/p2p"
"github.com/ethersphere/bee/v2/pkg/puller/intervalstore"
Expand All @@ -26,7 +28,6 @@ import (
"github.com/ethersphere/bee/v2/pkg/storer"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/topology"
ratelimit "golang.org/x/time/rate"
)

// loggerName is the tree path name of the logger for this package.
Expand Down Expand Up @@ -106,6 +107,9 @@ type Puller struct {

rate *rate.Rate // rate of historical syncing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you now count the active historical syncing using the number of active goroutines in radius bins, this thing is no longer used if i get it right. there are a couple of changes here - one is the fact we're no longer using these sliding window rate which is probably a good thing, but the problem is that the goroutine approach would be a bit tricky because it would basically flip on/off all the time. maybe a bit more thought needs to do be done here so we can get a smoothed reading that could be a bit more informative (but also that doesn't take half an hour to reach below the reading we need it to get to in order to conclude "not syncing")


activeHistSyncs [swarm.MaxBins]int
activeHistSyncsMu sync.RWMutex

start sync.Once

limiter *ratelimit.Limiter
Expand Down Expand Up @@ -159,6 +163,28 @@ func (p *Puller) SyncRate() float64 {
return p.rate.Rate()
}

// IsBinSyncing returns true if any peer is actively running historical sync for the given bin.
func (p *Puller) IsBinSyncing(bin uint8) bool {
if bin >= p.bins {
return false
}
p.activeHistSyncsMu.RLock()
defer p.activeHistSyncsMu.RUnlock()
return p.activeHistSyncs[bin] > 0
}

// IsReserveSynced returns true if no bins at or above the given depth are actively syncing.
func (p *Puller) IsReserveSynced(depth uint8) bool {
p.activeHistSyncsMu.RLock()
defer p.activeHistSyncsMu.RUnlock()
for bin := depth; bin < p.bins; bin++ {
if p.activeHistSyncs[bin] > 0 {
return false
}
}
return true
}

func (p *Puller) manage(ctx context.Context) {
defer p.wg.Done()

Expand Down Expand Up @@ -408,10 +434,19 @@ func (p *Puller) syncPeerBin(parentCtx context.Context, peer *syncPeer, bin uint
}
}

if cursor > 0 {
if cursor > 0 && bin < swarm.MaxBins {
p.activeHistSyncsMu.Lock()
p.activeHistSyncs[bin]++
Comment thread
aloknerurkar marked this conversation as resolved.
p.activeHistSyncsMu.Unlock()

peer.wg.Add(1)
p.wg.Add(1)
safe.Go(p.logger, "puller-sync-historical", func() {
defer func() {
p.activeHistSyncsMu.Lock()
p.activeHistSyncs[bin]--
p.activeHistSyncsMu.Unlock()
}()
sync(true, peer.address, cursor)
})
}
Expand Down
55 changes: 52 additions & 3 deletions pkg/puller/puller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"

"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/puller"
"github.com/ethersphere/bee/v2/pkg/puller/intervalstore"
Expand All @@ -23,7 +25,6 @@ import (
"github.com/ethersphere/bee/v2/pkg/swarm"
kadMock "github.com/ethersphere/bee/v2/pkg/topology/kademlia/mock"
"github.com/ethersphere/bee/v2/pkg/util/testutil"
"github.com/google/go-cmp/cmp"
)

// test that adding one peer starts syncing
Expand Down Expand Up @@ -462,10 +463,10 @@ func TestRadiusIncrease(t *testing.T) {
rs.SetStorageRadius(2)
kad.Trigger()
time.Sleep(100 * time.Millisecond)
if !p.IsBinSyncing(addr, 1) {
if !p.IsPeerBinSyncing(addr, 1) {
t.Fatalf("peer is not syncing but should")
}
if p.IsBinSyncing(addr, 2) {
if p.IsPeerBinSyncing(addr, 2) {
t.Fatalf("peer is syncing but shouldn't")
}
}
Expand Down Expand Up @@ -642,6 +643,54 @@ type opts struct {
syncSleepDur time.Duration
}

func TestIsReserveSynced(t *testing.T) {
t.Parallel()

var (
addr = swarm.RandAddress(t)
cursors = []uint64{1000, 1000, 1000, 1000}
replies = []mockps.SyncReply{
{Bin: 1, Start: 1, Topmost: 500, Peer: addr}, // partial sync, historical stays active
}
)

p, _, kad, pullsync := newPuller(t, opts{
kad: []kadMock.Option{
kadMock.WithEachPeerRevCalls(
kadMock.AddrTuple{Addr: addr, PO: 1},
),
},
pullSync: []mockps.Option{
mockps.WithCursors(cursors, 0),
mockps.WithReplies(replies...),
},
bins: 4,
rs: resMock.NewReserve(resMock.WithRadius(2)),
})

kad.Trigger()
waitCursorsCalled(t, pullsync, addr)
waitSyncCalledBins(t, pullsync, addr, 1)

// While bin 1 has an active historical sync and radius is 2:
err := spinlock.Wait(time.Second, func() bool {
return p.IsBinSyncing(1)
})
if err != nil {
t.Fatal("expected bin 1 to be syncing")
}

if p.IsBinSyncing(2) {
t.Fatal("expected bin 2 not to be syncing")
}
if !p.IsReserveSynced(2) {
t.Fatal("expected reserve to be synced for depth 2 when only bin 1 is syncing")
}
if p.IsReserveSynced(1) {
t.Fatal("expected reserve NOT to be synced for depth 1 when bin 1 is syncing")
}
}

func newPuller(t *testing.T, ops opts) (*puller.Puller, storage.StateStorer, *kadMock.Mock, *mockps.PullSyncMock) {
t.Helper()

Expand Down
12 changes: 6 additions & 6 deletions pkg/storageincentives/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type Agent struct {
batchExpirer postagecontract.PostageBatchExpirer
redistributionStatuser staking.RedistributionStatuser
store storer.Reserve
fullSyncedFunc func() bool
reserveSyncedFunc func(depth uint8) bool
overlay swarm.Address
quit chan struct{}
wg sync.WaitGroup
Expand All @@ -85,7 +85,7 @@ func New(overlay swarm.Address,
batchExpirer postagecontract.PostageBatchExpirer,
redistributionStatuser staking.RedistributionStatuser,
store storer.Reserve,
fullSyncedFunc func() bool,
reserveSyncedFunc func(depth uint8) bool,
blockTime time.Duration,
blocksPerRound,
blocksPerPhase uint64,
Expand All @@ -104,7 +104,7 @@ func New(overlay swarm.Address,
contract: contract,
batchExpirer: batchExpirer,
store: store,
fullSyncedFunc: fullSyncedFunc,
reserveSyncedFunc: reserveSyncedFunc,
blocksPerRound: blocksPerRound,
quit: make(chan struct{}),
redistributionStatuser: redistributionStatuser,
Expand Down Expand Up @@ -225,7 +225,7 @@ func (a *Agent) start(blockTime time.Duration, blocksPerRound, blocksPerPhase ui
a.logger.Info("entered new phase", "phase", currentPhase.String(), "round", round, "block", block)

a.state.SetCurrentEvent(currentPhase, round)
a.state.SetFullySynced(a.fullSyncedFunc())
a.state.SetFullySynced(a.reserveSyncedFunc(a.store.StorageRadius()))
Comment thread
aloknerurkar marked this conversation as resolved.
a.state.SetHealthy(a.health.IsHealthy())
safe.Go(a.logger, "storageincentives-purge-stale-round-data", func() {
a.state.purgeStaleRoundData()
Expand Down Expand Up @@ -425,8 +425,8 @@ func (a *Agent) handleSample(ctx context.Context, round uint64) (bool, error) {
a.metrics.NeighborhoodSelected.Inc()
a.logger.Info("neighbourhood chosen", "round", round)

if !a.state.IsFullySynced() {
a.logger.Info("skipping round because node is not fully synced")
if !a.reserveSyncedFunc(committedDepth) {
a.logger.Info("skipping round because reserve is not synced", "depth", committedDepth, "round", round)
return false, nil
}

Expand Down
Loading
Loading