table.go 5.95 KB
Newer Older
1 2
// package kbucket implements a kademlia 'k-bucket' routing table.
package kbucket
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
3 4

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"container/list"
6
	"fmt"
7
	"sort"
Jeromy's avatar
Jeromy committed
8
	"sync"
9
	"time"
10

11
	peer "github.com/jbenet/go-ipfs/peer"
Jeromy's avatar
Jeromy committed
12
	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
)

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18 19
// RoutingTable defines the routing table.
type RoutingTable struct {

20 21 22
	// ID of the local peer
	local ID

Jeromy's avatar
Jeromy committed
23 24 25
	// Blanket lock, refine later for better performance
	tabLock sync.RWMutex

26 27 28
	// latency metrics
	metrics peer.Metrics

29 30 31
	// Maximum acceptable latency for peers in this cluster
	maxLatency time.Duration

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
	// kBuckets define all the fingers to other nodes.
Jeromy's avatar
Jeromy committed
33
	Buckets    []*Bucket
34 35 36
	bucketsize int
}

Chas Leichner's avatar
Chas Leichner committed
37
// NewRoutingTable creates a new routing table with a given bucketsize, local ID, and latency tolerance.
38
func NewRoutingTable(bucketsize int, localID ID, latency time.Duration, m peer.Metrics) *RoutingTable {
39
	rt := new(RoutingTable)
40
	rt.Buckets = []*Bucket{newBucket()}
41
	rt.bucketsize = bucketsize
42
	rt.local = localID
43
	rt.maxLatency = latency
44
	rt.metrics = m
45
	return rt
46 47 48 49
}

// Update adds or moves the given peer to the front of its respective bucket
// If a peer gets removed from a bucket, it is returned
50
func (rt *RoutingTable) Update(p peer.ID) peer.ID {
Jeromy's avatar
Jeromy committed
51 52
	rt.tabLock.Lock()
	defer rt.tabLock.Unlock()
53
	peerID := ConvertPeerID(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54
	cpl := commonPrefixLen(peerID, rt.local)
55

56 57 58
	bucketID := cpl
	if bucketID >= len(rt.Buckets) {
		bucketID = len(rt.Buckets) - 1
59 60
	}

61
	bucket := rt.Buckets[bucketID]
62
	e := bucket.find(p)
63 64
	if e == nil {
		// New peer, add to bucket
65
		if rt.metrics.LatencyEWMA(p) > rt.maxLatency {
66
			// Connection doesnt meet requirements, skip!
67
			return ""
68
		}
69
		bucket.pushFront(p)
70 71

		// Are we past the max bucket size?
72
		if bucket.len() > rt.bucketsize {
73 74
			// If this bucket is the rightmost bucket, and its full
			// we need to split it and create a new bucket
75
			if bucketID == len(rt.Buckets)-1 {
76
				return rt.nextBucket()
77 78
			} else {
				// If the bucket cant split kick out least active node
79
				return bucket.popBack()
80 81
			}
		}
82
		return ""
83
	}
84 85 86 87
	// If the peer is already in the table, move it to the front.
	// This signifies that it it "more active" and the less active nodes
	// Will as a result tend towards the back of the list
	bucket.moveToFront(e)
88
	return ""
89 90
}

91
func (rt *RoutingTable) nextBucket() peer.ID {
92 93 94 95 96 97 98 99 100 101 102
	bucket := rt.Buckets[len(rt.Buckets)-1]
	newBucket := bucket.Split(len(rt.Buckets)-1, rt.local)
	rt.Buckets = append(rt.Buckets, newBucket)
	if newBucket.len() > rt.bucketsize {
		return rt.nextBucket()
	}

	// If all elements were on left side of split...
	if bucket.len() > rt.bucketsize {
		return bucket.popBack()
	}
103
	return ""
104 105
}

106 107
// A helper struct to sort peers by their distance to the local node
type peerDistance struct {
108
	p        peer.ID
109 110
	distance ID
}
111 112

// peerSorterArr implements sort.Interface to sort peers by xor distance
113
type peerSorterArr []*peerDistance
Jeromy's avatar
Jeromy committed
114 115 116

func (p peerSorterArr) Len() int      { return len(p) }
func (p peerSorterArr) Swap(a, b int) { p[a], p[b] = p[b], p[a] }
117
func (p peerSorterArr) Less(a, b int) bool {
118
	return p[a].distance.less(p[b].distance)
119
}
Jeromy's avatar
Jeromy committed
120

121 122
//

Jeromy's avatar
Jeromy committed
123
func copyPeersFromList(target ID, peerArr peerSorterArr, peerList *list.List) peerSorterArr {
Jeromy's avatar
Jeromy committed
124
	for e := peerList.Front(); e != nil; e = e.Next() {
125 126
		p := e.Value.(peer.ID)
		pID := ConvertPeerID(p)
127
		pd := peerDistance{
Jeromy's avatar
Jeromy committed
128
			p:        p,
129
			distance: xor(target, pID),
130 131
		}
		peerArr = append(peerArr, &pd)
132
		if e == nil {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
			log.Debug("list element was nil")
Jeromy's avatar
Jeromy committed
134 135
			return peerArr
		}
136 137 138 139
	}
	return peerArr
}

Jeromy's avatar
Jeromy committed
140
// Find a specific peer by ID or return nil
141
func (rt *RoutingTable) Find(id peer.ID) peer.ID {
Chas Leichner's avatar
Chas Leichner committed
142
	srch := rt.NearestPeers(ConvertPeerID(id), 1)
143 144
	if len(srch) == 0 || srch[0] != id {
		return ""
Jeromy's avatar
Jeromy committed
145 146 147 148
	}
	return srch[0]
}

149
// NearestPeer returns a single peer that is nearest to the given ID
150
func (rt *RoutingTable) NearestPeer(id ID) peer.ID {
151
	peers := rt.NearestPeers(id, 1)
152 153 154
	if len(peers) > 0 {
		return peers[0]
	}
155

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
156
	log.Errorf("NearestPeer: Returning nil, table size = %d", rt.Size())
157
	return ""
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
158 159
}

160
// NearestPeers returns a list of the 'count' closest peers to the given ID
161
func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
Jeromy's avatar
Jeromy committed
162 163
	rt.tabLock.RLock()
	defer rt.tabLock.RUnlock()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
164
	cpl := commonPrefixLen(id, rt.local)
165 166 167 168

	// Get bucket at cpl index or last bucket
	var bucket *Bucket
	if cpl >= len(rt.Buckets) {
169
		cpl = len(rt.Buckets) - 1
170
	}
171
	bucket = rt.Buckets[cpl]
172

173
	var peerArr peerSorterArr
174
	if bucket.len() == 0 {
175 176 177
		// In the case of an unusual split, one bucket may be empty.
		// if this happens, search both surrounding buckets for nearest peer
		if cpl > 0 {
178
			plist := rt.Buckets[cpl-1].list
Jeromy's avatar
Jeromy committed
179
			peerArr = copyPeersFromList(id, peerArr, plist)
180
		}
181

Jeromy's avatar
Jeromy committed
182
		if cpl < len(rt.Buckets)-1 {
183
			plist := rt.Buckets[cpl+1].list
Jeromy's avatar
Jeromy committed
184
			peerArr = copyPeersFromList(id, peerArr, plist)
185
		}
186
	} else {
187
		peerArr = copyPeersFromList(id, peerArr, bucket.list)
188 189
	}

190
	// Sort by distance to local peer
191 192
	sort.Sort(peerArr)

193
	var out []peer.ID
194 195 196 197 198
	for i := 0; i < count && i < peerArr.Len(); i++ {
		out = append(out, peerArr[i].p)
	}

	return out
199
}
Jeromy's avatar
Jeromy committed
200

