routing.go 8.49 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package dht

import (
4
	"bytes"
5
	"encoding/json"
6
	"errors"
7
	"math/rand"
8
	"sync"
9
	"time"
10

11 12
	proto "code.google.com/p/goprotobuf/proto"

13 14
	ma "github.com/jbenet/go-multiaddr"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
	peer "github.com/jbenet/go-ipfs/peer"
16
	kb "github.com/jbenet/go-ipfs/routing/kbucket"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18
	swarm "github.com/jbenet/go-ipfs/swarm"
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20
)

21 22 23
// Pool size is the number of nodes used for group find/set RPC calls
var PoolSize = 6

24 25
// TODO: determine a way of creating and managing message IDs
func GenerateMessageID() uint64 {
26 27
	//return (uint64(rand.Uint32()) << 32) & uint64(rand.Uint32())
	return uint64(rand.Uint32())
28 29
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32 33 34
// This file implements the Routing interface for the IpfsDHT struct.

// Basic Put/Get

// PutValue adds value corresponding to given Key.
35
// This is the top level "Store" operation of the DHT
Jeromy's avatar
Jeromy committed
36 37
func (s *IpfsDHT) PutValue(key u.Key, value []byte) {
	complete := make(chan struct{})
38
	for _, route := range s.routes {
Jeromy's avatar
Jeromy committed
39 40
		p := route.NearestPeer(kb.ConvertKey(key))
		if p == nil {
41
			s.network.Error(kb.ErrLookupFailure)
Jeromy's avatar
Jeromy committed
42 43 44
			go func() {
				complete <- struct{}{}
			}()
45
			continue
Jeromy's avatar
Jeromy committed
46 47 48 49
		}
		go func() {
			err := s.putValueToNetwork(p, string(key), value)
			if err != nil {
50
				s.network.Error(err)
Jeromy's avatar
Jeromy committed
51 52 53 54 55 56
			}
			complete <- struct{}{}
		}()
	}
	for _, _ = range s.routes {
		<-complete
57
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59
}

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// A counter for incrementing a variable across multiple threads
type counter struct {
	n   int
	mut sync.RWMutex
}

func (c *counter) Increment() {
	c.mut.Lock()
	c.n++
	c.mut.Unlock()
}

func (c *counter) Decrement() {
	c.mut.Lock()
	c.n--
	c.mut.Unlock()
}

func (c *counter) Size() int {
	c.mut.RLock()
	defer c.mut.RUnlock()
	return c.n
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
// GetValue searches for the value corresponding to given Key.
Jeromy's avatar
Jeromy committed
85 86
// If the search does not succeed, a multiaddr string of a closer peer is
// returned along with util.ErrSearchIncomplete
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87
func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
Jeromy's avatar
Jeromy committed
88 89 90 91 92
	ll := startNewRpc("GET")
	defer func() {
		ll.EndLog()
		ll.Print()
	}()
93

Jeromy's avatar
Jeromy committed
94 95
	// If we have it local, dont bother doing an RPC!
	// NOTE: this might not be what we want to do...
Jeromy's avatar
Jeromy committed
96 97 98 99
	val, err := s.GetLocal(key)
	if err == nil {
		ll.Success = true
		u.DOut("Found local, returning.")
Jeromy's avatar
Jeromy committed
100 101 102
		return val, nil
	}

103 104 105
	route_level := 0
	closest := s.routes[route_level].NearestPeers(kb.ConvertKey(key), PoolSize)
	if closest == nil || len(closest) == 0 {
106 107 108
		return nil, kb.ErrLookupFailure
	}

109 110 111 112 113
	val_chan := make(chan []byte)
	npeer_chan := make(chan *peer.Peer, 30)
	proc_peer := make(chan *peer.Peer, 30)
	err_chan := make(chan error)
	after := time.After(timeout)
114

115 116 117
	for _, p := range closest {
		npeer_chan <- p
	}
118

119 120 121 122 123 124 125 126 127 128 129 130
	c := counter{}

	// This limit value is referred to as k in the kademlia paper
	limit := 20
	count := 0
	go func() {
		for {
			select {
			case p := <-npeer_chan:
				count++
				if count >= limit {
					break
Jeromy's avatar
Jeromy committed
131
				}
132 133 134 135 136
				c.Increment()
				proc_peer <- p
			default:
				if c.Size() == 0 {
					err_chan <- u.ErrNotFound
137
				}
138 139 140
			}
		}
	}()
141

142 143 144 145 146 147 148 149 150
	process := func() {
		for {
			select {
			case p, ok := <-proc_peer:
				if !ok || p == nil {
					c.Decrement()
					return
				}
				val, peers, err := s.getValueOrPeers(p, key, timeout/4, route_level)
151
				if err != nil {
152 153
					u.DErr(err.Error())
					c.Decrement()
Jeromy's avatar
Jeromy committed
154
					continue
155
				}
156 157 158 159 160 161 162 163 164 165 166
				if val != nil {
					val_chan <- val
					c.Decrement()
					return
				}

				for _, np := range peers {
					// TODO: filter out peers that arent closer
					npeer_chan <- np
				}
				c.Decrement()
167
			}
168
		}
169
	}
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	concurFactor := 3
	for i := 0; i < concurFactor; i++ {
		go process()
	}

	select {
	case val := <-val_chan:
		close(npeer_chan)
		return val, nil
	case err := <-err_chan:
		close(npeer_chan)
		return nil, err
	case <-after:
		close(npeer_chan)
		return nil, u.ErrTimeout
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
187 188 189 190 191 192
}

// Value provider layer of indirection.
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.

// Announce that this node can provide value for given key
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
193
func (s *IpfsDHT) Provide(key u.Key) error {
194
	peers := s.routes[0].NearestPeers(kb.ConvertKey(key), PoolSize)
195
	if len(peers) == 0 {
196
		return kb.ErrLookupFailure
197 198
	}

199 200 201
	pmes := DHTMessage{
		Type: PBDHTMessage_ADD_PROVIDER,
		Key:  string(key),
202 203 204
	}
	pbmes := pmes.ToProtobuf()

205
	for _, p := range peers {
206
		mes := swarm.NewMessage(p, pbmes)
207
		s.network.Send(mes)
208 209
	}
	return nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
210 211 212
}

// FindProviders searches for peers who can provide the value for given key.
213
func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) ([]*peer.Peer, error) {
Jeromy's avatar
Jeromy committed
214 215 216 217 218 219
	ll := startNewRpc("FindProviders")
	defer func() {
		ll.EndLog()
		ll.Print()
	}()
	u.DOut("Find providers for: '%s'", key)
220
	p := s.routes[0].NearestPeer(kb.ConvertKey(key))
221 222 223
	if p == nil {
		return nil, kb.ErrLookupFailure
	}
224

Jeromy's avatar
Jeromy committed
225 226
	for level := 0; level < len(s.routes); {
		pmes, err := s.findProvidersSingle(p, key, level, timeout)
227 228 229
		if err != nil {
			return nil, err
		}
Jeromy's avatar
Jeromy committed
230 231 232 233 234 235 236 237
		if pmes.GetSuccess() {
			provs := s.addPeerList(key, pmes.GetPeers())
			ll.Success = true
			return provs, nil
		} else {
			closer := pmes.GetPeers()
			if len(closer) == 0 {
				level++
Jeromy's avatar
Jeromy committed
238 239
				continue
			}
Jeromy's avatar
Jeromy committed
240 241 242 243 244 245 246 247
			if peer.ID(closer[0].GetId()).Equal(s.self.ID) {
				u.DOut("Got myself back as a closer peer.")
				return nil, u.ErrNotFound
			}
			maddr, err := ma.NewMultiaddr(closer[0].GetAddr())
			if err != nil {
				// ??? Move up route level???
				panic("not yet implemented")
248 249
			}

Jeromy's avatar
Jeromy committed
250 251 252 253 254 255 256 257
			np, err := s.network.GetConnection(peer.ID(closer[0].GetId()), maddr)
			if err != nil {
				u.PErr("[%s] Failed to connect to: %s", s.self.ID.Pretty(), closer[0].GetAddr())
				level++
				continue
			}
			p = np
		}
258
	}
Jeromy's avatar
Jeromy committed
259
	return nil, u.ErrNotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
260 261 262 263 264 265
}

// Find specific Peer

// FindPeer searches for a peer with given ID.
func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
266 267 268 269 270 271
	// Check if were already connected to them
	p, _ := s.Find(id)
	if p != nil {
		return p, nil
	}

272
	route_level := 0
273
	p = s.routes[route_level].NearestPeer(kb.ConvertPeerID(id))
274 275
	if p == nil {
		return nil, kb.ErrLookupFailure
276
	}
277 278 279
	if p.ID.Equal(id) {
		return p, nil
	}
280

281 282 283 284 285
	for route_level < len(s.routes) {
		pmes, err := s.findPeerSingle(p, id, timeout, route_level)
		plist := pmes.GetPeers()
		if len(plist) == 0 {
			route_level++
286
		}
287 288 289
		found := plist[0]

		addr, err := ma.NewMultiaddr(found.GetAddr())
Jeromy's avatar
Jeromy committed
290
		if err != nil {
291
			return nil, u.WrapError(err, "FindPeer received bad info")
Jeromy's avatar
Jeromy committed
292 293
		}

294
		nxtPeer, err := s.network.GetConnection(peer.ID(found.GetId()), addr)
Jeromy's avatar
Jeromy committed
295
		if err != nil {
296
			return nil, u.WrapError(err, "FindPeer failed to connect to new peer.")
Jeromy's avatar
Jeromy committed
297
		}
298
		if pmes.GetSuccess() {
299 300 301
			if !id.Equal(nxtPeer.ID) {
				return nil, errors.New("got back invalid peer from 'successful' response")
			}
302 303 304
			return nxtPeer, nil
		} else {
			p = nxtPeer
Jeromy's avatar
Jeromy committed
305
		}
306
	}
307
	return nil, u.ErrNotFound
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
308
}
309 310 311 312 313 314

// Ping a peer, log the time it took
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
	// Thoughts: maybe this should accept an ID and do a peer lookup?
	u.DOut("Enter Ping.")

315
	pmes := DHTMessage{Id: GenerateMessageID(), Type: PBDHTMessage_PING}
316 317 318
	mes := swarm.NewMessage(p, pmes.ToProtobuf())

	before := time.Now()
319
	response_chan := dht.ListenFor(pmes.Id, 1, time.Minute)
320
	dht.network.Send(mes)
321 322 323 324 325

	tout := time.After(timeout)
	select {
	case <-response_chan:
		roundtrip := time.Since(before)
326
		p.SetLatency(roundtrip)
327
		u.DOut("Ping took %s.", roundtrip.String())
328 329 330 331 332 333 334 335
		return nil
	case <-tout:
		// Timed out, think about removing peer from network
		u.DOut("Ping peer timed out.")
		dht.Unlisten(pmes.Id)
		return u.ErrTimeout
	}
}
336 337 338 339

