node.go 8.2 KB

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