bitswap.go 13.2 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
	"sync/atomic"
Jeromy's avatar
Jeromy committed
10 11
	"time"

Jeromy's avatar
Jeromy committed
12 13
	decision "github.com/ipfs/go-bitswap/decision"
	bsmsg "github.com/ipfs/go-bitswap/message"
14
	bsmq "github.com/ipfs/go-bitswap/messagequeue"
Jeromy's avatar
Jeromy committed
15 16
	bsnet "github.com/ipfs/go-bitswap/network"
	notifications "github.com/ipfs/go-bitswap/notifications"
17
	bspm "github.com/ipfs/go-bitswap/peermanager"
18 19
	bssm "github.com/ipfs/go-bitswap/sessionmanager"
	bswm "github.com/ipfs/go-bitswap/wantmanager"
Jeromy's avatar
Jeromy committed
20

Jeromy's avatar
Jeromy committed
21 22 23 24 25 26 27 28 29 30 31
	blocks "github.com/ipfs/go-block-format"
	cid "github.com/ipfs/go-cid"
	blockstore "github.com/ipfs/go-ipfs-blockstore"
	delay "github.com/ipfs/go-ipfs-delay"
	exchange "github.com/ipfs/go-ipfs-exchange-interface"
	flags "github.com/ipfs/go-ipfs-flags"
	logging "github.com/ipfs/go-log"
	metrics "github.com/ipfs/go-metrics-interface"
	process "github.com/jbenet/goprocess"
	procctx "github.com/jbenet/goprocess/context"
	peer "github.com/libp2p/go-libp2p-peer"
32 33
)

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

36 37
var _ exchange.SessionExchange = (*Bitswap)(nil)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
38
const (
Brian Tiger Chow's avatar
Brian Tiger Chow committed
39 40 41
	// maxProvidersPerRequest specifies the maximum number of providers desired
	// from the network. This value is specified because the network streams
	// results.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
42 43
	// TODO: if a 'non-nice' strategy is implemented, consider increasing this value
	maxProvidersPerRequest = 3
Steven Allen's avatar
Steven Allen committed
44
	findProviderDelay      = 1 * time.Second
Brian Tiger Chow's avatar
Brian Tiger Chow committed
45
	providerRequestTimeout = time.Second * 10
46 47
	provideTimeout         = time.Second * 15
	sizeBatchRequestChan   = 32
Jeromy's avatar
Jeromy committed
48
)
49

