store.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. err = n.Remove(false, false, callback)
  285. if err != nil {
  286. return nil, err
  287. }
  288. s.WatcherHub.notify(e)
  289. s.Stats.Inc(CompareAndDeleteSuccess)
  290. return e, nil
  291. }
  292. func (s *store) Watch(key string, recursive, stream bool, sinceIndex uint64) (Watcher, error) {
  293. s.worldLock.RLock()
  294. defer s.worldLock.RUnlock()
  295. key = path.Clean(path.Join("/", key))
  296. if sinceIndex == 0 {
  297. sinceIndex = s.CurrentIndex + 1
  298. }
  299. // WatchHub does not know about the current index, so we need to pass it in
  300. w, err := s.WatcherHub.watch(key, recursive, stream, sinceIndex, s.CurrentIndex)
  301. if err != nil {
  302. return nil, err
  303. }
  304. return w, nil
  305. }
  306. // walk walks all the nodePath and apply the walkFunc on each directory
  307. func (s *store) walk(nodePath string, walkFunc func(prev *node, component string) (*node, *etcdErr.Error)) (*node, *etcdErr.Error) {
  308. components := strings.Split(nodePath, "/")
  309. curr := s.Root
  310. var err *etcdErr.Error
  311. for i := 1; i < len(components); i++ {
  312. if len(components[i]) == 0 { // ignore empty string
  313. return curr, nil
  314. }
  315. curr, err = walkFunc(curr, components[i])
  316. if err != nil {
  317. return nil, err
  318. }
  319. }
  320. return curr, nil
  321. }
  322. // Update updates the value/ttl of the node.
  323. // If the node is a file, the value and the ttl can be updated.
  324. // If the node is a directory, only the ttl can be updated.
  325. func (s *store) Update(nodePath string, newValue string, expireTime time.Time) (*Event, error) {
  326. s.worldLock.Lock()
  327. defer s.worldLock.Unlock()
  328. nodePath = path.Clean(path.Join("/", nodePath))
  329. // we do not allow the user to change "/"
  330. if s.readonlySet.Contains(nodePath) {
  331. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", s.CurrentIndex)
  332. }
  333. currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
  334. n, err := s.internalGet(nodePath)
  335. if err != nil { // if the node does not exist, return error
  336. s.Stats.Inc(UpdateFail)
  337. return nil, err
  338. }
  339. e := newEvent(Update, nodePath, nextIndex, n.CreatedIndex)
  340. e.EtcdIndex = nextIndex
  341. e.PrevNode = n.Repr(false, false, s.clock)
  342. eNode := e.Node
  343. if n.IsDir() && len(newValue) != 0 {
  344. // if the node is a directory, we cannot update value to non-empty
  345. s.Stats.Inc(UpdateFail)
  346. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, currIndex)
  347. }
  348. n.Write(newValue, nextIndex)
  349. if n.IsDir() {
  350. eNode.Dir = true
  351. } else {
  352. // copy the value for safety
  353. newValueCopy := newValue
  354. eNode.Value = &newValueCopy
  355. }
  356. // update ttl
  357. n.UpdateTTL(expireTime)
  358. eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
  359. s.WatcherHub.notify(e)
  360. s.Stats.Inc(UpdateSuccess)
  361. s.CurrentIndex = nextIndex
  362. return e, nil
  363. }
  364. func (s *store) internalCreate(nodePath string, dir bool, value string, unique, replace bool,
  365. expireTime time.Time, action string) (*Event, error) {
  366. currIndex, nextIndex := s.CurrentIndex, s.CurrentIndex+1
  367. if unique { // append unique item under the node path
  368. nodePath += "/" + strconv.FormatUint(nextIndex, 10)
  369. }
  370. nodePath = path.Clean(path.Join("/", nodePath))
  371. // we do not allow the user to change "/"
  372. if s.readonlySet.Contains(nodePath) {
  373. return nil, etcdErr.NewError(etcdErr.EcodeRootROnly, "/", currIndex)
  374. }
  375. // Assume expire times that are way in the past are
  376. // This can occur when the time is serialized to JS
  377. if expireTime.Before(minExpireTime) {
  378. expireTime = Permanent
  379. }
  380. dirName, nodeName := path.Split(nodePath)
  381. // walk through the nodePath, create dirs and get the last directory node
  382. d, err := s.walk(dirName, s.checkDir)
  383. if err != nil {
  384. s.Stats.Inc(SetFail)
  385. err.Index = currIndex
  386. return nil, err
  387. }
  388. e := newEvent(action, nodePath, nextIndex, nextIndex)
  389. eNode := e.Node
  390. n, _ := d.GetChild(nodeName)
  391. // force will try to replace a existing file
  392. if n != nil {
  393. if replace {
  394. if n.IsDir() {
  395. return nil, etcdErr.NewError(etcdErr.EcodeNotFile, nodePath, currIndex)
  396. }
  397. e.PrevNode = n.Repr(false, false, s.clock)
  398. n.Remove(false, false, nil)
  399. } else {
  400. return nil, etcdErr.NewError(etcdErr.EcodeNodeExist, nodePath, currIndex)
  401. }
  402. }
  403. if !dir { // create file
  404. // copy the value for safety
  405. valueCopy := value
  406. eNode.Value = &valueCopy
  407. n = newKV(s, nodePath, value, nextIndex, d, expireTime)
  408. } else { // create directory
  409. eNode.Dir = true
  410. n = newDir(s, nodePath, nextIndex, d, expireTime)
  411. }
  412. // we are sure d is a directory and does not have the children with name n.Name
  413. d.Add(n)
  414. // node with TTL
  415. if !n.IsPermanent() {
  416. s.ttlKeyHeap.push(n)
  417. eNode.Expiration, eNode.TTL = n.expirationAndTTL(s.clock)
  418. }
  419. s.CurrentIndex = nextIndex
  420. return e, nil
  421. }
  422. // InternalGet gets the node of the given nodePath.
  423. func (s *store) internalGet(nodePath string) (*node, *etcdErr.Error) {
  424. nodePath = path.Clean(path.Join("/", nodePath))
  425. walkFunc := func(parent *node, name string) (*node, *etcdErr.Error) {
  426. if !parent.IsDir() {
  427. err := etcdErr.NewError(etcdErr.EcodeNotDir, parent.Path, s.CurrentIndex)
  428. return nil, err
  429. }
  430. child, ok := parent.Children[name]
  431. if ok {
  432. return child, nil
  433. }
  434. return nil, etcdErr.NewError(etcdErr.EcodeKeyNotFound, path.Join(parent.Path, name), s.CurrentIndex)
  435. }
  436. f, err := s.walk(nodePath, walkFunc)
  437. if err != nil {
  438. return nil, err
  439. }
  440. return f, nil
  441. }
  442. // deleteExpiredKyes will delete all
  443. func (s *store) DeleteExpiredKeys(cutoff time.Time) {
  444. s.worldLock.Lock()
  445. defer s.worldLock.Unlock()
  446. for {
  447. node := s.ttlKeyHeap.top()
  448. if node == nil || node.ExpireTime.After(cutoff) {
  449. break
  450. }
  451. s.CurrentIndex++
  452. e := newEvent(Expire, node.Path, s.CurrentIndex, node.CreatedIndex)
  453. e.EtcdIndex = s.CurrentIndex
  454. e.PrevNode = node.Repr(false, false, s.clock)
  455. callback := func(path string) { // notify function
  456. // notify the watchers with deleted set true
  457. s.WatcherHub.notifyWatchers(e, path, true)
  458. }
  459. s.ttlKeyHeap.pop()
  460. node.Remove(true, true, callback)
  461. s.Stats.Inc(ExpireCount)
  462. s.WatcherHub.notify(e)
  463. }
  464. }
  465. // checkDir will check whether the component is a directory under parent node.
  466. // If it is a directory, this function will return the pointer to that node.
  467. // If it does not exist, this function will create a new directory and return the pointer to that node.
  468. // If it is a file, this function will return error.
  469. func (s *store) checkDir(parent *node, dirName string) (*node, *etcdErr.Error) {
  470. node, ok := parent.Children[dirName]
  471. if ok {
  472. if node.IsDir() {
  473. return node, nil
  474. }
  475. return nil, etcdErr.NewError(etcdErr.EcodeNotDir, node.Path, s.CurrentIndex)
  476. }
  477. n := newDir(s, path.Join(parent.Path, dirName), s.CurrentIndex+1, parent, Permanent)
  478. parent.Children[dirName] = n
  479. return n, nil
  480. }
  481. // Save saves the static state of the store system.
  482. // It will not be able to save the state of watchers.
  483. // It will not save the parent field of the node. Or there will
  484. // be cyclic dependencies issue for the json package.
  485. func (s *store) Save() ([]byte, error) {
  486. b, err := json.Marshal(s.Clone())
  487. if err != nil {
  488. return nil, err
  489. }
  490. return b, nil
  491. }
  492. func (s *store) SaveNoCopy() ([]byte, error) {
  493. b, err := json.Marshal(s)
  494. if err != nil {
  495. return nil, err
  496. }
  497. return b, nil
  498. }
  499. func (s *store) Clone() Store {
  500. s.worldLock.Lock()
  501. clonedStore := newStore()
  502. clonedStore.CurrentIndex = s.CurrentIndex
  503. clonedStore.Root = s.Root.Clone()
  504. clonedStore.WatcherHub = s.WatcherHub.clone()
  505. clonedStore.Stats = s.Stats.clone()
  506. clonedStore.CurrentVersion = s.CurrentVersion
  507. s.worldLock.Unlock()
  508. return clonedStore
  509. }
  510. // Recovery recovers the store system from a static state
  511. // It needs to recover the parent field of the nodes.
  512. // It needs to delete the expired nodes since the saved time and also
  513. // needs to create monitoring go routines.
  514. func (s *store) Recovery(state []byte) error {
  515. s.worldLock.Lock()
  516. defer s.worldLock.Unlock()
  517. err := json.Unmarshal(state, s)
  518. if err != nil {
  519. return err
  520. }
  521. s.ttlKeyHeap = newTtlKeyHeap()
  522. s.Root.recoverAndclean()
  523. return nil
  524. }
  525. func (s *store) JsonStats() []byte {
  526. s.Stats.Watchers = uint64(s.WatcherHub.count)
  527. return s.Stats.toJson()
  528. }