Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Bitswap Refactoring #140

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
// TODO(brian): perform this inside NewDHT factory method
dhtService.Handler = route // wire the handler to the service.

//Clarification- this is here because the bitswap strategy still has to be completely worked out
const alwaysSendToPeer = true // use YesManStrategy

exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer)

// TODO(brian): pass a context to initConnections
Expand Down
36 changes: 18 additions & 18 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,6 @@ import (
u "github.com/jbenet/go-ipfs/util"
)

// NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service
func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, nice bool) exchange.Interface {

networkAdapter := bsnet.NetMessageAdapter(s, nil)
bs := &bitswap{
blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(),
strategy: strategy.New(nice),
routing: directory,
sender: networkAdapter,
wantlist: u.NewKeySet(),
}
networkAdapter.SetDelegate(bs)

return bs
}

// bitswap instances implement the bitswap protocol.
type bitswap struct {

Expand All @@ -56,6 +38,24 @@ type bitswap struct {
wantlist u.KeySet
}

// NetMessageSession initializes a BitSwap session that communicates over the
// provided NetMessage service
func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, trusted bool) exchange.Interface {

networkAdapter := bsnet.NetMessageAdapter(s, nil)
bs := &bitswap{
blockstore: blockstore.NewBlockstore(d),
notifications: notifications.New(),
strategy: strategy.New(trusted),
routing: directory,
sender: networkAdapter,
wantlist: u.NewKeySet(),
}
networkAdapter.SetDelegate(bs)

return bs
}

// GetBlock attempts to retrieve a particular block from peers within the
// deadline enforced by the context
//
Expand Down
16 changes: 8 additions & 8 deletions exchange/bitswap/strategy/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ import (
// access/lookups.
type keySet map[u.Key]struct{}

func newLedger(p *peer.Peer, strategy strategyFunc) *ledger {
return &ledger{
wantList: keySet{},
Strategy: strategy,
Partner: p,
}
}

// ledger stores the data exchange relationship between two peers.
type ledger struct {
lock sync.RWMutex
Expand All @@ -45,6 +37,14 @@ type ledger struct {
Strategy strategyFunc
}

func newLedger(p *peer.Peer, strategy strategyFunc) *ledger {
return &ledger{
wantList: keySet{},
Strategy: strategy,
Partner: p,
}
}

func (l *ledger) ShouldSend() bool {
l.lock.Lock()
defer l.lock.Unlock()
Expand Down
16 changes: 7 additions & 9 deletions exchange/bitswap/strategy/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (

type strategyFunc func(*ledger) bool

// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause
// performance issues. Instead, instantiate a rand struct and use that to call
// Float64()
type debtRatio struct {
BytesSent uint64
BytesRecv uint64
}

func standardStrategy(l *ledger) bool {
return rand.Float64() <= probabilitySend(l.Accounting.Value())
r := rand.New(rand.NewSource(1234))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rand object we instantiate should be stored somewhere, possibily in the ledger. and should be constructed when the ledger is created.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the seed should be seeded by time, (or a random number from crypto/rand)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@llSourcell ... why did you introduce a constant seed? This is wrong. rand.Float64 will draw from the default source, which we can initialize with a random seed using rand.Seed.

return r.Float64() <= probabilitySend(l.Accounting.Value())
}

func yesManStrategy(l *ledger) bool {
Expand All @@ -24,11 +27,6 @@ func probabilitySend(ratio float64) float64 {
return 1 - y
}

type debtRatio struct {
BytesSent uint64
BytesRecv uint64
}

func (dr *debtRatio) Value() float64 {
return float64(dr.BytesSent) / float64(dr.BytesRecv+1)
}
27 changes: 13 additions & 14 deletions exchange/bitswap/strategy/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ import (
u "github.com/jbenet/go-ipfs/util"
)

type strategist struct {
ledgerMap
strategyFunc
}

// LedgerMap lists Ledgers by their Partner key.
type ledgerMap map[peerKey]*ledger

// FIXME share this externally
type peerKey u.Key

// TODO declare thread-safe datastore
// TODO niceness should be on a per-peer basis. Use-case: Certain peers are
// "trusted" and/or controlled by a single human user. The user may want for
// these peers to exchange data freely
func New(nice bool) Strategy {
func New(trusted bool) Strategy {
var stratFunc strategyFunc
if nice {
if trusted {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is misleading... trust has a very specific meaning in distributed systems. And this is not it. While this variable name choice is quite arbitrary, "niceness" is better and "trust" is misleading, so this change won't make it in.

stratFunc = yesManStrategy
} else {
stratFunc = standardStrategy
Expand All @@ -25,17 +36,6 @@ func New(nice bool) Strategy {
}
}

type strategist struct {
ledgerMap
strategyFunc
}

// LedgerMap lists Ledgers by their Partner key.
type ledgerMap map[peerKey]*ledger

// FIXME share this externally
type peerKey u.Key

// Peers returns a list of peers
func (s *strategist) Peers() []*peer.Peer {
response := make([]*peer.Peer, 0)
Expand Down Expand Up @@ -89,7 +89,6 @@ func (s *strategist) MessageSent(p *peer.Peer, m bsmsg.BitSwapMessage) error {
for _, block := range m.Blocks() {
l.SentBytes(len(block.Data))
}

// TODO remove these blocks from peer's want list

return nil
Expand Down