Jeromy's avatar
Jeromy committed
50
var (
51 52 53
	HasBlockBufferSize    = 256
	provideKeysBufferSize = 2048
	provideWorkerMax      = 512
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

Jeromy's avatar
Jeromy committed
59 60 61 62 63 64 65 66
func init() {
	if flags.LowMemMode {
		HasBlockBufferSize = 64
		provideKeysBufferSize = 512
		provideWorkerMax = 16
	}
}

67
var rebroadcastDelay = delay.Fixed(time.Minute)
68

Brian Tiger Chow's avatar
Brian Tiger Chow committed
69 70 71 72
// New initializes a BitSwap instance that communicates over the provided
// BitSwapNetwork. This function registers the returned instance as the network
// delegate.
// Runs until context is cancelled.
Łukasz Magiera's avatar
Łukasz Magiera committed
73 74
func New(parent context.Context, network bsnet.BitSwapNetwork,
	bstore blockstore.Blockstore) exchange.Interface {
75

76 77
	// 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
78
	// coupled to the concerns of the ipfs daemon in this way.
79 80 81 82
	//
	// 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
83
	ctx, cancelFunc := context.WithCancel(parent)
84
	ctx = metrics.CtxSubScope(ctx, "bitswap")
85
	dupHist := metrics.NewCtx(ctx, "recv_dup_blocks_bytes", "Summary of duplicate"+
86
		" data blocks recived").Histogram(metricsBuckets)
87
	allHist := metrics.NewCtx(ctx, "recv_all_blocks_bytes", "Summary of all"+
88
		" data blocks recived").Histogram(metricsBuckets)
89

90 91 92
	sentHistogram := metrics.NewCtx(ctx, "sent_all_blocks_bytes", "Histogram of blocks sent by"+
		" this bitswap").Histogram(metricsBuckets)

93
	notif := notifications.New()
94 95 96 97 98
	px := process.WithTeardown(func() error {
		notif.Shutdown()
		return nil
	})

99 100 101 102
	peerQueueFactory := func(p peer.ID) bspm.PeerQueue {
		return bsmq.New(p, network)
	}

103
	bs := &Bitswap{
104
		blockstore:    bstore,
105
		notifications: notif,
106
		engine:        decision.NewEngine(ctx, bstore), // TODO close the engine with Close() method
107
		network:       network,
108
		findKeys:      make(chan *blockRequest, sizeBatchRequestChan),
109
		process:       px,
110 111
		newBlocks:     make(chan cid.Cid, HasBlockBufferSize),
		provideKeys:   make(chan cid.Cid, provideKeysBufferSize),
112 113
		wm:            bswm.New(ctx),
		pm:            bspm.New(ctx, peerQueueFactory),
114
		sm:            bssm.New(),
115
		counters:      new(counters),
116 117 118
		dupMetric:     dupHist,
		allMetric:     allHist,
		sentHistogram: sentHistogram,
119
	}
120 121 122 123

	bs.wm.SetDelegate(bs.pm)
	bs.pm.Startup()
	bs.wm.Startup()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
124
	network.SetDelegate(bs)
125

126 127
	// Start up bitswaps async worker routines
	bs.startWorkers(px, ctx)
128 129 130 131 132 133 134 135 136

	// 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

137 138 139
	return bs
}

140 141
// Bitswap instances implement the bitswap protocol.
type Bitswap struct {
142 143
	// the peermanager manages sending messages to peers in a way that
	// wont block bitswap operation
144 145 146
	pm *bspm.PeerManager

	// the wantlist tracks global wants for bitswap
147
	wm *bswm.WantManager
148

149 150
	// 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
151

152 153
	// network delivers messages on behalf of the session
	network bsnet.BitSwapNetwork
154 155 156 157 158

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

159 160
	// notifications engine for receiving new blocks and routing them to the
	// appropriate user requests
161 162
	notifications notifications.PubSub

163
	// findKeys sends keys to a worker to find and connect to providers for them
164
	findKeys chan *blockRequest
165 166 167
	// 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
168
	newBlocks chan cid.Cid
169
	// provideKeys directly feeds provide workers
170
	provideKeys chan cid.Cid
171

172 173 174
	process process.Process

	// Counters for various statistics
175 176
	counterLk sync.Mutex
	counters  *counters
177 178

	// Metrics interface metrics
179 180 181
	dupMetric     metrics.Histogram
	allMetric     metrics.Histogram
	sentHistogram metrics.Histogram
Jeromy's avatar
Jeromy committed
182

183 184
	// the sessionmanager manages tracking sessions
	sm *bssm.SessionManager
185 186
}

187 188 189 190 191 192 193 194 195 196
type counters struct {
	blocksRecvd    uint64
	dupBlocksRecvd uint64
	dupDataRecvd   uint64
	blocksSent     uint64
	dataSent       uint64
	dataRecvd      uint64
	messagesRecvd  uint64
}

197
type blockRequest struct {
198
	Cid cid.Cid
199
	Ctx context.Context
200 201
}

202
// GetBlock attempts to retrieve a particular block from peers within the
203
// deadline enforced by the context.
204
func (bs *Bitswap) GetBlock(parent context.Context, k cid.Cid) (blocks.Block, error) {
Jeromy's avatar
Jeromy committed
205
	return getBlock(parent, k, bs.GetBlocks)
206 207
}

208 209
func (bs *Bitswap) WantlistForPeer(p peer.ID) []cid.Cid {
	var out []cid.Cid
210
	for _, e := range bs.engine.WantlistForPeer(p) {
211
		out = append(out, e.Cid)
212 213 214 215
	}
	return out
}

216 217 218 219
func (bs *Bitswap) LedgerForPeer(p peer.ID) *decision.Receipt {
	return bs.engine.LedgerForPeer(p)
}

220 221 222 223 224 225 226
// 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)
227
func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error) {
228
	if len(keys) == 0 {
229
		out := make(chan blocks.Block)
230 231 232 233
		close(out)
		return out, nil
	}

234 235 236 237 238
	select {
	case <-bs.process.Closing():
		return nil, errors.New("bitswap is closed")
	default:
	}
239
	promise := bs.notifications.Subscribe(ctx, keys...)
240

241
	for _, k := range keys {
242
		log.Event(ctx, "Bitswap.GetBlockRequest.Start", k)
243 244
	}

245
	mses := bs.sm.GetNextSessionID()
Jeromy's avatar
Jeromy committed
246 247

	bs.wm.WantBlocks(ctx, keys, nil, mses)
248

249
	remaining := cid.NewSet()
250
	for _, k := range keys {
251
		remaining.Add(k)
252 253 254 255 256 257 258 259
	}

	out := make(chan blocks.Block)
	go func() {
		ctx, cancel := context.WithCancel(ctx)
		defer cancel()
		defer close(out)
		defer func() {
260
			// can't just defer this call on its own, arguments are resolved *when* the defer is created
Jeromy's avatar
Jeromy committed
261
			bs.CancelWants(remaining.Keys(), mses)
262
		}()
Steven Allen's avatar
Steven Allen committed
263 264 265 266 267 268 269 270 271 272 273
		findProvsDelay := time.NewTimer(findProviderDelay)
		defer findProvsDelay.Stop()

		findProvsDelayCh := findProvsDelay.C
		req := &blockRequest{
			Cid: keys[0],
			Ctx: ctx,
		}

		var findProvsReqCh chan<- *blockRequest

274 275
		for {
			select {
Steven Allen's avatar
Steven Allen committed
276 277 278 279 280 281 282 283
			case <-findProvsDelayCh:
				// NB: Optimization. Assumes that providers of key[0] are likely to
				// be able to provide for all keys. This currently holds true in most
				// every situation. Later, this assumption may not hold as true.
				findProvsReqCh = bs.findKeys
				findProvsDelayCh = nil
			case findProvsReqCh <- req:
				findProvsReqCh = nil
284 285 286 287 288
			case blk, ok := <-promise:
				if !ok {
					return
				}

Steven Allen's avatar
Steven Allen committed
289 290 291 292 293
				// No need to find providers now.
				findProvsDelay.Stop()
				findProvsDelayCh = nil
				findProvsReqCh = nil

294
				bs.CancelWants([]cid.Cid{blk.Cid()}, mses)
295
				remaining.Remove(blk.Cid())
296 297 298 299 300 301 302 303 304 305 306
				select {
				case out <- blk:
				case <-ctx.Done():
					return
				}
			case <-ctx.Done():
				return
			}
		}
	}()

Steven Allen's avatar
Steven Allen committed
307
	return out, nil
Jeromy's avatar
Jeromy committed
308 309
}

