From 50613c555b750f35e675d244955561d9c47cb47f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 2 Aug 2013 20:21:11 -0700 Subject: [PATCH] simplify machine list. do not need a separate struct to store machines, since we have already stored them into etcd store. --- command.go | 6 +----- machines.go | 32 ++++++++++++-------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/command.go b/command.go index a0977ae83..a50260ea7 100644 --- a/command.go +++ b/command.go @@ -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()) diff --git a/machines.go b/machines.go index d3c0a855d..dc358a8e3 100644 --- a/machines.go +++ b/machines.go @@ -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) }