bitswap.go 13.5 KB
Newer Older
1
// Package bitswap implements the IPFS exchange interface with the BitSwap
Brian Tiger Chow's avatar
Brian Tiger Chow committed
2
// bilateral exchange protocol.
3 4 5
package bitswap

import (
6
	"context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"errors"
8
	"sync"
Jeromy's avatar
Jeromy committed
9 10
	"time"

11
	bssrs "github.com/ipfs/go-bitswap/sessionrequestsplitter"
12
	delay "github.com/ipfs/go-ipfs-delay"
13

Jeromy's avatar
Jeromy committed
14
	decision "github.com/ipfs/go-bitswap/decision"
15
	bsgetter "github.com/ipfs/go-bitswap/getter"
Jeromy's avatar
Jeromy committed
16
	bsmsg "github.com/ipfs/go-bitswap/message"
17
	bsmq "github.com/ipfs/go-bitswap/messagequeue"
Jeromy's avatar
Jeromy committed
18
	bsnet "github.com/ipfs/go-bitswap/network"
19
	bspm "github.com/ipfs/go-bitswap/peermanager"
20
	bspqm "github.com/ipfs/go-bitswap/providerquerymanager"
21
	bssession "github.com/ipfs/go-bitswap/session"
22
	bssm "github.com/ipfs/go-bitswap/sessionmanager"
23
	bsspm "github.com/ipfs/go-bitswap/sessionpeermanager"
24
	bswm "github.com/ipfs/go-bitswap/wantmanager"
Jeromy's avatar
Jeromy committed
25 26 27 28 29 30 31 32
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
	blockstore "github.com/ipfs/go-ipfs-blockstore"
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
	logging "github.com/ipfs/go-log"
	metrics "github.com/ipfs/go-metrics-interface"
	process "github.com/jbenet/goprocess"
	procctx "github.com/jbenet/goprocess/context"
Raúl Kripalani's avatar
Raúl Kripalani committed
33
	peer "github.com/libp2p/go-libp2p-core/peer"
34 35
)

Jeromy's avatar
Jeromy committed
36
var log = logging.Logger("bitswap")
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37

38 39
var _ exchange.SessionExchange = (*Bitswap)(nil)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
40
const (
41
	// these requests take at _least_ two minutes at the moment.
42 43
	provideTimeout         = time.Minute * 3
	defaultProvSearchDelay = time.Second
Jeromy's avatar
Jeromy committed
44
)
45

Jeromy's avatar
Jeromy committed
46
var (
47 48 49 50
	// HasBlockBufferSize is the buffer size of the channel for new blocks
	// that need to be provided. They should get pulled over by the
	// provideCollector even before they are actually provided.
	// TODO: Does this need to be this large givent that?
51 52
	HasBlockBufferSize    = 256
	provideKeysBufferSize = 2048
Steven Allen's avatar
Steven Allen committed
53
	provideWorkerMax      = 6
54 55 56

	// the 1<<18+15 is to observe old file chunks that are 1<<18 + 14 in size
	metricsBuckets = []float64{1 << 6, 1 << 10, 1 << 14, 1 << 18, 1<<18 + 15, 1 << 22}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
57
)
Jeromy's avatar
Jeromy committed
58

59 60 61 62 63 64 65 66 67 68 69
// Option defines the functional option type that can be used to configure
// bitswap instances
type Option func(*Bitswap)