310
// CancelWant removes a given key from the wantlist.
311
func (bs *Bitswap) CancelWants(cids []cid.Cid, ses uint64) {
312 313 314
	if len(cids) == 0 {
		return
	}
Jeromy's avatar
Jeromy committed
315
	bs.wm.CancelWants(context.Background(), cids, nil, ses)
316 317
}

Łukasz Magiera's avatar
Łukasz Magiera committed
318
// HasBlock announces the existence of a block to this bitswap service. The
319
// service will potentially notify its peers.
320
func (bs *Bitswap) HasBlock(blk blocks.Block) error {
321 322 323 324 325 326 327 328
	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 {
329 330 331 332 333
	select {
	case <-bs.process.Closing():
		return errors.New("bitswap is closed")
	default:
	}
334

335
	err := bs.blockstore.Put(blk)
336 337
	if err != nil {
		log.Errorf("Error writing block to datastore: %s", err)
338 339
		return err
	}
340

341 342 343 344 345
	// 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
346 347
	bs.notifications.Publish(blk)

348
	k := blk.Cid()
349
	ks := []cid.Cid{k}
350 351 352
	for _, s := range bs.SessionsForBlock(k) {
		s.receiveBlockFrom(from, blk)
		bs.CancelWants(ks, s.id)
353 354
	}

355 356
	bs.engine.AddBlock(blk)

357
	select {
358
	case bs.newBlocks <- blk.Cid():
359
		// send block off to be reprovided
360 361
	case <-bs.process.Closing():
		return bs.process.Close()
362 363
	}
	return nil
364 365
}

366
// SessionsForBlock returns a slice of all sessions that may be interested in the given cid.
367
func (bs *Bitswap) SessionsForBlock(c cid.Cid) []*Session {
Jeromy's avatar
Jeromy committed
368
	var out []*Session
369 370
	bs.sm.IterateSessions(func(session exchange.Fetcher) {
		s := session.(*Session)
Jeromy's avatar
Jeromy committed
371
		if s.interestedIn(c) {
Jeromy's avatar
Jeromy committed
372 373
			out = append(out, s)
		}
374
	})
Jeromy's avatar
Jeromy committed
375 376 377
	return out
}

378
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) {
379
	atomic.AddUint64(&bs.counters.messagesRecvd, 1)
Jeromy's avatar
Jeromy committed
380

Jeromy's avatar
Jeromy committed
381 382
	// This call records changes to wantlists, blocks received,
	// and number of bytes transfered.
383
	bs.engine.MessageReceived(p, incoming)
Jeromy's avatar
Jeromy committed
384 385
	// TODO: this is bad, and could be easily abused.
	// Should only track *useful* messages in ledger
386

387 388 389
	iblocks := incoming.Blocks()

	if len(iblocks) == 0 {
390 391 392
		return
	}

Jeromy's avatar
Jeromy committed
393 394
	wg := sync.WaitGroup{}
	for _, block := range iblocks {
395

Jeromy's avatar
Jeromy committed
396
		wg.Add(1)
397
		go func(b blocks.Block) { // TODO: this probably doesnt need to be a goroutine...
Jeromy's avatar
Jeromy committed
398
			defer wg.Done()
399

400
			bs.updateReceiveCounters(b)
401

402
			log.Debugf("got block %s from %s", b, p)
403

404
			// skip received blocks that are not in the wantlist
405
			if !bs.wm.IsWanted(b.Cid()) {
406 407 408
				return
			}

409 410
			if err := bs.receiveBlockFrom(b, p); err != nil {
				log.Warningf("ReceiveMessage recvBlockFrom error: %s", err)
Jeromy's avatar
Jeromy committed
411
			}
412
			log.Event(ctx, "Bitswap.GetBlockRequest.End", b.Cid())
Jeromy's avatar
Jeromy committed
413
		}(block)
414
	}
Jeromy's avatar
Jeromy committed
415
	wg.Wait()
416 417
}

