2018-04-18 17:55:28 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
package overlay
|
2018-04-18 16:34:15 +01:00
|
|
|
|
|
|
|
import (
|
2018-05-16 19:47:59 +01:00
|
|
|
"context"
|
2018-08-07 00:32:47 +01:00
|
|
|
"crypto/rand"
|
2018-08-13 09:39:45 +01:00
|
|
|
"log"
|
2018-04-18 16:34:15 +01:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-06-13 19:22:32 +01:00
|
|
|
"github.com/zeebo/errs"
|
2018-07-09 23:43:32 +01:00
|
|
|
"go.uber.org/zap"
|
2018-04-18 16:34:15 +01:00
|
|
|
|
2018-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-05-16 19:47:59 +01:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2018-04-18 16:34:15 +01:00
|
|
|
"storj.io/storj/protos/overlay"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/redis"
|
2018-04-18 16:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-07-09 23:43:32 +01:00
|
|
|
// ErrNodeNotFound error standardization
|
|
|
|
var ErrNodeNotFound = errs.New("Node not found")
|
|
|
|
|
|
|
|
// OverlayError creates class of errors for stack traces
|
|
|
|
var OverlayError = errs.Class("Overlay Error")
|
2018-06-05 22:06:37 +01:00
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
// Cache is used to store overlay data in Redis
|
|
|
|
type Cache struct {
|
|
|
|
DB storage.KeyValueStore
|
2018-06-22 14:33:57 +01:00
|
|
|
DHT dht.DHT
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// NewRedisOverlayCache returns a pointer to a new Cache instance with an initialized connection to Redis.
|
2018-06-22 14:33:57 +01:00
|
|
|
func NewRedisOverlayCache(address, password string, db int, DHT dht.DHT) (*Cache, error) {
|
2018-06-13 19:22:32 +01:00
|
|
|
rc, err := redis.NewClient(address, password, db)
|
2018-04-18 16:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-13 19:22:32 +01:00
|
|
|
return &Cache{
|
2018-06-05 22:06:37 +01:00
|
|
|
DB: rc,
|
|
|
|
DHT: DHT,
|
2018-04-18 17:55:28 +01:00
|
|
|
}, nil
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
// NewBoltOverlayCache returns a pointer to a new Cache instance with an initialized connection to a Bolt db.
|
2018-06-22 14:33:57 +01:00
|
|
|
func NewBoltOverlayCache(dbPath string, DHT dht.DHT) (*Cache, error) {
|
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
2018-07-24 17:08:28 +01:00
|
|
|
bc, err := boltdb.NewClient(zap.L(), dbPath, boltdb.OverlayBucket)
|
2018-06-13 19:22:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Cache{
|
|
|
|
DB: bc,
|
|
|
|
DHT: DHT,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:34:15 +01:00
|
|
|
// Get looks up the provided nodeID from the redis cache
|
2018-08-01 15:15:38 +01:00
|
|
|
func (o *Cache) Get(ctx context.Context, key string) (*overlay.Node, error) {
|
2018-06-13 19:22:32 +01:00
|
|
|
b, err := o.DB.Get([]byte(key))
|
2018-04-18 16:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-29 21:06:25 +01:00
|
|
|
if b.IsZero() {
|
|
|
|
// TODO: log? return an error?
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-04-18 16:34:15 +01:00
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
na := &overlay.Node{}
|
2018-06-05 22:06:37 +01:00
|
|
|
if err := proto.Unmarshal(b, na); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-18 16:34:15 +01:00
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
return na, nil
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
// Put adds a nodeID to the redis cache with a binary representation of proto defined Node
|
|
|
|
func (o *Cache) Put(nodeID string, value overlay.Node) error {
|
2018-04-18 16:34:15 +01:00
|
|
|
data, err := proto.Marshal(&value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-27 18:28:16 +01:00
|
|
|
return o.DB.Put(kademlia.StringToNodeID(nodeID).Bytes(), data)
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
2018-05-16 19:47:59 +01:00
|
|
|
|
|
|
|
// Bootstrap walks the initialized network and populates the cache
|
2018-06-13 19:22:32 +01:00
|
|
|
func (o *Cache) Bootstrap(ctx context.Context) error {
|
2018-08-09 20:20:39 +01:00
|
|
|
nodes, err := o.DHT.GetNodes(ctx, "", 1280)
|
2018-07-09 23:43:32 +01:00
|
|
|
if err != nil {
|
2018-08-22 07:39:57 +01:00
|
|
|
zap.Error(OverlayError.New("Error getting nodes from DHT: %v", err))
|
2018-07-09 23:43:32 +01:00
|
|
|
}
|
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
for _, v := range nodes {
|
2018-06-22 14:33:57 +01:00
|
|
|
found, err := o.DHT.FindNode(ctx, kademlia.StringToNodeID(v.Id))
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
2018-07-09 23:43:32 +01:00
|
|
|
zap.Error(ErrNodeNotFound)
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-08-03 14:15:52 +01:00
|
|
|
|
|
|
|
node, err := proto.Marshal(&found)
|
2018-07-16 20:22:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:15:52 +01:00
|
|
|
if err := o.DB.Put(kademlia.StringToNodeID(found.Id).Bytes(), node); err != nil {
|
2018-07-16 20:22:34 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
return err
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
// Refresh updates the cache db with the current DHT.
|
|
|
|
// We currently do not penalize nodes that are unresponsive,
|
|
|
|
// but should in the future.
|
2018-06-13 19:22:32 +01:00
|
|
|
func (o *Cache) Refresh(ctx context.Context) error {
|
2018-08-07 00:32:47 +01:00
|
|
|
log.Print("starting cache refresh")
|
|
|
|
r, err := randomID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
rid := kademlia.NodeID(r)
|
|
|
|
near, err := o.DHT.GetNodes(ctx, rid.String(), 128)
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
for _, node := range near {
|
|
|
|
pinged, err := o.DHT.Ping(ctx, *node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = o.DB.Put([]byte(pinged.Id), []byte(pinged.Address.Address))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
// TODO: Kademlia hooks to do this automatically rather than at interval
|
|
|
|
nodes, err := o.DHT.GetNodes(ctx, "", 128)
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
for _, node := range nodes {
|
|
|
|
pinged, err := o.DHT.Ping(ctx, *node)
|
2018-07-09 23:43:32 +01:00
|
|
|
if err != nil {
|
|
|
|
zap.Error(ErrNodeNotFound)
|
|
|
|
return err
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-08-09 20:20:39 +01:00
|
|
|
err = o.DB.Put([]byte(pinged.Id), []byte(pinged.Address.Address))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-13 09:39:45 +01:00
|
|
|
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-08-07 00:32:47 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk iterates over each node in each bucket to traverse the network
|
|
|
|
func (o *Cache) Walk(ctx context.Context) error {
|
|
|
|
// TODO: This should walk the cache, rather than be a duplicate of refresh
|
2018-06-05 22:06:37 +01:00
|
|
|
return nil
|
2018-05-16 19:47:59 +01:00
|
|
|
}
|
2018-08-07 00:32:47 +01:00
|
|
|
|
|
|
|
func randomID() ([]byte, error) {
|
|
|
|
result := make([]byte, 64)
|
|
|
|
_, err := rand.Read(result)
|
|
|
|
return result, err
|
|
|
|
}
|