This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
feat(sessions): add rebroadcasting, search backoff #133
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49a96fb
feat(sessions): add rebroadcasting, search backoff
hannahhoward 3104b2d
fix(sessions): fix data race in test
hannahhoward d948827
fix(sessions): consecutive ticks only when wants present
hannahhoward e2e3343
feat(session): minor code clean-up
hannahhoward 92a8279
feat(session): allow configuring delays per instance
hannahhoward File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ package session | |
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"time" | ||
|
||
lru "github.com/hashicorp/golang-lru" | ||
bsgetter "github.com/ipfs/go-bitswap/getter" | ||
notifications "github.com/ipfs/go-bitswap/notifications" | ||
blocks "github.com/ipfs/go-block-format" | ||
cid "github.com/ipfs/go-cid" | ||
delay "github.com/ipfs/go-ipfs-delay" | ||
logging "github.com/ipfs/go-log" | ||
peer "github.com/libp2p/go-libp2p-core/peer" | ||
loggables "github.com/libp2p/go-libp2p-loggables" | ||
|
@@ -75,14 +77,18 @@ type Session struct { | |
tickDelayReqs chan time.Duration | ||
|
||
// do not touch outside run loop | ||
tofetch *cidQueue | ||
interest *lru.Cache | ||
pastWants *cidQueue | ||
liveWants map[cid.Cid]time.Time | ||
tick *time.Timer | ||
baseTickDelay time.Duration | ||
latTotal time.Duration | ||
fetchcnt int | ||
tofetch *cidQueue | ||
interest *lru.Cache | ||
pastWants *cidQueue | ||
liveWants map[cid.Cid]time.Time | ||
tick *time.Timer | ||
rebroadcast *time.Timer | ||
baseTickDelay time.Duration | ||
latTotal time.Duration | ||
fetchcnt int | ||
consecutiveTicks int | ||
provSearchDelay time.Duration | ||
rebroadcastDelay delay.D | ||
// identifiers | ||
notif notifications.PubSub | ||
uuid logging.Loggable | ||
|
@@ -91,25 +97,33 @@ type Session struct { | |
|
||
// New creates a new bitswap session whose lifetime is bounded by the | ||
// given context. | ||
func New(ctx context.Context, id uint64, wm WantManager, pm PeerManager, srs RequestSplitter) *Session { | ||
func New(ctx context.Context, | ||
id uint64, | ||
wm WantManager, | ||
pm PeerManager, | ||
srs RequestSplitter, | ||
provSearchDelay time.Duration, | ||
rebroadcastDelay delay.D) *Session { | ||
s := &Session{ | ||
liveWants: make(map[cid.Cid]time.Time), | ||
newReqs: make(chan []cid.Cid), | ||
cancelKeys: make(chan []cid.Cid), | ||
tofetch: newCidQueue(), | ||
pastWants: newCidQueue(), | ||
interestReqs: make(chan interestReq), | ||
latencyReqs: make(chan chan time.Duration), | ||
tickDelayReqs: make(chan time.Duration), | ||
ctx: ctx, | ||
wm: wm, | ||
pm: pm, | ||
srs: srs, | ||
incoming: make(chan blkRecv), | ||
notif: notifications.New(), | ||
uuid: loggables.Uuid("GetBlockRequest"), | ||
baseTickDelay: time.Millisecond * 500, | ||
id: id, | ||
liveWants: make(map[cid.Cid]time.Time), | ||
newReqs: make(chan []cid.Cid), | ||
cancelKeys: make(chan []cid.Cid), | ||
tofetch: newCidQueue(), | ||
pastWants: newCidQueue(), | ||
interestReqs: make(chan interestReq), | ||
latencyReqs: make(chan chan time.Duration), | ||
tickDelayReqs: make(chan time.Duration), | ||
ctx: ctx, | ||
wm: wm, | ||
pm: pm, | ||
srs: srs, | ||
incoming: make(chan blkRecv), | ||
notif: notifications.New(), | ||
uuid: loggables.Uuid("GetBlockRequest"), | ||
baseTickDelay: time.Millisecond * 500, | ||
id: id, | ||
provSearchDelay: provSearchDelay, | ||
rebroadcastDelay: rebroadcastDelay, | ||
} | ||
|
||
cache, _ := lru.New(2048) | ||
|
@@ -222,17 +236,11 @@ func (s *Session) SetBaseTickDelay(baseTickDelay time.Duration) { | |
} | ||
} | ||
|
||
var provSearchDelay = time.Second | ||
|
||
// SetProviderSearchDelay overwrites the global provider search delay | ||
func SetProviderSearchDelay(newProvSearchDelay time.Duration) { | ||
provSearchDelay = newProvSearchDelay | ||
} | ||
|
||
// Session run loop -- everything function below here should not be called | ||
// of this loop | ||
func (s *Session) run(ctx context.Context) { | ||
s.tick = time.NewTimer(provSearchDelay) | ||
s.tick = time.NewTimer(s.provSearchDelay) | ||
s.rebroadcast = time.NewTimer(s.rebroadcastDelay.Get()) | ||
for { | ||
select { | ||
case blk := <-s.incoming: | ||
|
@@ -247,6 +255,8 @@ func (s *Session) run(ctx context.Context) { | |
s.handleCancel(keys) | ||
case <-s.tick.C: | ||
s.handleTick(ctx) | ||
case <-s.rebroadcast.C: | ||
s.handleRebroadcast(ctx) | ||
case lwchk := <-s.interestReqs: | ||
lwchk.resp <- s.cidIsWanted(lwchk.c) | ||
case resp := <-s.latencyReqs: | ||
|
@@ -310,12 +320,42 @@ func (s *Session) handleTick(ctx context.Context) { | |
s.pm.RecordPeerRequests(nil, live) | ||
s.wm.WantBlocks(ctx, live, nil, s.id) | ||
|
||
if len(live) > 0 { | ||
// do no find providers on consecutive ticks | ||
// -- just rely on periodic rebroadcast | ||
if len(live) > 0 && (s.consecutiveTicks == 0) { | ||
s.pm.FindMorePeers(ctx, live[0]) | ||
} | ||
s.resetTick() | ||
|
||
if len(s.liveWants) > 0 { | ||
s.consecutiveTicks++ | ||
} | ||
} | ||
|
||
func (s *Session) handleRebroadcast(ctx context.Context) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't actually rebroadcast anything, does it? We should probably just call this something like |
||
|
||
if len(s.liveWants) == 0 { | ||
return | ||
} | ||
|
||
// TODO: come up with a better strategy for determining when to search | ||
// for new providers for blocks. | ||
s.pm.FindMorePeers(ctx, s.randomLiveWant()) | ||
|
||
s.rebroadcast.Reset(s.rebroadcastDelay.Get()) | ||
} | ||
|
||
func (s *Session) randomLiveWant() cid.Cid { | ||
i := rand.Intn(len(s.liveWants)) | ||
// picking a random live want | ||
for k := range s.liveWants { | ||
if i == 0 { | ||
return k | ||
} | ||
i-- | ||
} | ||
return cid.Cid{} | ||
} | ||
func (s *Session) handleShutdown() { | ||
s.tick.Stop() | ||
s.notif.Shutdown() | ||
|
@@ -347,6 +387,8 @@ func (s *Session) receiveBlock(ctx context.Context, blk blocks.Block) { | |
s.tofetch.Remove(c) | ||
} | ||
s.fetchcnt++ | ||
// we've received new wanted blocks, so future ticks are not consecutive | ||
s.consecutiveTicks = 0 | ||
s.notif.Publish(blk) | ||
|
||
toAdd := s.wantBudget() | ||
|
@@ -395,12 +437,15 @@ func (s *Session) averageLatency() time.Duration { | |
} | ||
|
||
func (s *Session) resetTick() { | ||
var tickDelay time.Duration | ||
if s.latTotal == 0 { | ||
s.tick.Reset(provSearchDelay) | ||
tickDelay = s.provSearchDelay | ||
} else { | ||
avLat := s.averageLatency() | ||
s.tick.Reset(s.baseTickDelay + (3 * avLat)) | ||
tickDelay = s.baseTickDelay + (3 * avLat) | ||
} | ||
tickDelay = tickDelay * time.Duration(1+s.consecutiveTicks) | ||
s.tick.Reset(tickDelay) | ||
} | ||
|
||
func (s *Session) wantBudget() int { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'm not sure using
delay.D
is all that useful here as we always just reuse the same initial delay.delay.D
is designed for random delays (we'd need to use a tickerand reset it withrebroadcast.Reset(s.rebroadcastDelay.NextWaitTime())
).