// ProvideEnabled is an option for enabling/disabling provide announcements
func ProvideEnabled(enabled bool) Option {
	return func(bs *Bitswap) {
		bs.provideEnabled = enabled
	}
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83
// ProviderSearchDelay overwrites the global provider search delay
func ProviderSearchDelay(newProvSearchDelay time.Duration) Option {
	return func(bs *Bitswap) {
		bs.provSearchDelay = newProvSearchDelay
	}
}

// RebroadcastDelay overwrites the global provider rebroadcast delay
func RebroadcastDelay(newRebroadcastDelay delay.D) Option {
	return func(bs *Bitswap) {
		bs.rebroadcastDelay = newRebroadcastDelay
	}
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
84 85
// New initializes a BitSwap instance that communicates over the provided
// BitSwapNetwork. This function registers the returned instance as the network
86
// delegate. Runs until context is cancelled or bitswap.Close is called.
Łukasz Magiera's avatar
Łukasz Magiera committed
87
func New(parent context.Context, network bsnet.BitSwapNetwork,
88
	bstore blockstore.Blockstore, options ...Option) exchange.Interface {
89

90 91
	// important to use provided parent context (since it may include important
	// loggable data). It's probably not a good idea to allow bitswap to be
92
	// coupled to the concerns of the ipfs daemon in this way.
93 94 95 96
	//
	// FIXME(btc) Now that bitswap manages itself using a process, it probably
	// shouldn't accept a context anymore. Clients should probably use Close()
	// exclusively. We should probably find another way to share logging data
97
	ctx, cancelFunc := context.WithCancel(parent)
98
	ctx = metrics.CtxSubScope(ctx, "bitswap")
99
	dupHist := metrics.NewCtx(ctx, "recv_dup_blocks_bytes", "Summary of duplicate"+
100
		" data blocks recived").Histogram(metricsBuckets)
101
	allHist := metrics.NewCtx(ctx, "recv_all_blocks_bytes", "Summary of all"+
102
		" data blocks recived").Histogram(metricsBuckets)
103

104 105 106
	sentHistogram := metrics.NewCtx(ctx, "sent_all_blocks_bytes", "Histogram of blocks sent by"+
		" this bitswap").Histogram(metricsBuckets)

107 108 109 110
	px := process.WithTeardown(func() error {
		return nil
	})

111 112
	peerQueueFactory := func(ctx context.Context, p peer.ID) bspm.PeerQueue {
		return bsmq.New(ctx, p, network)
113 114
	}

115
	wm := bswm.New(ctx, bspm.New(ctx, peerQueueFactory))
116 117
	pqm := bspqm.New(ctx, network)

118 119 120 121
	sessionFactory := func(ctx context.Context, id uint64, pm bssession.PeerManager, srs bssession.RequestSplitter,
		provSearchDelay time.Duration,
		rebroadcastDelay delay.D) bssm.Session {
		return bssession.New(ctx, id, wm, pm, srs, provSearchDelay, rebroadcastDelay)
122 123
	}
	sessionPeerManagerFactory := func(ctx context.Context, id uint64) bssession.PeerManager {
124
		return bsspm.New(ctx, id, network.ConnectionManager(), pqm)
125
	}
126 127 128
	sessionRequestSplitterFactory := func(ctx context.Context) bssession.RequestSplitter {
		return bssrs.New(ctx)
	}
129

130
	bs := &Bitswap{
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		blockstore:       bstore,
		engine:           decision.NewEngine(ctx, bstore, network.ConnectionManager()), // TODO close the engine with Close() method
		network:          network,
		process:          px,
		newBlocks:        make(chan cid.Cid, HasBlockBufferSize),
		provideKeys:      make(chan cid.Cid, provideKeysBufferSize),
		wm:               wm,
		pqm:              pqm,
		sm:               bssm.New(ctx, sessionFactory, sessionPeerManagerFactory, sessionRequestSplitterFactory),
		counters:         new(counters),
		dupMetric:        dupHist,
		allMetric:        allHist,
		sentHistogram:    sentHistogram,
		provideEnabled:   true,
		provSearchDelay:  defaultProvSearchDelay,
		rebroadcastDelay: delay.Fixed(time.Minute),
147 148 149 150 151
	}

	// apply functional options before starting and running bitswap
	for _, option := range options {
		option(bs)
152
	}
153 154

	bs.wm.Startup()
155
	bs.pqm.Startup()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
156
	network.SetDelegate(bs)
157

158
	// Start up bitswaps async worker routines
159
	bs.startWorkers(ctx, px)
160 161 162 163 164 165 166 167 168

	// bind the context and process.
	// do it over here to avoid closing before all setup is done.
	go func() {
		<-px.Closing() // process closes first
		cancelFunc()
	}()
	procctx.CloseAfterContext(px, ctx) // parent cancelled first

169 170 171
	return bs
}

172 173
// Bitswap instances implement the bitswap protocol.
type Bitswap struct {
174
	// the wantlist tracks global wants for bitswap
175
	wm *bswm.WantManager
176

177 178 179
	// the provider query manager manages requests to find providers
	pqm *bspqm.ProviderQueryManager

180 181
	// the engine is the bit of logic that decides who to send which blocks to
	engine *decision.Engine
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
182

183 184
	// network delivers messages on behalf of the session
	network bsnet.BitSwapNetwork
185 186 187 188 189

	// blockstore is the local database
	// NB: ensure threadsafety
	blockstore blockstore.Blockstore

190 191 192
	// newBlocks is a channel for newly added blocks to be provided to the
	// network.  blocks pushed down this channel get buffered and fed to the
	// provideKeys channel later on to avoid too much network activity
193
	newBlocks chan cid.Cid
194
	// provideKeys directly feeds provide workers
195
	provideKeys chan cid.Cid
196

197 198 199
	process process.Process

	// Counters for various statistics
200 201
	counterLk sync.Mutex
	counters  *counters
202 203

	// Metrics interface metrics
204 205 206
	dupMetric     metrics.Histogram
	allMetric     metrics.Histogram
	sentHistogram metrics.Histogram
Jeromy's avatar
Jeromy committed
207

208 209
	// the sessionmanager manages tracking sessions
	sm *bssm.SessionManager
210 211 212

	// whether or not to make provide announcements
	provideEnabled bool
213 214 215 216 217 218

	// how long to wait before looking for providers in a session
	provSearchDelay time.Duration

	// how often to rebroadcast providing requests to find more optimized providers
	rebroadcastDelay delay.D
219 220
}

221 222 223 224 225 226 227 228 229 230
type counters struct {
	blocksRecvd    uint64
	dupBlocksRecvd uint64
	dupDataRecvd   uint64
	blocksSent     uint64
	dataSent       uint64
	dataRecvd      uint64
	messagesRecvd  uint64
}

231
// GetBlock attempts to retrieve a particular block from peers within the
232
// deadline enforced by the context.
233
func (bs *Bitswap) GetBlock(parent context.Context, k cid.Cid) (blocks.Block, error) {
234
	return bsgetter.SyncGetBlock(parent, k, bs.GetBlocks)
235 236
}

237 238
// WantlistForPeer returns the currently understood list of blocks requested by a
// given peer.
239 240
func (bs *Bitswap) WantlistForPeer(p peer.ID) []cid.Cid {
	var out []cid.Cid
241
	for _, e := range bs.engine.WantlistForPeer(p) {
242
		out = append(out, e.Cid)
243 244 245 246
	}
	return out
}

247 248
// LedgerForPeer returns aggregated data about blocks swapped and communication
// with a given peer.
249 250 251 252
func (bs *Bitswap) LedgerForPeer(p peer.ID) *decision.Receipt {
	return bs.engine.LedgerForPeer(p)
}

253 254 255 256 257 258 259
// GetBlocks returns a channel where the caller may receive blocks that
// correspond to the provided |keys|. Returns an error if BitSwap is unable to
// begin this request within the deadline enforced by the context.
//
// NB: Your request remains open until the context expires. To conserve
// resources, provide a context with a reasonably short deadline (ie. not one
// that lasts throughout the lifetime of the server)
260
func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error) {
261
	session := bs.sm.NewSession(ctx, bs.provSearchDelay, bs.rebroadcastDelay)
262
	return session.GetBlocks(ctx, keys)
Jeromy's avatar
Jeromy committed
263 264
}

Łukasz Magiera's avatar
Łukasz Magiera committed
265
// HasBlock announces the existence of a block to this bitswap service. The
266
// service will potentially notify its peers.
267
func (bs *Bitswap) HasBlock(blk blocks.Block) error {
268 269 270 271 272 273 274 275
	return bs.receiveBlockFrom(blk, "")
}

// TODO: Some of this stuff really only needs to be done when adding a block
// from the user, not when receiving it from the network.
// In case you run `git blame` on this comment, I'll save you some time: ask
// @whyrusleeping, I don't know the answers you seek.
func (bs *Bitswap) receiveBlockFrom(blk blocks.Block, from peer.ID) error {
276 277 278 279 280
	select {
	case <-bs.process.Closing():
		return errors.New("bitswap is closed")
	default:
	}
281

282
	err := bs.blockstore.Put(blk)
283 284
	if err != nil {
		log.Errorf("Error writing block to datastore: %s", err)
285 286
		return err
	}
287

288 289 290 291 292
	// NOTE: There exists the possiblity for a race condition here.  If a user
	// creates a node, then adds it to the dagservice while another goroutine
	// is waiting on a GetBlock for that object, they will receive a reference
	// to the same node. We should address this soon, but i'm not going to do
	// it now as it requires more thought and isnt causing immediate problems.
Jeromy's avatar
Jeromy committed
293

294
	bs.sm.ReceiveBlockFrom(from, blk)
295

296 297
	bs.engine.AddBlock(blk)

298
	if bs.provideEnabled {
299 300 301 302 303 304
		select {
		case bs.newBlocks <- blk.Cid():
			// send block off to be reprovided
		case <-bs.process.Closing():
			return bs.process.Close()
		}
305 306
	}
	return nil
307 308
}

309 310
// ReceiveMessage is called by the network interface when a new message is
// received.
311
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) {
Steven Allen's avatar
Steven Allen committed
312 313 314
	bs.counterLk.Lock()
	bs.counters.messagesRecvd++
	bs.counterLk.Unlock()
Jeromy's avatar
Jeromy committed
315

Jeromy's avatar
Jeromy committed
316 317
	// This call records changes to wantlists, blocks received,
	// and number of bytes transfered.
318
	bs.engine.MessageReceived(p, incoming)
Jeromy's avatar
Jeromy committed
319 320
	// TODO: this is bad, and could be easily abused.
	// Should only track *useful* messages in ledger
321

322 323 324
	iblocks := incoming.Blocks()

	if len(iblocks) == 0 {
325 326 327
		return
	}

Jeromy's avatar
Jeromy committed
328 329
	wg := sync.WaitGroup{}
	for _, block := range iblocks {
330

Jeromy's avatar
Jeromy committed
331
		wg.Add(1)
332
		go func(b blocks.Block) { // TODO: this probably doesnt need to be a goroutine...
Jeromy's avatar
Jeromy committed
333
			defer wg.Done()
334

335
			bs.updateReceiveCounters(b)
336
			bs.sm.UpdateReceiveCounters(b)
337
			log.Debugf("got block %s from %s", b, p)
338

339
			// skip received blocks that are not in the wantlist
340
			if !bs.wm.IsWanted(b.Cid()) {
341 342 343
				return
			}

344 345
			if err := bs.receiveBlockFrom(b, p); err != nil {
				log.Warningf("ReceiveMessage recvBlockFrom error: %s", err)
Jeromy's avatar
Jeromy committed
346
			}
347
			log.Event(ctx, "Bitswap.GetBlockRequest.End", b.Cid())
Jeromy's avatar
Jeromy committed
348
		}(block)
349
	}
Jeromy's avatar
Jeromy committed
350
	wg.Wait()
351 352
}

