create db directory if it does not exist (#439)
* create db directory if it does not exist * linter fix * pass db path in from config * change mkdir to mkdirAll * windows love * PR comments * changing the path * change the config default to $CONFDIR/kademlia
This commit is contained in:
parent
3de34ab6cb
commit
783f055417
@ -33,6 +33,7 @@ type Config struct {
|
||||
BootstrapAddr string `help:"the kademlia node to bootstrap against" default:"bootstrap-dev.storj.io:8080"`
|
||||
// TODO(jt): remove this! kademlia should just use the grpc server
|
||||
TODOListenAddr string `help:"the host/port for kademlia to listen on. TODO(jt): this should be removed!" default:"127.0.0.1:7776"`
|
||||
DBPath string `help:"the path for our db services to be created on" default:"$CONFDIR/kademlia"`
|
||||
}
|
||||
|
||||
// Run implements provider.Responsibility
|
||||
@ -50,7 +51,7 @@ func (c Config) Run(ctx context.Context, server *provider.Provider) (
|
||||
// TODO(jt): kademlia should register on server.GRPC() instead of listening
|
||||
// itself
|
||||
in.Id = "foo"
|
||||
kad, err := NewKademlia(server.Identity().ID, []pb.Node{*in}, c.TODOListenAddr, server.Identity())
|
||||
kad, err := NewKademlia(server.Identity().ID, []pb.Node{*in}, c.TODOListenAddr, server.Identity(), c.DBPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zeebo/errs"
|
||||
"google.golang.org/grpc"
|
||||
@ -52,11 +54,18 @@ type Kademlia struct {
|
||||
}
|
||||
|
||||
// NewKademlia returns a newly configured Kademlia instance
|
||||
func NewKademlia(id dht.NodeID, bootstrapNodes []pb.Node, address string, identity *provider.FullIdentity) (*Kademlia, error) {
|
||||
func NewKademlia(id dht.NodeID, bootstrapNodes []pb.Node, address string, identity *provider.FullIdentity, path string) (*Kademlia, error) {
|
||||
self := pb.Node{Id: id.String(), Address: &pb.NodeAddress{Address: address}}
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(path, 0777); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
bucketIdentifier := id.String()[:5] // need a way to differentiate between nodes if running more than one simultaneously
|
||||
rt, err := NewRoutingTable(&self, &RoutingOptions{
|
||||
kpath: fmt.Sprintf("db/kbucket_%s.db", id.String()[:5]),
|
||||
npath: fmt.Sprintf("db/nbucket_%s.db", id.String()[:5]),
|
||||
kpath: filepath.Join(path, fmt.Sprintf("kbucket_%s.db", bucketIdentifier)),
|
||||
npath: filepath.Join(path, fmt.Sprintf("nbucket_%s.db", bucketIdentifier)),
|
||||
idLength: defaultIDLength,
|
||||
bucketSize: defaultBucketSize,
|
||||
rcBucketSize: defaultReplacementCacheSize,
|
||||
|
@ -6,6 +6,7 @@ package kademlia
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -24,6 +25,7 @@ func TestNewKademlia(t *testing.T) {
|
||||
bn []pb.Node
|
||||
addr string
|
||||
expectedErr error
|
||||
setup func() error
|
||||
}{
|
||||
{
|
||||
id: func() *node.ID {
|
||||
@ -31,17 +33,29 @@ func TestNewKademlia(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
return id
|
||||
}(),
|
||||
bn: []pb.Node{pb.Node{Id: "foo"}},
|
||||
addr: "127.0.0.1:8080",
|
||||
bn: []pb.Node{pb.Node{Id: "foo"}},
|
||||
addr: "127.0.0.1:8080",
|
||||
setup: func() error { return nil },
|
||||
},
|
||||
{
|
||||
id: func() *node.ID {
|
||||
id, err := node.NewID()
|
||||
assert.NoError(t, err)
|
||||
return id
|
||||
}(),
|
||||
bn: []pb.Node{pb.Node{Id: "foo"}},
|
||||
addr: "127.0.0.1:8080",
|
||||
setup: func() error { return os.RemoveAll("db") },
|
||||
},
|
||||
}
|
||||
|
||||
for _, v := range cases {
|
||||
assert.NoError(t, v.setup())
|
||||
ca, err := provider.NewCA(ctx, 12, 4)
|
||||
assert.NoError(t, err)
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
actual, err := NewKademlia(v.id, v.bn, v.addr, identity)
|
||||
actual, err := NewKademlia(v.id, v.bn, v.addr, identity, "db")
|
||||
assert.Equal(t, v.expectedErr, err)
|
||||
assert.Equal(t, actual.bootstrapNodes, v.bn)
|
||||
assert.NotNil(t, actual.nodeClient)
|
||||
@ -68,7 +82,7 @@ func TestLookup(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
k, err := NewKademlia(id, []pb.Node{pb.Node{Id: id2.String(), Address: &pb.NodeAddress{Address: lis.Addr().String()}}}, lis.Addr().String(), identity)
|
||||
k, err := NewKademlia(id, []pb.Node{pb.Node{Id: id2.String(), Address: &pb.NodeAddress{Address: lis.Addr().String()}}}, lis.Addr().String(), identity, "db")
|
||||
assert.NoError(t, err)
|
||||
return k
|
||||
}()
|
||||
@ -150,7 +164,7 @@ func testNode(t *testing.T, bn []pb.Node) (*Kademlia, *grpc.Server) {
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
// new kademlia
|
||||
k, err := NewKademlia(id, bn, lis.Addr().String(), identity)
|
||||
k, err := NewKademlia(id, bn, lis.Addr().String(), identity, "db")
|
||||
assert.NoError(t, err)
|
||||
s := node.NewServer(k)
|
||||
|
||||
|
@ -55,7 +55,7 @@ func newTestKademlia(t *testing.T, ip, port string, d dht.DHT, b pb.Node) *kadem
|
||||
assert.NoError(t, err)
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
kad, err := kademlia.NewKademlia(&id, n, fmt.Sprintf("%s:%s", ip, port), identity)
|
||||
kad, err := kademlia.NewKademlia(&id, n, fmt.Sprintf("%s:%s", ip, port), identity, "db")
|
||||
assert.NoError(t, err)
|
||||
|
||||
return kad
|
||||
@ -79,7 +79,7 @@ func bootstrapTestNetwork(t *testing.T, ip, port string) ([]dht.DHT, pb.Node) {
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
|
||||
boot, err := kademlia.NewKademlia(&bnid, []pb.Node{*intro}, fmt.Sprintf("%s:%s", ip, pm), identity)
|
||||
boot, err := kademlia.NewKademlia(&bnid, []pb.Node{*intro}, fmt.Sprintf("%s:%s", ip, pm), identity, "db")
|
||||
|
||||
assert.NoError(t, err)
|
||||
rt, err := boot.GetRoutingTable(context.Background())
|
||||
@ -104,7 +104,7 @@ func bootstrapTestNetwork(t *testing.T, ip, port string) ([]dht.DHT, pb.Node) {
|
||||
identity, err := ca.NewIdentity()
|
||||
assert.NoError(t, err)
|
||||
|
||||
dht, err := kademlia.NewKademlia(&id, []pb.Node{bootNode}, fmt.Sprintf("%s:%s", ip, gg), identity)
|
||||
dht, err := kademlia.NewKademlia(&id, []pb.Node{bootNode}, fmt.Sprintf("%s:%s", ip, gg), identity, "db")
|
||||
assert.NoError(t, err)
|
||||
|
||||
p++
|
||||
|
Loading…
Reference in New Issue
Block a user