-
Notifications
You must be signed in to change notification settings - Fork 337
/
status.go
198 lines (167 loc) · 4.83 KB
/
status.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
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2023 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 status
import (
"context"
"fmt"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/status/internal/pb"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "status"
const (
protocolName = "status"
protocolVersion = "1.1.0"
streamName = "status"
)
// Snapshot is the current snapshot of the system.
type Snapshot pb.Snapshot
// SyncReporter defines the interface to report syncing rate.
type SyncReporter interface {
SyncRate() float64
}
// Reserve defines the reserve storage related information required.
type Reserve interface {
ReserveSize() int
StorageRadius() uint8
}
type topologyDriver interface {
topology.PeerIterator
IsReachable() bool
}
// Service is the status service.
type Service struct {
logger log.Logger
streamer p2p.Streamer
topologyDriver topologyDriver
beeMode string
reserve Reserve
sync SyncReporter
commitment postage.CommitmentGetter
}
// NewService creates a new status service.
func NewService(
logger log.Logger,
streamer p2p.Streamer,
topology topologyDriver,
beeMode string,
commitment postage.CommitmentGetter,
reserve Reserve,
) *Service {
return &Service{
logger: logger.WithName(loggerName).Register(),
streamer: streamer,
topologyDriver: topology,
beeMode: beeMode,
commitment: commitment,
reserve: reserve,
}
}
// 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
)
if s.reserve != nil {
storageRadius = s.reserve.StorageRadius()
reserveSize = uint64(s.reserve.ReserveSize())
}
if s.sync != nil {
syncRate = s.sync.SyncRate()
}
commitment, err := s.commitment.Commitment()
if err != nil {
return nil, fmt.Errorf("batchstore commitment: %w", err)
}
err = s.topologyDriver.EachConnectedPeer(
func(_ swarm.Address, po uint8) (bool, bool, error) {
connectedPeers++
if po >= storageRadius {
neighborhoodSize++
}
return false, false, nil
},
topology.Select{},
)
if err != nil {
return nil, fmt.Errorf("iterate connected peers: %w", err)
}
return &Snapshot{
BeeMode: s.beeMode,
ReserveSize: reserveSize,
PullsyncRate: syncRate,
StorageRadius: uint32(storageRadius),
ConnectedPeers: connectedPeers,
NeighborhoodSize: neighborhoodSize,
BatchCommitment: commitment,
IsReachable: s.topologyDriver.IsReachable(),
}, nil
}
// PeerSnapshot sends request for status snapshot to the peer.
func (s *Service) PeerSnapshot(ctx context.Context, peer swarm.Address) (*Snapshot, error) {
stream, err := s.streamer.NewStream(ctx, peer, nil, protocolName, protocolVersion, streamName)
if err != nil {
return nil, fmt.Errorf("new stream: %w", err)
}
defer func() {
go stream.FullClose()
}()
w, r := protobuf.NewWriterAndReader(stream)
if err := w.WriteMsgWithContext(ctx, new(pb.Get)); err != nil {
return nil, fmt.Errorf("write message failed: %w", err)
}
ss := new(pb.Snapshot)
if err := r.ReadMsgWithContext(ctx, ss); err != nil {
return nil, fmt.Errorf("read message failed: %w", err)
}
return (*Snapshot)(ss), nil
}
// Protocol returns the protocol specification.
func (s *Service) Protocol() p2p.ProtocolSpec {
return p2p.ProtocolSpec{
Name: protocolName,
Version: protocolVersion,
StreamSpecs: []p2p.StreamSpec{{
Name: streamName,
Handler: s.handler,
}},
}
}
// handler handles the status stream request/response.
func (s *Service) handler(ctx context.Context, _ p2p.Peer, stream p2p.Stream) error {
loggerV2 := s.logger.V(2).Register()
w, r := protobuf.NewWriterAndReader(stream)
defer func() {
if err := stream.FullClose(); err != nil {
loggerV2.Debug("stream full close failed: %v", "error", err)
}
}()
var msgGet pb.Get
if err := r.ReadMsgWithContext(ctx, &msgGet); err != nil {
loggerV2.Debug("read message failed", "error", err)
return fmt.Errorf("read message: %w", err)
}
snapshot, err := s.LocalSnapshot()
if err != nil {
loggerV2.Debug("local snapshot failed", "error", err)
return fmt.Errorf("local snapshot: %w", err)
}
if err := w.WriteMsgWithContext(ctx, (*pb.Snapshot)(snapshot)); err != nil {
loggerV2.Debug("write message failed", "error", err)
return fmt.Errorf("write message: %w", err)
}
return nil
}
func (s *Service) SetSync(sync SyncReporter) {
s.sync = sync
}