node.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. package store
  2. import (
  3. "path"
  4. "sort"
  5. "time"
  6. etcdErr "github.com/coreos/etcd/error"
  7. )
  8. var (
  9. Permanent time.Time
  10. )
  11. const (
  12. normal = iota
  13. removed
  14. )
  15. type Node struct {
  16. Path string
  17. CreateIndex uint64
  18. CreateTerm uint64
  19. ModifiedIndex uint64
  20. ModifiedTerm uint64
  21. Parent *Node `json:"-"`
  22. ExpireTime time.Time
  23. ACL string
  24. Value string // for key-value pair
  25. Children map[string]*Node // for directory
  26. status int
  27. stopExpire chan bool // stop expire routine channel
  28. }
  29. func newFile(nodePath string, value string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  30. return &Node{
  31. Path: nodePath,
  32. CreateIndex: createIndex,
  33. CreateTerm: createTerm,
  34. ModifiedIndex: createIndex,
  35. ModifiedTerm: createTerm,
  36. Parent: parent,
  37. ACL: ACL,
  38. stopExpire: make(chan bool, 1),
  39. ExpireTime: expireTime,
  40. Value: value,
  41. }
  42. }
  43. func newDir(nodePath string, createIndex uint64, createTerm uint64, parent *Node, ACL string, expireTime time.Time) *Node {
  44. return &Node{
  45. Path: nodePath,
  46. CreateIndex: createIndex,
  47. CreateTerm: createTerm,
  48. Parent: parent,
  49. ACL: ACL,
  50. stopExpire: make(chan bool, 1),
  51. ExpireTime: expireTime,
  52. Children: make(map[string]*Node),
  53. }
  54. }
  55. // Remove function remove the node.
  56. // If the node is a directory and recursive is true, the function will recursively remove
  57. // add nodes under the receiver node.
  58. func (n *Node) Remove(recursive bool, callback func(path string)) error {
  59. if n.status == removed { // check race between remove and expire
  60. return nil
  61. }
  62. if !n.IsDir() { // file node: key-value pair
  63. _, name := path.Split(n.Path)
  64. if n.Parent != nil && n.Parent.Children[name] == n {
  65. // This is the only pointer to Node object
  66. // Handled by garbage collector
  67. delete(n.Parent.Children, name)
  68. if callback != nil {
  69. callback(n.Path)
  70. }
  71. n.stopExpire <- true
  72. n.status = removed
  73. }
  74. return nil
  75. }
  76. if !recursive {
  77. return etcdErr.NewError(etcdErr.EcodeNotFile, "")
  78. }
  79. for _, child := range n.Children { // delete all children
  80. child.Remove(true, callback)
  81. }
  82. // delete self
  83. _, name := path.Split(n.Path)
  84. if n.Parent != nil && n.Parent.Children[name] == n {
  85. delete(n.Parent.Children, name)
  86. if callback != nil {
  87. callback(n.Path)
  88. }
  89. n.stopExpire <- true
  90. n.status = removed
  91. }
  92. return nil
  93. }
  94. // Read function gets the value of the node.
  95. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  96. func (n *Node) Read() (string, error) {
  97. if n.IsDir() {
  98. return "", etcdErr.NewError(etcdErr.EcodeNotFile, "")
  99. }
  100. return n.Value, nil
  101. }
  102. // Write function set the value of the node to the given value.
  103. // If the receiver node is a directory, a "Not A File" error will be returned.
  104. func (n *Node) Write(value string, index uint64, term uint64) error {
  105. if n.IsDir() {
  106. return etcdErr.NewError(etcdErr.EcodeNotFile, "")
  107. }
  108. n.Value = value
  109. n.ModifiedIndex = index
  110. n.ModifiedTerm = term
  111. return nil
  112. }
  113. // List function return a slice of nodes under the receiver node.
  114. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  115. func (n *Node) List() ([]*Node, error) {
  116. if !n.IsDir() {
  117. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "")
  118. }
  119. nodes := make([]*Node, len(n.Children))
  120. i := 0
  121. for _, node := range n.Children {
  122. nodes[i] = node
  123. i++
  124. }
  125. return nodes, nil
  126. }
  127. // GetFile function returns the file node under the directory node.
  128. // On success, it returns the file node
  129. // If the node that calls this function is not a directory, it returns
  130. // Not Directory Error
  131. // If the node corresponding to the name string is not file, it returns
  132. // Not File Error
  133. func (n *Node) GetFile(name string) (*Node, error) {
  134. if !n.IsDir() {
  135. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path)
  136. }
  137. f, ok := n.Children[name]
  138. if ok {
  139. if !f.IsDir() {
  140. return f, nil
  141. } else {
  142. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, f.Path)
  143. }
  144. }
  145. return nil, nil
  146. }
  147. // Add function adds a node to the receiver node.
  148. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  149. // If there is a existing node with the same name under the directory, a "Already Exist"
  150. // error will be returned
  151. func (n *Node) Add(child *Node) error {
  152. if !n.IsDir() {
  153. return etcdErr.NewError(etcdErr.EcodeNotDir, "")
  154. }
  155. _, name := path.Split(child.Path)
  156. _, ok := n.Children[name]
  157. if ok {
  158. return etcdErr.NewError(etcdErr.EcodeNodeExist, "")
  159. }
  160. n.Children[name] = child
  161. return nil
  162. }
  163. // Clone function clone the node recursively and return the new node.
  164. // If the node is a directory, it will clone all the content under this directory.
  165. // If the node is a key-value pair, it will clone the pair.
  166. func (n *Node) Clone() *Node {
  167. if !n.IsDir() {
  168. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  169. }
  170. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  171. for key, child := range n.Children {
  172. clone.Children[key] = child.Clone()
  173. }
  174. return clone
  175. }
  176. func (n *Node) recoverAndclean(s *Store) {
  177. if n.IsDir() {
  178. for _, child := range n.Children {
  179. child.Parent = n
  180. child.recoverAndclean(s)
  181. }
  182. }
  183. n.stopExpire = make(chan bool, 1)
  184. n.Expire(s)
  185. }
  186. // Expire function will test if the node is expired.
  187. // if the node is already expired, delete the node and return.
  188. // if the node is permemant (this shouldn't happen), return at once.
  189. // else wait for a period time, then remove the node. and notify the watchhub.
  190. func (n *Node) Expire(s *Store) {
  191. expired, duration := n.IsExpired()
  192. if expired { // has been expired
  193. // since the parent function of Expire() runs serially,
  194. // there is no need for lock here
  195. e := newEvent(Expire, n.Path, UndefIndex, UndefTerm)
  196. s.WatcherHub.notify(e)
  197. n.Remove(true, nil)
  198. s.Stats.Inc(ExpireCount)
  199. return
  200. }
  201. if duration == 0 { // Permanent Node
  202. return
  203. }
  204. go func() { // do monitoring
  205. select {
  206. // if timeout, delete the node
  207. case <-time.After(duration):
  208. s.worldLock.Lock()
  209. defer s.worldLock.Unlock()
  210. e := newEvent(Expire, n.Path, UndefIndex, UndefTerm)
  211. s.WatcherHub.notify(e)
  212. n.Remove(true, nil)
  213. s.Stats.Inc(ExpireCount)
  214. return
  215. // if stopped, return
  216. case <-n.stopExpire:
  217. return
  218. }
  219. }()
  220. }
  221. // IsHidden function checks if the node is a hidden node. A hidden node
  222. // will begin with '_'
  223. // A hidden node will not be shown via get command under a directory
  224. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  225. // will only return /foo/notHidden
  226. func (n *Node) IsHidden() bool {
  227. _, name := path.Split(n.Path)
  228. return name[0] == '_'
  229. }
  230. func (n *Node) IsPermanent() bool {
  231. return n.ExpireTime.Sub(Permanent) == 0
  232. }
  233. func (n *Node) IsExpired() (bool, time.Duration) {
  234. if n.IsPermanent() {
  235. return false, 0
  236. }
  237. duration := n.ExpireTime.Sub(time.Now())
  238. if duration <= 0 {
  239. return true, 0
  240. }
  241. return false, duration
  242. }
  243. // IsDir function checks whether the node is a directory.
  244. // If the node is a directory, the function will return true.
  245. // Otherwise the function will return false.
  246. func (n *Node) IsDir() bool {
  247. return !(n.Children == nil)
  248. }
  249. func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
  250. if n.IsDir() {
  251. pair := KeyValuePair{
  252. Key: n.Path,
  253. Dir: true,
  254. }
  255. if !recurisive {
  256. return pair
  257. }
  258. children, _ := n.List()
  259. pair.KVPairs = make([]KeyValuePair, len(children))
  260. // we do not use the index in the children slice directly
  261. // we need to skip the hidden one
  262. i := 0
  263. for _, child := range children {
  264. if child.IsHidden() { // get will not list hidden node
  265. continue
  266. }
  267. pair.KVPairs[i] = child.Pair(recurisive, sorted)
  268. i++
  269. }
  270. // eliminate hidden nodes
  271. pair.KVPairs = pair.KVPairs[:i]
  272. if sorted {
  273. sort.Sort(pair)
  274. }
  275. return pair
  276. }
  277. return KeyValuePair{
  278. Key: n.Path,
  279. Value: n.Value,
  280. }
  281. }
  282. func (n *Node) UpdateTTL(expireTime time.Time, s *Store) {
  283. if !n.IsPermanent() {
  284. expired, _ := n.IsExpired()
  285. if !expired {
  286. n.stopExpire <- true // suspend it to modify the expiration
  287. }
  288. }
  289. if expireTime.Sub(Permanent) != 0 {
  290. n.ExpireTime = expireTime
  291. n.Expire(s)
  292. }
  293. }