engine.go 8.65 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
// package decision implements the decision engine for the bitswap service.
2
package decision
3 4 5

import (
	"sync"
Jeromy's avatar
Jeromy committed
6
	"time"
7

8
	blocks "github.com/ipfs/go-ipfs/blocks"
9 10 11
	bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"
	wl "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
12 13
	logging "gx/ipfs/QmNQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR/go-log"
	peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
Jeromy's avatar
Jeromy committed
14
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
15 16
)

17 18 19 20 21 22 23 24
// TODO consider taking responsibility for other types of requests. For
// example, there could be a |cancelQueue| for all of the cancellation
// messages that need to go out. There could also be a |wantlistQueue| for
// the local peer's wantlists. Alternatively, these could all be bundled
// into a single, intelligent global queue that efficiently
// batches/combines and takes all of these into consideration.
//
// Right now, messages go onto the network for four reasons:
25 26
// 1. an initial `sendwantlist` message to a provider of the first key in a
//    request
27 28 29 30 31 32 33 34 35 36 37 38
// 2. a periodic full sweep of `sendwantlist` messages to all providers
// 3. upon receipt of blocks, a `cancel` message to all peers
// 4. draining the priority queue of `blockrequests` from peers
//
// Presently, only `blockrequests` are handled by the decision engine.
// However, there is an opportunity to give it more responsibility! If the
// decision engine is given responsibility for all of the others, it can
// intelligently decide how to combine requests efficiently.
//
// Some examples of what would be possible:
//
// * when sending out the wantlists, include `cancel` requests
39 40
// * when handling `blockrequests`, include `sendwantlist` and `cancel` as
//   appropriate
41
// * when handling `cancel`, if we recently received a wanted block from a
42
//   peer, include a partial wantlist that contains a few other high priority
43 44 45 46 47 48
//   blocks
//
// In a sense, if we treat the decision engine as a black box, it could do
// whatever it sees fit to produce desired outcomes (get wanted keys
// quickly, maintain good relationships with peers, etc).

Jeromy's avatar
Jeromy committed
49
var log = logging.Logger("engine")
50

Brian Tiger Chow's avatar
Brian Tiger Chow committed
51
const (
52 53
	// outboxChanBuffer must be 0 to prevent stale messages from being sent
	outboxChanBuffer = 0
Brian Tiger Chow's avatar
Brian Tiger Chow committed
54 55
)

56
// Envelope contains a message for a Peer
57
type Envelope struct {
58
	// Peer is the intended recipient
59
	Peer peer.ID
60 61

	// Block is the payload
62
	Block blocks.Block
Jeromy's avatar
Jeromy committed
63 64 65

	// A callback to notify the decision queue that the task is complete
	Sent func()
66 67
}

68
type Engine struct {
69 70 71
	// peerRequestQueue is a priority queue of requests received from peers.
	// Requests are popped from the queue, packaged up, and placed in the
	// outbox.
Jeromy's avatar
Jeromy committed
72
	peerRequestQueue *prq
73

74 75 76 77 78
	// FIXME it's a bit odd for the client and the worker to both share memory
	// (both modify the peerRequestQueue) and also to communicate over the
	// workSignal channel. consider sending requests over the channel and
	// allowing the worker to have exclusive access to the peerRequestQueue. In
	// that case, no lock would be required.
Jeromy's avatar
Jeromy committed
79
	workSignal chan struct{}
80

81 82
	// outbox contains outgoing messages to peers. This is owned by the
	// taskWorker goroutine
Brian Tiger Chow's avatar
Brian Tiger Chow committed
83
	outbox chan (<-chan *Envelope)
84 85 86

	bs bstore.Blockstore

87
	lock sync.Mutex // protects the fields immediatly below
88
	// ledgerMap lists Ledgers by their Partner key.
89
	ledgerMap map[peer.ID]*ledger
Jeromy's avatar
Jeromy committed
90 91

	ticker *time.Ticker
92 93
}

