mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
refactor remove the extra function
This commit is contained in:
parent
2b291aabea
commit
a635f6b17c
@ -45,7 +45,7 @@ func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
|
||||
ps.registry.Invalidate(c.Name)
|
||||
|
||||
// Check if the join command is from a previous machine, who lost all its previous log.
|
||||
if _, ok := ps.registry.URL(c.Name); ok {
|
||||
if _, ok := ps.registry.ClientURL(c.Name); ok {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,7 @@ func (s *PeerServer) ListenAndServe(snapshot bool, cluster []string) {
|
||||
|
||||
} else {
|
||||
// Rejoin the previous cluster
|
||||
cluster = s.registry.PeerURLs(s.Leader(), s.name)
|
||||
cluster = s.registry.URLs(s.Leader(), s.name, s.registry.peerURL)
|
||||
for i := 0; i < len(cluster); i++ {
|
||||
u, err := url.Parse(cluster[i])
|
||||
if err != nil {
|
||||
|
@ -73,14 +73,14 @@ func (r *Registry) Count() int {
|
||||
return len(e.KVPairs)
|
||||
}
|
||||
|
||||
// Retrieves the URL for a given node by name.
|
||||
func (r *Registry) URL(name string) (string, bool) {
|
||||
// Retrieves the client URL for a given node by name.
|
||||
func (r *Registry) ClientURL(name string) (string, bool) {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
return r.url(name)
|
||||
return r.clientURL(name)
|
||||
}
|
||||
|
||||
func (r *Registry) url(name string) (string, bool) {
|
||||
func (r *Registry) clientURL(name string) (string, bool) {
|
||||
if r.nodes[name] == nil {
|
||||
r.load(name)
|
||||
}
|
||||
@ -92,33 +92,6 @@ func (r *Registry) url(name string) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Retrieves the URLs for all nodes.
|
||||
func (r *Registry) URLs(leaderName, selfName string) []string {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
// Build list including the leader and self.
|
||||
urls := make([]string, 0)
|
||||
if url, _ := r.url(leaderName); len(url) > 0 {
|
||||
urls = append(urls, url)
|
||||
}
|
||||
|
||||
// Retrieve a list of all nodes.
|
||||
if e, _ := r.store.Get(RegistryKey, false, false, 0, 0); e != nil {
|
||||
// Lookup the URL for each one.
|
||||
for _, pair := range e.KVPairs {
|
||||
_, name := filepath.Split(pair.Key)
|
||||
if url, _ := r.url(name); len(url) > 0 && name != leaderName {
|
||||
urls = append(urls, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("URLs: %s / %s (%s)", leaderName, selfName, strings.Join(urls, ","))
|
||||
|
||||
return urls
|
||||
}
|
||||
|
||||
// Retrieves the peer URL for a given node by name.
|
||||
func (r *Registry) PeerURL(name string) (string, bool) {
|
||||
r.Lock()
|
||||
@ -138,14 +111,14 @@ func (r *Registry) peerURL(name string) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// Retrieves the peer URLs for all nodes.
|
||||
func (r *Registry) PeerURLs(leaderName, selfName string) []string {
|
||||
// Retrieves the URLs for all nodes using url function.
|
||||
func (r *Registry) URLs(leaderName, selfName string, url func(name string) (string, bool)) []string {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
// Build list including the leader and self.
|
||||
urls := make([]string, 0)
|
||||
if url, _ := r.peerURL(leaderName); len(url) > 0 {
|
||||
if url, _ := url(leaderName); len(url) > 0 {
|
||||
urls = append(urls, url)
|
||||
}
|
||||
|
||||
@ -154,13 +127,13 @@ func (r *Registry) PeerURLs(leaderName, selfName string) []string {
|
||||
// Lookup the URL for each one.
|
||||
for _, pair := range e.KVPairs {
|
||||
_, name := filepath.Split(pair.Key)
|
||||
if url, _ := r.peerURL(name); len(url) > 0 && name != leaderName {
|
||||
if url, _ := url(name); len(url) > 0 && name != leaderName {
|
||||
urls = append(urls, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Infof("PeerURLs: %s / %s (%s)", leaderName, selfName, strings.Join(urls, ","))
|
||||
log.Infof("URLs: %s / %s (%s)", leaderName, selfName, strings.Join(urls, ","))
|
||||
|
||||
return urls
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Reque
|
||||
case *JoinCommand, *RemoveCommand:
|
||||
url, _ = s.registry.PeerURL(leader)
|
||||
default:
|
||||
url, _ = s.registry.URL(leader)
|
||||
url, _ = s.registry.ClientURL(leader)
|
||||
}
|
||||
redirect(url, w, req)
|
||||
|
||||
@ -258,7 +258,7 @@ func (s *Server) GetLeaderHandler(w http.ResponseWriter, req *http.Request) erro
|
||||
|
||||
// Handler to return all the known machines in the current cluster.
|
||||
func (s *Server) GetMachinesHandler(w http.ResponseWriter, req *http.Request) error {
|
||||
machines := s.registry.URLs(s.peerServer.Leader(), s.name)
|
||||
machines := s.registry.URLs(s.peerServer.Leader(), s.name, s.registry.clientURL)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(strings.Join(machines, ", ")))
|
||||
return nil
|
||||
@ -281,7 +281,7 @@ func (s *Server) GetLeaderStatsHandler(w http.ResponseWriter, req *http.Request)
|
||||
if leader == "" {
|
||||
return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
|
||||
}
|
||||
hostname, _ := s.registry.URL(leader)
|
||||
hostname, _ := s.registry.ClientURL(leader)
|
||||
redirect(hostname, w, req)
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user