registry.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package server
  2. import (
  3. "fmt"
  4. "net/url"
  5. "path"
  6. "path/filepath"
  7. "sort"
  8. "strings"
  9. "sync"
  10. "github.com/coreos/etcd/log"
  11. "github.com/coreos/etcd/store"
  12. )
  13. // The location of the peer URL data.
  14. const RegistryKey = "/_etcd/machines"
  15. // The Registry stores URL information for nodes.
  16. type Registry struct {
  17. sync.Mutex
  18. store store.Store
  19. peers map[string]*node
  20. }
  21. // The internal storage format of the registry.
  22. type node struct {
  23. peerVersion string
  24. peerURL string
  25. url string
  26. }
  27. // Creates a new Registry.
  28. func NewRegistry(s store.Store) *Registry {
  29. return &Registry{
  30. store: s,
  31. peers: make(map[string]*node),
  32. }
  33. }
  34. // Register adds a peer to the registry.
  35. func (r *Registry) Register(name string, peerURL string, machURL string) error {
  36. // Write data to store.
  37. v := url.Values{}
  38. v.Set("raft", peerURL)
  39. v.Set("etcd", machURL)
  40. log.Debugf("Register: %s", name)
  41. if _, err := r.store.Create(path.Join(RegistryKey, name), false, v.Encode(), false, store.Permanent); err != nil {
  42. return err
  43. }
  44. r.Lock()
  45. defer r.Unlock()
  46. r.peers[name] = r.load(RegistryKey, name)
  47. return nil
  48. }
  49. // Unregister removes a peer from the registry.
  50. func (r *Registry) Unregister(name string) error {
  51. // Remove the key from the store.
  52. log.Debugf("Unregister: %s", name)
  53. _, err := r.store.Delete(path.Join(RegistryKey, name), false, false)
  54. return err
  55. }
  56. // Count returns the number of peers in the cluster.
  57. func (r *Registry) Count() int {
  58. e, err := r.store.Get(RegistryKey, false, false)
  59. if err != nil {
  60. return 0
  61. }
  62. return len(e.Node.Nodes)
  63. }
  64. // Exists checks if a peer with the given name exists.
  65. func (r *Registry) Exists(name string) bool {
  66. e, err := r.store.Get(path.Join(RegistryKey, name), false, false)
  67. if err != nil {
  68. return false
  69. }
  70. return (e.Node != nil)
  71. }
  72. // Retrieves the client URL for a given node by name.
  73. func (r *Registry) ClientURL(name string) (string, bool) {
  74. r.Lock()
  75. defer r.Unlock()
  76. return r.clientURL(RegistryKey, name)
  77. }
  78. func (r *Registry) clientURL(key, name string) (string, bool) {
  79. if r.peers[name] == nil {
  80. if peer := r.load(key, name); peer != nil {
  81. r.peers[name] = peer
  82. }
  83. }
  84. if peer := r.peers[name]; peer != nil {
  85. return peer.url, true
  86. }
  87. return "", false
  88. }
  89. // TODO(yichengq): have all of the code use a full URL with scheme
  90. // and remove this method
  91. // PeerHost retrieves the host part of peer URL for a given node by name.
  92. func (r *Registry) PeerHost(name string) (string, bool) {
  93. rawurl, ok := r.PeerURL(name)
  94. if ok {
  95. u, _ := url.Parse(rawurl)
  96. return u.Host, ok
  97. }
  98. return rawurl, ok
  99. }
  100. // Retrieves the peer URL for a given node by name.
  101. func (r *Registry) PeerURL(name string) (string, bool) {
  102. r.Lock()
  103. defer r.Unlock()
  104. return r.peerURL(RegistryKey, name)
  105. }
  106. func (r *Registry) peerURL(key, name string) (string, bool) {
  107. if r.peers[name] == nil {
  108. if peer := r.load(key, name); peer != nil {
  109. r.peers[name] = peer
  110. }
  111. }
  112. if peer := r.peers[name]; peer != nil {
  113. return peer.peerURL, true
  114. }
  115. return "", false
  116. }
  117. // UpdatePeerURL updates peer URL in registry
  118. func (r *Registry) UpdatePeerURL(name string, peerURL string) error {
  119. machURL, _ := r.clientURL(RegistryKey, name)
  120. // Write data to store.
  121. v := url.Values{}
  122. v.Set("raft", peerURL)
  123. v.Set("etcd", machURL)
  124. log.Debugf("Update PeerURL: %s", name)
  125. if _, err := r.store.Update(path.Join(RegistryKey, name), v.Encode(), store.Permanent); err != nil {
  126. return err
  127. }
  128. r.Lock()
  129. defer r.Unlock()
  130. // Invalidate outdated cache.
  131. r.invalidate(name)
  132. return nil
  133. }
  134. func (r *Registry) name(key, name string) (string, bool) {
  135. return name, true
  136. }
  137. // Names returns a list of cached peer names.
  138. func (r *Registry) Names() []string {
  139. names := r.urls(RegistryKey, "", "", r.name)
  140. sort.Sort(sort.StringSlice(names))
  141. return names
  142. }
  143. // Retrieves the Client URLs for all nodes.
  144. func (r *Registry) ClientURLs(leaderName, selfName string) []string {
  145. return r.urls(RegistryKey, leaderName, selfName, r.clientURL)
  146. }
  147. // Retrieves the Peer URLs for all nodes.
  148. func (r *Registry) PeerURLs(leaderName, selfName string) []string {
  149. return r.urls(RegistryKey, leaderName, selfName, r.peerURL)
  150. }
  151. // Retrieves the URLs for all nodes using url function.
  152. func (r *Registry) urls(key, leaderName, selfName string, url func(key, name string) (string, bool)) []string {
  153. r.Lock()
  154. defer r.Unlock()
  155. // Build list including the leader and self.
  156. urls := make([]string, 0)
  157. if url, _ := url(key, leaderName); len(url) > 0 {
  158. urls = append(urls, url)
  159. }
  160. // Retrieve a list of all nodes.
  161. if e, _ := r.store.Get(key, false, false); e != nil {
  162. // Lookup the URL for each one.
  163. for _, pair := range e.Node.Nodes {
  164. _, name := filepath.Split(pair.Key)
  165. if url, _ := url(key, name); len(url) > 0 && name != leaderName {
  166. urls = append(urls, url)
  167. }
  168. }
  169. }
  170. log.Debugf("URLs: %s: %s / %s (%s)", key, leaderName, selfName, strings.Join(urls, ","))
  171. return urls
  172. }
  173. // Removes a node from the cache.
  174. func (r *Registry) Invalidate(name string) {
  175. r.Lock()
  176. defer r.Unlock()
  177. r.invalidate(name)
  178. }
  179. func (r *Registry) invalidate(name string) {
  180. delete(r.peers, name)
  181. }
  182. // Loads the given node by name from the store into the cache.
  183. func (r *Registry) load(key, name string) *node {
  184. if name == "" {
  185. return nil
  186. }
  187. // Retrieve from store.
  188. e, err := r.store.Get(path.Join(key, name), false, false)
  189. if err != nil {
  190. return nil
  191. }
  192. // Parse as a query string.
  193. m, err := url.ParseQuery(*e.Node.Value)
  194. if err != nil {
  195. panic(fmt.Sprintf("Failed to parse peers entry: %s", name))
  196. }
  197. // Create node.
  198. return &node{
  199. url: m["etcd"][0],
  200. peerURL: m["raft"][0],
  201. }
  202. }