Browse Source

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

Xiang Li 12 years ago
parent
commit
50613c555b
2 changed files with 12 additions and 24 deletions
  1. 1 5
      command.go
  2. 11 19
      machines.go

+ 1 - 5
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())
 

+ 11 - 19
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) {
+	response, _ := etcdStore.RawGet(path.Join("_etcd/machines", name))
 
-}
+	values := strings.Split(response[0].Value, ",")
 
-func getClientAddr(name string) (string, bool) {
-	machine, ok := machinesMap[name]
-	if !ok {
-		return "", false
-	}
+	hostname := values[0]
+	clientPort := values[2]
 
-	addr := fmt.Sprintf("%s:%v", machine.hostname, machine.clientPort)
+	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)
 }