node.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright 2015 The etcd Authors
  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 v2store
  15. import (
  16. "path"
  17. "sort"
  18. "time"
  19. "go.etcd.io/etcd/etcdserver/api/v2error"
  20. "github.com/jonboulle/clockwork"
  21. )
  22. // explanations of Compare function result
  23. const (
  24. CompareMatch = iota
  25. CompareIndexNotMatch
  26. CompareValueNotMatch
  27. CompareNotMatch
  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, *v2error.Error) {
  93. if n.IsDir() {
  94. return "", v2error.NewError(v2error.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) *v2error.Error {
  101. if n.IsDir() {
  102. return v2error.NewError(v2error.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, *v2error.Error) {
  130. if !n.IsDir() {
  131. return nil, v2error.NewError(v2error.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, *v2error.Error) {
  144. if !n.IsDir() {
  145. return nil, v2error.NewError(v2error.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 an existing node with the same name under the directory, a "Already Exist"
  156. // error will be returned
  157. func (n *node) Add(child *node) *v2error.Error {
  158. if !n.IsDir() {
  159. return v2error.NewError(v2error.EcodeNotDir, "", n.store.CurrentIndex)
  160. }
  161. _, name := path.Split(child.Path)
  162. if _, ok := n.Children[name]; ok {
  163. return v2error.NewError(v2error.EcodeNodeExist, "", n.store.CurrentIndex)
  164. }
  165. n.Children[name] = child
  166. return nil
  167. }
  168. // Remove function remove the node.
  169. func (n *node) Remove(dir, recursive bool, callback func(path string)) *v2error.Error {
  170. if !n.IsDir() { // key-value pair
  171. _, name := path.Split(n.Path)
  172. // find its parent and remove the node from the map
  173. if n.Parent != nil && n.Parent.Children[name] == n {
  174. delete(n.Parent.Children, name)
  175. }
  176. if callback != nil {
  177. callback(n.Path)
  178. }
  179. if !n.IsPermanent() {
  180. n.store.ttlKeyHeap.remove(n)
  181. }
  182. return nil
  183. }
  184. if !dir {
  185. // cannot delete a directory without dir set to true
  186. return v2error.NewError(v2error.EcodeNotFile, n.Path, n.store.CurrentIndex)
  187. }
  188. if len(n.Children) != 0 && !recursive {
  189. // cannot delete a directory if it is not empty and the operation
  190. // is not recursive
  191. return v2error.NewError(v2error.EcodeDirNotEmpty, n.Path, n.store.CurrentIndex)
  192. }
  193. for _, child := range n.Children { // delete all children
  194. child.Remove(true, true, callback)
  195. }
  196. // delete self
  197. _, name := path.Split(n.Path)
  198. if n.Parent != nil && n.Parent.Children[name] == n {
  199. delete(n.Parent.Children, name)
  200. if callback != nil {
  201. callback(n.Path)
  202. }
  203. if !n.IsPermanent() {
  204. n.store.ttlKeyHeap.remove(n)
  205. }
  206. }
  207. return nil
  208. }
  209. func (n *node) Repr(recursive, sorted bool, clock clockwork.Clock) *NodeExtern {
  210. if n.IsDir() {
  211. node := &NodeExtern{
  212. Key: n.Path,
  213. Dir: true,
  214. ModifiedIndex: n.ModifiedIndex,
  215. CreatedIndex: n.CreatedIndex,
  216. }
  217. node.Expiration, node.TTL = n.expirationAndTTL(clock)
  218. if !recursive {
  219. return node
  220. }
  221. children, _ := n.List()
  222. node.Nodes = make(NodeExterns, len(children))
  223. // we do not use the index in the children slice directly
  224. // we need to skip the hidden one
  225. i := 0
  226. for _, child := range children {
  227. if child.IsHidden() { // get will not list hidden node
  228. continue
  229. }
  230. node.Nodes[i] = child.Repr(recursive, sorted, clock)
  231. i++
  232. }
  233. // eliminate hidden nodes
  234. node.Nodes = node.Nodes[:i]
  235. if sorted {
  236. sort.Sort(node.Nodes)
  237. }
  238. return node
  239. }
  240. // since n.Value could be changed later, so we need to copy the value out
  241. value := n.Value
  242. node := &NodeExtern{
  243. Key: n.Path,
  244. Value: &value,
  245. ModifiedIndex: n.ModifiedIndex,
  246. CreatedIndex: n.CreatedIndex,
  247. }
  248. node.Expiration, node.TTL = n.expirationAndTTL(clock)
  249. return node
  250. }
  251. func (n *node) UpdateTTL(expireTime time.Time) {
  252. if !n.IsPermanent() {
  253. if expireTime.IsZero() {
  254. // from ttl to permanent
  255. n.ExpireTime = expireTime
  256. // remove from ttl heap
  257. n.store.ttlKeyHeap.remove(n)
  258. return
  259. }
  260. // update ttl
  261. n.ExpireTime = expireTime
  262. // update ttl heap
  263. n.store.ttlKeyHeap.update(n)
  264. return
  265. }
  266. if expireTime.IsZero() {
  267. return
  268. }
  269. // from permanent to ttl
  270. n.ExpireTime = expireTime
  271. // push into ttl heap
  272. n.store.ttlKeyHeap.push(n)
  273. }
  274. // Compare function compares node index and value with provided ones.
  275. // second result value explains result and equals to one of Compare.. constants
  276. func (n *node) Compare(prevValue string, prevIndex uint64) (ok bool, which int) {
  277. indexMatch := prevIndex == 0 || n.ModifiedIndex == prevIndex
  278. valueMatch := prevValue == "" || n.Value == prevValue
  279. ok = valueMatch && indexMatch
  280. switch {
  281. case valueMatch && indexMatch:
  282. which = CompareMatch
  283. case indexMatch && !valueMatch:
  284. which = CompareValueNotMatch
  285. case valueMatch && !indexMatch:
  286. which = CompareIndexNotMatch
  287. default:
  288. which = CompareNotMatch
  289. }
  290. return ok, which
  291. }
  292. // Clone function clone the node recursively and return the new node.
  293. // If the node is a directory, it will clone all the content under this directory.
  294. // If the node is a key-value pair, it will clone the pair.
  295. func (n *node) Clone() *node {
  296. if !n.IsDir() {
  297. newkv := newKV(n.store, n.Path, n.Value, n.CreatedIndex, n.Parent, n.ExpireTime)
  298. newkv.ModifiedIndex = n.ModifiedIndex
  299. return newkv
  300. }
  301. clone := newDir(n.store, n.Path, n.CreatedIndex, n.Parent, n.ExpireTime)
  302. clone.ModifiedIndex = n.ModifiedIndex
  303. for key, child := range n.Children {
  304. clone.Children[key] = child.Clone()
  305. }
  306. return clone
  307. }
  308. // recoverAndclean function help to do recovery.
  309. // Two things need to be done: 1. recovery structure; 2. delete expired nodes
  310. //
  311. // If the node is a directory, it will help recover children's parent pointer and recursively
  312. // call this function on its children.
  313. // We check the expire last since we need to recover the whole structure first and add all the
  314. // notifications into the event history.
  315. func (n *node) recoverAndclean() {
  316. if n.IsDir() {
  317. for _, child := range n.Children {
  318. child.Parent = n
  319. child.store = n.store
  320. child.recoverAndclean()
  321. }
  322. }
  323. if !n.ExpireTime.IsZero() {
  324. n.store.ttlKeyHeap.push(n)
  325. }
  326. }