store.go 17 KB

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