ledgermanager.go 4.54 KB
Newer Older
1 2 3 4 5
package strategy

import (
	"sync"

Jeromy's avatar
Jeromy committed
6 7 8
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"

	bstore "github.com/jbenet/go-ipfs/blocks/blockstore"
9 10 11 12 13 14
	bsmsg "github.com/jbenet/go-ipfs/exchange/bitswap/message"
	wl "github.com/jbenet/go-ipfs/exchange/bitswap/wantlist"
	peer "github.com/jbenet/go-ipfs/peer"
	u "github.com/jbenet/go-ipfs/util"
)

15 16
var log = u.Logger("strategy")

17 18 19 20 21 22
// LedgerMap lists Ledgers by their Partner key.
type ledgerMap map[peerKey]*ledger

// FIXME share this externally
type peerKey u.Key

23 24 25 26 27
type Envelope struct {
	Peer    peer.Peer
	Message bsmsg.BitSwapMessage
}

Jeromy's avatar
Jeromy committed
28 29 30 31 32
type LedgerManager struct {
	lock       sync.RWMutex
	ledgerMap  ledgerMap
	bs         bstore.Blockstore
	tasklist   *TaskList
33
	outbox     chan Envelope
Jeromy's avatar
Jeromy committed
34
	workSignal chan struct{}
35 36
}

37
func NewLedgerManager(ctx context.Context, bs bstore.Blockstore) *LedgerManager {
Jeromy's avatar
Jeromy committed
38 39 40 41
	lm := &LedgerManager{
		ledgerMap:  make(ledgerMap),
		bs:         bs,
		tasklist:   NewTaskList(),
42
		outbox:     make(chan Envelope, 4), // TODO extract constant
Jeromy's avatar
Jeromy committed
43
		workSignal: make(chan struct{}),
44
	}
45
	go lm.taskWorker(ctx)
Jeromy's avatar
Jeromy committed
46 47 48
	return lm
}

49
func (lm *LedgerManager) taskWorker(ctx context.Context) {
Jeromy's avatar
Jeromy committed
50
	for {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
51
		nextTask := lm.tasklist.Pop()
Jeromy's avatar
Jeromy committed
52 53 54 55
		if nextTask == nil {
			// No tasks in the list?
			// Wait until there are!
			select {
56
			case <-ctx.Done():
Jeromy's avatar
Jeromy committed
57 58 59 60 61
				return
			case <-lm.workSignal:
			}
			continue
		}
62 63 64 65 66 67 68 69 70
		block, err := lm.bs.Get(nextTask.Key)
		if err != nil {
			continue // TODO maybe return an error
		}
		// construct message here so we can make decisions about any additional
		// information we may want to include at this time.
		m := bsmsg.New()
		m.AddBlock(block)
		// TODO: maybe add keys from our wantlist?
Jeromy's avatar
Jeromy committed
71
		select {
72
		case <-ctx.Done():
Jeromy's avatar
Jeromy committed
73
			return
74
		case lm.outbox <- Envelope{Peer: nextTask.Target, Message: m}:
Jeromy's avatar
Jeromy committed
75 76 77 78
		}
	}
}

79 80
func (lm *LedgerManager) Outbox() <-chan Envelope {
	return lm.outbox
81 82 83
}

// Returns a slice of Peers with whom the local node has active sessions
Jeromy's avatar
Jeromy committed
84 85 86
func (lm *LedgerManager) Peers() []peer.Peer {
	lm.lock.RLock()
	defer lm.lock.RUnlock()
87 88

	response := make([]peer.Peer, 0)
Jeromy's avatar
Jeromy committed
89
	for _, ledger := range lm.ledgerMap {
90 91 92 93 94 95 96
		response = append(response, ledger.Partner)
	}
	return response
}

// BlockIsWantedByPeer returns true if peer wants the block given by this
// key
Jeromy's avatar
Jeromy committed
97 98 99
func (lm *LedgerManager) BlockIsWantedByPeer(k u.Key, p peer.Peer) bool {
	lm.lock.RLock()
	defer lm.lock.RUnlock()
100

Brian Tiger Chow's avatar
Brian Tiger Chow committed
101
	ledger := lm.findOrCreate(p)
102 103 104 105 106
	return ledger.WantListContains(k)
}

