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-06-22 14:33:57 +01:00
|
|
|
"storj.io/storj/pkg/dht"
|
2018-10-08 16:09:37 +01:00
|
|
|
"storj.io/storj/pkg/node"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-06-13 19:22:32 +01:00
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/redis"
|
2018-09-10 10:52:53 +01:00
|
|
|
"storj.io/storj/storage/storelogger"
|
2018-04-18 16:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-09-05 17:10:35 +01:00
|
|
|
const (
|
|
|
|
// OverlayBucket is the string representing the bucket used for a bolt-backed overlay dht cache
|
|
|
|
OverlayBucket = "overlay"
|
|
|
|
)
|
|
|
|
|
2018-07-09 23:43:32 +01:00
|
|
|
// 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-10-26 17:54:00 +01:00
|
|
|
func NewRedisOverlayCache(address, password string, dbindex int, dht dht.DHT) (*Cache, error) {
|
|
|
|
db, err := redis.NewClient(address, password, dbindex)
|
2018-04-18 16:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-26 17:54:00 +01:00
|
|
|
return NewOverlayCache(storelogger.New(zap.L(), db), dht), 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-10-26 17:54:00 +01:00
|
|
|
func NewBoltOverlayCache(dbPath string, dht dht.DHT) (*Cache, error) {
|
|
|
|
db, err := boltdb.New(dbPath, OverlayBucket)
|
2018-06-13 19:22:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-26 17:54:00 +01:00
|
|
|
return NewOverlayCache(storelogger.New(zap.L(), db), dht), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOverlayCache returns a new Cache
|
|
|
|
func NewOverlayCache(db storage.KeyValueStore, dht dht.DHT) *Cache {
|
2018-11-14 21:30:07 +00:00
|
|
|
return &Cache{DB: db, DHT: dht}
|
2018-06-13 19:22:32 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
// Get looks up the provided nodeID from the overlay cache
|
2018-09-18 05:39:06 +01:00
|
|
|
func (o *Cache) Get(ctx context.Context, key string) (*pb.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-09-18 05:39:06 +01:00
|
|
|
na := &pb.Node{}
|
2018-06-05 22:06:37 +01:00
|
|
|
if err := proto.Unmarshal(b, na); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return na, nil
|
2018-04-18 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-09-11 05:52:14 +01:00
|
|
|
// GetAll looks up the provided nodeIDs from the overlay cache
|
2018-09-18 05:39:06 +01:00
|
|
|
func (o *Cache) GetAll(ctx context.Context, keys []string) ([]*pb.Node, error) {
|
2018-09-11 05:52:14 +01:00
|
|
|
if len(keys) == 0 {
|
|
|
|
return nil, OverlayError.New("no keys provided")
|
|
|
|
}
|
|
|
|
var ks storage.Keys
|
|
|
|
for _, v := range keys {
|
|
|
|
ks = append(ks, storage.Key(v))
|
|
|
|
}
|
|
|
|
vs, err := o.DB.GetAll(ks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
var ns []*pb.Node
|
2018-09-11 05:52:14 +01:00
|
|
|
for _, v := range vs {
|
|
|
|
if v == nil {
|
|
|
|
ns = append(ns, nil)
|
|
|
|
continue
|
|
|
|
}
|
2018-09-18 05:39:06 +01:00
|
|
|
na := &pb.Node{}
|
2018-09-11 05:52:14 +01:00
|
|
|
err := proto.Unmarshal(v, na)
|
|
|
|
if err != nil {
|
|
|
|
return nil, OverlayError.New("could not unmarshal non-nil node: %v", err)
|
|
|
|
}
|
|
|
|
ns = append(ns, na)
|
|
|
|
}
|
|
|
|
return ns, nil
|
|
|
|
}
|
|
|
|
|
2018-08-01 15:15:38 +01:00
|
|
|
// Put adds a nodeID to the redis cache with a binary representation of proto defined Node
|
2018-09-18 05:39:06 +01:00
|
|
|
func (o *Cache) Put(nodeID string, value pb.Node) error {
|
2018-04-18 16:34:15 +01:00
|
|
|
data, err := proto.Marshal(&value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
return o.DB.Put(node.IDFromString(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-11-12 19:53:20 +00:00
|
|
|
return 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-10-08 16:09:37 +01:00
|
|
|
found, err := o.DHT.FindNode(ctx, node.IDFromString(v.Id))
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Info("Node find failed", zap.String("nodeID", v.Id))
|
|
|
|
continue
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
n, err := proto.Marshal(&found)
|
2018-07-16 20:22:34 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node marshall failed", zap.String("nodeID", v.Id))
|
|
|
|
continue
|
2018-07-16 20:22:34 +01:00
|
|
|
}
|
2018-10-08 16:09:37 +01:00
|
|
|
if err := o.DB.Put(node.IDFromString(found.Id).Bytes(), n); err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node cache put failed", zap.String("nodeID", v.Id))
|
|
|
|
continue
|
2018-07-16 20:22:34 +01:00
|
|
|
}
|
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-10-08 16:09:37 +01:00
|
|
|
rid := node.ID(r)
|
2018-08-07 00:32:47 +01:00
|
|
|
near, err := o.DHT.GetNodes(ctx, rid.String(), 128)
|
2018-06-05 22:06:37 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-08 13:53:27 +00:00
|
|
|
for _, n := range near {
|
|
|
|
pinged, err := o.DHT.Ping(ctx, *n)
|
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Info("Node ping failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
2018-11-08 13:53:27 +00:00
|
|
|
}
|
|
|
|
data, err := proto.Marshal(&pinged)
|
2018-08-07 00:32:47 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node marshall failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
2018-08-07 00:32:47 +01:00
|
|
|
}
|
2018-11-08 13:53:27 +00:00
|
|
|
err = o.DB.Put(node.IDFromString(pinged.Id).Bytes(), data)
|
2018-08-07 00:32:47 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node cache put failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
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-11-08 13:53:27 +00:00
|
|
|
for _, n := range nodes {
|
|
|
|
pinged, err := o.DHT.Ping(ctx, *n)
|
2018-07-09 23:43:32 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Info("Node ping failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-11-08 13:53:27 +00:00
|
|
|
data, err := proto.Marshal(&pinged)
|
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node marshall failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
2018-11-08 13:53:27 +00:00
|
|
|
}
|
|
|
|
err = o.DB.Put(node.IDFromString(pinged.Id).Bytes(), data)
|
2018-08-09 20:20:39 +01:00
|
|
|
if err != nil {
|
2018-11-12 19:53:20 +00:00
|
|
|
zap.L().Error("Node cache put failed", zap.String("nodeID", n.GetId()))
|
|
|
|
continue
|
2018-08-09 20:20:39 +01:00
|
|
|
}
|
2018-06-05 22:06:37 +01:00
|
|
|
}
|
2018-11-14 21:30:07 +00:00
|
|
|
|
2018-11-12 19:53:20 +00:00
|
|
|
return nil
|
2018-08-07 00:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|