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