-
Notifications
You must be signed in to change notification settings - Fork 338
/
depthmonitor.go
186 lines (154 loc) · 4.87 KB
/
depthmonitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2022 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package depthmonitor
import (
"errors"
"fmt"
"time"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/postage"
topologyDriver "github.com/ethersphere/bee/pkg/topology"
"go.uber.org/atomic"
)
const loggerName = "depthmonitor"
// DefaultWakeupInterval is the default value
// for the depth monitor wake-up interval.
const DefaultWakeupInterval = 5 * time.Minute
// defaultMinimumRadius is the default value
// for the depth monitor minimum radius.
const defaultMinimumRadius uint8 = 0
// SyncReporter interface needs to be implemented by the syncing component of the node (puller).
type SyncReporter interface {
// Number of active historical syncing jobs.
SyncRate() float64
}
type ReserveReporter interface {
// ReserveSize returns the current reserve size.
ReserveSize() uint64
}
// Reserve interface defines the functionality required from the local storage
// of the node to report information about the reserve. The reserve storage is the storage
// pledged by the node to the network.
type Reserve interface {
// Current size of the reserve.
ComputeReserveSize(uint8) (uint64, error)
// Capacity of the reserve that is configured.
ReserveCapacity() uint64
}
// Topology interface encapsulates the functionality required by the topology component
// of the node.
type Topology interface {
topologyDriver.SetStorageRadiuser
topologyDriver.PeersCounter
}
// Service implements the depthmonitor service
type Service struct {
topology Topology
syncer SyncReporter
reserve Reserve
logger log.Logger
bs postage.Storer
quit chan struct{} // to request service to stop
stopped chan struct{} // to signal stopping of bg worker
minimumRadius uint8
lastRSize *atomic.Uint64
}
// New constructs a new depthmonitor service
func New(
t Topology,
syncer SyncReporter,
reserve Reserve,
bs postage.Storer,
logger log.Logger,
warmupTime time.Duration,
wakeupInterval time.Duration,
freshNode bool,
) *Service {
s := &Service{
topology: t,
syncer: syncer,
reserve: reserve,
bs: bs,
logger: logger.WithName(loggerName).Register(),
quit: make(chan struct{}),
stopped: make(chan struct{}),
minimumRadius: defaultMinimumRadius,
lastRSize: atomic.NewUint64(0),
}
go s.manage(warmupTime, wakeupInterval, freshNode)
return s
}
func (s *Service) manage(warmupTime, wakeupInterval time.Duration, freshNode bool) {
defer close(s.stopped)
// wire up batchstore to start reporting storage radius to kademlia
s.bs.SetStorageRadiusSetter(s.topology)
// if it's a new fresh node, then we set the storage radius to the reserve radius
// to prevent syncing from starting at radius zero.
if freshNode {
reserveRadius := s.bs.GetReserveState().Radius
err := s.bs.SetStorageRadius(func(radius uint8) uint8 {
// if we are starting from scratch, we can use the reserve radius.
if radius == 0 {
radius = reserveRadius
}
return radius
})
if err != nil {
s.logger.Error(err, "depthmonitor: batchstore set storage radius")
}
}
// wait for warmup
select {
case <-s.quit:
return
case <-time.After(warmupTime):
}
s.logger.Info("depthmonitor: warmup period complete, starting worker", "radius", s.bs.StorageRadius())
targetSize := s.reserve.ReserveCapacity() * 4 / 10 // 40% of the capacity
for {
select {
case <-s.quit:
return
case <-time.After(wakeupInterval):
}
radius := s.bs.StorageRadius()
currentSize, err := s.reserve.ComputeReserveSize(radius)
if err != nil {
s.logger.Error(err, "depthmonitor: failed reading reserve size")
continue
}
// save last calculated reserve size
s.lastRSize.Store(currentSize)
rate := s.syncer.SyncRate()
s.logger.Info("depthmonitor: state", "size", currentSize, "radius", radius, "sync_rate", fmt.Sprintf("%.2f ch/s", rate))
if currentSize > targetSize {
continue
}
// if historical syncing rate is at zero, we proactively decrease the storage radius to allow nodes to widen their neighbourhoods
if rate == 0 && s.topology.PeersCount(topologyDriver.Filter{}) != 0 {
err = s.bs.SetStorageRadius(func(radius uint8) uint8 {
if radius > s.minimumRadius {
radius--
s.logger.Info("depthmonitor: reducing storage depth", "depth", radius)
}
return radius
})
if err != nil {
s.logger.Error(err, "depthmonitor: batchstore set storage radius")
}
}
}
}
func (s *Service) IsFullySynced() bool {
return s.syncer.SyncRate() == 0 && s.lastRSize.Load() > s.reserve.ReserveCapacity()*4/10
}
func (s *Service) Close() error {
close(s.quit)
select {
case <-s.stopped:
return nil
case <-time.After(5 * time.Second):
return errors.New("stopping depthmonitor with ongoing worker goroutine")
}
}