bitswap.go 11.4 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 12
	bssrs "github.com/ipfs/go-bitswap/sessionrequestsplitter"

Jeromy's avatar
Jeromy committed
13
	decision "github.com/ipfs/go-bitswap/decision"
14
	bsgetter "github.com/ipfs/go-bitswap/getter"
Jeromy's avatar
Jeromy committed
15
	bsmsg "github.com/ipfs/go-bitswap/message"
16
	bsmq "github.com/ipfs/go-bitswap/messagequeue"
Jeromy's avatar
Jeromy committed
17
	bsnet "github.com/ipfs/go-bitswap/network"
18
	bspm "github.com/ipfs/go-bitswap/peermanager"
19
	bspqm "github.com/ipfs/go-bitswap/providerquerymanager"
20
	bssession "github.com/ipfs/go-bitswap/session"
21
	bssm "github.com/ipfs/go-bitswap/sessionmanager"
22
	bsspm "github.com/ipfs/go-bitswap/sessionpeermanager"
23
	bswm "github.com/ipfs/go-bitswap/wantmanager"
Jeromy's avatar
Jeromy committed
24 25 26 27 28 29 30 31 32 33
	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"
	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"
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 (
Brian Tiger Chow's avatar
Brian Tiger Chow committed
41 42 43
	// 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
44 45
	// TODO: if a 'non-nice' strategy is implemented, consider increasing this value
	maxProvidersPerRequest = 3
Steven Allen's avatar
Steven Allen committed
46
	findProviderDelay      = 1 * time.Second
Brian Tiger Chow's avatar
Brian Tiger Chow committed
47
	providerRequestTimeout = time.Second * 10
48 49 50
	// these requests take at _least_ two minutes at the moment.
	provideTimeout       = time.Minute * 3
	sizeBatchRequestChan = 32
Jeromy's avatar
Jeromy committed
51
)
52

Jeromy's avatar
Jeromy committed
53
var (
54 55
	HasBlockBufferSize    = 256
	provideKeysBufferSize = 2048
Steven Allen's avatar
Steven Allen committed
56
	provideWorkerMax      = 6
57 58 59

	// 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
60
)
Jeromy's avatar
Jeromy committed
61

62
var rebroadcastDelay = delay.Fixed(time.Minute)
63

Brian Tiger Chow's avatar
Brian Tiger Chow committed
64 65 66 67
// 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
68 69
func New(parent context.Context, network bsnet.BitSwapNetwork,
	bstore blockstore.Blockstore) exchange.Interface {
70

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

85 86 87
	sentHistogram := metrics.NewCtx(ctx, "sent_all_blocks_bytes", "Histogram of blocks sent by"+
		" this bitswap").Histogram(metricsBuckets)

88 89 90 91
	px := process.WithTeardown(func() error {
		return nil
	})

92 93
	peerQueueFactory := func(ctx context.Context, p peer.ID) bspm.PeerQueue {
		return bsmq.New(ctx, p, network)
94 95
	}

96
	wm := bswm.New(ctx)
97 98
	pqm := bspqm.New(ctx, network)

99 100
	sessionFactory := func(ctx context.Context, id uint64, pm bssession.PeerManager, srs bssession.RequestSplitter) bssm.Session {
		return bssession.New(ctx, id, wm, pm, srs)
101 102
	}
	sessionPeerManagerFactory := func(ctx context.Context, id uint64) bssession.PeerManager {
103
		return bsspm.New(ctx, id, network.ConnectionManager(), pqm)
104
	}
105 106 107
	sessionRequestSplitterFactory := func(ctx context.Context) bssession.RequestSplitter {
		return bssrs.New(ctx)
	}
108

109
	bs := &Bitswap{
110
		blockstore:    bstore,
111
		engine:        decision.NewEngine(ctx, bstore), // TODO close the engine with Close() method
112
		network:       network,
113
		process:       px,
114 115
		newBlocks:     make(chan cid.Cid, HasBlockBufferSize),
		provideKeys:   make(chan cid.Cid, provideKeysBufferSize),
116
		wm:            wm,
117
		pqm:           pqm,
118
		pm:            bspm.New(ctx, peerQueueFactory),
119
		sm:            bssm.New(ctx, sessionFactory, sessionPeerManagerFactory, sessionRequestSplitterFactory),
120
		counters:      new(counters),
121 122 123
		dupMetric:     dupHist,
		allMetric:     allHist,
		sentHistogram: sentHistogram,
124
	}
125 126 127

	bs.wm.SetDelegate(bs.pm)
	bs.wm.Startup()
128
	bs.pqm.Startup()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
129
	network.SetDelegate(bs)
130

131 132
	// Start up bitswaps async worker routines
	bs.startWorkers(px, ctx)
133 134 135 136 137 138 139 140 141

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

142 143 144
	return bs
}

