Skip to content

Commit 525116d

Browse files
zsfelfoldifjl
authored andcommitted
les: implement request distributor, fix blocking issues (bnb-chain#3660)
* les: implement request distributor, fix blocking issues * core: moved header validation before chain mutex lock
1 parent 1c1dc0e commit 525116d

19 files changed

Lines changed: 877 additions & 322 deletions

‎core/blockchain.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,11 @@ Error: %v
13131313
// of the header retrieval mechanisms already need to verify nonces, as well as
13141314
// because nonces can be verified sparsely, not needing to check each.
13151315
func (self *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
1316+
start := time.Now()
1317+
if i, err := self.hc.ValidateHeaderChain(chain, checkFreq); err != nil {
1318+
return i, err
1319+
}
1320+
13161321
// Make sure only one thread manipulates the chain at once
13171322
self.chainmu.Lock()
13181323
defer self.chainmu.Unlock()
@@ -1328,7 +1333,7 @@ func (self *BlockChain) InsertHeaderChain(chain []*types.Header, checkFreq int)
13281333
return err
13291334
}
13301335

1331-
return self.hc.InsertHeaderChain(chain, checkFreq, whFunc)
1336+
return self.hc.InsertHeaderChain(chain, whFunc, start)
13321337
}
13331338

13341339
// writeHeader writes a header into the local chain, given that its parent is

