mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
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:
parent
67b66f75e0
commit
50613c555b
@ -129,12 +129,8 @@ func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
|
|||||||
// add peer in raft
|
// add peer in raft
|
||||||
err := raftServer.AddPeer(c.Name)
|
err := raftServer.AddPeer(c.Name)
|
||||||
|
|
||||||
// add machine in etcd
|
|
||||||
addMachine(c.Name, c.Hostname, c.RaftPort, c.ClientPort)
|
|
||||||
|
|
||||||
// add machine in etcd storage
|
// add machine in etcd storage
|
||||||
nodeName := fmt.Sprintf("%s%d", "node", raftServer.CommitIndex())
|
key := path.Join("_etcd/machines", c.Name)
|
||||||
key := path.Join("_etcd/machines", nodeName)
|
|
||||||
value := fmt.Sprintf("%s,%d,%d", c.Hostname, c.RaftPort, c.ClientPort)
|
value := fmt.Sprintf("%s,%d,%d", c.Hostname, c.RaftPort, c.ClientPort)
|
||||||
etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
|
etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
|
||||||
|
|
||||||
|
32
machines.go
32
machines.go
@ -2,34 +2,26 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"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) {
|
func getClientAddr(name string) (string, bool) {
|
||||||
machine, ok := machinesMap[name]
|
response, _ := etcdStore.RawGet(path.Join("_etcd/machines", name))
|
||||||
if !ok {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
return addr, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// machineNum returns the number of machines in the cluster
|
// machineNum returns the number of machines in the cluster
|
||||||
func machineNum() int {
|
func machineNum() int {
|
||||||
return len(machinesMap)
|
response, _ := etcdStore.RawGet("_etcd/machines")
|
||||||
|
|
||||||
|
return len(response)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user