2018-05-16 19:47:59 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package kademlia
|
|
|
|
|
|
|
|
import (
|
2018-08-13 09:39:45 +01:00
|
|
|
"encoding/binary"
|
2018-06-05 12:48:19 +01:00
|
|
|
"encoding/hex"
|
2018-08-09 20:20:39 +01:00
|
|
|
"sync"
|
2018-05-16 19:47:59 +01:00
|
|
|
"time"
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-06-05 12:48:19 +01:00
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-10-26 17:54:00 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-09-11 14:57:12 +01:00
|
|
|
"storj.io/storj/pkg/utils"
|
2018-08-09 20:20:39 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-05-16 19:47:59 +01:00
|
|
|
)
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
const (
|
|
|
|
// KademliaBucket is the string representing the bucket used for the kademlia routing table k-bucket ids
|
|
|
|
KademliaBucket = "kbuckets"
|
|
|
|
// NodeBucket is the string representing the bucket used for the kademlia routing table node ids
|
|
|
|
NodeBucket = "nodes"
|
|
|
|
)
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
// RoutingErr is the class for all errors pertaining to routing table operations
|
|
|
|
var RoutingErr = errs.Class("routing table error")
|
|
|
|
|
|
|
|
// RoutingTable implements the RoutingTable interface
|
|
|
|
type RoutingTable struct {
|
2018-10-26 17:54:00 +01:00
|
|
|
self pb.Node
|
2018-08-17 20:11:46 +01:00
|
|
|
kadBucketDB storage.KeyValueStore
|
|
|
|
nodeBucketDB storage.KeyValueStore
|
2018-09-18 05:39:06 +01:00
|
|
|
transport *pb.NodeTransport
|
2018-08-17 20:11:46 +01:00
|
|
|
mutex *sync.Mutex
|
2018-11-20 16:54:52 +00:00
|
|
|
seen map[string]*pb.Node
|
2018-09-18 05:39:06 +01:00
|
|
|
replacementCache map[string][]*pb.Node
|
2018-08-17 20:11:46 +01:00
|
|
|
idLength int // kbucket and node id bit length (SHA256) = 256
|
|
|
|
bucketSize int // max number of nodes stored in a kbucket = 20 (k)
|
|
|
|
rcBucketSize int // replacementCache bucket max length
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2018-06-05 12:48:19 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
// NewRoutingTable returns a newly configured instance of a RoutingTable
|
2018-10-26 17:54:00 +01:00
|
|
|
func NewRoutingTable(localNode pb.Node, kdb, ndb storage.KeyValueStore) (*RoutingTable, error) {
|
2018-08-09 20:20:39 +01:00
|
|
|
rt := &RoutingTable{
|
2018-11-20 16:54:52 +00:00
|
|
|
self: localNode,
|
|
|
|
kadBucketDB: kdb,
|
|
|
|
nodeBucketDB: ndb,
|
|
|
|
transport: &defaultTransport,
|
|
|
|
|
2018-08-17 20:11:46 +01:00
|
|
|
mutex: &sync.Mutex{},
|
2018-11-20 16:54:52 +00:00
|
|
|
seen: make(map[string]*pb.Node),
|
2018-10-18 17:20:23 +01:00
|
|
|
replacementCache: make(map[string][]*pb.Node),
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
idLength: len(storj.NodeID{}) * 8, // NodeID length in bits
|
|
|
|
bucketSize: *flagBucketSize,
|
|
|
|
rcBucketSize: *flagReplacementCacheSize,
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-10-26 17:54:00 +01:00
|
|
|
ok, err := rt.addNode(&localNode)
|
2018-08-27 18:28:16 +01:00
|
|
|
if !ok || err != nil {
|
2018-08-09 20:20:39 +01:00
|
|
|
return nil, RoutingErr.New("could not add localNode to routing table: %s", err)
|
|
|
|
}
|
|
|
|
return rt, nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 14:57:12 +01:00
|
|
|
// Close closes underlying databases
|
|
|
|
func (rt *RoutingTable) Close() error {
|
2018-10-18 17:20:23 +01:00
|
|
|
return utils.CombineErrors(
|
|
|
|
rt.kadBucketDB.Close(),
|
|
|
|
rt.nodeBucketDB.Close(),
|
|
|
|
)
|
2018-09-11 14:57:12 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
// Local returns the local nodes ID
|
2018-09-18 05:39:06 +01:00
|
|
|
func (rt *RoutingTable) Local() pb.Node {
|
2018-10-26 17:54:00 +01:00
|
|
|
return rt.self
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// K returns the currently configured maximum of nodes to store in a bucket
|
2018-08-09 20:20:39 +01:00
|
|
|
func (rt *RoutingTable) K() int {
|
|
|
|
return rt.bucketSize
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
// CacheSize returns the total current size of the replacement cache
|
2018-08-09 20:20:39 +01:00
|
|
|
func (rt *RoutingTable) CacheSize() int {
|
2018-08-21 19:44:42 +01:00
|
|
|
return rt.rcBucketSize
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
// GetBucket retrieves the corresponding kbucket from node id
|
|
|
|
// Note: id doesn't need to be stored at time of search
|
|
|
|
func (rt *RoutingTable) GetBucket(id string) (bucket dht.Bucket, ok bool) {
|
|
|
|
i, err := hex.DecodeString(id)
|
|
|
|
if err != nil {
|
Testcoverage kademlia (#154)
* Unit test covarege increased for kademlia pkg
go style formatting added
Removed DHT param from newTestKademlia method, added comments for Bucket methods that informs that these tests will need to be updated
unnecessary comment deleted from newTestKademlia
Adjust Segment Store to the updated interface (#160)
* Adjust Segment Store to the updated interface
* Move /pkg/storage/segment to /pkg/storage/segments
* Fix overlay client tests
* Revert changes in NewOverlayClient return value
* Rename `rem` to `seg`
* Implement Meta()
captplanet (#159)
* captplanet
I kind of went overboard this weekend.
The major goal of this changeset is to provide an environment
for local development where all of the various services can
be easily run together. Developing on Storj v3 should be as
easy as running a setup command and a run command!
To do this, this changeset introduces a new tool called
captplanet, which combines the powers of the Overlay Cache,
the PointerDB, the PieceStore, Kademlia, the Minio Gateway,
etc.
Running 40 farmers and a heavy client inside the same process
forced a rethinking of the "services" that we had. To
avoid confusion by reusing prior terms, this changeset
introduces two new types: Providers and Responsibilities.
I wanted to avoid as many merge conflicts as possible, so
I left the existing Services and code for now, but if people
like this route we can clean up the duplication.
A Responsibility is a collection of gRPC methods and
corresponding state. The following systems are examples of
Responsibilities:
* Kademlia
* OverlayCache
* PointerDB
* StatDB
* PieceStore
* etc.
A Provider is a collection of Responsibilities that
share an Identity, such as:
* The heavy client
* The farmer
* The gateway
An Identity is a public/private key pair, a node id, etc.
Farmers all need different Identities, so captplanet
needs to support running multiple concurrent Providers
with different Identities.
Each Responsibility and Provider should allow for configuration
of multiple copies on its own so creating Responsibilities and
Providers use a new workflow.
To make a Responsibility, one should create a "config"
struct, such as:
```
type Config struct {
RepairThreshold int `help:"If redundancy falls below this number of
pieces, repair is triggered" default:"30"`
SuccessThreshold int `help:"If redundancy is above this number then
no additional uploads are needed" default:"40"`
}
```
To use "config" structs, this changeset introduces another
new library called 'cfgstruct', which allows for the configuration
of arbitrary structs through flagsets, and thus through cobra and
viper.
cfgstruct relies on Go's "struct tags" feature to document
help information and default values. Config structs can be
configured via cfgstruct.Bind for binding the struct to a flagset.
Because this configuration system makes setup and configuration
easier *in general*, additional commands are provided that allow
for easy standup of separate Providers. Please make sure to
check out:
* cmd/captplanet/farmer/main.go (a new farmer binary)
* cmd/captplanet/hc/main.go (a new heavy client binary)
* cmd/captplanet/gw/main.go (a new minio gateway binary)
Usage:
```
$ go install -v storj.io/storj/cmd/captplanet
$ captplanet setup
$ captplanet run
```
Configuration is placed by default in `~/.storj/capt/`
Other changes:
* introduces new config structs for currently existing
Responsibilities that conform to the new Responsibility
interface. Please see the `pkg/*/config.go` files for
examples.
* integrates the PointerDB API key with other global
configuration via flags, instead of through environment
variables through viper like it's been doing. (ultimately
this should also change to use the PointerDB config
struct but this is an okay shortterm solution).
* changes the Overlay cache to use a URL for database
configuration instead of separate redis and bolt config
settings.
* stubs out some peer identity skeleton code (but not the
meat).
* Fixes the SegmentStore to use the overlay client and
pointerdb clients instead of gRPC client code directly
* Leaves a very clear spot where we need to tie the object to
stream to segment store together. There's sort of a "golden
spike" opportunity to connect all the train tracks together
at the bottom of pkg/miniogw/config.go, labeled with a
bunch of TODOs.
Future stuff:
* I now prefer this design over the original
pkg/process.Service thing I had been pushing before (sorry!)
* The experience of trying to have multiple farmers
configurable concurrently led me to prefer config structs
over global flags (I finally came around) or using viper
directly. I think global flags are okay sometimes but in
general going forward we should try and get all relevant
config into config structs.
* If you all like this direction, I think we can go delete my
old Service interfaces and a bunch of flags and clean up a
bunch of stuff.
* If you don't like this direction, it's no sweat at all, and
despite how much code there is here I'm not very tied to any
of this! Considering a lot of this was written between midnight
and 6 am, it might not be any good!
* bind tests
Add files for testing builds in docker (#161)
* Add files for testing builds in docker
* Make tests check for redis running before trying to start redis-server, which may not exist.
* Clean redis server before any tests use it.
* Add more debugging for travis
* Explicitly requiring redis for travis
pkg/provider: with pkg/provider merged, make a single heavy client binary, gateway binary, and deprecate old services (#165)
* pkg/provider: with pkg/provider merged, make a single heavy client binary and deprecate old services
* add setup to gw binary too
* captplanet: output what addresses everything is listening on
* revert peertls/io_util changes
* define config flag across all commands
* use trimsuffix
fix docker makefile (#170)
* fix makefile
protos: update protobufs with go generate (#169)
the import for timestamp and duration should use
the path provided by a standard protocol buffer library
installation
Refactor List in PointerDB (#163)
* Refactor List in Pointer DB
* Fix pointerdb-client example
* Fix issue in Path type related to empty paths
* Test for the PointerDB service with some fixes
* Fixed debug message in example: trancated --> more
* GoDoc comments for unexported methods
* TODO comment to check if Put is overwriting
* Log warning if protobuf timestamp cannot be converted
* TODO comment to make ListPageLimit configurable
* Rename 'segment' package to 'segments' to reflect folder name
Minio integration with Object store (#156)
* initial WIP integration with Object store
* List WIP
* minio listobject function changes complete
* Code review changes and work in progress for the mock objectstore unit testing cases
* Warning fix redeclaration of err
* Warning fix redeclaration of err
* code review comments & unit testing inprogress
* fix compilation bug
* Fixed code review comments & added GetObject Mock test case
* rearraged the mock test file and gateway storj test file in to the proper directory
* added the missing file
* code clean up
* fix lint error on the mock generated code
* modified per code review comments
* added the PutObject mock test case
* added the GetObjectInfo mock test case
* added listobject mock test case
* fixed package from storj to miniogw
* resolved the gateway-storj.go initialization merge conflict
update readme (#174)
added assertion for unused errors (#152)
merging this PR to avoid future issues
updating github user to personal account (#171)
Test coverage ranger (#168)
* Fixed go panic for corner case
* Initial test coverage for ranger pkg
streamstore: add passthrough implementation (#176)
this doesn't implement streamstore, this just allows us to try and
get the june demo working again in the meantime
StatDB (#144)
* add statdb proto and example client
* server logic
* update readme
* remove boltdb from service.go
* sqlite3
* add statdb server executable file
* create statdb node table if it does not exist already
* get UpdateBatch working
* update based on jt review
* remove some commented lines
* fix linting issues
* reformat
* apiKey -> APIKey
* update statdb client apiKey->APIKey
Update README.md
Update README.md
overlay: correct dockerfile db (#179)
cmd/hc, cmd/gw, cmd/captplanet: simplify setup/run commands (#178)
also allows much more customization of services within captain planet,
such as reconfiguring the overlay service to use redis
pkg/process: don't require json formatting (#177)
Cleanup metadata across layers (#180)
* Cleanup metadata across layers
* Fix pointer db tests
Kademlia Routing Table (#164)
* adds comment
* runs deps
* creates boltdb kademlia routing table
* protobuf updates
* adds reverselist to mockkeyvaluestore interface
* xor wip
* xor wip
* fixes xor sort
* runs go fmt
* fixes
* goimports again
* trying to fix travis tests
* fixes mock tests
Ranger refactoring (#158)
* Fixed go panic for corner case
* Cosmetic changes, and small error fixes
miniogw: log all errors (#182)
* miniogw: log all errors
* tests added
* doc comment to satisfy linter
* fix test failure
Jennifer added to CLA list
* Temporary fix for storage/redis list method test
2018-08-02 19:36:57 +01:00
|
|
|
return &KBucket{}, false
|
|
|
|
}
|
2018-08-09 20:20:39 +01:00
|
|
|
bucketID, err := rt.getKBucketID(i)
|
2018-06-05 12:48:19 +01:00
|
|
|
if err != nil {
|
2018-06-22 14:33:57 +01:00
|
|
|
return &KBucket{}, false
|
2018-06-05 12:48:19 +01:00
|
|
|
}
|
2018-08-09 20:20:39 +01:00
|
|
|
if bucketID == nil {
|
2018-06-22 14:33:57 +01:00
|
|
|
return &KBucket{}, false
|
2018-06-05 12:48:19 +01:00
|
|
|
}
|
2018-08-09 20:20:39 +01:00
|
|
|
unmarshaledNodes, err := rt.getUnmarshaledNodesFromBucket(bucketID)
|
|
|
|
if err != nil {
|
|
|
|
return &KBucket{}, false
|
|
|
|
}
|
|
|
|
return &KBucket{nodes: unmarshaledNodes}, true
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuckets retrieves all buckets from the local node
|
2018-08-09 20:20:39 +01:00
|
|
|
func (rt *RoutingTable) GetBuckets() (k []dht.Bucket, err error) {
|
2018-06-22 14:33:57 +01:00
|
|
|
bs := []dht.Bucket{}
|
2018-08-09 20:20:39 +01:00
|
|
|
kbuckets, err := rt.kadBucketDB.List(nil, 0)
|
|
|
|
if err != nil {
|
|
|
|
return bs, RoutingErr.New("could not get bucket ids %s", err)
|
|
|
|
}
|
|
|
|
for _, v := range kbuckets {
|
|
|
|
unmarshaledNodes, err := rt.getUnmarshaledNodesFromBucket(v)
|
|
|
|
if err != nil {
|
|
|
|
return bs, err
|
|
|
|
}
|
|
|
|
bs = append(bs, &KBucket{nodes: unmarshaledNodes})
|
2018-06-05 12:48:19 +01:00
|
|
|
}
|
|
|
|
return bs, nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-10-08 16:09:37 +01:00
|
|
|
// FindNear returns the node corresponding to the provided nodeID
|
|
|
|
// returns all Nodes closest via XOR to the provided nodeID up to the provided limit
|
|
|
|
// always returns limit + self
|
2018-09-18 05:39:06 +01:00
|
|
|
func (rt *RoutingTable) FindNear(id dht.NodeID, limit int) ([]*pb.Node, error) {
|
2018-08-23 16:20:11 +01:00
|
|
|
// if id is not in the routing table
|
2018-08-09 20:20:39 +01:00
|
|
|
nodeIDs, err := rt.nodeBucketDB.List(nil, 0)
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return []*pb.Node{}, RoutingErr.New("could not get node ids %s", err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-11-02 16:46:59 +00:00
|
|
|
sortByXOR(nodeIDs, id.Bytes())
|
|
|
|
if len(nodeIDs) >= limit {
|
|
|
|
nodeIDs = nodeIDs[:limit]
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-11-02 16:46:59 +00:00
|
|
|
ids, serializedNodes, err := rt.getNodesFromIDs(nodeIDs)
|
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 nodes %s", err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
unmarshaledNodes, err := unmarshalNodes(ids, serializedNodes)
|
|
|
|
if err != nil {
|
2018-09-18 05:39:06 +01:00
|
|
|
return []*pb.Node{}, RoutingErr.New("could not unmarshal nodes %s", err)
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-08-09 20:20:39 +01:00
|
|
|
return unmarshaledNodes, nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// ConnectionSuccess updates or adds a node to the routing table when
|
2018-08-21 19:44:42 +01:00
|
|
|
// a successful connection is made to the node on the network
|
2018-09-18 05:39:06 +01:00
|
|
|
func (rt *RoutingTable) ConnectionSuccess(node *pb.Node) error {
|
2018-11-20 16:54:52 +00:00
|
|
|
// valid to connect to node without ID but don't store connection
|
|
|
|
if node.GetId() == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rt.mutex.Lock()
|
|
|
|
rt.seen[node.GetId()] = node
|
|
|
|
rt.mutex.Unlock()
|
2018-08-21 19:44:42 +01:00
|
|
|
v, err := rt.nodeBucketDB.Get(storage.Key(node.Id))
|
|
|
|
if err != nil && !storage.ErrKeyNotFound.Has(err) {
|
|
|
|
return RoutingErr.New("could not get node %s", err)
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
if v != nil {
|
|
|
|
err = rt.updateNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not update node %s", err)
|
|
|
|
}
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
_, err = rt.addNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not add node %s", err)
|
|
|
|
}
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
return nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-21 19:44:42 +01:00
|
|
|
// ConnectionFailed removes a node from the routing table when
|
|
|
|
// a connection fails for the node on the network
|
2018-09-18 05:39:06 +01:00
|
|
|
func (rt *RoutingTable) ConnectionFailed(node *pb.Node) error {
|
2018-08-21 19:44:42 +01:00
|
|
|
nodeID := storage.Key(node.Id)
|
|
|
|
bucketID, err := rt.getKBucketID(nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not get k bucket %s", err)
|
|
|
|
}
|
|
|
|
err = rt.removeNode(bucketID, nodeID)
|
|
|
|
if err != nil {
|
|
|
|
return RoutingErr.New("could not remove node %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetBucketTimestamp updates the last updated time for a bucket
|
2018-08-09 20:20:39 +01:00
|
|
|
func (rt *RoutingTable) SetBucketTimestamp(id string, now time.Time) error {
|
|
|
|
rt.mutex.Lock()
|
|
|
|
defer rt.mutex.Unlock()
|
|
|
|
err := rt.createOrUpdateKBucket([]byte(id), now)
|
2018-06-05 12:48:19 +01:00
|
|
|
if err != nil {
|
2018-08-09 20:20:39 +01:00
|
|
|
return NodeErr.New("could not update bucket timestamp %s", err)
|
2018-06-05 12:48:19 +01:00
|
|
|
}
|
2018-05-16 19:47:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketTimestamp retrieves the last updated time for a bucket
|
2018-08-09 20:20:39 +01:00
|
|
|
func (rt *RoutingTable) GetBucketTimestamp(id string, bucket dht.Bucket) (time.Time, error) {
|
|
|
|
t, err := rt.kadBucketDB.Get([]byte(id))
|
|
|
|
if err != nil {
|
|
|
|
return time.Now(), RoutingErr.New("could not get bucket timestamp %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp, _ := binary.Varint(t)
|
|
|
|
|
|
|
|
return time.Unix(0, timestamp).UTC(), nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
2018-10-16 16:22:31 +01:00
|
|
|
|
|
|
|
func (rt *RoutingTable) iterate(opts storage.IterateOptions, f func(it storage.Iterator) error) error {
|
|
|
|
return rt.nodeBucketDB.Iterate(opts, f)
|
|
|
|
}
|