node.go 10 KB

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