node.go 8.6 KB

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