94 95
func NewEngine(ctx context.Context, bs bstore.Blockstore) *Engine {
	e := &Engine{
96
		ledgerMap:        make(map[peer.ID]*ledger),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
97
		bs:               bs,
Brian Tiger Chow's avatar
Brian Tiger Chow committed
98
		peerRequestQueue: newPRQ(),
Brian Tiger Chow's avatar
Brian Tiger Chow committed
99
		outbox:           make(chan (<-chan *Envelope), outboxChanBuffer),
100
		workSignal:       make(chan struct{}, 1),
Jeromy's avatar
Jeromy committed
101
		ticker:           time.NewTicker(time.Millisecond * 100),
102
	}
103 104
	go e.taskWorker(ctx)
	return e
Jeromy's avatar
Jeromy committed
105 106
}

107 108 109 110 111 112 113 114 115 116
func (e *Engine) WantlistForPeer(p peer.ID) (out []wl.Entry) {
	e.lock.Lock()
	partner, ok := e.ledgerMap[p]
	if ok {
		out = partner.wantList.SortedEntries()
	}
	e.lock.Unlock()
	return out
}

117
func (e *Engine) taskWorker(ctx context.Context) {
118 119
	defer close(e.outbox) // because taskWorker uses the channel exclusively
	for {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
120
		oneTimeUse := make(chan *Envelope, 1) // buffer to prevent blocking
121 122 123 124 125 126 127 128 129 130 131 132
		select {
		case <-ctx.Done():
			return
		case e.outbox <- oneTimeUse:
		}
		// receiver is ready for an outoing envelope. let's prepare one. first,
		// we must acquire a task from the PQ...
		envelope, err := e.nextEnvelope(ctx)
		if err != nil {
			close(oneTimeUse)
			return // ctx cancelled
		}
Brian Tiger Chow's avatar
Brian Tiger Chow committed
133
		oneTimeUse <- envelope // buffered. won't block
134 135 136 137 138 139 140
		close(oneTimeUse)
	}
}

// nextEnvelope runs in the taskWorker goroutine. Returns an error if the
// context is cancelled before the next Envelope can be created.
func (e *Engine) nextEnvelope(ctx context.Context) (*Envelope, error) {
Jeromy's avatar
Jeromy committed
141
	for {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
142
		nextTask := e.peerRequestQueue.Pop()
143
		for nextTask == nil {
Jeromy's avatar
Jeromy committed
144
			select {
145
			case <-ctx.Done():
146
				return nil, ctx.Err()
147
			case <-e.workSignal:
148
				nextTask = e.peerRequestQueue.Pop()
Jeromy's avatar
Jeromy committed
149 150 151
			case <-e.ticker.C:
				e.peerRequestQueue.thawRound()
				nextTask = e.peerRequestQueue.Pop()
Jeromy's avatar
Jeromy committed
152 153
			}
		}
154 155

		// with a task in hand, we're ready to prepare the envelope...
156

157
		block, err := e.bs.Get(nextTask.Entry.Key)
158
		if err != nil {
Jeromy's avatar
Jeromy committed
159 160 161
			// If we don't have the block, don't hold that against the peer
			// make sure to update that the task has been 'completed'
			nextTask.Done()
Brian Tiger Chow's avatar
Brian Tiger Chow committed
162
			continue
163
		}
164

165
		return &Envelope{
166 167
			Peer:  nextTask.Target,
			Block: block,
168 169 170 171 172 173 174 175 176
			Sent: func() {
				nextTask.Done()
				select {
				case e.workSignal <- struct{}{}:
					// work completing may mean that our queue will provide new
					// work to be done.
				default:
				}
			},
177
		}, nil
Jeromy's avatar
Jeromy committed
178 179 180
	}
}

181
// Outbox returns a channel of one-time use Envelope channels.
Brian Tiger Chow's avatar
Brian Tiger Chow committed
182
func (e *Engine) Outbox() <-chan (<-chan *Envelope) {
183
	return e.outbox
184 185 186
}

// Returns a slice of Peers with whom the local node has active sessions
187
func (e *Engine) Peers() []peer.ID {
188 189
	e.lock.Lock()
	defer e.lock.Unlock()
190

191
	response := make([]peer.ID, 0)
192
	for _, ledger := range e.ledgerMap {
193 194 195 196 197 198 199
		response = append(response, ledger.Partner)
	}
	return response
}

