registry.go 7.5 KB

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