145 146
// Bitswap instances implement the bitswap protocol.
type Bitswap struct {
147 148
	// the peermanager manages sending messages to peers in a way that
	// wont block bitswap operation
149 150 151
	pm *bspm.PeerManager

	// the wantlist tracks global wants for bitswap
152
	wm *bswm.WantManager
153

154 155 156
	// the provider query manager manages requests to find providers
	pqm *bspqm.ProviderQueryManager

157 158
	// 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
159

160 161
	// network delivers messages on behalf of the session
	network bsnet.BitSwapNetwork
162 163 164 165 166

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

167 168 169
	// 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
170
	newBlocks chan cid.Cid
171
	// provideKeys directly feeds provide workers
172
	provideKeys chan cid.Cid
173

174 175 176
	process process.Process

	// Counters for various statistics
177 178
	counterLk sync.Mutex
	counters  *counters
179 180

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

185 186
	// the sessionmanager manages tracking sessions
	sm *bssm.SessionManager
187 188
}

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

199
type blockRequest struct {
200
	Cid cid.Cid
201
	Ctx context.Context
202 203
}

204
// GetBlock attempts to retrieve a particular block from peers within the
205
// deadline enforced by the context.
206
func (bs *Bitswap) GetBlock(parent context.Context, k cid.Cid) (blocks.Block, error) {
207
	return bsgetter.SyncGetBlock(parent, k, bs.GetBlocks)
208 209
}

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

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

222 223 224 225 226 227 228
// 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)
229
func (bs *Bitswap) GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error) {
230 231
	session := bs.sm.NewSession(ctx)
	return session.GetBlocks(ctx, keys)
Jeromy's avatar
Jeromy committed
232 233
}

