gitee.com/liu-zhao234568/cntest@v1.0.0/les/retrieve.go (about)

     1  // Copyright 2017 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 les
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"sync"
    23  	"time"
    24  
    25  	"gitee.com/liu-zhao234568/cntest/light"
    26  )
    27  
    28  var (
    29  	retryQueue         = time.Millisecond * 100
    30  	hardRequestTimeout = time.Second * 10
    31  )
    32  
    33  // retrieveManager is a layer on top of requestDistributor which takes care of
    34  // matching replies by request ID and handles timeouts and resends if necessary.
    35  type retrieveManager struct {
    36  	dist               *requestDistributor
    37  	peers              *serverPeerSet
    38  	softRequestTimeout func() time.Duration
    39  
    40  	lock     sync.RWMutex
    41  	sentReqs map[uint64]*sentReq
    42  }
    43  
    44  // validatorFunc is a function that processes a reply message
    45  type validatorFunc func(distPeer, *Msg) error
    46  
    47  // sentReq represents a request sent and tracked by retrieveManager
    48  type sentReq struct {
    49  	rm       *retrieveManager
    50  	req      *distReq
    51  	id       uint64
    52  	validate validatorFunc
    53  
    54  	eventsCh chan reqPeerEvent
    55  	stopCh   chan struct{}
    56  	stopped  bool
    57  	err      error
    58  
    59  	lock   sync.RWMutex // protect access to sentTo map
    60  	sentTo map[distPeer]sentReqToPeer
    61  
    62  	lastReqQueued bool     // last request has been queued but not sent
    63  	lastReqSentTo distPeer // if not nil then last request has been sent to given peer but not timed out
    64  	reqSrtoCount  int      // number of requests that reached soft (but not hard) timeout
    65  }
    66  
    67  // sentReqToPeer notifies the request-from-peer goroutine (tryRequest) about a response
    68  // delivered by the given peer. Only one delivery is allowed per request per peer,
    69  // after which delivered is set to true, the validity of the response is sent on the
    70  // valid channel and no more responses are accepted.
    71  type sentReqToPeer struct {
    72  	delivered, frozen bool
    73  	event             chan int
    74  }
    75  
    76  // reqPeerEvent is sent by the request-from-peer goroutine (tryRequest) to the
    77  // request state machine (retrieveLoop) through the eventsCh channel.
    78  type reqPeerEvent struct {
    79  	event int
    80  	peer  distPeer
    81  }
    82  
    83  const (
    84  	rpSent = iota // if peer == nil, not sent (no suitable peers)
    85  	rpSoftTimeout
    86  	rpHardTimeout
    87  	rpDeliveredValid
    88  	rpDeliveredInvalid
    89  	rpNotDelivered
    90  )
    91  
    92  // newRetrieveManager creates the retrieve manager
    93  func newRetrieveManager(peers *serverPeerSet, dist *requestDistributor, srto func() time.Duration) *retrieveManager {
    94  	return &retrieveManager{
    95  		peers:              peers,
    96  		dist:               dist,
    97  		sentReqs:           make(map[uint64]*sentReq),
    98  		softRequestTimeout: srto,
    99  	}
   100  }
   101  
   102  // retrieve sends a request (to multiple peers if necessary) and waits for an answer
   103  // that is delivered through the deliver function and successfully validated by the
   104  // validator callback. It returns when a valid answer is delivered or the context is
   105  // cancelled.
   106  func (rm *retrieveManager) retrieve(ctx context.Context, reqID uint64, req *distReq, val validatorFunc, shutdown chan struct{}) error {
   107  	sentReq := rm.sendReq(reqID, req, val)
   108  	select {
   109  	case <-sentReq.stopCh:
   110  	case <-ctx.Done():
   111  		sentReq.stop(ctx.Err())
   112  	case <-shutdown:
   113  		sentReq.stop(fmt.Errorf("client is shutting down"))
   114  	}
   115  	return sentReq.getError()
   116  }
   117  
   118  // sendReq starts a process that keeps trying to retrieve a valid answer for a
   119  // request from any suitable peers until stopped or succeeded.
   120  func (rm *retrieveManager) sendReq(reqID uint64, req *distReq, val validatorFunc) *sentReq {
   121  	r := &sentReq{
   122  		rm:       rm,
   123  		req:      req,
   124  		id:       reqID,
   125  		sentTo:   make(map[distPeer]sentReqToPeer),
   126  		stopCh:   make(chan struct{}),
   127  		eventsCh: make(chan reqPeerEvent, 10),
   128  		validate: val,
   129  	}
   130  
   131  	canSend := req.canSend
   132  	req.canSend = func(p distPeer) bool {
   133  		// add an extra check to canSend: the request has not been sent to the same peer before
   134  		r.lock.RLock()
   135  		_, sent := r.sentTo[p]
   136  		r.lock.RUnlock()
   137  		return !sent && canSend(p)
   138  	}
   139  
   140  	request := req.request
   141  	req.request = func(p distPeer) func() {
   142  		// before actually sending the request, put an entry into the sentTo map
   143  		r.lock.Lock()
   144  		r.sentTo[p] = sentReqToPeer{delivered: false, frozen: false, event: make(chan int, 1)}
   145  		r.lock.Unlock()
   146  		return request(p)
   147  	}
   148  	rm.lock.Lock()
   149  	rm.sentReqs[reqID] = r
   150  	rm.lock.Unlock()
   151  
   152  	go r.retrieveLoop()
   153  	return r
   154  }
   155  
   156  // requested reports whether the request with given reqid is sent by the retriever.
   157  func (rm *retrieveManager) requested(reqId uint64) bool {
   158  	rm.lock.RLock()
   159  	defer rm.lock.RUnlock()
   160  
   161  	_, ok := rm.sentReqs[reqId]
   162  	return ok
   163  }
   164  
   165  // deliver is called by the LES protocol manager to deliver reply messages to waiting requests
   166  func (rm *retrieveManager) deliver(peer distPeer, msg *Msg) error {
   167  	rm.lock.RLock()
   168  	req, ok := rm.sentReqs[msg.ReqID]
   169  	rm.lock.RUnlock()
   170  
   171  	if ok {
   172  		return req.deliver(peer, msg)
   173  	}
   174  	return errResp(ErrUnexpectedResponse, "reqID = %v", msg.ReqID)
   175  }
   176  
   177  // frozen is called by the LES protocol manager when a server has suspended its service and we
   178  // should not expect an answer for the requests already sent there
   179  func (rm *retrieveManager) frozen(peer distPeer) {
   180  	rm.lock.RLock()
   181  	defer rm.lock.RUnlock()
   182  
   183  	for _, req := range rm.sentReqs {
   184  		req.frozen(peer)
   185  	}
   186  }
   187  
   188  // reqStateFn represents a state of the retrieve loop state machine
   189  type reqStateFn func() reqStateFn
   190  
   191  // retrieveLoop is the retrieval state machine event loop
   192  func (r *sentReq) retrieveLoop() {
   193  	go r.tryRequest()
   194  	r.lastReqQueued = true
   195  	state := r.stateRequesting
   196  
   197  	for state != nil {
   198  		state = state()
   199  	}
   200  
   201  	r.rm.lock.Lock()
   202  	delete(r.rm.sentReqs, r.id)
   203  	r.rm.lock.Unlock()
   204  }
   205  
   206  // stateRequesting: a request has been queued or sent recently; when it reaches soft timeout,
   207  // a new request is sent to a new peer
   208  func (r *sentReq) stateRequesting() reqStateFn {
   209  	select {
   210  	case ev := <-r.eventsCh:
   211  		r.update(ev)
   212  		switch ev.event {
   213  		case rpSent:
   214  			if ev.peer == nil {
   215  				// request send failed, no more suitable peers
   216  				if r.waiting() {
   217  					// we are already waiting for sent requests which may succeed so keep waiting
   218  					return r.stateNoMorePeers
   219  				}
   220  				// nothing to wait for, no more peers to ask, return with error
   221  				r.stop(light.ErrNoPeers)
   222  				// no need to go to stopped state because waiting() already returned false
   223  				return nil
   224  			}
   225  		case rpSoftTimeout:
   226  			// last request timed out, try asking a new peer
   227  			go r.tryRequest()
   228  			r.lastReqQueued = true
   229  			return r.stateRequesting
   230  		case rpDeliveredInvalid, rpNotDelivered:
   231  			// if it was the last sent request (set to nil by update) then start a new one
   232  			if !r.lastReqQueued && r.lastReqSentTo == nil {
   233  				go r.tryRequest()
   234  				r.lastReqQueued = true
   235  			}
   236  			return r.stateRequesting
   237  		case rpDeliveredValid:
   238  			r.stop(nil)
   239  			return r.stateStopped
   240  		}
   241  		return r.stateRequesting
   242  	case <-r.stopCh:
   243  		return r.stateStopped
   244  	}
   245  }
   246  
   247  // stateNoMorePeers: could not send more requests because no suitable peers are available.
   248  // Peers may become suitable for a certain request later or new peers may appear so we
   249  // keep trying.
   250  func (r *sentReq) stateNoMorePeers() reqStateFn {
   251  	select {
   252  	case <-time.After(retryQueue):
   253  		go r.tryRequest()
   254  		r.lastReqQueued = true
   255  		return r.stateRequesting
   256  	case ev := <-r.eventsCh:
   257  		r.update(ev)
   258  		if ev.event == rpDeliveredValid {
   259  			r.stop(nil)
   260  			return r.stateStopped
   261  		}
   262  		if r.waiting() {
   263  			return r.stateNoMorePeers
   264  		}
   265  		r.stop(light.ErrNoPeers)
   266  		return nil
   267  	case <-r.stopCh:
   268  		return r.stateStopped
   269  	}
   270  }
   271  
   272  // stateStopped: request succeeded or cancelled, just waiting for some peers
   273  // to either answer or time out hard
   274  func (r *sentReq) stateStopped() reqStateFn {
   275  	for r.waiting() {
   276  		r.update(<-r.eventsCh)
   277  	}
   278  	return nil
   279  }
   280  
   281  // update updates the queued/sent flags and timed out peers counter according to the event
   282  func (r *sentReq) update(ev reqPeerEvent) {
   283  	switch ev.event {
   284  	case rpSent:
   285  		r.lastReqQueued = false
   286  		r.lastReqSentTo = ev.peer
   287  	case rpSoftTimeout:
   288  		r.lastReqSentTo = nil
   289  		r.reqSrtoCount++
   290  	case rpHardTimeout:
   291  		r.reqSrtoCount--
   292  	case rpDeliveredValid, rpDeliveredInvalid, rpNotDelivered:
   293  		if ev.peer == r.lastReqSentTo {
   294  			r.lastReqSentTo = nil
   295  		} else {
   296  			r.reqSrtoCount--
   297  		}
   298  	}
   299  }
   300  
   301  // waiting returns true if the retrieval mechanism is waiting for an answer from
   302  // any peer
   303  func (r *sentReq) waiting() bool {
   304  	return r.lastReqQueued || r.lastReqSentTo != nil || r.reqSrtoCount > 0
   305  }
   306  
   307  // tryRequest tries to send the request to a new peer and waits for it to either
   308  // succeed or time out if it has been sent. It also sends the appropriate reqPeerEvent
   309  // messages to the request's event channel.
   310  func (r *sentReq) tryRequest() {
   311  	sent := r.rm.dist.queue(r.req)
   312  	var p distPeer
   313  	select {
   314  	case p = <-sent:
   315  	case <-r.stopCh:
   316  		if r.rm.dist.cancel(r.req) {
   317  			p = nil
   318  		} else {
   319  			p = <-sent
   320  		}
   321  	}
   322  
   323  	r.eventsCh <- reqPeerEvent{rpSent, p}
   324  	if p == nil {
   325  		return
   326  	}
   327  
   328  	hrto := false
   329  
   330  	r.lock.RLock()
   331  	s, ok := r.sentTo[p]
   332  	r.lock.RUnlock()
   333  	if !ok {
   334  		panic(nil)
   335  	}
   336  
   337  	defer func() {
   338  		pp, ok := p.(*serverPeer)
   339  		if hrto && ok {
   340  			pp.Log().Debug("Request timed out hard")
   341  			if r.rm.peers != nil {
   342  				r.rm.peers.unregister(pp.id)
   343  			}
   344  		}
   345  	}()
   346  
   347  	select {
   348  	case event := <-s.event:
   349  		if event == rpNotDelivered {
   350  			r.lock.Lock()
   351  			delete(r.sentTo, p)
   352  			r.lock.Unlock()
   353  		}
   354  		r.eventsCh <- reqPeerEvent{event, p}
   355  		return
   356  	case <-time.After(r.rm.softRequestTimeout()):
   357  		r.eventsCh <- reqPeerEvent{rpSoftTimeout, p}
   358  	}
   359  
   360  	select {
   361  	case event := <-s.event:
   362  		if event == rpNotDelivered {
   363  			r.lock.Lock()
   364  			delete(r.sentTo, p)
   365  			r.lock.Unlock()
   366  		}
   367  		r.eventsCh <- reqPeerEvent{event, p}
   368  	case <-time.After(hardRequestTimeout):
   369  		hrto = true
   370  		r.eventsCh <- reqPeerEvent{rpHardTimeout, p}
   371  	}
   372  }
   373  
   374  // deliver a reply belonging to this request
   375  func (r *sentReq) deliver(peer distPeer, msg *Msg) error {
   376  	r.lock.Lock()
   377  	defer r.lock.Unlock()
   378  
   379  	s, ok := r.sentTo[peer]
   380  	if !ok || s.delivered {
   381  		return errResp(ErrUnexpectedResponse, "reqID = %v", msg.ReqID)
   382  	}
   383  	if s.frozen {
   384  		return nil
   385  	}
   386  	valid := r.validate(peer, msg) == nil
   387  	r.sentTo[peer] = sentReqToPeer{delivered: true, frozen: false, event: s.event}
   388  	if valid {
   389  		s.event <- rpDeliveredValid
   390  	} else {
   391  		s.event <- rpDeliveredInvalid
   392  	}
   393  	if !valid {
   394  		return errResp(ErrInvalidResponse, "reqID = %v", msg.ReqID)
   395  	}
   396  	return nil
   397  }
   398  
   399  // frozen sends a "not delivered" event to the peer event channel belonging to the
   400  // given peer if the request has been sent there, causing the state machine to not
   401  // expect an answer and potentially even send the request to the same peer again
   402  // when canSend allows it.
   403  func (r *sentReq) frozen(peer distPeer) {
   404  	r.lock.Lock()
   405  	defer r.lock.Unlock()
   406  
   407  	s, ok := r.sentTo[peer]
   408  	if ok && !s.delivered && !s.frozen {
   409  		r.sentTo[peer] = sentReqToPeer{delivered: false, frozen: true, event: s.event}
   410  		s.event <- rpNotDelivered
   411  	}
   412  }
   413  
   414  // stop stops the retrieval process and sets an error code that will be returned
   415  // by getError
   416  func (r *sentReq) stop(err error) {
   417  	r.lock.Lock()
   418  	if !r.stopped {
   419  		r.stopped = true
   420  		r.err = err
   421  		close(r.stopCh)
   422  	}
   423  	r.lock.Unlock()
   424  }
   425  
   426  // getError returns any retrieval error (either internally generated or set by the
   427  // stop function) after stopCh has been closed
   428  func (r *sentReq) getError() error {
   429  	return r.err
   430  }