353
func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
354
	blkLen := len(b.RawData())
355
	has, err := bs.blockstore.Has(b.Cid())
356 357
	if err != nil {
		log.Infof("blockstore.Has error: %s", err)
358
		return
359
	}
360 361 362

	bs.allMetric.Observe(float64(blkLen))
	if has {
363
		bs.dupMetric.Observe(float64(blkLen))
364 365
	}

366 367
	bs.counterLk.Lock()
	defer bs.counterLk.Unlock()
368
	c := bs.counters
369

370 371
	c.blocksRecvd++
	c.dataRecvd += uint64(len(b.RawData()))
372
	if has {
373 374
		c.dupBlocksRecvd++
		c.dupDataRecvd += uint64(blkLen)
375 376 377
	}
}

378 379
// PeerConnected is called by the network interface
// when a peer initiates a new connection to bitswap.
380
func (bs *Bitswap) PeerConnected(p peer.ID) {
381
	bs.wm.Connected(p)
382
	bs.engine.PeerConnected(p)
383 384
}

385 386
// PeerDisconnected is called by the network interface when a peer
// closes a connection
387
func (bs *Bitswap) PeerDisconnected(p peer.ID) {
388
	bs.wm.Disconnected(p)
389
	bs.engine.PeerDisconnected(p)
390 391
}