201
// Size returns the total number of peers in the routing table
Jeromy's avatar
Jeromy committed
202 203
func (rt *RoutingTable) Size() int {
	var tot int
Jeromy's avatar
Jeromy committed
204
	for _, buck := range rt.Buckets {
205
		tot += buck.len()
Jeromy's avatar
Jeromy committed
206 207 208
	}
	return tot
}
209

Chas Leichner's avatar
Chas Leichner committed
210
// ListPeers takes a RoutingTable and returns a list of all peers from all buckets in the table.
211
// NOTE: This is potentially unsafe... use at your own risk
212 213
func (rt *RoutingTable) ListPeers() []peer.ID {
	var peers []peer.ID
Jeromy's avatar
Jeromy committed
214
	for _, buck := range rt.Buckets {
215
		for e := buck.getIter(); e != nil; e = e.Next() {
216
			peers = append(peers, e.Value.(peer.ID))
217 218 219 220
		}
	}
	return peers
}
221

Chas Leichner's avatar
Chas Leichner committed
222 223
// Print prints a descriptive statement about the provided RoutingTable
func (rt *RoutingTable) Print() {
224 225
	fmt.Printf("Routing Table, bs = %d, Max latency = %d\n", rt.bucketsize, rt.maxLatency)
	rt.tabLock.RLock()
226 227 228 229 230 231 232 233 234 235

	for i, b := range rt.Buckets {
		fmt.Printf("\tbucket: %d\n", i)

		b.lk.RLock()
		for e := b.list.Front(); e != nil; e = e.Next() {
			p := e.Value.(peer.ID)
			fmt.Printf("\t\t- %s %s\n", p.Pretty(), rt.metrics.LatencyEWMA(p).String())
		}
		b.lk.RUnlock()
236
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
237
	rt.tabLock.RUnlock()
238
}