storj/pkg/kademlia/routing.go
Yaroslav Vorobiov f6fb41c5cc 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 14:36:57 -04:00

127 lines
3.3 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package kademlia
import (
"context"
"encoding/hex"
"fmt"
"strconv"
"time"
bkad "github.com/coyle/kademlia"
"storj.io/storj/pkg/dht"
proto "storj.io/storj/protos/overlay"
)
// RouteTable implements the RoutingTable interface
type RouteTable struct {
ht *bkad.HashTable
dht *bkad.DHT
}
// NewRouteTable returns a newly configured instance of a RouteTable
func NewRouteTable(dht Kademlia) RouteTable {
return RouteTable{
ht: dht.dht.HT,
dht: dht.dht,
}
}
// Local returns the local nodes ID
func (rt RouteTable) Local() proto.Node {
return proto.Node{
Id: string(rt.dht.HT.Self.ID),
Address: &proto.NodeAddress{
Transport: defaultTransport, // TODO(coyle): this should be stored on the route table
Address: fmt.Sprintf("%s:%d", rt.dht.HT.Self.IP.String(), rt.dht.HT.Self.Port),
},
}
}
// K returns the currently configured maximum of nodes to store in a bucket
func (rt RouteTable) K() int {
return rt.dht.NumNodes()
}
// CacheSize returns the total current size of the cache
func (rt RouteTable) CacheSize() int {
// TODO: How is this calculated ? size of the routing table ? is it total bytes, mb, kb etc .?
return 0
}
// GetBucket retrieves a bucket from the local node
func (rt RouteTable) GetBucket(id string) (bucket dht.Bucket, ok bool) {
if id == "" {
return &KBucket{}, false
}
i, err := hex.DecodeString(id)
if err != nil {
return &KBucket{}, false
}
b := rt.ht.GetBucket(i)
if b == nil {
return &KBucket{}, false
}
return &KBucket{
nodes: convertNetworkNodes(b),
}, true
}
// GetBuckets retrieves all buckets from the local node
func (rt RouteTable) GetBuckets() (k []dht.Bucket, err error) {
bs := []dht.Bucket{}
b := rt.ht.GetBuckets()
for _, v := range b {
bs = append(bs, &KBucket{nodes: convertNetworkNodes(v)})
}
return bs, nil
}
// FindNear finds all Nodes near the provided nodeID up to the provided limit
func (rt RouteTable) FindNear(id dht.NodeID, limit int) ([]*proto.Node, error) {
return convertNetworkNodes(rt.ht.GetClosestContacts(id.Bytes(), limit)), nil
}
// ConnectionSuccess handles the details of what kademlia should do when
// a successful connection is made to node on the network
func (rt RouteTable) ConnectionSuccess(id string, address proto.NodeAddress) {
// TODO: What should we do ?
return
}
// ConnectionFailed handles the details of what kademlia should do when
// a connection fails for a node on the network
func (rt RouteTable) ConnectionFailed(id string, address proto.NodeAddress) {
// TODO: What should we do ?
return
}
// SetBucketTimestamp updates the last updated time for a bucket
func (rt RouteTable) SetBucketTimestamp(id string, now time.Time) error {
i, err := strconv.Atoi(id)
if err != nil {
return NodeErr.New("unable to convert id to int")
}
rt.ht.SetBucketTime(i, now)
return nil
}
// GetBucketTimestamp retrieves the last updated time for a bucket
func (rt RouteTable) GetBucketTimestamp(id string, bucket dht.Bucket) (time.Time, error) {
return rt.dht.GetExpirationTime([]byte(id)), nil
}
// GetNodeRoutingTable gets a routing table for a given node rather than the local node's routing table
func GetNodeRoutingTable(ctx context.Context, ID NodeID) (RouteTable, error) {
return RouteTable{}, nil
}