// MessageReceived performs book-keeping. Returns error if passed invalid
// arguments.
Jeromy's avatar
Jeromy committed
107 108 109 110
func (lm *LedgerManager) MessageReceived(p peer.Peer, m bsmsg.BitSwapMessage) error {
	lm.lock.Lock()
	defer lm.lock.Unlock()

Brian Tiger Chow's avatar
Brian Tiger Chow committed
111
	l := lm.findOrCreate(p)
112 113 114 115 116 117
	if m.Full() {
		l.wantList = wl.New()
	}
	for _, e := range m.Wantlist() {
		if e.Cancel {
			l.CancelWant(e.Key)
Jeromy's avatar
Jeromy committed
118
			lm.tasklist.Cancel(e.Key, p)
119 120
		} else {
			l.Wants(e.Key, e.Priority)
Brian Tiger Chow's avatar
Brian Tiger Chow committed
121
			lm.tasklist.Push(e.Key, e.Priority, p)
Jeromy's avatar
Jeromy committed
122 123 124 125 126 127

			// Signal task generation to restart (if stopped!)
			select {
			case lm.workSignal <- struct{}{}:
			default:
			}
128 129
		}
	}
Jeromy's avatar
Jeromy committed
130

131 132 133
	for _, block := range m.Blocks() {
		// FIXME extract blocks.NumBytes(block) or block.NumBytes() method
		l.ReceivedBytes(len(block.Data))
Jeromy's avatar
Jeromy committed
134 135
		for _, l := range lm.ledgerMap {
			if l.WantListContains(block.Key()) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
136
				lm.tasklist.Push(block.Key(), 1, l.Partner)
Jeromy's avatar
Jeromy committed
137 138 139 140 141 142 143 144 145

				// Signal task generation to restart (if stopped!)
				select {
				case lm.workSignal <- struct{}{}:
				default:
				}

			}
		}
146 147 148 149 150 151 152 153 154 155
	}
	return nil
}

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

Jeromy's avatar
Jeromy committed
156 157 158
func (lm *LedgerManager) MessageSent(p peer.Peer, m bsmsg.BitSwapMessage) error {
	lm.lock.Lock()
	defer lm.lock.Unlock()
159

Brian Tiger Chow's avatar
Brian Tiger Chow committed
160
	l := lm.findOrCreate(p)
161 162 163
	for _, block := range m.Blocks() {
		l.SentBytes(len(block.Data))
		l.wantList.Remove(block.Key())
Jeromy's avatar
Jeromy committed
164
		lm.tasklist.Cancel(block.Key(), p)
165 166 167 168 169
	}

	return nil
}

Jeromy's avatar
Jeromy committed
170 171 172
func (lm *LedgerManager) NumBytesSentTo(p peer.Peer) uint64 {
	lm.lock.RLock()
	defer lm.lock.RUnlock()
173

Brian Tiger Chow's avatar
Brian Tiger Chow committed
174
	return lm.findOrCreate(p).Accounting.BytesSent
175 176
}

Jeromy's avatar
Jeromy committed
177 178 179
func (lm *LedgerManager) NumBytesReceivedFrom(p peer.Peer) uint64 {
	lm.lock.RLock()
	defer lm.lock.RUnlock()
180

Brian Tiger Chow's avatar
Brian Tiger Chow committed
181
	return lm.findOrCreate(p).Accounting.BytesRecv
182 183 184
}

// ledger lazily instantiates a ledger
Brian Tiger Chow's avatar
Brian Tiger Chow committed
185
func (lm *LedgerManager) findOrCreate(p peer.Peer) *ledger {
Jeromy's avatar
Jeromy committed
186
	l, ok := lm.ledgerMap[peerKey(p.Key())]
187 188
	if !ok {
		l = newLedger(p)
Jeromy's avatar
Jeromy committed
189
		lm.ledgerMap[peerKey(p.Key())] = l
190 191 192
	}
	return l
}