registry.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 RegistryPeerKey = "/_etcd/machines"
  15. // The location of the proxy URL data.
  16. const RegistryProxyKey = "/_etcd/proxies"
  17. // The Registry stores URL information for nodes.
  18. type Registry struct {
  19. sync.Mutex
  20. store store.Store
  21. peers map[string]*node
  22. proxies map[string]*node
  23. }
  24. // The internal storage format of the registry.
  25. type node struct {
  26. peerVersion string
  27. peerURL string
  28. url string
  29. }
  30. // Creates a new Registry.
  31. func NewRegistry(s store.Store) *Registry {
  32. return &Registry{
  33. store: s,
  34. peers: make(map[string]*node),
  35. proxies: make(map[string]*node),
  36. }
  37. }
  38. // Peers returns a list of cached peer names.
  39. func (r *Registry) Peers() []string {
  40. names := make([]string, 0, len(r.peers))
  41. for name, _ := range r.peers {
  42. names = append(names, name)
  43. }
  44. sort.Sort(sort.StringSlice(names))
  45. return names
  46. }
  47. // Proxies returns a list of cached proxy names.
  48. func (r *Registry) Proxies() []string {
  49. names := make([]string, 0, len(r.proxies))
  50. for name, _ := range r.proxies {
  51. names = append(names, name)
  52. }
  53. sort.Sort(sort.StringSlice(names))
  54. return names
  55. }
  56. // RegisterPeer adds a peer to the registry.
  57. func (r *Registry) RegisterPeer(name string, peerURL string, machURL string) error {
  58. if err := r.register(RegistryPeerKey, name, peerURL, machURL); err != nil {
  59. return err
  60. }
  61. r.peers[name] = r.load(RegistryPeerKey, name)
  62. return nil
  63. }
  64. // RegisterProxy adds a proxy to the registry.
  65. func (r *Registry) RegisterProxy(name string, peerURL string, machURL string) error {
  66. if err := r.register(RegistryProxyKey, name, peerURL, machURL); err != nil {
  67. return err
  68. }
  69. r.proxies[name] = r.load(RegistryProxyKey, name)
  70. return nil
  71. }
  72. func (r *Registry) register(key, name string, peerURL string, machURL string) error {
  73. r.Lock()
  74. defer r.Unlock()
  75. // Write data to store.
  76. v := url.Values{}
  77. v.Set("raft", peerURL)
  78. v.Set("etcd", machURL)
  79. _, err := r.store.Create(path.Join(key, name), false, v.Encode(), false, store.Permanent)
  80. log.Debugf("Register: %s", name)
  81. return err
  82. }
  83. // UnregisterPeer removes a peer from the registry.
  84. func (r *Registry) UnregisterPeer(name string) error {
  85. return r.unregister(RegistryPeerKey, name)
  86. }
  87. // UnregisterProxy removes a proxy from the registry.
  88. func (r *Registry) UnregisterProxy(name string) error {
  89. return r.unregister(RegistryProxyKey, name)
  90. }
  91. func (r *Registry) unregister(key, name string) error {
  92. r.Lock()
  93. defer r.Unlock()
  94. // Remove the key from the store.
  95. _, err := r.store.Delete(path.Join(key, name), false, false)
  96. log.Debugf("Unregister: %s", name)
  97. return err
  98. }
  99. // PeerCount returns the number of peers in the cluster.
  100. func (r *Registry) PeerCount() int {
  101. return r.count(RegistryPeerKey)
  102. }
  103. // ProxyCount returns the number of proxies in the cluster.
  104. func (r *Registry) ProxyCount() int {
  105. return r.count(RegistryProxyKey)
  106. }
  107. // Returns the number of nodes in the cluster.
  108. func (r *Registry) count(key string) int {
  109. e, err := r.store.Get(key, false, false)
  110. if err != nil {
  111. return 0
  112. }
  113. return len(e.Node.Nodes)
  114. }
  115. // PeerExists checks if a peer with the given name exists.
  116. func (r *Registry) PeerExists(name string) bool {
  117. return r.exists(RegistryPeerKey, name)
  118. }
  119. // ProxyExists checks if a proxy with the given name exists.
  120. func (r *Registry) ProxyExists(name string) bool {
  121. return r.exists(RegistryProxyKey, name)
  122. }
  123. func (r *Registry) exists(key, name string) bool {
  124. e, err := r.store.Get(path.Join(key, name), false, false)
  125. if err != nil {
  126. return false
  127. }
  128. return (e.Node != nil)
  129. }
  130. // Retrieves the client URL for a given node by name.
  131. func (r *Registry) ClientURL(name string) (string, bool) {
  132. r.Lock()
  133. defer r.Unlock()
  134. return r.clientURL(RegistryPeerKey, name)
  135. }
  136. func (r *Registry) clientURL(key, name string) (string, bool) {
  137. if r.peers[name] == nil {
  138. if node := r.load(key, name); node != nil {
  139. r.peers[name] = node
  140. }
  141. }
  142. if node := r.peers[name]; node != nil {
  143. return node.url, true
  144. }
  145. return "", false
  146. }
  147. // TODO(yichengq): have all of the code use a full URL with scheme
  148. // and remove this method
  149. // PeerHost retrieves the host part of peer URL for a given node by name.
  150. func (r *Registry) PeerHost(name string) (string, bool) {
  151. rawurl, ok := r.PeerURL(name)
  152. if ok {
  153. u, _ := url.Parse(rawurl)
  154. return u.Host, ok
  155. }
  156. return rawurl, ok
  157. }
  158. // Retrieves the peer URL for a given node by name.
  159. func (r *Registry) PeerURL(name string) (string, bool) {
  160. r.Lock()
  161. defer r.Unlock()
  162. return r.peerURL(RegistryPeerKey, name)
  163. }
  164. func (r *Registry) peerURL(key, name string) (string, bool) {
  165. if r.peers[name] == nil {
  166. if node := r.load(key, name); node != nil {
  167. r.peers[name] = node
  168. }
  169. }
  170. if node := r.peers[name]; node != nil {
  171. return node.peerURL, true
  172. }
  173. return "", false
  174. }
  175. // Retrieves the client URL for a given proxy by name.
  176. func (r *Registry) ProxyClientURL(name string) (string, bool) {
  177. r.Lock()
  178. defer r.Unlock()
  179. return r.proxyClientURL(RegistryProxyKey, name)
  180. }
  181. func (r *Registry) proxyClientURL(key, name string) (string, bool) {
  182. if r.proxies[name] == nil {
  183. if node := r.load(key, name); node != nil {
  184. r.proxies[name] = node
  185. }
  186. }
  187. if node := r.proxies[name]; node != nil {
  188. return node.url, true
  189. }
  190. return "", false
  191. }
  192. // Retrieves the peer URL for a given proxy by name.
  193. func (r *Registry) ProxyPeerURL(name string) (string, bool) {
  194. r.Lock()
  195. defer r.Unlock()
  196. return r.proxyPeerURL(RegistryProxyKey, name)
  197. }
  198. func (r *Registry) proxyPeerURL(key, name string) (string, bool) {
  199. if r.proxies[name] == nil {
  200. if node := r.load(key, name); node != nil {
  201. r.proxies[name] = node
  202. }
  203. }
  204. if node := r.proxies[name]; node != nil {
  205. return node.peerURL, true
  206. }
  207. return "", false
  208. }
  209. // Retrieves the Client URLs for all nodes.
  210. func (r *Registry) ClientURLs(leaderName, selfName string) []string {
  211. return r.urls(RegistryPeerKey, leaderName, selfName, r.clientURL)
  212. }
  213. // Retrieves the Peer URLs for all nodes.
  214. func (r *Registry) PeerURLs(leaderName, selfName string) []string {
  215. return r.urls(RegistryPeerKey, leaderName, selfName, r.peerURL)
  216. }
  217. // Retrieves the URLs for all nodes using url function.
  218. func (r *Registry) urls(key, leaderName, selfName string, url func(key, name string) (string, bool)) []string {
  219. r.Lock()
  220. defer r.Unlock()
  221. // Build list including the leader and self.
  222. urls := make([]string, 0)
  223. if url, _ := url(key, leaderName); len(url) > 0 {
  224. urls = append(urls, url)
  225. }
  226. // Retrieve a list of all nodes.
  227. if e, _ := r.store.Get(key, false, false); e != nil {
  228. // Lookup the URL for each one.
  229. for _, pair := range e.Node.Nodes {
  230. _, name := filepath.Split(pair.Key)
  231. if url, _ := url(key, name); len(url) > 0 && name != leaderName {
  232. urls = append(urls, url)
  233. }
  234. }
  235. }
  236. log.Infof("URLs: %s: %s / %s (%s)", key, leaderName, selfName, strings.Join(urls, ","))
  237. return urls
  238. }
  239. // Removes a node from the cache.
  240. func (r *Registry) Invalidate(name string) {
  241. delete(r.peers, name)
  242. delete(r.proxies, name)
  243. }
  244. // Loads the given node by name from the store into the cache.
  245. func (r *Registry) load(key, name string) *node {
  246. if name == "" {
  247. return nil
  248. }
  249. // Retrieve from store.
  250. e, err := r.store.Get(path.Join(key, name), false, false)
  251. if err != nil {
  252. return nil
  253. }
  254. // Parse as a query string.
  255. m, err := url.ParseQuery(*e.Node.Value)
  256. if err != nil {
  257. panic(fmt.Sprintf("Failed to parse peers entry: %s", name))
  258. }
  259. // Create node.
  260. return &node{
  261. url: m["etcd"][0],
  262. peerURL: m["raft"][0],
  263. }
  264. }