store.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. "encoding/json"
  17. "fmt"
  18. "path"
  19. "strconv"
  20. "strings"
  21. "sync"
  22. "time"
  23. "go.etcd.io/etcd/etcdserver/api/v2error"
  24. "go.etcd.io/etcd/pkg/types"
  25. "github.com/jonboulle/clockwork"
  26. )
  27. // The default version to set when the store is first initialized.
  28. const defaultVersion = 2
  29. var minExpireTime time.Time
  30. func init() {
  31. minExpireTime, _ = time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
  32. }
  33. type Store interface {
  34. Version() int
  35. Index() uint64
  36. Get(nodePath string, recursive, sorted bool) (*Event, error)
  37. Set(nodePath string, dir bool, value string, expireOpts TTLOptionSet) (*Event, error)
  38. Update(nodePath string, newValue string, expireOpts TTLOptionSet) (*Event, error)
  39. Create(nodePath string, dir bool, value string, unique bool,
  40. expireOpts TTLOptionSet) (*Event, error)
  41. CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
  42. value string, expireOpts TTLOptionSet) (*Event, error)
  43. Delete(nodePath string, dir, recursive bool) (*Event, error)
  44. CompareAndDelete(nodePath string, prevValue string, prevIndex uint64) (*Event, error)
  45. Watch(prefix string, recursive, stream bool, sinceIndex uint64) (Watcher, error)
  46. Save() ([]byte, error)
  47. Recovery(state []byte) error
  48. Clone() Store
  49. SaveNoCopy() ([]byte, error)
  50. JsonStats() []byte
  51. DeleteExpiredKeys(cutoff time.Time)
  52. HasTTLKeys() bool
  53. }
  54. type TTLOptionSet struct {
  55. ExpireTime time.Time
  56. Refresh bool
  57. }
  58. type store struct {
  59. Root *node
  60. WatcherHub *watcherHub
  61. CurrentIndex uint64
  62. Stats *Stats
  63. CurrentVersion int
  64. ttlKeyHeap *ttlKeyHeap // need to recovery manually
  65. worldLock sync.RWMutex // stop the world lock
  66. clock clockwork.Clock
  67. readonlySet types.Set
  68. }
  69. // New creates a store where the given namespaces will be created as initial directories.
  70. func New(namespaces ...string) Store {
  71. s := newStore(namespaces...)
  72. s.clock = clockwork.NewRealClock()
  73. return s
  74. }
  75. func newStore(namespaces ...string) *store {
  76. s := new(store)
  77. s.CurrentVersion = defaultVersion
  78. s.Root = newDir(s, "/", s.CurrentIndex, nil, Permanent)
  79. for _, namespace := range namespaces {
  80. s.Root.Add(newDir(s, namespace, s.CurrentIndex, s.Root, Permanent))
  81. }
  82. s.Stats = newStats()
  83. s.WatcherHub = newWatchHub(1000)
  84. s.ttlKeyHeap = newTtlKeyHeap()
  85. s.readonlySet = types.NewUnsafeSet(append(namespaces, "/")...)
  86. return s
  87. }
  88. // Version retrieves current version of the store.
  89. func (s *store) Version() int {
  90. return s.CurrentVersion
  91. }
  92. // Index retrieves the current index of the store.
  93. func (s *store) Index() uint64 {
  94. s.worldLock.RLock()
  95. defer s.worldLock.RUnlock()
  96. return s.CurrentIndex
  97. }
  98. // Get returns a get event.
  99. // If recursive is true, it will return all the content under the node path.
  100. // If sorted is true, it will sort the content by keys.
  101. func (s *store) Get(nodePath string, recursive, sorted bool) (*Event, error) {
  102. var err *v2error.Error
  103. s.worldLock.RLock()
  104. defer s.worldLock.RUnlock()
  105. defer func() {
  106. if err == nil {
  107. s.Stats.Inc(GetSuccess)
  108. if recursive {
  109. reportReadSuccess(GetRecursive)
  110. } else {
  111. reportReadSuccess(Get)
  112. }
  113. return
  114. }
  115. s.Stats.Inc(GetFail)
  116. if recursive {
  117. reportReadFailure(GetRecursive)
  118. } else {
  119. reportReadFailure(Get)
  120. }
  121. }()
  122. n, err := s.internalGet(nodePath)
  123. if err != nil {
  124. return nil, err
  125. }
  126. e := newEvent(Get, nodePath, n.ModifiedIndex, n.CreatedIndex)
  127. e.EtcdIndex = s.CurrentIndex
  128. e.Node.loadInternalNode(n, recursive, sorted, s.clock)
  129. return e, nil
  130. }
  131. // Create creates the node at nodePath. Create will help to create intermediate directories with no ttl.
  132. // If the node has already existed, create will fail.
  133. // If any node on the path is a file, create will fail.
  134. func (s *store) Create(nodePath string, dir bool, value string, unique bool, expireOpts TTLOptionSet) (*Event, error) {
  135. var err *v2error.Error
  136. s.worldLock.Lock()
  137. defer s.worldLock.Unlock()
  138. defer func() {
  139. if err == nil {
  140. s.Stats.Inc(CreateSuccess)
  141. reportWriteSuccess(Create)
  142. return
  143. }
  144. s.Stats.Inc(CreateFail)
  145. reportWriteFailure(Create)
  146. }()
  147. e, err := s.internalCreate(nodePath, dir, value, unique, false, expireOpts.ExpireTime, Create)
  148. if err != nil {
  149. return nil, err
  150. }
  151. e.EtcdIndex = s.CurrentIndex
  152. s.WatcherHub.notify(e)
  153. return e, nil
  154. }
  155. // Set creates or replace the node at nodePath.
  156. func (s *store) Set(nodePath string, dir bool, value string, expireOpts TTLOptionSet) (*Event, error) {
  157. var err *v2error.Error
  158. s.worldLock.Lock()
  159. defer s.worldLock.Unlock()
  160. defer func() {
  161. if err == nil {
  162. s.Stats.Inc(SetSuccess)
  163. reportWriteSuccess(Set)
  164. return
  165. }
  166. s.Stats.Inc(SetFail)
  167. reportWriteFailure(Set)
  168. }()
  169. // Get prevNode value
  170. n, getErr := s.internalGet(nodePath)
  171. if getErr != nil && getErr.ErrorCode != v2error.EcodeKeyNotFound {
  172. err = getErr
  173. return nil, err
  174. }
  175. if expireOpts.Refresh {
  176. if getErr != nil {
  177. err = getErr
  178. return nil, err
  179. }
  180. value = n.Value
  181. }
  182. // Set new value
  183. e, err := s.internalCreate(nodePath, dir, value, false, true, expireOpts.ExpireTime, Set)
  184. if err != nil {
  185. return nil, err
  186. }
  187. e.EtcdIndex = s.CurrentIndex
  188. // Put prevNode into event
  189. if getErr == nil {
  190. prev := newEvent(Get, nodePath, n.ModifiedIndex, n.CreatedIndex)
  191. prev.Node.loadInternalNode(n, false, false, s.clock)
  192. e.PrevNode = prev.Node
  193. }
  194. if !expireOpts.Refresh {
  195. s.WatcherHub.notify(e)
  196. } else {
  197. e.SetRefresh()
  198. s.WatcherHub.add(e)
  199. }
  200. return e, nil
  201. }
  202. // returns user-readable cause of failed comparison
  203. func getCompareFailCause(n *node, which int, prevValue string, prevIndex uint64) string {
  204. switch which {
  205. case CompareIndexNotMatch:
  206. return fmt.Sprintf("[%v != %v]", prevIndex, n.ModifiedIndex)
  207. case CompareValueNotMatch:
  208. return fmt.Sprintf("[%v != %v]", prevValue, n.Value)
  209. default:
  210. return fmt.Sprintf("[%v != %v] [%v != %v]", prevValue, n.Value, prevIndex, n.ModifiedIndex)
  211. }
  212. }
  213. func (s *store) CompareAndSwap(nodePath string, prevValue string, prevIndex uint64,
  214. value string, expireOpts TTLOptionSet) (*Event, error) {
  215. var err *v2error.Error
  216. s.worldLock.Lock()
  217. defer s.worldLock.Unlock()
  218. defer func() {
  219. if err == nil {
  220. s.Stats.Inc(CompareAndSwapSuccess)
  221. reportWriteSuccess(CompareAndSwap)
  222. return
  223. }
  224. s.Stats.Inc(CompareAndSwapFail)
  225. reportWriteFailure(CompareAndSwap)
  226. }()
  227. nodePath = path.Clean(path.Join("/", nodePath))
  228. // we do not allow the user to change "/"
  229. if s.readonlySet.Contains(nodePath) {
  230. return nil, v2error.NewError(v2error.EcodeRootROnly, "/", s.CurrentIndex)
  231. }
  232. n, err := s.internalGet(nodePath)
  233. if err != nil {
  234. return nil, err
  235. }
  236. if n.IsDir() { // can only compare and swap file
  237. err = v2error.NewError(v2error.EcodeNotFile, nodePath, s.CurrentIndex)
  238. return nil, err
  239. }
  240. // If both of the prevValue and prevIndex are given, we will test both of them.
  241. // Command will be executed, only if both of the tests are successful.
  242. if ok, which := n.Compare(prevValue, prevIndex); !ok {
  243. cause := getCompareFailCause(n, which, prevValue, prevIndex)
  244. err = v2error.NewError(v2error.EcodeTestFailed, cause, s.CurrentIndex)
  245. return nil, err
  246. }
  247. if expireOpts.Refresh {
  248. value = n.Value
  249. }
  250. // update etcd index
  251. s.CurrentIndex++
  252. e := newEvent(CompareAndSwap, nodePath, s.CurrentIndex, n.CreatedIndex)
  253. e.EtcdIndex = s.CurrentIndex
  254. e.PrevNode = n.Repr(false, false, s.clock)
  255. eNode := e.Node
  256. // if test succeed, write the value
  257. n.Write(value, s.CurrentIndex)
  258. n.UpdateTTL(expireOpts.ExpireTime)
  259. // copy the value for safety
  260. valueCopy := value
  261. eNode.Value = &valueCopy
  262. eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
  263. if !expireOpts.Refresh {
  264. s.WatcherHub.notify(e)
  265. } else {
  266. e.SetRefresh()
  267. s.WatcherHub.add(e)
  268. }
  269. return e, nil
  270. }
  271. // Delete deletes the node at the given path.
  272. // If the node is a directory, recursive must be true to delete it.
  273. func (s *store) Delete(nodePath string, dir, recursive bool) (*Event, error) {
  274. var err *v2error.Error
  275. s.worldLock.Lock()
  276. defer s.worldLock.Unlock()
  277. defer func() {
  278. if err == nil {
  279. s.Stats.Inc(DeleteSuccess)
  280. reportWriteSuccess(Delete)
  281. return
  282. }
  283. s.Stats.Inc(DeleteFail)
  284. reportWriteFailure(Delete)
  285. }()
  286. nodePath = path.Clean(path.Join("/", nodePath))
  287. // we do not allow the user to change "/"
  288. if s.readonlySet.Contains(nodePath) {
  289. return nil, v2error.NewError(v2error.EcodeRootROnly, "/", s.CurrentIndex)
  290. }
  291. // recursive implies dir
  292. if recursive {
  293. dir = true
  294. }
  295. n, err := s.internalGet(nodePath)
  296. if err != nil { // if the node does not exist, return error
  297. return nil, err
  298. }
  299. nextIndex := s.CurrentIndex + 1
  300. e := newEvent(Delete, nodePath, nextIndex, n.CreatedIndex)
  301. e.EtcdIndex = nextIndex
  302. e.PrevNode = n.Repr(false, false, s.clock)
  303. eNode := e.Node
  304. if n.IsDir() {
  305. eNode.Dir = true
  306. }
  307. callback := func(path string) { // notify function
  308. // notify the watchers with deleted set true
  309. s.WatcherHub.notifyWatchers(e, path, true)
  310. }
  311. err = n.Remove(dir, recursive, callback)
  312. if err != nil {
  313. return nil, err
  314. }
  315. // update etcd index
  316. s.CurrentIndex++
  317. s.WatcherHub.notify(e)
  318. return e, nil
  319. }
  320. func (s *store) CompareAndDelete(nodePath string, prevValue string, prevIndex uint64) (*Event, error) {
  321. var err *v2error.Error
  322. s.worldLock.Lock()
  323. defer s.worldLock.Unlock()
  324. defer func() {
  325. if err == nil {
  326. s.Stats.Inc(CompareAndDeleteSuccess)
  327. reportWriteSuccess(CompareAndDelete)
  328. return
  329. }
  330. s.Stats.Inc(CompareAndDeleteFail)
  331. reportWriteFailure(CompareAndDelete)
  332. }()
  333. nodePath = path.Clean(path.Join("/", nodePath))
  334. n, err := s.internalGet(nodePath)
  335. if err != nil { // if the node does not exist, return error
  336. return nil, err
  337. }
  338. if n.IsDir() { // can only compare and delete file
  339. return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, s.CurrentIndex)
  340. }
  341. // If both of the prevValue and prevIndex are given, we will test both of them.
  342. // Command will be executed, only if both of the tests are successful.
  343. if ok, which := n.Compare(prevValue, prevIndex); !ok {
  344. cause := getCompareFailCause(n, which, prevValue, prevIndex)
  345. return nil, v2error.NewError(v2error.EcodeTestFailed, cause, s.CurrentIndex)
  346. }
  347. // update etcd index
  348. s.CurrentIndex++
  349. e := newEvent(CompareAndDelete, nodePath, s.CurrentIndex, n.CreatedIndex)
  350. e.EtcdIndex = s.CurrentIndex
  351. e.PrevNode = n.Repr(false, false, s.clock)
  352. callback := func(path string) { // notify function
  353. // notify the watchers with deleted set true
  354. s.WatcherHub.notifyWatchers(e, path, true)
  355. }
  356. err = n.Remove(false, false, callback)
  357. if err != nil {
  358. return nil, err
  359. }
  360. s.WatcherHub.notify(e)
  361. return e, nil
  362. }
  363. func (s *store) Watch(key string, recursive, stream bool, sinceIndex uint64) (Watcher, error) {
  364. s.worldLock.RLock()
  365. defer s.worldLock.RUnlock()
  366. key = path.Clean(path.Join("/", key))
  367. if sinceIndex == 0 {
  368. sinceIndex = s.CurrentIndex + 1
  369. }
  370. // WatcherHub does not know about the current index, so we need to pass it in
  371. w, err := s.WatcherHub.watch(key, recursive, stream, sinceIndex, s.CurrentIndex)
  372. if err != nil {
  373. return nil, err
  374. }
  375. return w, nil
  376. }
  377. // walk walks all the nodePath and apply the walkFunc on each directory
  378. func (s *store) walk(nodePath string, walkFunc func(prev *node, component string) (*node, *v2error.Error)) (*node, *v2error.Error) {
  379. components := strings.Split(nodePath, "/")
  380. curr := s.Root
  381. var err *v2error.Error
  382. for i := 1; i < len(components); i++ {
  383. if len(components[i]) == 0 { // ignore empty string
  384. return curr, nil
  385. }
  386. curr, err = walkFunc(curr, components[i])
  387. if err != nil {
  388. return nil, err
  389. }
  390. }
  391. return curr, nil
  392. }
  393. // Update updates the value/ttl of the node.
  394. // If the node is a file, the value and the ttl can be updated.
  395. // If the node is a directory, only the ttl can be updated.
  396. func (s *store) Update(nodePath string, newValue string, expireOpts TTLOptionSet) (*Event, error) {
  397. var err *v2error.Error
  398. s.worldLock.Lock()
  399. defer s.worldLock.Unlock()
  400. defer func() {
  401. if err == nil {
  402. s.Stats.Inc(UpdateSuccess)
  403. reportWriteSuccess(Update)
  404. return
  405. }
  406. s.Stats.Inc(UpdateFail)
  407. reportWriteFailure(Update)
  408. }()
  409. nodePath = path.Clean(path.Join("/", nodePath))
  410. // we do not allow the user to change "/"
  411. if s.readonlySet.Contains(nodePath) {
  412. return nil, v2error.NewError(v2error.EcodeRootROnly, "/", s.CurrentIndex)
  413. }
  414. currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
  415. n, err := s.internalGet(nodePath)
  416. if err != nil { // if the node does not exist, return error
  417. return nil, err
  418. }
  419. if n.IsDir() && len(newValue) != 0 {
  420. // if the node is a directory, we cannot update value to non-empty
  421. return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)
  422. }
  423. if expireOpts.Refresh {
  424. newValue = n.Value
  425. }
  426. e := newEvent(Update, nodePath, nextIndex, n.CreatedIndex)
  427. e.EtcdIndex = nextIndex
  428. e.PrevNode = n.Repr(false, false, s.clock)
  429. eNode := e.Node
  430. n.Write(newValue, nextIndex)
  431. if n.IsDir() {
  432. eNode.Dir = true
  433. } else {
  434. // copy the value for safety
  435. newValueCopy := newValue
  436. eNode.Value = &newValueCopy
  437. }
  438. // update ttl
  439. n.UpdateTTL(expireOpts.ExpireTime)
  440. eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
  441. if !expireOpts.Refresh {
  442. s.WatcherHub.notify(e)
  443. } else {
  444. e.SetRefresh()
  445. s.WatcherHub.add(e)
  446. }
  447. s.CurrentIndex = nextIndex
  448. return e, nil
  449. }
  450. func (s *store) internalCreate(nodePath string, dir bool, value string, unique, replace bool,
  451. expireTime time.Time, action string) (*Event, *v2error.Error) {
  452. currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
  453. if unique { // append unique item under the node path
  454. nodePath += "/" + fmt.Sprintf("%020s", strconv.FormatUint(nextIndex, 10))
  455. }
  456. nodePath = path.Clean(path.Join("/", nodePath))
  457. // we do not allow the user to change "/"
  458. if s.readonlySet.Contains(nodePath) {
  459. return nil, v2error.NewError(v2error.EcodeRootROnly, "/", currIndex)
  460. }
  461. // Assume expire times that are way in the past are
  462. // This can occur when the time is serialized to JS
  463. if expireTime.Before(minExpireTime) {
  464. expireTime = Permanent
  465. }
  466. dirName, nodeName := path.Split(nodePath)
  467. // walk through the nodePath, create dirs and get the last directory node
  468. d, err := s.walk(dirName, s.checkDir)
  469. if err != nil {
  470. s.Stats.Inc(SetFail)
  471. reportWriteFailure(action)
  472. err.Index = currIndex
  473. return nil, err
  474. }
  475. e := newEvent(action, nodePath, nextIndex, nextIndex)
  476. eNode := e.Node
  477. n, _ := d.GetChild(nodeName)
  478. // force will try to replace an existing file
  479. if n != nil {
  480. if replace {
  481. if n.IsDir() {
  482. return nil, v2error.NewError(v2error.EcodeNotFile, nodePath, currIndex)
  483. }
  484. e.PrevNode = n.Repr(false, false, s.clock)
  485. n.Remove(false, false, nil)
  486. } else {
  487. return nil, v2error.NewError(v2error.EcodeNodeExist, nodePath, currIndex)
  488. }
  489. }
  490. if !dir { // create file
  491. // copy the value for safety
  492. valueCopy := value
  493. eNode.Value = &valueCopy
  494. n = newKV(s, nodePath, value, nextIndex, d, expireTime)
  495. } else { // create directory
  496. eNode.Dir = true
  497. n = newDir(s, nodePath, nextIndex, d, expireTime)
  498. }
  499. // we are sure d is a directory and does not have the children with name n.Name
  500. d.Add(n)
  501. // node with TTL
  502. if !n.IsPermanent() {
  503. s.ttlKeyHeap.push(n)
  504. eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
  505. }
  506. s.CurrentIndex = nextIndex
  507. return e, nil
  508. }
  509. // InternalGet gets the node of the given nodePath.
  510. func (s *store) internalGet(nodePath string) (*node, *v2error.Error) {
  511. nodePath = path.Clean(path.Join("/", nodePath))
  512. walkFunc := func(parent *node, name string) (*node, *v2error.Error) {
  513. if !parent.IsDir() {
  514. err := v2error.NewError(v2error.EcodeNotDir, parent.Path, s.CurrentIndex)
  515. return nil, err
  516. }
  517. child, ok := parent.Children[name]
  518. if ok {
  519. return child, nil
  520. }
  521. return nil, v2error.NewError(v2error.EcodeKeyNotFound, path.Join(parent.Path, name), s.CurrentIndex)
  522. }
  523. f, err := s.walk(nodePath, walkFunc)
  524. if err != nil {
  525. return nil, err
  526. }
  527. return f, nil
  528. }
  529. // DeleteExpiredKeys will delete all expired keys
  530. func (s *store) DeleteExpiredKeys(cutoff time.Time) {
  531. s.worldLock.Lock()
  532. defer s.worldLock.Unlock()
  533. for {
  534. node := s.ttlKeyHeap.top()
  535. if node == nil || node.ExpireTime.After(cutoff) {
  536. break
  537. }
  538. s.CurrentIndex++
  539. e := newEvent(Expire, node.Path, s.CurrentIndex, node.CreatedIndex)
  540. e.EtcdIndex = s.CurrentIndex
  541. e.PrevNode = node.Repr(false, false, s.clock)
  542. if node.IsDir() {
  543. e.Node.Dir = true
  544. }
  545. callback := func(path string) { // notify function
  546. // notify the watchers with deleted set true
  547. s.WatcherHub.notifyWatchers(e, path, true)
  548. }
  549. s.ttlKeyHeap.pop()
  550. node.Remove(true, true, callback)
  551. reportExpiredKey()
  552. s.Stats.Inc(ExpireCount)
  553. s.WatcherHub.notify(e)
  554. }
  555. }
  556. // checkDir will check whether the component is a directory under parent node.
  557. // If it is a directory, this function will return the pointer to that node.
  558. // If it does not exist, this function will create a new directory and return the pointer to that node.
  559. // If it is a file, this function will return error.
  560. func (s *store) checkDir(parent *node, dirName string) (*node, *v2error.Error) {
  561. node, ok := parent.Children[dirName]
  562. if ok {
  563. if node.IsDir() {
  564. return node, nil
  565. }
  566. return nil, v2error.NewError(v2error.EcodeNotDir, node.Path, s.CurrentIndex)
  567. }
  568. n := newDir(s, path.Join(parent.Path, dirName), s.CurrentIndex+1, parent, Permanent)
  569. parent.Children[dirName] = n
  570. return n, nil
  571. }
  572. // Save saves the static state of the store system.
  573. // It will not be able to save the state of watchers.
  574. // It will not save the parent field of the node. Or there will
  575. // be cyclic dependencies issue for the json package.
  576. func (s *store) Save() ([]byte, error) {
  577. b, err := json.Marshal(s.Clone())
  578. if err != nil {
  579. return nil, err
  580. }
  581. return b, nil
  582. }
  583. func (s *store) SaveNoCopy() ([]byte, error) {
  584. b, err := json.Marshal(s)
  585. if err != nil {
  586. return nil, err
  587. }
  588. return b, nil
  589. }
  590. func (s *store) Clone() Store {
  591. s.worldLock.Lock()
  592. clonedStore := newStore()
  593. clonedStore.CurrentIndex = s.CurrentIndex
  594. clonedStore.Root = s.Root.Clone()
  595. clonedStore.WatcherHub = s.WatcherHub.clone()
  596. clonedStore.Stats = s.Stats.clone()
  597. clonedStore.CurrentVersion = s.CurrentVersion
  598. s.worldLock.Unlock()
  599. return clonedStore
  600. }
  601. // Recovery recovers the store system from a static state
  602. // It needs to recover the parent field of the nodes.
  603. // It needs to delete the expired nodes since the saved time and also
  604. // needs to create monitoring go routines.
  605. func (s *store) Recovery(state []byte) error {
  606. s.worldLock.Lock()
  607. defer s.worldLock.Unlock()
  608. err := json.Unmarshal(state, s)
  609. if err != nil {
  610. return err
  611. }
  612. s.ttlKeyHeap = newTtlKeyHeap()
  613. s.Root.recoverAndclean()
  614. return nil
  615. }
  616. func (s *store) JsonStats() []byte {
  617. s.Stats.Watchers = uint64(s.WatcherHub.count)
  618. return s.Stats.toJson()
  619. }
  620. func (s *store) HasTTLKeys() bool {
  621. s.worldLock.RLock()
  622. defer s.worldLock.RUnlock()
  623. return s.ttlKeyHeap.Len() != 0
  624. }