node.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package store
  2. import (
  3. "path"
  4. "sort"
  5. "time"
  6. etcdErr "github.com/coreos/etcd/error"
  7. )
  8. // explanations of Compare function result
  9. const (
  10. CompareMatch = 0
  11. CompareIndexNotMatch = 1
  12. CompareValueNotMatch = 2
  13. CompareNotMatch = 3
  14. )
  15. var Permanent time.Time
  16. // node is the basic element in the store system.
  17. // A key-value pair will have a string value
  18. // A directory will have a children map
  19. type node struct {
  20. Path string
  21. CreatedIndex uint64
  22. ModifiedIndex uint64
  23. Parent *node `json:"-"` // should not encode this field! avoid circular dependency.
  24. ExpireTime time.Time
  25. ACL string
  26. Value string // for key-value pair
  27. Children map[string]*node // for directory
  28. // A reference to the store this node is attached to.
  29. store *store
  30. }
  31. // newKV creates a Key-Value pair
  32. func newKV(store *store, nodePath string, value string, createdIndex uint64,
  33. parent *node, ACL string, expireTime time.Time) *node {
  34. return &node{
  35. Path: nodePath,
  36. CreatedIndex: createdIndex,
  37. ModifiedIndex: createdIndex,
  38. Parent: parent,
  39. ACL: ACL,
  40. store: store,
  41. ExpireTime: expireTime,
  42. Value: value,
  43. }
  44. }
  45. // newDir creates a directory
  46. func newDir(store *store, nodePath string, createdIndex uint64, parent *node,
  47. ACL string, expireTime time.Time) *node {
  48. return &node{
  49. Path: nodePath,
  50. CreatedIndex: createdIndex,
  51. ModifiedIndex: createdIndex,
  52. Parent: parent,
  53. ACL: ACL,
  54. ExpireTime: expireTime,
  55. Children: make(map[string]*node),
  56. store: store,
  57. }
  58. }
  59. // IsHidden function checks if the node is a hidden node. A hidden node
  60. // will begin with '_'
  61. // A hidden node will not be shown via get command under a directory
  62. // For example if we have /foo/_hidden and /foo/notHidden, get "/foo"
  63. // will only return /foo/notHidden
  64. func (n *node) IsHidden() bool {
  65. _, name := path.Split(n.Path)
  66. return name[0] == '_'
  67. }
  68. // IsPermanent function checks if the node is a permanent one.
  69. func (n *node) IsPermanent() bool {
  70. // we use a uninitialized time.Time to indicate the node is a
  71. // permanent one.
  72. // the uninitialized time.Time should equal zero.
  73. return n.ExpireTime.IsZero()
  74. }
  75. // IsDir function checks whether the node is a directory.
  76. // If the node is a directory, the function will return true.
  77. // Otherwise the function will return false.
  78. func (n *node) IsDir() bool {
  79. return !(n.Children == nil)
  80. }
  81. // Read function gets the value of the node.
  82. // If the receiver node is not a key-value pair, a "Not A File" error will be returned.
  83. func (n *node) Read() (string, *etcdErr.Error) {
  84. if n.IsDir() {
  85. return "", etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.CurrentIndex)
  86. }
  87. return n.Value, nil
  88. }
  89. // Write function set the value of the node to the given value.
  90. // If the receiver node is a directory, a "Not A File" error will be returned.
  91. func (n *node) Write(value string, index uint64) *etcdErr.Error {
  92. if n.IsDir() {
  93. return etcdErr.NewError(etcdErr.EcodeNotFile, "", n.store.CurrentIndex)
  94. }
  95. n.Value = value
  96. n.ModifiedIndex = index
  97. return nil
  98. }
  99. func (n *node) ExpirationAndTTL() (*time.Time, int64) {
  100. if !n.IsPermanent() {
  101. /* compute ttl as:
  102. ceiling( (expireTime - timeNow) / nanosecondsPerSecond )
  103. which ranges from 1..n
  104. rather than as:
  105. ( (expireTime - timeNow) / nanosecondsPerSecond ) + 1
  106. which ranges 1..n+1
  107. */
  108. ttlN := n.ExpireTime.Sub(time.Now())
  109. ttl := ttlN / time.Second
  110. if (ttlN % time.Second) > 0 {
  111. ttl++
  112. }
  113. return &n.ExpireTime, int64(ttl)
  114. }
  115. return nil, 0
  116. }
  117. // List function return a slice of nodes under the receiver node.
  118. // If the receiver node is not a directory, a "Not A Directory" error will be returned.
  119. func (n *node) List() ([]*node, *etcdErr.Error) {
  120. if !n.IsDir() {
  121. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.CurrentIndex)
  122. }
  123. nodes := make([]*node, len(n.Children))
  124. i := 0
  125. for _, node := range n.Children {
  126. nodes[i] = node
  127. i++
  128. }
  129. return nodes, nil
  130. }
  131. // GetChild function returns the child node under the directory node.
  132. // On success, it returns the file node
  133. func (n *node) GetChild(name string) (*node, *etcdErr.Error) {
  134. if !n.IsDir() {
  135. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, n.Path, n.store.CurrentIndex)
  136. }
  137. child, ok := n.Children[name]
  138. if ok {
  139. return child, nil
  140. }
  141. return nil, nil
  142. }
  143. // Add function adds a node to the receiver node.
  144. // If the receiver is not a directory, a "Not A Directory" error will be returned.
  145. // If there is a existing node with the same name under the directory, a "Already Exist"
  146. // error will be returned
  147. func (n *node) Add(child *node) *etcdErr.Error {
  148. if !n.IsDir() {
  149. return etcdErr.NewError(etcdErr.EcodeNotDir, "", n.store.CurrentIndex)
  150. }
  151. _, name := path.Split(child.Path)
  152. _, ok := n.Children[name]
  153. if ok {
  154. return etcdErr.NewError(etcdErr.EcodeNodeExist, "", n.store.CurrentIndex)
  155. }
  156. n.Children[name] = child
  157. return nil
  158. }
  159. // Remove function remove the node.
  160. func (n *node) Remove(dir, recursive bool, callback func(path string)) *etcdErr.Error {
  161. if n.IsDir() {
  162. if !dir {
  163. // cannot delete a directory without recursive set to true
  164. return etcdErr.NewError(etcdErr.EcodeNotFile, n.Path, n.store.CurrentIndex)
  165. }
  166. if len(n.Children) != 0 && !recursive {
  167. // cannot delete a directory if it is not empty and the operation
  168. // is not recursive
  169. return etcdErr.NewError(etcdErr.EcodeDirNotEmpty, n.Path, n.store.CurrentIndex)
  170. }
  171. }
  172. if !n.IsDir() { // key-value pair
  173. _, name := path.Split(n.Path)
  174. // find its parent and remove the node from the map
  175. if n.Parent != nil && n.Parent.Children[name] == n {
  176. delete(n.Parent.Children, name)
  177. }
  178. if callback != nil {
  179. callback(n.Path)
  180. }
  181. if !n.IsPermanent() {
  182. n.store.ttlKeyHeap.remove(n)
  183. }
  184. return nil
  185. }
  186. for _, child := range n.Children { // delete all children
  187. child.Remove(true, true, callback)
  188. }
  189. // delete self
  190. _, name := path.Split(n.Path)
  191. if n.Parent != nil && n.Parent.Children[name] == n {
  192. delete(n.Parent.Children, name)
  193. if callback != nil {
  194. callback(n.Path)
  195. }
  196. if !n.IsPermanent() {
  197. n.store.ttlKeyHeap.remove(n)
  198. }
  199. }
  200. return nil
  201. }
  202. func (n *node) Repr(recursive, sorted bool) *NodeExtern {
  203. if n.IsDir() {
  204. node := &NodeExtern{
  205. Key: n.Path,
  206. Dir: true,
  207. ModifiedIndex: n.ModifiedIndex,
  208. CreatedIndex: n.CreatedIndex,
  209. }
  210. node.Expiration, node.TTL = n.ExpirationAndTTL()
  211. if !recursive {
  212. return node
  213. }
  214. children, _ := n.List()
  215. node.Nodes = make(NodeExterns, len(children))
  216. // we do not use the index in the children slice directly
  217. // we need to skip the hidden one
  218. i := 0
  219. for _, child := range children {
  220. if child.IsHidden() { // get will not list hidden node
  221. continue
  222. }
  223. node.Nodes[i] = child.Repr(recursive, sorted)
  224. i++
  225. }
  226. // eliminate hidden nodes
  227. node.Nodes = node.Nodes[:i]
  228. if sorted {
  229. sort.Sort(node.Nodes)
  230. }
  231. return node
  232. }
  233. // since n.Value could be changed later, so we need to copy the value out
  234. value := n.Value
  235. node := &NodeExtern{
  236. Key: n.Path,
  237. Value: &value,
  238. ModifiedIndex: n.ModifiedIndex,
  239. CreatedIndex: n.CreatedIndex,
  240. }
  241. node.Expiration, node.TTL = n.ExpirationAndTTL()
  242. return node
  243. }
  244. func (n *node) UpdateTTL(expireTime time.Time) {
  245. if !n.IsPermanent() {
  246. if expireTime.IsZero() {
  247. // from ttl to permanent
  248. n.ExpireTime = expireTime
  249. // remove from ttl heap
  250. n.store.ttlKeyHeap.remove(n)
  251. } else {
  252. // update ttl
  253. n.ExpireTime = expireTime
  254. // update ttl heap
  255. n.store.ttlKeyHeap.update(n)
  256. }
  257. } else {
  258. if !expireTime.IsZero() {
  259. // from permanent to ttl
  260. n.ExpireTime = expireTime
  261. // push into ttl heap
  262. n.store.ttlKeyHeap.push(n)
  263. }
  264. }
  265. }
  266. // Compare function compares node index and value with provided ones.
  267. // second result value explains result and equals to one of Compare.. constants
  268. func (n *node) Compare(prevValue string, prevIndex uint64) (ok bool, which int) {
  269. indexMatch := (prevIndex == 0 || n.ModifiedIndex == prevIndex)
  270. valueMatch := (prevValue == "" || n.Value == prevValue)
  271. ok = valueMatch && indexMatch
  272. switch {
  273. case valueMatch && indexMatch:
  274. which = CompareMatch
  275. case indexMatch && !valueMatch:
  276. which = CompareValueNotMatch
  277. case valueMatch && !indexMatch:
  278. which = CompareIndexNotMatch
  279. default:
  280. which = CompareNotMatch
  281. }
  282. return
  283. }
  284. // Clone function clone the node recursively and return the new node.
  285. // If the node is a directory, it will clone all the content under this directory.
  286. // If the node is a key-value pair, it will clone the pair.
  287. func (n *node) Clone() *node {
  288. if !n.IsDir() {
  289. return newKV(n.store, n.Path, n.Value, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  290. }
  291. clone := newDir(n.store, n.Path, n.CreatedIndex, n.Parent, n.ACL, n.ExpireTime)
  292. for key, child := range n.Children {
  293. clone.Children[key] = child.Clone()
  294. }
  295. return clone
  296. }
  297. // recoverAndclean function help to do recovery.
  298. // Two things need to be done: 1. recovery structure; 2. delete expired nodes
  299. // If the node is a directory, it will help recover children's parent pointer and recursively
  300. // call this function on its children.
  301. // We check the expire last since we need to recover the whole structure first and add all the
  302. // notifications into the event history.
  303. func (n *node) recoverAndclean() {
  304. if n.IsDir() {
  305. for _, child := range n.Children {
  306. child.Parent = n
  307. child.store = n.store
  308. child.recoverAndclean()
  309. }
  310. }
  311. if !n.ExpireTime.IsZero() {
  312. n.store.ttlKeyHeap.push(n)
  313. }
  314. }