392 393
// ReceiveError is called by the network interface when an error happens
// at the network layer. Currently just logs error.
394
func (bs *Bitswap) ReceiveError(err error) {
395
	log.Infof("Bitswap ReceiveError: %s", err)
396 397
	// TODO log the network error
	// TODO bubble the network error up to the parent context/error logger
398 399
}

400
// Close is called to shutdown Bitswap
401
func (bs *Bitswap) Close() error {
402
	return bs.process.Close()
403
}
404

405
// GetWantlist returns the current local wantlist.
406
func (bs *Bitswap) GetWantlist() []cid.Cid {
407
	entries := bs.wm.CurrentWants()
408
	out := make([]cid.Cid, 0, len(entries))
409
	for _, e := range entries {
410
		out = append(out, e.Cid)
411 412 413
	}
	return out
}
414

415
// IsOnline is needed to match go-ipfs-exchange-interface
416 417 418
func (bs *Bitswap) IsOnline() bool {
	return true
}
419

420 421 422 423 424 425
// NewSession generates a new Bitswap session. You should use this, rather
// that calling Bitswap.GetBlocks, any time you intend to do several related
// block requests in a row. The session returned will have it's own GetBlocks
// method, but the session will use the fact that the requests are related to
// be more efficient in its requests to peers. If you are using a session
// from go-blockservice, it will create a bitswap session automatically.
426
func (bs *Bitswap) NewSession(ctx context.Context) exchange.Fetcher {
427
	return bs.sm.NewSession(ctx, bs.provSearchDelay, bs.rebroadcastDelay)
428
}