418 419
var ErrAlreadyHaveBlock = errors.New("already have block")

420
func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
421
	blkLen := len(b.RawData())
422
	has, err := bs.blockstore.Has(b.Cid())
423 424
	if err != nil {
		log.Infof("blockstore.Has error: %s", err)
425
		return
426
	}
427 428 429

	bs.allMetric.Observe(float64(blkLen))
	if has {
430
		bs.dupMetric.Observe(float64(blkLen))
431 432
	}

433 434
	bs.counterLk.Lock()
	defer bs.counterLk.Unlock()
435
	c := bs.counters
436

437 438
	c.blocksRecvd++
	c.dataRecvd += uint64(len(b.RawData()))
439
	if has {
440 441
		c.dupBlocksRecvd++
		c.dupDataRecvd += uint64(blkLen)
442 443 444
	}
}

445
// Connected/Disconnected warns bitswap about peer connections.
446
func (bs *Bitswap) PeerConnected(p peer.ID) {
447 448
	initialWants := bs.wm.CurrentBroadcastWants()
	bs.pm.Connected(p, initialWants)
449
	bs.engine.PeerConnected(p)
450 451
}

452
// Connected/Disconnected warns bitswap about peer connections.
453
func (bs *Bitswap) PeerDisconnected(p peer.ID) {
454
	bs.pm.Disconnected(p)
455
	bs.engine.PeerDisconnected(p)
456 457
}

458
func (bs *Bitswap) ReceiveError(err error) {
459
	log.Infof("Bitswap ReceiveError: %s", err)
460 461
	// TODO log the network error
	// TODO bubble the network error up to the parent context/error logger
462 463
}

464
func (bs *Bitswap) Close() error {
465
	return bs.process.Close()
466
}
467

468
func (bs *Bitswap) GetWantlist() []cid.Cid {
469
	entries := bs.wm.CurrentWants()
470
	out := make([]cid.Cid, 0, len(entries))
471
	for _, e := range entries {
472
		out = append(out, e.Cid)
473 474 475
	}
	return out
}
476 477 478 479

func (bs *Bitswap) IsOnline() bool {
	return true
}