// MessageReceived performs book-keeping. Returns error if passed invalid
// arguments.
200
func (e *Engine) MessageReceived(p peer.ID, m bsmsg.BitSwapMessage) error {
201
	if len(m.Wantlist()) == 0 && len(m.Blocks()) == 0 {
202
		log.Debugf("received empty message from %s", p)
203 204
	}

205 206 207
	newWorkExists := false
	defer func() {
		if newWorkExists {
208
			e.signalNewWork()
209 210
		}
	}()
211

212
	l := e.findOrCreate(p)
Jeromy's avatar
Jeromy committed
213 214
	l.lk.Lock()
	defer l.lk.Unlock()
215 216 217
	if m.Full() {
		l.wantList = wl.New()
	}
218

219 220
	for _, entry := range m.Wantlist() {
		if entry.Cancel {
Jeromy's avatar
Jeromy committed
221
			log.Debugf("cancel %s", entry.Key)
222
			l.CancelWant(entry.Key)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
223
			e.peerRequestQueue.Remove(entry.Key, p)
224
		} else {
Jeromy's avatar
Jeromy committed
225
			log.Debugf("wants %s - %d", entry.Key, entry.Priority)
226
			l.Wants(entry.Key, entry.Priority)
227
			if exists, err := e.bs.Has(entry.Key); err == nil && exists {
228
				e.peerRequestQueue.Push(entry.Entry, p)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
229
				newWorkExists = true
230
			}
231 232
		}
	}
Jeromy's avatar
Jeromy committed
233

234
	for _, block := range m.Blocks() {
235 236
		log.Debugf("got block %s %d bytes", block.Key(), len(block.Data()))
		l.ReceivedBytes(len(block.Data()))
237 238 239 240
	}
	return nil
}

241
func (e *Engine) addBlock(block blocks.Block) {
242 243 244
	work := false

	for _, l := range e.ledgerMap {
Jeromy's avatar
Jeromy committed
245
		l.lk.Lock()
246 247 248 249
		if entry, ok := l.WantListContains(block.Key()); ok {
			e.peerRequestQueue.Push(entry, l.Partner)
			work = true
		}
Jeromy's avatar
Jeromy committed
250
		l.lk.Unlock()
251 252 253 254 255 256 257
	}

	if work {
		e.signalNewWork()
	}
}

258
func (e *Engine) AddBlock(block blocks.Block) {
259 260 261 262 263 264
	e.lock.Lock()
	defer e.lock.Unlock()

	e.addBlock(block)
}

265 266 267 268 269 270
// TODO add contents of m.WantList() to my local wantlist? NB: could introduce
// race conditions where I send a message, but MessageSent gets handled after
// MessageReceived. The information in the local wantlist could become
// inconsistent. Would need to ensure that Sends and acknowledgement of the
// send happen atomically

271
func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
272
	l := e.findOrCreate(p)
273
	for _, block := range m.Blocks() {
274
		l.SentBytes(len(block.Data()))
275
		l.wantList.Remove(block.Key())
Brian Tiger Chow's avatar
Brian Tiger Chow committed
276
		e.peerRequestQueue.Remove(block.Key(), p)
277 278 279 280 281
	}

	return nil
}

282 283 284 285
func (e *Engine) PeerDisconnected(p peer.ID) {
	// TODO: release ledger
}

286
func (e *Engine) numBytesSentTo(p peer.ID) uint64 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
287
	// NB not threadsafe
288
	return e.findOrCreate(p).Accounting.BytesSent
289 290
}

291
func (e *Engine) numBytesReceivedFrom(p peer.ID) uint64 {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
292
	// NB not threadsafe
293
	return e.findOrCreate(p).Accounting.BytesRecv
294 295 296
}

// ledger lazily instantiates a ledger
297
func (e *Engine) findOrCreate(p peer.ID) *ledger {
Jeromy's avatar
Jeromy committed
298
	e.lock.Lock()
299
	l, ok := e.ledgerMap[p]
300 301
	if !ok {
		l = newLedger(p)
302
		e.ledgerMap[p] = l
303
	}
Jeromy's avatar
Jeromy committed
304
	e.lock.Unlock()
305 306
	return l
}
307 308 309 310 311 312 313 314

func (e *Engine) signalNewWork() {
	// Signal task generation to restart (if stopped!)
	select {
	case e.workSignal <- struct{}{}:
	default:
	}
}