store.go 18 KB

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