mostly removed things (#2199)

This commit is contained in:
Bill Thorp 2019-06-13 12:06:37 -04:00 committed by GitHub
parent bc33964729
commit 703e5fee49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 193 deletions

View File

@ -1,36 +0,0 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/pkg/overlay"
"storj.io/storj/satellite/satellitedb"
)
type cacheConfig struct {
NodesPath string `help:"the path to a JSON file containing an object with IP keys and nodeID values"`
Database string `help:"overlay database connection string" default:"sqlite3://$CONFDIR/master.db"`
}
func (c cacheConfig) open(ctx context.Context) (cache *overlay.Cache, dbClose func(), err error) {
database, err := satellitedb.New(zap.L().Named("db"), c.Database)
if err != nil {
return nil, nil, errs.New("error connecting to database: %+v", err)
}
dbClose = func() {
err := database.Close()
if err != nil {
fmt.Printf("error closing connection to database: %+v\n", err)
}
}
return overlay.NewCache(zap.L(), database.OverlayCache(), overlay.NodeSelectionConfig{OnlineWindow: time.Hour}), dbClose, nil
}

View File

@ -1,129 +0,0 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"text/tabwriter"
"github.com/spf13/cobra"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/storj"
)
var (
rootCmd = &cobra.Command{
Use: "overlay",
Short: "Overlay cache management",
}
addCmd = &cobra.Command{
Use: "add",
Short: "Add nodes to the overlay cache",
RunE: cmdAdd,
}
listCmd = &cobra.Command{
Use: "list",
Short: "List nodes in the overlay cache",
RunE: cmdList,
}
cacheCfg struct {
cacheConfig
}
)
func init() {
defaults := cfgstruct.DefaultsFlag(rootCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(listCmd)
process.Bind(addCmd, &cacheCfg, defaults)
process.Bind(listCmd, &cacheCfg, defaults)
}
func cmdList(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
keys, err := cache.Inspect(ctx)
if err != nil {
return err
}
nodeIDs, err := storj.NodeIDsFromBytes(keys.ByteSlices())
if err != nil {
return err
}
const padding = 3
w := tabwriter.NewWriter(os.Stdout, 0, 0, padding, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Node ID\t Address")
for _, id := range nodeIDs {
n, err := cache.Get(process.Ctx(cmd), id)
if err != nil {
fmt.Fprintln(w, id.String(), "\t", "error getting value")
}
if n != nil {
fmt.Fprintln(w, id.String(), "\t", n.Address.Address)
continue
}
fmt.Fprintln(w, id.String(), "\tnil")
}
return w.Flush()
}
func cmdAdd(cmd *cobra.Command, args []string) (err error) {
ctx := process.Ctx(cmd)
j, err := ioutil.ReadFile(cacheCfg.NodesPath)
if err != nil {
return errs.New("Unable to read file with nodes: %+v", err)
}
var nodes map[string]string
if err := json.Unmarshal(j, &nodes); err != nil {
return errs.Wrap(err)
}
cache, dbClose, err := cacheCfg.open(ctx)
if err != nil {
return err
}
defer dbClose()
for i, a := range nodes {
id, err := storj.NodeIDFromString(i)
if err != nil {
zap.S().Error(err)
}
fmt.Printf("adding node ID: %s; Address: %s", i, a)
err = cache.Put(process.Ctx(cmd), id, pb.Node{
Id: id,
Address: &pb.NodeAddress{
Transport: 0,
Address: a,
},
})
if err != nil {
return err
}
}
return nil
}
func main() {
process.Exec(rootCmd)
}

View File

@ -34,6 +34,19 @@ func TestCache_Database(t *testing.T) {
}) })
} }
// returns a NodeSelectionConfig with sensible test values
func testNodeSelectionConfig() overlay.NodeSelectionConfig {
return overlay.NodeSelectionConfig{
UptimeRatio: 0,
UptimeCount: 0,
AuditSuccessRatio: 0,
AuditCount: 0,
NewNodePercentage: 0,
OnlineWindow: time.Hour,
DistinctIP: false,
}
}
func testCache(ctx context.Context, t *testing.T, store overlay.DB) { func testCache(ctx context.Context, t *testing.T, store overlay.DB) {
valid1ID := storj.NodeID{} valid1ID := storj.NodeID{}
valid2ID := storj.NodeID{} valid2ID := storj.NodeID{}
@ -44,7 +57,7 @@ func testCache(ctx context.Context, t *testing.T, store overlay.DB) {
_, _ = rand.Read(valid2ID[:]) _, _ = rand.Read(valid2ID[:])
_, _ = rand.Read(missingID[:]) _, _ = rand.Read(missingID[:])
cache := overlay.NewCache(zaptest.NewLogger(t), store, overlay.NodeSelectionConfig{OnlineWindow: time.Hour}) cache := overlay.NewCache(zaptest.NewLogger(t), store, testNodeSelectionConfig())
{ // Put { // Put
err := cache.Put(ctx, valid1ID, pb.Node{Id: valid1ID, Address: address}) err := cache.Put(ctx, valid1ID, pb.Node{Id: valid1ID, Address: address})

View File

@ -4,13 +4,10 @@
package overlay package overlay
import ( import (
"strings"
"time" "time"
"github.com/zeebo/errs" "github.com/zeebo/errs"
monkit "gopkg.in/spacemonkeygo/monkit.v2" monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/pkg/storj"
) )
var ( var (
@ -25,12 +22,6 @@ type Config struct {
Node NodeSelectionConfig Node NodeSelectionConfig
} }
// LookupConfig is a configuration struct for querying the overlay cache with one or more node IDs
type LookupConfig struct {
NodeIDsString string `help:"one or more string-encoded node IDs, delimited by Delimiter"`
Delimiter string `help:"delimiter used for parsing node IDs" default:","`
}
// NodeSelectionConfig is a configuration struct to determine the minimum // NodeSelectionConfig is a configuration struct to determine the minimum
// values for nodes to select // values for nodes to select
type NodeSelectionConfig struct { type NodeSelectionConfig struct {
@ -56,21 +47,3 @@ type NodeSelectionConfig struct {
ReputationUptimeLambda float64 `help:"the forgetting factor used to calculate the uptime SNs reputation" default:"1.0"` ReputationUptimeLambda float64 `help:"the forgetting factor used to calculate the uptime SNs reputation" default:"1.0"`
ReputationUptimeOmega float64 `help:"the normalization weight used to calculate the uptime SNs reputation" default:"1.0"` ReputationUptimeOmega float64 `help:"the normalization weight used to calculate the uptime SNs reputation" default:"1.0"`
} }
// ParseIDs converts the base58check encoded node ID strings from the config into node IDs
func (c LookupConfig) ParseIDs() (ids storj.NodeIDList, err error) {
var idErrs []error
idStrs := strings.Split(c.NodeIDsString, c.Delimiter)
for _, s := range idStrs {
id, err := storj.NodeIDFromString(s)
if err != nil {
idErrs = append(idErrs, err)
continue
}
ids = append(ids, id)
}
if err := errs.Combine(idErrs...); err != nil {
return nil, err
}
return ids, nil
}