registry.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 standby URL data.
  16. const RegistryStandbyKey = "/_etcd/standbys"
  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. standbys 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. standbys: 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. // Standbys returns a list of cached standby names.
  50. func (r *Registry) Standbys() []string {
  51. r.Lock()
  52. defer r.Unlock()
  53. names := make([]string, 0, len(r.standbys))
  54. for name := range r.standbys {
  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. // RegisterStandby adds a standby to the registry.
  71. func (r *Registry) RegisterStandby(name string, peerURL string, machURL string) error {
  72. if err := r.register(RegistryStandbyKey, name, peerURL, machURL); err != nil {
  73. return err
  74. }
  75. r.Lock()
  76. defer r.Unlock()
  77. r.standbys[name] = r.load(RegistryStandbyKey, 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. // UpdatePeerURL updates peer URL in registry
  90. func (r *Registry) UpdatePeerURL(name string, peerURL string) error {
  91. r.Lock()
  92. defer r.Unlock()
  93. machURL, _ := r.clientURL(RegistryPeerKey, name)
  94. // Write data to store.
  95. key := path.Join(RegistryPeerKey, name)
  96. v := url.Values{}
  97. v.Set("raft", peerURL)
  98. v.Set("etcd", machURL)
  99. _, err := r.store.Update(key, v.Encode(), store.Permanent)
  100. // Invalidate outdated cache.
  101. r.invalidate(name)
  102. log.Debugf("Update PeerURL: %s", name)
  103. return err
  104. }
  105. // UnregisterPeer removes a peer from the registry.
  106. func (r *Registry) UnregisterPeer(name string) error {
  107. return r.unregister(RegistryPeerKey, name)
  108. }
  109. // UnregisterStandby removes a standby from the registry.
  110. func (r *Registry) UnregisterStandby(name string) error {
  111. return r.unregister(RegistryStandbyKey, name)
  112. }
  113. func (r *Registry) unregister(key, name string) error {
  114. // Remove the key from the store.
  115. _, err := r.store.Delete(path.Join(key, name), false, false)
  116. log.Debugf("Unregister: %s", name)
  117. return err
  118. }
  119. // PeerCount returns the number of peers in the cluster.
  120. func (r *Registry) PeerCount() int {
  121. return r.count(RegistryPeerKey)
  122. }
  123. // StandbyCount returns the number of standbys in the cluster.
  124. func (r *Registry) StandbyCount() int {
  125. return r.count(RegistryStandbyKey)
  126. }
  127. // Returns the number of nodes in the cluster.
  128. func (r *Registry) count(key string) int {
  129. e, err := r.store.Get(key, false, false)
  130. if err != nil {
  131. return 0
  132. }
  133. return len(e.Node.Nodes)
  134. }
  135. // PeerExists checks if a peer with the given name exists.
  136. func (r *Registry) PeerExists(name string) bool {
  137. return r.exists(RegistryPeerKey, name)
  138. }
  139. // StandbyExists checks if a standby with the given name exists.
  140. func (r *Registry) StandbyExists(name string) bool {
  141. return r.exists(RegistryStandbyKey, name)
  142. }
  143. func (r *Registry) exists(key, name string) bool {
  144. e, err := r.store.Get(path.Join(key, name), false, false)
  145. if err != nil {
  146. return false
  147. }
  148. return (e.Node != nil)
  149. }
  150. // Retrieves the client URL for a given node by name.
  151. func (r *Registry) ClientURL(name string) (string, bool) {
  152. r.Lock()
  153. defer r.Unlock()
  154. return r.clientURL(RegistryPeerKey, name)
  155. }
  156. func (r *Registry) clientURL(key, name string) (string, bool) {
  157. if r.peers[name] == nil {
  158. if node := r.load(key, name); node != nil {
  159. r.peers[name] = node
  160. }
  161. }
  162. if node := r.peers[name]; node != nil {
  163. return node.url, true
  164. }
  165. return "", false
  166. }
  167. // TODO(yichengq): have all of the code use a full URL with scheme
  168. // and remove this method
  169. // PeerHost retrieves the host part of peer URL for a given node by name.
  170. func (r *Registry) PeerHost(name string) (string, bool) {
  171. rawurl, ok := r.PeerURL(name)
  172. if ok {
  173. u, _ := url.Parse(rawurl)
  174. return u.Host, ok
  175. }
  176. return rawurl, ok
  177. }
  178. // Retrieves the peer URL for a given node by name.
  179. func (r *Registry) PeerURL(name string) (string, bool) {
  180. r.Lock()
  181. defer r.Unlock()
  182. return r.peerURL(RegistryPeerKey, name)
  183. }
  184. func (r *Registry) peerURL(key, name string) (string, bool) {
  185. if r.peers[name] == nil {
  186. if node := r.load(key, name); node != nil {
  187. r.peers[name] = node
  188. }
  189. }
  190. if node := r.peers[name]; node != nil {
  191. return node.peerURL, true
  192. }
  193. return "", false
  194. }
  195. // Retrieves the client URL for a given standby by name.
  196. func (r *Registry) StandbyClientURL(name string) (string, bool) {
  197. r.Lock()
  198. defer r.Unlock()
  199. return r.standbyClientURL(RegistryStandbyKey, name)
  200. }
  201. func (r *Registry) standbyClientURL(key, name string) (string, bool) {
  202. if r.standbys[name] == nil {
  203. if node := r.load(key, name); node != nil {
  204. r.standbys[name] = node
  205. }
  206. }
  207. if node := r.standbys[name]; node != nil {
  208. return node.url, true
  209. }
  210. return "", false
  211. }
  212. // Retrieves the peer URL for a given standby by name.
  213. func (r *Registry) StandbyPeerURL(name string) (string, bool) {
  214. r.Lock()
  215. defer r.Unlock()
  216. return r.standbyPeerURL(RegistryStandbyKey, name)
  217. }
  218. func (r *Registry) standbyPeerURL(key, name string) (string, bool) {
  219. if r.standbys[name] == nil {
  220. if node := r.load(key, name); node != nil {
  221. r.standbys[name] = node
  222. }
  223. }
  224. if node := r.standbys[name]; node != nil {
  225. return node.peerURL, true
  226. }
  227. return "", false
  228. }
  229. // Retrieves the Client URLs for all nodes.
  230. func (r *Registry) ClientURLs(leaderName, selfName string) []string {
  231. return r.urls(RegistryPeerKey, leaderName, selfName, r.clientURL)
  232. }
  233. // Retrieves the Peer URLs for all nodes.
  234. func (r *Registry) PeerURLs(leaderName, selfName string) []string {
  235. return r.urls(RegistryPeerKey, leaderName, selfName, r.peerURL)
  236. }
  237. // Retrieves the URLs for all nodes using url function.
  238. func (r *Registry) urls(key, leaderName, selfName string, url func(key, name string) (string, bool)) []string {
  239. r.Lock()
  240. defer r.Unlock()
  241. // Build list including the leader and self.
  242. urls := make([]string, 0)
  243. if url, _ := url(key, leaderName); len(url) > 0 {
  244. urls = append(urls, url)
  245. }
  246. // Retrieve a list of all nodes.
  247. if e, _ := r.store.Get(key, false, false); e != nil {
  248. // Lookup the URL for each one.
  249. for _, pair := range e.Node.Nodes {
  250. _, name := filepath.Split(pair.Key)
  251. if url, _ := url(key, name); len(url) > 0 && name != leaderName {
  252. urls = append(urls, url)
  253. }
  254. }
  255. }
  256. log.Debugf("URLs: %s: %s / %s (%s)", key, leaderName, selfName, strings.Join(urls, ","))
  257. return urls
  258. }
  259. // Removes a node from the cache.
  260. func (r *Registry) Invalidate(name string) {
  261. r.Lock()
  262. defer r.Unlock()
  263. r.invalidate(name)
  264. }
  265. func (r *Registry) invalidate(name string) {
  266. delete(r.peers, name)
  267. delete(r.standbys, name)
  268. }
  269. // Loads the given node by name from the store into the cache.
  270. func (r *Registry) load(key, name string) *node {
  271. if name == "" {
  272. return nil
  273. }
  274. // Retrieve from store.
  275. e, err := r.store.Get(path.Join(key, name), false, false)
  276. if err != nil {
  277. return nil
  278. }
  279. // Parse as a query string.
  280. m, err := url.ParseQuery(*e.Node.Value)
  281. if err != nil {
  282. panic(fmt.Sprintf("Failed to parse peers entry: %s", name))
  283. }
  284. // Create node.
  285. return &node{
  286. url: m["etcd"][0],
  287. peerURL: m["raft"][0],
  288. }
  289. }