node.go 9.4 KB

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