Skip to content

Commit

Permalink
Automatic assign mac to peer id (#11)
Browse files Browse the repository at this point in the history
* added automatic assign mac to peer id

* refactor config into separate file

* add test validation error on bcache_test

* log test

* update docs and use single source of peerID
  • Loading branch information
yogyrahmawan authored and iwanbk committed Jan 2, 2019
1 parent 9f85f9e commit 3aa67f0
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 24 deletions.
31 changes: 8 additions & 23 deletions bcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,25 @@ type Bcache struct {
logger Logger
}

// Config represents bcache configuration
type Config struct {
// PeerID is unique ID of this bcache
PeerID uint64

// ListenAddr is listen addr of this bcache peer.
// used to communicate with other peers
ListenAddr string

// Peers is the address of the known peers.
// we don't need to know all of the other peers,
// gossip protocol will find the other peers
Peers []string

// MaxKeys defines max number of keys in this cache
MaxKeys int

// Logger to be used
// leave it nil to use default logger which do nothing
Logger Logger
}

// New creates new bcache from the given config
func New(cfg Config) (*Bcache, error) {
const (
connLimit = 64 // mesh router connection limit
)

var (
peerName = mesh.PeerName(cfg.PeerID)
peerName mesh.PeerName
nickName = cfg.ListenAddr
logger = cfg.Logger
)

if err := cfg.setDefault(); err != nil {
return nil, err
}

// set peer name
peerName = mesh.PeerName(cfg.PeerID)

// if logger is nil, create default nopLogger
if logger == nil {
logger = &nopLogger{}
Expand Down
30 changes: 29 additions & 1 deletion bcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func TestSimple(t *testing.T) {
val2 = "val2"
)
b1, err := New(Config{
PeerID: 1,
ListenAddr: "127.0.0.1:12345",
Peers: nil,
MaxKeys: 1000,
Expand Down Expand Up @@ -111,3 +110,32 @@ func TestJoinLater(t *testing.T) {
require.Equal(t, v, got)
}
}

func TestValidationError(t *testing.T) {
// invalid address because no port
c := Config{
PeerID: 1,
ListenAddr: "127.0.0.1",
Peers: nil,
MaxKeys: 1000,
Logger: logrus.New(),
}

_, err := New(c)
require.Error(t, err)

// invalid address because wrong port
c.ListenAddr = "127.0.0.1:abcd"
_, err = New(c)
require.Error(t, err)

// null logger not cause error
c.Logger = nil
c.ListenAddr = "127.0.0.1:12666"

b1, err := New(c)
require.NoError(t, err)
require.NotNil(t, b1)
defer b1.Close()

}
43 changes: 43 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bcache

import "github.com/weaveworks/mesh"

// Config represents bcache configuration
type Config struct {
// PeerID is unique ID of this bcache
// if PeerID = 0, it will be set using mac address
PeerID uint64

// ListenAddr is listen addr of this bcache peer.
// used to communicate with other peers
ListenAddr string

// Peers is the address of the known peers.
// we don't need to know all of the other peers,
// gossip protocol will find the other peers
Peers []string

// MaxKeys defines max number of keys in this cache
MaxKeys int

// Logger to be used
// leave it nil to use default logger which do nothing
Logger Logger
}

func (c *Config) setDefault() error {
if c.PeerID == uint64(0) {
mac, err := getMacAddress()
if err != nil {
return err
}

pName, err := mesh.PeerNameFromString(mac)
if err != nil {
return err
}

c.PeerID = uint64(pName)
}
return nil
}
35 changes: 35 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package bcache

import (
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)

func TestConfig(t *testing.T) {
// should be using mac addr
c := Config{
ListenAddr: "127.0.0.1:12345",
Peers: nil,
MaxKeys: 1000,
Logger: logrus.New(),
}

err := c.setDefault()
require.NoError(t, err)
require.NotEqual(t, uint64(0), c.PeerID)

// should be using predefined id
cfgManual := Config{
ListenAddr: "127.0.0.1:12345",
Peers: nil,
MaxKeys: 1000,
Logger: logrus.New(),
PeerID: uint64(2),
}

err = cfgManual.setDefault()
require.NoError(t, err)
require.Equal(t, uint64(2), cfgManual.PeerID)
}
10 changes: 10 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bcache

import "testing"

func TestLog(t *testing.T) {
logger := &nopLogger{}
logger.Errorf("Nothing %s", "nothing")
logger.Printf("Nothing %s", "nothing")
logger.Debugf("Nothing %s", "nothing")
}
26 changes: 26 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package bcache

import (
"errors"
"net"
)

var (
errMacAddressNotFound = errors.New("mac address not found in this machine")
)

// getMacAddress get mac address of this machine
func getMacAddress() (string, error) {
interfaces, err := net.Interfaces()
if err != nil {
return "", err
}

for _, intf := range interfaces {
if intf.Flags&net.FlagUp != 0 && intf.HardwareAddr != nil {
return intf.HardwareAddr.String(), nil
}
}

return "", errMacAddressNotFound
}
13 changes: 13 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package bcache

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestGetMacAddress(t *testing.T) {
mac, err := getMacAddress()
require.NoError(t, err)
require.NotEqual(t, "", mac)
}

0 comments on commit 3aa67f0

Please sign in to comment.