func (dht *IpfsDHT) GetDiagnostic(timeout time.Duration) ([]*diagInfo, error) {
	u.DOut("Begin Diagnostic")
	//Send to N closest peers
340
	targets := dht.routes[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
341 342

	// TODO: Add timeout to this struct so nodes know when to return
343 344 345
	pmes := DHTMessage{
		Type: PBDHTMessage_DIAGNOSTIC,
		Id:   GenerateMessageID(),
346 347
	}

Jeromy's avatar
Jeromy committed
348
	listenChan := dht.ListenFor(pmes.Id, len(targets), time.Minute*2)
349 350

	pbmes := pmes.ToProtobuf()
351
	for _, p := range targets {
352
		mes := swarm.NewMessage(p, pbmes)
353
		dht.network.Send(mes)
354 355 356 357 358 359 360 361 362
	}

	var out []*diagInfo
	after := time.After(timeout)
	for count := len(targets); count > 0; {
		select {
		case <-after:
			u.DOut("Diagnostic request timed out.")
			return out, u.ErrTimeout
Jeromy's avatar
Jeromy committed
363
		case resp := <-listenChan:
364
			pmes_out := new(PBDHTMessage)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
			err := proto.Unmarshal(resp.Data, pmes_out)
			if err != nil {
				// NOTE: here and elsewhere, need to audit error handling,
				//		some errors should be continued on from
				return out, err
			}

			dec := json.NewDecoder(bytes.NewBuffer(pmes_out.GetValue()))
			for {
				di := new(diagInfo)
				err := dec.Decode(di)
				if err != nil {
					break
				}

				out = append(out, di)
			}
		}
	}

385
	return nil, nil
386
}