table.go 5.77 KB
Newer Older
1 2
// package kbucket implements a kademlia 'k-bucket' routing table.
package kbucket
3 4

import (
5
	"fmt"
6 7
	"sort"
	"sync"
8
	"time"
9

George Antoniadis's avatar
George Antoniadis committed
10
	logging "github.com/ipfs/go-log"
Jeromy's avatar
Jeromy committed
11 12
	peer "github.com/libp2p/go-libp2p-peer"
	pstore "github.com/libp2p/go-libp2p-peerstore"
13 14
)

Jeromy's avatar
Jeromy committed
15
var log = logging.Logger("table")
16

17 18 19 20 21 22 23 24 25
// RoutingTable defines the routing table.
type RoutingTable struct {

	// ID of the local peer
	local ID

	// Blanket lock, refine later for better performance
	tabLock sync.RWMutex

26
	// latency metrics
Jeromy's avatar
Jeromy committed
27
	metrics pstore.Metrics
28

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

32
	// kBuckets define all the fingers to other nodes.
Jeromy's avatar
Jeromy committed
33
	Buckets    []*Bucket
34
	bucketsize int
Jeromy's avatar
Jeromy committed
35 36 37 38

	// notification functions
	PeerRemoved func(peer.ID)
	PeerAdded   func(peer.ID)
39 40
}

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

53 54 55 56 57
	return rt
}

// 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
58
func (rt *RoutingTable) Update(p peer.ID) {
59
	peerID := ConvertPeerID(p)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60
	cpl := commonPrefixLen(peerID, rt.local)
61

62 63
	rt.tabLock.Lock()
	defer rt.tabLock.Unlock()
64 65 66
	bucketID := cpl
	if bucketID >= len(rt.Buckets) {
		bucketID = len(rt.Buckets) - 1
67 68
	}

69
	bucket := rt.Buckets[bucketID]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	if bucket.Has(p) {
		// 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(p)
		return
	}

	if rt.metrics.LatencyEWMA(p) > rt.maxLatency {
		// Connection doesnt meet requirements, skip!
		return
	}

	// New peer, add to bucket
	bucket.PushFront(p)
Jeromy's avatar
Jeromy committed
85
	rt.PeerAdded(p)
86 87 88 89 90 91

	// Are we past the max bucket size?
	if bucket.Len() > rt.bucketsize {
		// If this bucket is the rightmost bucket, and its full
		// we need to split it and create a new bucket
		if bucketID == len(rt.Buckets)-1 {
Jeromy's avatar
Jeromy committed
92
			rt.PeerRemoved(rt.nextBucket())
93 94 95
			return
		} else {
			// If the bucket cant split kick out least active node
Jeromy's avatar
Jeromy committed
96
			rt.PeerRemoved(bucket.PopBack())
97
			return
98 99 100 101
		}
	}
}

102 103 104 105 106 107 108 109 110 111 112 113 114 115
// Remove deletes a peer from the routing table. This is to be used
// when we are sure a node has disconnected completely.
func (rt *RoutingTable) Remove(p peer.ID) {
	rt.tabLock.Lock()
	defer rt.tabLock.Unlock()
	peerID := ConvertPeerID(p)
	cpl := commonPrefixLen(peerID, rt.local)

	bucketID := cpl
	if bucketID >= len(rt.Buckets) {
		bucketID = len(rt.Buckets) - 1
	}

	bucket := rt.Buckets[bucketID]
116
	bucket.Remove(p)
Jeromy's avatar
Jeromy committed
117
	rt.PeerRemoved(p)
118 119
}

120
func (rt *RoutingTable) nextBucket() peer.ID {
121 122 123
	bucket := rt.Buckets[len(rt.Buckets)-1]
	newBucket := bucket.Split(len(rt.Buckets)-1, rt.local)
	rt.Buckets = append(rt.Buckets, newBucket)
124
	if newBucket.Len() > rt.bucketsize {
125 126 127 128
		return rt.nextBucket()
	}

	// If all elements were on left side of split...
129 130
	if bucket.Len() > rt.bucketsize {
		return bucket.PopBack()
131
	}
132
	return ""
133 134
}

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

144
// NearestPeer returns a single peer that is nearest to the given ID
145
func (rt *RoutingTable) NearestPeer(id ID) peer.ID {
146 147 148 149
	peers := rt.NearestPeers(id, 1)
	if len(peers) > 0 {
		return peers[0]
	}
150

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
151
	log.Debugf("NearestPeer: Returning nil, table size = %d", rt.Size())
152
	return ""
153 154
}

155
// NearestPeers returns a list of the 'count' closest peers to the given ID
156
func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157
	cpl := commonPrefixLen(id, rt.local)
158

Jeromy's avatar
Jeromy committed
159 160
	rt.tabLock.RLock()

161 162 163 164 165 166 167
	// Get bucket at cpl index or last bucket
	var bucket *Bucket
	if cpl >= len(rt.Buckets) {
		cpl = len(rt.Buckets) - 1
	}
	bucket = rt.Buckets[cpl]

168
	peerArr := make(peerSorterArr, 0, count)
169 170 171 172
	peerArr = copyPeersFromList(id, peerArr, bucket.list)
	if len(peerArr) < count {
		// In the case of an unusual split, one bucket may be short or empty.
		// if this happens, search both surrounding buckets for nearby peers
173
		if cpl > 0 {
174
			plist := rt.Buckets[cpl-1].list
175 176 177
			peerArr = copyPeersFromList(id, peerArr, plist)
		}

Jeromy's avatar
Jeromy committed
178
		if cpl < len(rt.Buckets)-1 {
179
			plist := rt.Buckets[cpl+1].list
180 181 182
			peerArr = copyPeersFromList(id, peerArr, plist)
		}
	}
Jeromy's avatar
Jeromy committed
183
	rt.tabLock.RUnlock()
184 185 186 187

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

188 189 190 191 192 193 194
	if count < len(peerArr) {
		peerArr = peerArr[:count]
	}

	out := make([]peer.ID, 0, len(peerArr))
	for _, p := range peerArr {
		out = append(out, p.p)
195 196 197 198 199
	}

	return out
}

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

Chas Leichner's avatar
Chas Leichner committed
211
// ListPeers takes a RoutingTable and returns a list of all peers from all buckets in the table.
212 213
func (rt *RoutingTable) ListPeers() []peer.ID {
	var peers []peer.ID
214
	rt.tabLock.RLock()
Jeromy's avatar
Jeromy committed
215
	for _, buck := range rt.Buckets {
216
		peers = append(peers, buck.Peers()...)
217
	}
218
	rt.tabLock.RUnlock()
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
}