2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-07-30 20:25:18 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
2019-06-04 12:36:27 +01:00
|
|
|
"context"
|
2018-08-09 20:20:39 +01:00
|
|
|
"encoding/binary"
|
2018-08-13 09:39:45 +01:00
|
|
|
"time"
|
|
|
|
|
2018-11-20 18:29:07 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-07-30 20:25:18 +01:00
|
|
|
|
2018-11-30 13:40:13 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-07-30 20:25:18 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
// addNode attempts to add a new contact to the routing table
|
|
|
|
// Requires node not already in table
|
2018-08-17 20:11:46 +01:00
|
|
|
// Returns true if node was added successfully
|
2019-06-04 12:36:27 +01:00
|
|
|
func (rt *RoutingTable) addNode(node *pb.Node) (_ bool, err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-07-30 20:25:18 +01:00
|
|
|
rt.mutex.Lock()
|
|
|
|
defer rt.mutex.Unlock()
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2019-01-02 18:57:11 +00:00
|
|
|
if node.Id == rt.self.Id {
|
2019-06-05 15:23:10 +01:00
|
|
|
err := rt.createOrUpdateKBucket(ctx, firstBucketID, time.Now())
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not create initial K bucket: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
err = rt.putNode(node)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not add initial node to nodeBucketDB: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2018-08-17 20:11:46 +01:00
|
|
|
return true, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-06-05 15:23:10 +01:00
|
|
|
kadBucketID, err := rt.getKBucketID(ctx, node.Id)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not getKBucketID: %s", err)
|
2018-08-13 09:39:45 +01:00
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
hasRoom, err := rt.kadBucketHasRoom(kadBucketID)
|
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, err
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
containsLocal, err := rt.kadBucketContainsLocalNode(kadBucketID)
|
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, err
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:07:53 +00:00
|
|
|
withinK, err := rt.wouldBeInNearestK(node.Id)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not determine if node is within k: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
for !hasRoom {
|
|
|
|
if containsLocal || withinK {
|
|
|
|
depth, err := rt.determineLeafDepth(kadBucketID)
|
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not determine leaf depth: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
kadBucketID = rt.splitBucket(kadBucketID, depth)
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.createOrUpdateKBucket(ctx, kadBucketID, time.Now())
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not split and create K bucket: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-06-05 15:23:10 +01:00
|
|
|
kadBucketID, err = rt.getKBucketID(ctx, node.Id)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not get k bucket Id within add node split bucket checks: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
hasRoom, err = rt.kadBucketHasRoom(kadBucketID)
|
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, err
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
containsLocal, err = rt.kadBucketContainsLocalNode(kadBucketID)
|
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, err
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2018-08-17 20:11:46 +01:00
|
|
|
rt.addToReplacementCache(kadBucketID, node)
|
|
|
|
return false, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
err = rt.putNode(node)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not add node to nodeBucketDB: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.createOrUpdateKBucket(ctx, kadBucketID, time.Now())
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return false, RoutingErr.New("could not create or update K bucket: %s", err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2018-08-17 20:11:46 +01:00
|
|
|
return true, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateNode will update the node information given that
|
|
|
|
// the node is already in the routing table.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (rt *RoutingTable) updateNode(node *pb.Node) (err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-29 18:39:27 +00:00
|
|
|
if err := rt.putNode(node); err != nil {
|
2018-08-17 20:11:46 +01:00
|
|
|
return RoutingErr.New("could not update node: %v", err)
|
|
|
|
}
|
2018-07-30 20:25:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-17 20:11:46 +01:00
|
|
|
// removeNode will remove churned nodes and replace those entries with nodes from the replacement cache.
|
2019-06-04 12:36:27 +01:00
|
|
|
func (rt *RoutingTable) removeNode(node *pb.Node) (err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-02-22 18:39:29 +00:00
|
|
|
rt.mutex.Lock()
|
|
|
|
defer rt.mutex.Unlock()
|
2019-06-05 15:23:10 +01:00
|
|
|
kadBucketID, err := rt.getKBucketID(ctx, node.Id)
|
2019-02-22 18:39:29 +00:00
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not get k bucket %s", err)
|
|
|
|
}
|
2019-02-22 18:39:29 +00:00
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
existingMarshalled, err := rt.nodeBucketDB.Get(ctx, node.Id.Bytes())
|
2018-08-21 19:44:42 +01:00
|
|
|
if storage.ErrKeyNotFound.Has(err) {
|
2019-02-22 18:39:29 +00:00
|
|
|
//check replacement cache
|
|
|
|
rt.removeFromReplacementCache(kadBucketID, node)
|
2018-08-21 19:44:42 +01:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return RoutingErr.New("could not get node %s", err)
|
|
|
|
}
|
2019-02-22 18:39:29 +00:00
|
|
|
|
|
|
|
var existing pb.Node
|
|
|
|
err = proto.Unmarshal(existingMarshalled, &existing)
|
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not unmarshal node %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pb.AddressEqual(existing.Address, node.Address) {
|
|
|
|
// don't remove a node if the address is different
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.nodeBucketDB.Delete(ctx, node.Id.Bytes())
|
2018-08-17 20:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not delete node %s", err)
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes := rt.replacementCache[kadBucketID]
|
2018-08-17 20:11:46 +01:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
err = rt.putNode(nodes[len(nodes)-1])
|
2018-08-17 20:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
rt.replacementCache[kadBucketID] = nodes[:len(nodes)-1]
|
2018-07-30 20:25:18 +01:00
|
|
|
return nil
|
2019-02-22 18:39:29 +00:00
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
// putNode: helper, adds or updates Node and ID to nodeBucketDB
|
2019-06-04 12:36:27 +01:00
|
|
|
func (rt *RoutingTable) putNode(node *pb.Node) (err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-29 18:39:27 +00:00
|
|
|
v, err := proto.Marshal(node)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2018-11-29 18:39:27 +00:00
|
|
|
return RoutingErr.Wrap(err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.nodeBucketDB.Put(ctx, node.Id.Bytes(), v)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not add key value pair to nodeBucketDB: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createOrUpdateKBucket: helper, adds or updates given kbucket
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) createOrUpdateKBucket(ctx context.Context, bID bucketID, now time.Time) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-08-09 20:20:39 +01:00
|
|
|
dateTime := make([]byte, binary.MaxVarintLen64)
|
|
|
|
binary.PutVarint(dateTime, now.UnixNano())
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.kadBucketDB.Put(ctx, bID[:], dateTime)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not add or update k bucket: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
// getKBucketID: helper, returns the id of the corresponding k bucket given a node id.
|
|
|
|
// The node doesn't have to be in the routing table at time of search
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) getKBucketID(ctx context.Context, nodeID storj.NodeID) (_ bucketID, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-02-26 16:07:53 +00:00
|
|
|
match := bucketID{}
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.kadBucketDB.Iterate(ctx, storage.IterateOptions{First: storage.Key{}, Recurse: true},
|
|
|
|
func(ctx context.Context, it storage.Iterator) error {
|
2019-02-26 16:07:53 +00:00
|
|
|
var item storage.ListItem
|
2019-06-05 15:23:10 +01:00
|
|
|
for it.Next(ctx, &item) {
|
2019-02-26 16:07:53 +00:00
|
|
|
match = keyToBucketID(item.Key)
|
|
|
|
if nodeID.Less(match) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2019-02-26 16:07:53 +00:00
|
|
|
return bucketID{}, RoutingErr.Wrap(err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
return match, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:07:53 +00:00
|
|
|
// wouldBeInNearestK: helper, returns true if the node in question is within the nearest k from local node
|
|
|
|
func (rt *RoutingTable) wouldBeInNearestK(nodeID storj.NodeID) (bool, error) {
|
|
|
|
closestNodes, err := rt.FindNear(rt.self.Id, rt.bucketSize)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
2019-02-26 16:07:53 +00:00
|
|
|
return false, RoutingErr.Wrap(err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
if len(closestNodes) < rt.bucketSize {
|
2018-07-30 20:25:18 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
var furthestIDWithinK storj.NodeID
|
|
|
|
if len(closestNodes) <= rt.bucketSize {
|
|
|
|
furthestIDWithinK = closestNodes[len(closestNodes)-1].Id
|
|
|
|
} else {
|
|
|
|
furthestIDWithinK = closestNodes[rt.bucketSize].Id
|
2018-11-29 18:39:27 +00:00
|
|
|
}
|
2019-01-02 18:57:11 +00:00
|
|
|
|
|
|
|
existingXor := xorNodeID(furthestIDWithinK, rt.self.Id)
|
|
|
|
newXor := xorNodeID(nodeID, rt.self.Id)
|
|
|
|
return newXor.Less(existingXor), nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// kadBucketContainsLocalNode returns true if the kbucket in question contains the local node
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) kadBucketContainsLocalNode(queryID bucketID) (_ bool, err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
bID, err := rt.getKBucketID(ctx, rt.self.Id)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-01-02 18:57:11 +00:00
|
|
|
return queryID == bID, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// kadBucketHasRoom: helper, returns true if it has fewer than k nodes
|
2018-11-29 18:39:27 +00:00
|
|
|
func (rt *RoutingTable) kadBucketHasRoom(bID bucketID) (bool, error) {
|
|
|
|
nodes, err := rt.getNodeIDsWithinKBucket(bID)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(nodes) < rt.bucketSize {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNodeIDsWithinKBucket: helper, returns a collection of all the node ids contained within the kbucket
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) getNodeIDsWithinKBucket(bID bucketID) (_ storj.NodeIDList, err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-29 18:39:27 +00:00
|
|
|
endpoints, err := rt.getKBucketRange(bID)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
left := endpoints[0]
|
|
|
|
right := endpoints[1]
|
2019-02-26 16:07:53 +00:00
|
|
|
var ids []storj.NodeID
|
|
|
|
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.iterateNodes(ctx, left, func(ctx context.Context, nodeID storj.NodeID, protoNode []byte) error {
|
2019-02-26 16:07:53 +00:00
|
|
|
if left.Less(nodeID) && (nodeID.Less(right) || nodeID == right) {
|
|
|
|
ids = append(ids, nodeID)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
return nil
|
|
|
|
}, false)
|
2018-11-29 18:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
return ids, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 18:39:27 +00:00
|
|
|
// getNodesFromIDsBytes: helper, returns array of encoded nodes from node ids
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) getNodesFromIDsBytes(nodeIDs storj.NodeIDList) (_ []*pb.Node, err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-29 18:39:27 +00:00
|
|
|
var marshaledNodes []storage.Value
|
2018-08-09 20:20:39 +01:00
|
|
|
for _, v := range nodeIDs {
|
2019-06-05 15:23:10 +01:00
|
|
|
n, err := rt.nodeBucketDB.Get(ctx, v.Bytes())
|
2018-08-09 20:20:39 +01:00
|
|
|
if err != nil {
|
2018-11-29 18:39:27 +00:00
|
|
|
return nil, RoutingErr.New("could not get node id %v, %s", v, err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
marshaledNodes = append(marshaledNodes, n)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
return unmarshalNodes(marshaledNodes)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
// unmarshalNodes: helper, returns slice of reconstructed node pointers given a map of nodeIDs:serialized nodes
|
2018-11-29 18:39:27 +00:00
|
|
|
func unmarshalNodes(nodes []storage.Value) ([]*pb.Node, error) {
|
2018-09-18 05:39:06 +01:00
|
|
|
var unmarshaled []*pb.Node
|
2018-11-29 18:39:27 +00:00
|
|
|
for _, n := range nodes {
|
2018-09-18 05:39:06 +01:00
|
|
|
node := &pb.Node{}
|
|
|
|
err := proto.Unmarshal(n, node)
|
2018-08-09 20:20:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return unmarshaled, RoutingErr.New("could not unmarshal node %s", err)
|
|
|
|
}
|
|
|
|
unmarshaled = append(unmarshaled, node)
|
|
|
|
}
|
|
|
|
return unmarshaled, nil
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
// getUnmarshaledNodesFromBucket: helper, gets nodes within kbucket
|
2018-11-29 18:39:27 +00:00
|
|
|
func (rt *RoutingTable) getUnmarshaledNodesFromBucket(bID bucketID) ([]*pb.Node, error) {
|
|
|
|
nodeIDsBytes, err := rt.getNodeIDsWithinKBucket(bID)
|
2018-08-09 20:20:39 +01:00
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return []*pb.Node{}, RoutingErr.New("could not get nodeIds within kbucket %s", err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
nodes, err := rt.getNodesFromIDsBytes(nodeIDsBytes)
|
2018-08-09 20:20:39 +01:00
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return []*pb.Node{}, RoutingErr.New("could not get node values %s", err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-29 18:39:27 +00:00
|
|
|
return nodes, nil
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-30 20:25:18 +01:00
|
|
|
// getKBucketRange: helper, returns the left and right endpoints of the range of node ids contained within the bucket
|
2019-06-05 15:23:10 +01:00
|
|
|
func (rt *RoutingTable) getKBucketRange(bID bucketID) (_ []bucketID, err error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-02-13 17:27:03 +00:00
|
|
|
previousBucket := bucketID{}
|
2019-02-26 16:07:53 +00:00
|
|
|
endpoints := []bucketID{}
|
2019-06-05 15:23:10 +01:00
|
|
|
err = rt.kadBucketDB.Iterate(ctx, storage.IterateOptions{First: storage.Key{}, Recurse: true},
|
|
|
|
func(ctx context.Context, it storage.Iterator) error {
|
2019-02-26 16:07:53 +00:00
|
|
|
var item storage.ListItem
|
2019-06-05 15:23:10 +01:00
|
|
|
for it.Next(ctx, &item) {
|
2019-02-26 16:07:53 +00:00
|
|
|
thisBucket := keyToBucketID(item.Key)
|
|
|
|
if thisBucket == bID {
|
|
|
|
endpoints = []bucketID{previousBucket, bID}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
previousBucket = thisBucket
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return endpoints, RoutingErr.Wrap(err)
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
2019-02-26 16:07:53 +00:00
|
|
|
return endpoints, nil
|
2018-07-30 20:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// determineLeafDepth determines the level of the bucket id in question.
|
|
|
|
// Eg level 0 means there is only 1 bucket, level 1 means the bucket has been split once, and so on
|
2018-11-29 18:39:27 +00:00
|
|
|
func (rt *RoutingTable) determineLeafDepth(bID bucketID) (int, error) {
|
|
|
|
bucketRange, err := rt.getKBucketRange(bID)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, RoutingErr.New("could not get k bucket range %s", err)
|
|
|
|
}
|
|
|
|
smaller := bucketRange[0]
|
2019-01-02 18:57:11 +00:00
|
|
|
diffBit, err := determineDifferingBitIndex(bID, smaller)
|
2018-07-30 20:25:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return diffBit + 1, RoutingErr.New("could not determine differing bit %s", err)
|
|
|
|
}
|
|
|
|
return diffBit + 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitBucket: helper, returns the smaller of the two new bucket ids
|
|
|
|
// the original bucket id becomes the greater of the 2 new
|
2018-11-29 18:39:27 +00:00
|
|
|
func (rt *RoutingTable) splitBucket(bID bucketID, depth int) bucketID {
|
|
|
|
var newID bucketID
|
|
|
|
copy(newID[:], bID[:])
|
|
|
|
byteIndex := depth / 8
|
|
|
|
bitInByteIndex := 7 - (depth % 8)
|
2018-07-30 20:25:18 +01:00
|
|
|
toggle := byte(1 << uint(bitInByteIndex))
|
|
|
|
newID[byteIndex] ^= toggle
|
|
|
|
return newID
|
|
|
|
}
|