simplify machine list. do not need a separate struct to store machines, since we have already stored them into etcd store.

This commit is contained in:
Xiang Li 2013-08-02 20:21:11 -07:00
parent 67b66f75e0
commit 50613c555b
2 changed files with 13 additions and 25 deletions

View File

@ -129,12 +129,8 @@ func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
// add peer in raft
err := raftServer.AddPeer(c.Name)
// add machine in etcd
addMachine(c.Name, c.Hostname, c.RaftPort, c.ClientPort)
// add machine in etcd storage
nodeName := fmt.Sprintf("%s%d", "node", raftServer.CommitIndex())
key := path.Join("_etcd/machines", nodeName)
key := path.Join("_etcd/machines", c.Name)
value := fmt.Sprintf("%s,%d,%d", c.Hostname, c.RaftPort, c.ClientPort)
etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())

View File

@ -2,34 +2,26 @@ package main
import (
"fmt"
"path"
"strings"
)
type machine struct {
hostname string
raftPort int
clientPort int
}
var machinesMap = map[string]machine{}
func addMachine(name string, hostname string, raftPort int, clientPort int) {
machinesMap[name] = machine{hostname, raftPort, clientPort}
}
func getClientAddr(name string) (string, bool) {
machine, ok := machinesMap[name]
if !ok {
return "", false
}
response, _ := etcdStore.RawGet(path.Join("_etcd/machines", name))
addr := fmt.Sprintf("%s:%v", machine.hostname, machine.clientPort)
values := strings.Split(response[0].Value, ",")
hostname := values[0]
clientPort := values[2]
addr := fmt.Sprintf("%s:%s", hostname, clientPort)
return addr, true
}
// machineNum returns the number of machines in the cluster
func machineNum() int {
return len(machinesMap)
response, _ := etcdStore.RawGet("_etcd/machines")
return len(response)
}