node.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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)) *etcdErr.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, "", UndefIndex, UndefTerm)
  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, *etcdErr.Error) {
  97. if n.IsDir() {
  98. return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", UndefIndex, UndefTerm)
  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) *etcdErr.Error {
  105. if n.IsDir() {
  106. return etcdErr.NewError(etcdErr.EcodeNotFile, "", UndefIndex, UndefTerm)
  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, *etcdErr.Error) {
  116. if !n.IsDir() {
  117. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", UndefIndex, UndefTerm)
  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. // GetChild function returns the child node under the directory node.
  128. // On success, it returns the file node
  129. func (n *Node) GetChild(name string) (*Node, *etcdErr.Error) {
  130. if !n.IsDir() {
  131. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, UndefIndex, UndefTerm)
  132. }
  133. child, ok := n.Children[name]
  134. if ok {
  135. return child, nil
  136. }
  137. return nil, nil
  138. }
  139. // Add function adds a node to the receiver node.
  140. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  141. // If there is a existing node with the same name under the directory, a "Already Exist"
  142. // error will be returned
  143. func (n *Node) Add(child *Node) *etcdErr.Error {
  144. if !n.IsDir() {
  145. return etcdErr.NewError(etcdErr.EcodeNotDir, "", UndefIndex, UndefTerm)
  146. }
  147. _, name := path.Split(child.Path)
  148. _, ok := n.Children[name]
  149. if ok {
  150. return etcdErr.NewError(etcdErr.EcodeNodeExist, "", UndefIndex, UndefTerm)
  151. }
  152. n.Children[name] = child
  153. return nil
  154. }
  155. // Clone function clone the node recursively and return the new node.
  156. // If the node is a directory, it will clone all the content under this directory.
  157. // If the node is a key-value pair, it will clone the pair.
  158. func (n *Node) Clone() *Node {
  159. if !n.IsDir() {
  160. return newFile(n.Path, n.Value, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  161. }
  162. clone := newDir(n.Path, n.CreateIndex, n.CreateTerm, n.Parent, n.ACL, n.ExpireTime)
  163. for key, child := range n.Children {
  164. clone.Children[key] = child.Clone()
  165. }
  166. return clone
  167. }
  168. func (n *Node) recoverAndclean(s *Store) {
  169. if n.IsDir() {
  170. for _, child := range n.Children {
  171. child.Parent = n
  172. child.recoverAndclean(s)
  173. }
  174. }
  175. n.stopExpire = make(chan bool, 1)
  176. n.Expire(s)
  177. }
  178. // Expire function will test if the node is expired.
  179. // if the node is already expired, delete the node and return.
  180. // if the node is permemant (this shouldn't happen), return at once.
  181. // else wait for a period time, then remove the node. and notify the watchhub.
  182. func (n *Node) Expire(s *Store) {
  183. expired, duration := n.IsExpired()
  184. if expired { // has been expired
  185. // since the parent function of Expire() runs serially,
  186. // there is no need for lock here
  187. e := newEvent(Expire, n.Path, UndefIndex, UndefTerm)
  188. s.WatcherHub.notify(e)
  189. n.Remove(true, nil)
  190. s.Stats.Inc(ExpireCount)
  191. return
  192. }
  193. if duration == 0 { // Permanent Node
  194. return
  195. }
  196. go func() { // do monitoring
  197. select {
  198. // if timeout, delete the node
  199. case <-time.After(duration):
  200. s.worldLock.Lock()
  201. defer s.worldLock.Unlock()
  202. e := newEvent(Expire, n.Path, UndefIndex, UndefTerm)
  203. s.WatcherHub.notify(e)
  204. n.Remove(true, nil)
  205. s.Stats.Inc(ExpireCount)
  206. return
  207. // if stopped, return
  208. case <-n.stopExpire:
  209. return
  210. }
  211. }()
  212. }
  213. // IsHidden function checks if the node is a hidden node. A hidden node
  214. // will begin with '_'
  215. // A hidden node will not be shown via get command under a directory
  216. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  217. // will only return /foo/notHidden
  218. func (n *Node) IsHidden() bool {
  219. _, name := path.Split(n.Path)
  220. return name[0] == '_'
  221. }
  222. func (n *Node) IsPermanent() bool {
  223. return n.ExpireTime.Sub(Permanent) == 0
  224. }
  225. func (n *Node) IsExpired() (bool, time.Duration) {
  226. if n.IsPermanent() {
  227. return false, 0
  228. }
  229. duration := n.ExpireTime.Sub(time.Now())
  230. if duration <= 0 {
  231. return true, 0
  232. }
  233. return false, duration
  234. }
  235. // IsDir function checks whether the node is a directory.
  236. // If the node is a directory, the function will return true.
  237. // Otherwise the function will return false.
  238. func (n *Node) IsDir() bool {
  239. return !(n.Children == nil)
  240. }
  241. func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
  242. if n.IsDir() {
  243. pair := KeyValuePair{
  244. Key: n.Path,
  245. Dir: true,
  246. }
  247. if !recurisive {
  248. return pair
  249. }
  250. children, _ := n.List()
  251. pair.KVPairs = make([]KeyValuePair, len(children))
  252. // we do not use the index in the children slice directly
  253. // we need to skip the hidden one
  254. i := 0
  255. for _, child := range children {
  256. if child.IsHidden() { // get will not list hidden node
  257. continue
  258. }
  259. pair.KVPairs[i] = child.Pair(recurisive, sorted)
  260. i++
  261. }
  262. // eliminate hidden nodes
  263. pair.KVPairs = pair.KVPairs[:i]
  264. if sorted {
  265. sort.Sort(pair)
  266. }
  267. return pair
  268. }
  269. return KeyValuePair{
  270. Key: n.Path,
  271. Value: n.Value,
  272. }
  273. }
  274. func (n *Node) UpdateTTL(expireTime time.Time, s *Store) {
  275. if !n.IsPermanent() {
  276. // check if the node has been expired
  277. // if the node is not expired, we need to stop the go routine associated with
  278. // that node.
  279. expired, _ := n.IsExpired()
  280. if !expired {
  281. n.stopExpire <- true // suspend it to modify the expiration
  282. }
  283. }
  284. if expireTime.Sub(Permanent) != 0 {
  285. n.ExpireTime = expireTime
  286. n.Expire(s)
  287. }
  288. }