‎core/headerchain.go‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ type WhCallback func(*types.Header) error
219219
// should be done or not. The reason behind the optional check is because some
220220
// of the header retrieval mechanisms already need to verfy nonces, as well as
221221
// because nonces can be verified sparsely, not needing to check each.
222-
func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, writeHeader WhCallback) (int, error) {
222+
223+
func (hc *HeaderChain) ValidateHeaderChain(chain []*types.Header, checkFreq int) (int, error) {
223224
// Do a sanity check that the provided chain is actually ordered and linked
224225
for i := 1; i < len(chain); i++ {
225226
if chain[i].Number.Uint64() != chain[i-1].Number.Uint64()+1 || chain[i].ParentHash != chain[i-1].Hash() {
@@ -231,9 +232,6 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, w
231232
chain[i-1].Hash().Bytes()[:4], i, chain[i].Number, chain[i].Hash().Bytes()[:4], chain[i].ParentHash[:4])
232233
}
233234
}
234-
// Collect some import statistics to report on
235-
stats := struct{ processed, ignored int }{}
236-
start := time.Now()
237235

238236
// Generate the list of headers that should be POW verified
239237
verify := make([]bool, len(chain))
@@ -309,6 +307,13 @@ func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, checkFreq int, w
309307
}
310308
}
311309
}
310+
311+
return 0, nil
312+
}
313+
314+
func (hc *HeaderChain) InsertHeaderChain(chain []*types.Header, writeHeader WhCallback, start time.Time) (int, error) {
315+
// Collect some import statistics to report on
316+
stats := struct{ processed, ignored int }{}
312317
// All headers passed verification, import them into the database
313318
for i, header := range chain {
314319
// Short circuit insertion if shutting down

‎les/backend.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
107107
if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil {
108108
return nil, err
109109
}
110+
relay.ps = eth.protocolManager.peers
111+
relay.reqDist = eth.protocolManager.reqDist
110112

111113
eth.ApiBackend = &LesApiBackend{eth, nil}
112114
eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend)

‎les/distributor.go‎

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package light implements on-demand retrieval capable state and chain objects
18+
// for the Ethereum Light Client.
19+
package les
20+
21+
import (
22+
"container/list"
23+
"errors"
24+
"sync"
25+
"time"
26+
)
27+
28+
// ErrNoPeers is returned if no peers capable of serving a queued request are available
29+
var ErrNoPeers = errors.New("no suitable peers available")
30+
31+
// requestDistributor implements a mechanism that distributes requests to
32+
// suitable peers, obeying flow control rules and prioritizing them in creation
33+
// order (even when a resend is necessary).
34+
type requestDistributor struct {
35+
reqQueue *list.List
36+
lastReqOrder uint64
37+
stopChn, loopChn chan struct{}
38+
loopNextSent bool
39+
lock sync.Mutex
40+
41+
getAllPeers func() map[distPeer]struct{}
42+
}
43+
44+
// distPeer is an LES server peer interface for the request distributor.
45+
// waitBefore returns either the necessary waiting time before sending a request
46+
// with the given upper estimated cost or the estimated remaining relative buffer
47+
// value after sending such a request (in which case the request can be sent
48+
// immediately). At least one of these values is always zero.
49+
type distPeer interface {
50+
waitBefore(uint64) (time.Duration, float64)
51+
canQueue() bool
52+
queueSend(f func())
53+
}
54+
55+
// distReq is the request abstraction used by the distributor. It is based on
56+
// three callback functions:
57+
// - getCost returns the upper estimate of the cost of sending the request to a given peer
58+
// - canSend tells if the server peer is suitable to serve the request
59+
// - request prepares sending the request to the given peer and returns a function that
60+
// does the actual sending. Request order should be preserved but the callback itself should not
61+
// block until it is sent because other peers might still be able to receive requests while
62+
// one of them is blocking. Instead, the returned function is put in the peer's send queue.
63+
type distReq struct {
64+
getCost func(distPeer) uint64
65+
canSend func(distPeer) bool
66+
request func(distPeer) func()
67+
68+
reqOrder uint64
69+
sentChn chan distPeer
70+
element *list.Element
71+
}
72+
73+
// newRequestDistributor creates a new request distributor
74+
func newRequestDistributor(getAllPeers func() map[distPeer]struct{}, stopChn chan struct{}) *requestDistributor {
75+
r := &requestDistributor{
76+
reqQueue: list.New(),
77+
loopChn: make(chan struct{}, 2),
78+
stopChn: stopChn,
79+
getAllPeers: getAllPeers,
80+
}
81+
go r.loop()
82+
return r
83+
}
84+
85+
// distMaxWait is the maximum waiting time after which further necessary waiting
86+
// times are recalculated based on new feedback from the servers
87+
const distMaxWait = time.Millisecond * 10
88+
89+
// main event loop
90+
func (d *requestDistributor) loop() {
91+
for {
92+
select {
93+
case <-d.stopChn:
94+
d.lock.Lock()
95+
elem := d.reqQueue.Front()
96+
for elem != nil {
97+
close(elem.Value.(*distReq).sentChn)
98+
elem = elem.Next()
99+
}
100+
d.lock.Unlock()
101+
return
102+
case <-d.loopChn:
103+
d.lock.Lock()
104+
d.loopNextSent = false
105+
loop:
106+
for {
107+
peer, req, wait := d.nextRequest()
108+
if req != nil && wait == 0 {
109+
chn := req.sentChn // save sentChn because remove sets it to nil
110+
d.remove(req)
111+
send := req.request(peer)
112+
if send != nil {
113+
peer.queueSend(send)
114+
}
115+
chn <- peer
116+
close(chn)
117+
} else {
118+
if wait == 0 {
119+
// no request to send and nothing to wait for; the next
120+
// queued request will wake up the loop
121+
break loop
122+
}
123+
d.loopNextSent = true // a "next" signal has been sent, do not send another one until this one has been received
124+
if wait > distMaxWait {
125+
// waiting times may be reduced by incoming request replies, if it is too long, recalculate it periodically
126+
wait = distMaxWait
127+
}
128+
go func() {
129+
time.Sleep(wait)
130+
d.loopChn <- struct{}{}
131+
}()
132+
break loop
133+
}
134+
}
135+
d.lock.Unlock()
136+
}
137+
}
138+
}
139+
140+
// selectPeerItem represents a peer to be selected for a request by weightedRandomSelect
141+
type selectPeerItem struct {
142+
peer distPeer
143+
req *distReq
144+
weight int64
145+
}
146+
147+
// Weight implements wrsItem interface
148+
func (sp selectPeerItem) Weight() int64 {
149+
return sp.weight
150+
}
151+
152+
// nextRequest returns the next possible request from any peer, along with the
153+
// associated peer and necessary waiting time
154+
func (d *requestDistributor) nextRequest() (distPeer, *distReq, time.Duration) {
155+
peers := d.getAllPeers()
156+
157+
elem := d.reqQueue.Front()
158+
var (
159+
bestPeer distPeer
160+
bestReq *distReq
161+
bestWait time.Duration
162+
sel *weightedRandomSelect
163+
)
164+
165+
for (len(peers) > 0 || elem == d.reqQueue.Front()) && elem != nil {
166+
req := elem.Value.(*distReq)
167+
canSend := false
168+
for peer, _ := range peers {
169+
if peer.canQueue() && req.canSend(peer) {
170+
canSend = true
171+
cost := req.getCost(peer)
172+
wait, bufRemain := peer.waitBefore(cost)
173+
if wait == 0 {
174+
if sel == nil {
175+
sel = newWeightedRandomSelect()
176+
}
177+
sel.update(selectPeerItem{peer: peer, req: req, weight: int64(bufRemain*1000000) + 1})
178+
} else {
179+
if bestReq == nil || wait < bestWait {
180+
bestPeer = peer
181+
bestReq = req
182+
bestWait = wait
183+
}
184+
}
185+
delete(peers, peer)
186+
}
187+
}
188+
next := elem.Next()
189+
if !canSend && elem == d.reqQueue.Front() {
190+
close(req.sentChn)
191+
d.remove(req)
192+
}
193+
elem = next
194+
}
195+
196+
if sel != nil {
197+
c := sel.choose().(selectPeerItem)
198+
return c.peer, c.req, 0
199+
}
200+
return bestPeer, bestReq, bestWait
201+
}
202+
203+
// queue adds a request to the distribution queue, returns a channel where the
204+
// receiving peer is sent once the request has been sent (request callback returned).
205+
// If the request is cancelled or timed out without suitable peers, the channel is
206+
// closed without sending any peer references to it.
207+
func (d *requestDistributor) queue(r *distReq) chan distPeer {
208+
d.lock.Lock()
209+
defer d.lock.Unlock()
210+
211+
if r.reqOrder == 0 {
212+
d.lastReqOrder++
213+
r.reqOrder = d.lastReqOrder
214+
}
215+
216+
back := d.reqQueue.Back()
217+
if back == nil || r.reqOrder > back.Value.(*distReq).reqOrder {
218+
r.element = d.reqQueue.PushBack(r)
219+
} else {
220+
before := d.reqQueue.Front()
221+
for before.Value.(*distReq).reqOrder < r.reqOrder {
222+
before = before.Next()
223+
}
224+
r.element = d.reqQueue.InsertBefore(r, before)
225+
}
226+
227+
if !d.loopNextSent {
228+
d.loopNextSent = true
229+
d.loopChn <- struct{}{}
230+
}
231+
232+
r.sentChn = make(chan distPeer, 1)
233+
return r.sentChn
234+
}
235+
236+
// cancel removes a request from the queue if it has not been sent yet (returns
237+
// false if it has been sent already). It is guaranteed that the callback functions
238+
// will not be called after cancel returns.
239+
func (d *requestDistributor) cancel(r *distReq) bool {
240+
d.lock.Lock()
241+
defer d.lock.Unlock()
242+
243+
if r.sentChn == nil {
244+
return false
245+
}
246+
247+
close(r.sentChn)
248+
d.remove(r)
249+
return true
250+
}
251+
252+
// remove removes a request from the queue
253+
func (d *requestDistributor) remove(r *distReq) {
254+
r.sentChn = nil
255+
if r.element != nil {
256+
d.reqQueue.Remove(r.element)
257+
r.element = nil
258+
}
259+
}

0 commit comments

Comments
 (0)