Merge pull request #227 from xiangli-cmu/0.2

refactor remove the extra function
This commit is contained in:
Xiang Li 2013-10-13 22:20:52 -07:00
commit 755c18d491
3 changed files with 22 additions and 39 deletions

View File

@ -45,7 +45,7 @@ func (c *JoinCommand) Apply(server *raft.Server) (interface{}, error) {
ps.registry.Invalidate(c.Name) ps.registry.Invalidate(c.Name)
// Check if the join command is from a previous machine, who lost all its previous log. // 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 return b, nil
} }

View File

@ -73,14 +73,14 @@ func (r *Registry) Count() int {
return len(e.KVPairs) return len(e.KVPairs)
} }
// Retrieves the URL for a given node by name. // Retrieves the client URL for a given node by name.
func (r *Registry) URL(name string) (string, bool) { func (r *Registry) ClientURL(name string) (string, bool) {
r.Lock() r.Lock()
defer r.Unlock() 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 { if r.nodes[name] == nil {
r.load(name) r.load(name)
} }
@ -92,33 +92,6 @@ func (r *Registry) url(name string) (string, bool) {
return "", false 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. // Retrieves the peer URL for a given node by name.
func (r *Registry) PeerURL(name string) (string, bool) { func (r *Registry) PeerURL(name string) (string, bool) {
r.Lock() r.Lock()
@ -138,14 +111,24 @@ func (r *Registry) peerURL(name string) (string, bool) {
return "", false return "", false
} }
// Retrieves the peer URLs for all nodes. // Retrieves the Client URLs for all nodes.
func (r *Registry) ClientURLs(leaderName, selfName string) []string {
return r.urls(leaderName, selfName, r.clientURL)
}
// Retrieves the Peer URLs for all nodes.
func (r *Registry) PeerURLs(leaderName, selfName string) []string { func (r *Registry) PeerURLs(leaderName, selfName string) []string {
return r.urls(leaderName, selfName, r.peerURL)
}
// 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() r.Lock()
defer r.Unlock() defer r.Unlock()
// Build list including the leader and self. // Build list including the leader and self.
urls := make([]string, 0) urls := make([]string, 0)
if url, _ := r.peerURL(leaderName); len(url) > 0 { if url, _ := url(leaderName); len(url) > 0 {
urls = append(urls, url) urls = append(urls, url)
} }
@ -154,13 +137,13 @@ func (r *Registry) PeerURLs(leaderName, selfName string) []string {
// Lookup the URL for each one. // Lookup the URL for each one.
for _, pair := range e.KVPairs { for _, pair := range e.KVPairs {
_, name := filepath.Split(pair.Key) _, 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) 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 return urls
} }

View File

@ -207,7 +207,7 @@ func (s *Server) Dispatch(c raft.Command, w http.ResponseWriter, req *http.Reque
case *JoinCommand, *RemoveCommand: case *JoinCommand, *RemoveCommand:
url, _ = s.registry.PeerURL(leader) url, _ = s.registry.PeerURL(leader)
default: default:
url, _ = s.registry.URL(leader) url, _ = s.registry.ClientURL(leader)
} }
redirect(url, w, req) 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. // Handler to return all the known machines in the current cluster.
func (s *Server) GetMachinesHandler(w http.ResponseWriter, req *http.Request) error { func (s *Server) GetMachinesHandler(w http.ResponseWriter, req *http.Request) error {
machines := s.registry.URLs(s.peerServer.Leader(), s.name) machines := s.registry.ClientURLs(s.peerServer.Leader(), s.name)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write([]byte(strings.Join(machines, ", "))) w.Write([]byte(strings.Join(machines, ", ")))
return nil return nil
@ -281,7 +281,7 @@ func (s *Server) GetLeaderStatsHandler(w http.ResponseWriter, req *http.Request)
if leader == "" { if leader == "" {
return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm) return etcdErr.NewError(300, "", store.UndefIndex, store.UndefTerm)
} }
hostname, _ := s.registry.URL(leader) hostname, _ := s.registry.ClientURL(leader)
redirect(hostname, w, req) redirect(hostname, w, req)
return nil return nil
} }