Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
beaulian committed Nov 28, 2017
1 parent c8cb855 commit 7c0942a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/github.com/shenaishiren/pentadb/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package args

type InitArgs struct {
// a array consisting of each node's ipaddr
Nodes []string
Self string
// a array consisting of other nodes' ipaddr
OtherNodes []string

// replicas
Replicas int
Expand Down
8 changes: 6 additions & 2 deletions src/github.com/shenaishiren/pentadb/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (

func TestNewClient(t *testing.T) {
var nodes = []string{
"127.0.0.1:4567",
"10.20.204.75:4567",
"10.19.126.55:4567",
}
client, err := NewClient(nodes, nil, 0)
client, err := NewClient(nodes, nil, 1)
if err != nil {
t.Error(err.Error())
return
}
defer client.Close()
if len(client.nodes) != len(nodes) {
Expand All @@ -29,6 +31,8 @@ func TestNewClient(t *testing.T) {
client.Put([]byte("p"), []byte("v"))
if value := client.Get([]byte("p")); value == nil {
t.Error("wrong get")
} else {
LOG.Debug("value: ", value)
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/github.com/shenaishiren/pentadb/client/node_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,16 @@ func (np *NodeProxy) call(serviceMethod string, args interface{}, unreachableCha
}

func (np *NodeProxy) Init(nodeIpaddrs []string, replicas int, unreachableChan chan string) {
var otherNodes []string
for _, node := range nodeIpaddrs {
if node != np.node.Ipaddr {
otherNodes = append(otherNodes, node)
}
}
args := &args.InitArgs{
Nodes: nodeIpaddrs, Replicas: replicas,
Self: np.node.Ipaddr,
OtherNodes: otherNodes,
Replicas: replicas,
}
np.call("Node.Init", args, unreachableChan)
}
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/shenaishiren/pentadb/commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Server struct {
}

func (s *Server) listen(port string, path string) {
s.Node = server.NewNode("127.0.0.1:" + port)
s.Node = server.NewNode(":" + port)
db, err := leveldb.OpenFile(path, nil)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/github.com/shenaishiren/pentadb/opt/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package opt
import "time"

const (
DefaultReplicas = 0 // default replicas for raft algorithm
DefaultReplicas = 1 // default replicas for raft algorithm
DeafultPath = "/tmp/pentadb" // default path for levelDB
DefaultProtocol = "tcp"
DefaultTimeout = 3 * time.Second
Expand Down
25 changes: 9 additions & 16 deletions src/github.com/shenaishiren/pentadb/server/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ package server

import (
"sync"
"errors"
"math/rand"
"github.com/syndtr/goleveldb/leveldb"
"github.com/shenaishiren/pentadb/args"
"github.com/shenaishiren/pentadb/log"
"fmt"
)

var LOG = log.DefaultLog
Expand Down Expand Up @@ -72,7 +74,7 @@ func NewNode(ipaddr string) *Node {
}
}

func (n *Node) randomChoice(list []string, k int) ([]string, error) {
func (n *Node) randomChoice(list []string, k int) []string {
pool := list
p := len(pool)
result := make([]string, k)
Expand All @@ -81,27 +83,18 @@ func (n *Node) randomChoice(list []string, k int) ([]string, error) {
result[i] = pool[j]
pool[j] = result[p - i - 1]
}

return result, nil
return result
}


func (n *Node) Init(args *args.InitArgs, result *[]byte) error {
n.mutex.Lock()
defer n.mutex.Unlock()

nodes := args.Nodes
otherNodes := make([]string, len(nodes) - 1)
for _, node := range nodes {
if node != n.Ipaddr {
otherNodes = append(otherNodes, node)
}
}

n.OtherNodes = otherNodes
replicaNodes, err := n.randomChoice(otherNodes, args.Replicas)
if err != nil {
return err
n.Ipaddr = args.Self
n.OtherNodes = args.OtherNodes
replicaNodes := n.randomChoice(args.OtherNodes, args.Replicas)
if len(replicaNodes) == 0 {
return errors.New(fmt.Sprintf("node %s init failed", n.Ipaddr))
}
replicaNodes = append(replicaNodes, n.Ipaddr)
n.ReplicaNodes = replicaNodes
Expand Down

0 comments on commit 7c0942a

Please sign in to comment.