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"`
|
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
|
// 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"`
|
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
|
// 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
|
// TODO(jt): kademlia should register on server.GRPC() instead of listening
|
||||||
// itself
|
// itself
|
||||||
in.Id = "foo"
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/zeebo/errs"
|
"github.com/zeebo/errs"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@ -52,11 +54,18 @@ type Kademlia struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewKademlia returns a newly configured Kademlia instance
|
// 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}}
|
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{
|
rt, err := NewRoutingTable(&self, &RoutingOptions{
|
||||||
kpath: fmt.Sprintf("db/kbucket_%s.db", id.String()[:5]),
|
kpath: filepath.Join(path, fmt.Sprintf("kbucket_%s.db", bucketIdentifier)),
|
||||||
npath: fmt.Sprintf("db/nbucket_%s.db", id.String()[:5]),
|
npath: filepath.Join(path, fmt.Sprintf("nbucket_%s.db", bucketIdentifier)),
|
||||||
idLength: defaultIDLength,
|
idLength: defaultIDLength,
|
||||||
bucketSize: defaultBucketSize,
|
bucketSize: defaultBucketSize,
|
||||||
rcBucketSize: defaultReplacementCacheSize,
|
rcBucketSize: defaultReplacementCacheSize,
|
||||||
|
@ -6,6 +6,7 @@ package kademlia
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -24,6 +25,7 @@ func TestNewKademlia(t *testing.T) {
|
|||||||
bn []pb.Node
|
bn []pb.Node
|
||||||
addr string
|
addr string
|
||||||
expectedErr error
|
expectedErr error
|
||||||
|
setup func() error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
id: func() *node.ID {
|
id: func() *node.ID {
|
||||||
@ -33,15 +35,27 @@ func TestNewKademlia(t *testing.T) {
|
|||||||
}(),
|
}(),
|
||||||
bn: []pb.Node{pb.Node{Id: "foo"}},
|
bn: []pb.Node{pb.Node{Id: "foo"}},
|
||||||
addr: "127.0.0.1:8080",
|
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 {
|
for _, v := range cases {
|
||||||
|
assert.NoError(t, v.setup())
|
||||||
ca, err := provider.NewCA(ctx, 12, 4)
|
ca, err := provider.NewCA(ctx, 12, 4)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
identity, err := ca.NewIdentity()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
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, v.expectedErr, err)
|
||||||
assert.Equal(t, actual.bootstrapNodes, v.bn)
|
assert.Equal(t, actual.bootstrapNodes, v.bn)
|
||||||
assert.NotNil(t, actual.nodeClient)
|
assert.NotNil(t, actual.nodeClient)
|
||||||
@ -68,7 +82,7 @@ func TestLookup(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
identity, err := ca.NewIdentity()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
return k
|
return k
|
||||||
}()
|
}()
|
||||||
@ -150,7 +164,7 @@ func testNode(t *testing.T, bn []pb.Node) (*Kademlia, *grpc.Server) {
|
|||||||
identity, err := ca.NewIdentity()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// new kademlia
|
// 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)
|
assert.NoError(t, err)
|
||||||
s := node.NewServer(k)
|
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)
|
assert.NoError(t, err)
|
||||||
identity, err := ca.NewIdentity()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
return kad
|
return kad
|
||||||
@ -79,7 +79,7 @@ func bootstrapTestNetwork(t *testing.T, ip, port string) ([]dht.DHT, pb.Node) {
|
|||||||
identity, err := ca.NewIdentity()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
rt, err := boot.GetRoutingTable(context.Background())
|
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()
|
identity, err := ca.NewIdentity()
|
||||||
assert.NoError(t, err)
|
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)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
p++
|
p++
|
||||||
|
Loading…
Reference in New Issue
Block a user