Łukasz Magiera's avatar
Łukasz Magiera committed
234
// HasBlock announces the existence of a block to this bitswap service. The
235
// service will potentially notify its peers.
236
func (bs *Bitswap) HasBlock(blk blocks.Block) error {
237 238 239 240 241 242 243 244
	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 {
245 246 247 248 249
	select {
	case <-bs.process.Closing():
		return errors.New("bitswap is closed")
	default:
	}
250

251
	err := bs.blockstore.Put(blk)
252 253
	if err != nil {
		log.Errorf("Error writing block to datastore: %s", err)
254 255
		return err
	}
256

257 258 259 260 261
	// 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
262

263
	bs.sm.ReceiveBlockFrom(from, blk)
264

265 266
	bs.engine.AddBlock(blk)

267
	select {
268
	case bs.newBlocks <- blk.Cid():
269
		// send block off to be reprovided
270 271
	case <-bs.process.Closing():
		return bs.process.Close()
272 273
	}
	return nil
274 275
}

276
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg.BitSwapMessage) {
Steven Allen's avatar
Steven Allen committed
277 278 279
	bs.counterLk.Lock()
	bs.counters.messagesRecvd++
	bs.counterLk.Unlock()
Jeromy's avatar
Jeromy committed
280

Jeromy's avatar
Jeromy committed
281 282
	// This call records changes to wantlists, blocks received,
	// and number of bytes transfered.
283
	bs.engine.MessageReceived(p, incoming)
Jeromy's avatar
Jeromy committed
284 285
	// TODO: this is bad, and could be easily abused.
	// Should only track *useful* messages in ledger
286

287 288 289
	iblocks := incoming.Blocks()

	if len(iblocks) == 0 {
290 291 292
		return
	}

Jeromy's avatar
Jeromy committed
293 294
	wg := sync.WaitGroup{}
	for _, block := range iblocks {
295

Jeromy's avatar
Jeromy committed
296
		wg.Add(1)
297
		go func(b blocks.Block) { // TODO: this probably doesnt need to be a goroutine...
Jeromy's avatar
Jeromy committed
298
			defer wg.Done()
299

300
			bs.updateReceiveCounters(b)
301
			bs.sm.UpdateReceiveCounters(b)
302
			log.Debugf("got block %s from %s", b, p)
303

304
			// skip received blocks that are not in the wantlist
305
			if !bs.wm.IsWanted(b.Cid()) {
306 307 308
				return
			}

309 310
			if err := bs.receiveBlockFrom(b, p); err != nil {
				log.Warningf("ReceiveMessage recvBlockFrom error: %s", err)
Jeromy's avatar
Jeromy committed
311
			}
312
			log.Event(ctx, "Bitswap.GetBlockRequest.End", b.Cid())
Jeromy's avatar
Jeromy committed
313
		}(block)
314
	}
Jeromy's avatar
Jeromy committed
315
	wg.Wait()
316 317
}

318 319
var ErrAlreadyHaveBlock = errors.New("already have block")

320
func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
321
	blkLen := len(b.RawData())
322
	has, err := bs.blockstore.Has(b.Cid())
323 324
	if err != nil {
		log.Infof("blockstore.Has error: %s", err)
325
		return
326
	}
327 328 329

	bs.allMetric.Observe(float64(blkLen))
	if has {
330
		bs.dupMetric.Observe(float64(blkLen))
331 332
	}

333 334
	bs.counterLk.Lock()
	defer bs.counterLk.Unlock()
335
	c := bs.counters
336

337 338
	c.blocksRecvd++
	c.dataRecvd += uint64(len(b.RawData()))
339
	if has {
340 341
		c.dupBlocksRecvd++
		c.dupDataRecvd += uint64(blkLen)
342 343 344
	}
}

345
// Connected/Disconnected warns bitswap about peer connections.
346
func (bs *Bitswap) PeerConnected(p peer.ID) {
347
	bs.wm.Connected(p)
348
	bs.engine.PeerConnected(p)
349 350
}

351
// Connected/Disconnected warns bitswap about peer connections.
352
func (bs *Bitswap) PeerDisconnected(p peer.ID) {
353
	bs.wm.Disconnected(p)
354
	bs.engine.PeerDisconnected(p)
355 356
}

357
func (bs *Bitswap) ReceiveError(err error) {
358
	log.Infof("Bitswap ReceiveError: %s", err)
359 360
	// TODO log the network error
	// TODO bubble the network error up to the parent context/error logger
361 362
}

363
func (bs *Bitswap) Close() error {
364
	return bs.process.Close()
365
}
366

367
func (bs *Bitswap) GetWantlist() []cid.Cid {
368
	entries := bs.wm.CurrentWants()
369
	out := make([]cid.Cid, 0, len(entries))
370
	for _, e := range entries {
371
		out = append(out, e.Cid)
372 373 374
	}
	return out
}
375 376 377 378

func (bs *Bitswap) IsOnline() bool {
	return true
}
379 380 381 382

func (bs *Bitswap) NewSession(ctx context.Context) exchange.Fetcher {
	return bs.sm.NewSession(ctx)
}