store_ttl_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Copyright 2017 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 store
  15. import (
  16. "testing"
  17. "time"
  18. etcdErr "github.com/coreos/etcd/error"
  19. "github.com/coreos/etcd/pkg/testutil"
  20. "github.com/jonboulle/clockwork"
  21. )
  22. // Ensure that any TTL <= minExpireTime becomes Permanent
  23. func TestMinExpireTime(t *testing.T) {
  24. s := newStore()
  25. fc := clockwork.NewFakeClock()
  26. s.clock = fc
  27. // FakeClock starts at 0, so minExpireTime should be far in the future.. but just in case
  28. testutil.AssertTrue(t, minExpireTime.After(fc.Now()), "minExpireTime should be ahead of FakeClock!")
  29. s.Create("/foo", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(3 * time.Second)})
  30. fc.Advance(5 * time.Second)
  31. // Ensure it hasn't expired
  32. s.DeleteExpiredKeys(fc.Now())
  33. var eidx uint64 = 1
  34. e, err := s.Get("/foo", true, false)
  35. testutil.AssertNil(t, err)
  36. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  37. testutil.AssertEqual(t, e.Action, "get")
  38. testutil.AssertEqual(t, e.Node.Key, "/foo")
  39. testutil.AssertEqual(t, e.Node.TTL, int64(0))
  40. }
  41. // Ensure that the store can recursively retrieve a directory listing.
  42. // Note that hidden files should not be returned.
  43. func TestStoreGetDirectory(t *testing.T) {
  44. s := newStore()
  45. fc := newFakeClock()
  46. s.clock = fc
  47. s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
  48. s.Create("/foo/bar", false, "X", false, TTLOptionSet{ExpireTime: Permanent})
  49. s.Create("/foo/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent})
  50. s.Create("/foo/baz", true, "", false, TTLOptionSet{ExpireTime: Permanent})
  51. s.Create("/foo/baz/bat", false, "Y", false, TTLOptionSet{ExpireTime: Permanent})
  52. s.Create("/foo/baz/_hidden", false, "*", false, TTLOptionSet{ExpireTime: Permanent})
  53. s.Create("/foo/baz/ttl", false, "Y", false, TTLOptionSet{ExpireTime: fc.Now().Add(time.Second * 3)})
  54. var eidx uint64 = 7
  55. e, err := s.Get("/foo", true, false)
  56. testutil.AssertNil(t, err)
  57. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  58. testutil.AssertEqual(t, e.Action, "get")
  59. testutil.AssertEqual(t, e.Node.Key, "/foo")
  60. testutil.AssertEqual(t, len(e.Node.Nodes), 2)
  61. var bazNodes NodeExterns
  62. for _, node := range e.Node.Nodes {
  63. switch node.Key {
  64. case "/foo/bar":
  65. testutil.AssertEqual(t, *node.Value, "X")
  66. testutil.AssertEqual(t, node.Dir, false)
  67. case "/foo/baz":
  68. testutil.AssertEqual(t, node.Dir, true)
  69. testutil.AssertEqual(t, len(node.Nodes), 2)
  70. bazNodes = node.Nodes
  71. default:
  72. t.Errorf("key = %s, not matched", node.Key)
  73. }
  74. }
  75. for _, node := range bazNodes {
  76. switch node.Key {
  77. case "/foo/baz/bat":
  78. testutil.AssertEqual(t, *node.Value, "Y")
  79. testutil.AssertEqual(t, node.Dir, false)
  80. case "/foo/baz/ttl":
  81. testutil.AssertEqual(t, *node.Value, "Y")
  82. testutil.AssertEqual(t, node.Dir, false)
  83. testutil.AssertEqual(t, node.TTL, int64(3))
  84. default:
  85. t.Errorf("key = %s, not matched", node.Key)
  86. }
  87. }
  88. }
  89. // Ensure that the store can update the TTL on a value.
  90. func TestStoreUpdateValueTTL(t *testing.T) {
  91. s := newStore()
  92. fc := newFakeClock()
  93. s.clock = fc
  94. var eidx uint64 = 2
  95. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  96. _, err := s.Update("/foo", "baz", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  97. testutil.AssertNil(t, err)
  98. e, _ := s.Get("/foo", false, false)
  99. testutil.AssertEqual(t, *e.Node.Value, "baz")
  100. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  101. fc.Advance(600 * time.Millisecond)
  102. s.DeleteExpiredKeys(fc.Now())
  103. e, err = s.Get("/foo", false, false)
  104. testutil.AssertNil(t, e)
  105. testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound)
  106. }
  107. // Ensure that the store can update the TTL on a directory.
  108. func TestStoreUpdateDirTTL(t *testing.T) {
  109. s := newStore()
  110. fc := newFakeClock()
  111. s.clock = fc
  112. var eidx uint64 = 3
  113. s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
  114. s.Create("/foo/bar", false, "baz", false, TTLOptionSet{ExpireTime: Permanent})
  115. e, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  116. testutil.AssertNil(t, err)
  117. testutil.AssertEqual(t, e.Node.Dir, true)
  118. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  119. e, _ = s.Get("/foo/bar", false, false)
  120. testutil.AssertEqual(t, *e.Node.Value, "baz")
  121. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  122. fc.Advance(600 * time.Millisecond)
  123. s.DeleteExpiredKeys(fc.Now())
  124. e, err = s.Get("/foo/bar", false, false)
  125. testutil.AssertNil(t, e)
  126. testutil.AssertEqual(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound)
  127. }
  128. // Ensure that the store can watch for key expiration.
  129. func TestStoreWatchExpire(t *testing.T) {
  130. s := newStore()
  131. fc := newFakeClock()
  132. s.clock = fc
  133. var eidx uint64 = 3
  134. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(400 * time.Millisecond)})
  135. s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(450 * time.Millisecond)})
  136. s.Create("/foodir", true, "", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  137. w, _ := s.Watch("/", true, false, 0)
  138. testutil.AssertEqual(t, w.StartIndex(), eidx)
  139. c := w.EventChan()
  140. e := nbselect(c)
  141. testutil.AssertNil(t, e)
  142. fc.Advance(600 * time.Millisecond)
  143. s.DeleteExpiredKeys(fc.Now())
  144. eidx = 4
  145. e = nbselect(c)
  146. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  147. testutil.AssertEqual(t, e.Action, "expire")
  148. testutil.AssertEqual(t, e.Node.Key, "/foo")
  149. w, _ = s.Watch("/", true, false, 5)
  150. eidx = 6
  151. testutil.AssertEqual(t, w.StartIndex(), eidx)
  152. e = nbselect(w.EventChan())
  153. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  154. testutil.AssertEqual(t, e.Action, "expire")
  155. testutil.AssertEqual(t, e.Node.Key, "/foofoo")
  156. w, _ = s.Watch("/", true, false, 6)
  157. e = nbselect(w.EventChan())
  158. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  159. testutil.AssertEqual(t, e.Action, "expire")
  160. testutil.AssertEqual(t, e.Node.Key, "/foodir")
  161. testutil.AssertEqual(t, e.Node.Dir, true)
  162. }
  163. // Ensure that the store can watch for key expiration when refreshing.
  164. func TestStoreWatchExpireRefresh(t *testing.T) {
  165. s := newStore()
  166. fc := newFakeClock()
  167. s.clock = fc
  168. var eidx uint64 = 2
  169. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  170. s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1200 * time.Millisecond), Refresh: true})
  171. // Make sure we set watch updates when Refresh is true for newly created keys
  172. w, _ := s.Watch("/", true, false, 0)
  173. testutil.AssertEqual(t, w.StartIndex(), eidx)
  174. c := w.EventChan()
  175. e := nbselect(c)
  176. testutil.AssertNil(t, e)
  177. fc.Advance(600 * time.Millisecond)
  178. s.DeleteExpiredKeys(fc.Now())
  179. eidx = 3
  180. e = nbselect(c)
  181. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  182. testutil.AssertEqual(t, e.Action, "expire")
  183. testutil.AssertEqual(t, e.Node.Key, "/foo")
  184. s.Update("/foofoo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  185. w, _ = s.Watch("/", true, false, 4)
  186. fc.Advance(700 * time.Millisecond)
  187. s.DeleteExpiredKeys(fc.Now())
  188. eidx = 5 // We should skip 4 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true
  189. testutil.AssertEqual(t, w.StartIndex(), eidx-1)
  190. e = nbselect(w.EventChan())
  191. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  192. testutil.AssertEqual(t, e.Action, "expire")
  193. testutil.AssertEqual(t, e.Node.Key, "/foofoo")
  194. }
  195. // Ensure that the store can watch for key expiration when refreshing with an empty value.
  196. func TestStoreWatchExpireEmptyRefresh(t *testing.T) {
  197. s := newStore()
  198. fc := newFakeClock()
  199. s.clock = fc
  200. var eidx uint64
  201. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  202. // Should be no-op
  203. fc.Advance(200 * time.Millisecond)
  204. s.DeleteExpiredKeys(fc.Now())
  205. s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  206. w, _ := s.Watch("/", true, false, 2)
  207. fc.Advance(700 * time.Millisecond)
  208. s.DeleteExpiredKeys(fc.Now())
  209. eidx = 3 // We should skip 2 because a TTL update should occur with no watch notification if set `TTLOptionSet.Refresh` to true
  210. testutil.AssertEqual(t, w.StartIndex(), eidx-1)
  211. e := nbselect(w.EventChan())
  212. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  213. testutil.AssertEqual(t, e.Action, "expire")
  214. testutil.AssertEqual(t, e.Node.Key, "/foo")
  215. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  216. }
  217. // Update TTL of a key (set TTLOptionSet.Refresh to false) and send notification
  218. func TestStoreWatchNoRefresh(t *testing.T) {
  219. s := newStore()
  220. fc := newFakeClock()
  221. s.clock = fc
  222. var eidx uint64
  223. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  224. // Should be no-op
  225. fc.Advance(200 * time.Millisecond)
  226. s.DeleteExpiredKeys(fc.Now())
  227. // Update key's TTL with setting `TTLOptionSet.Refresh` to false will cause an update event
  228. s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: false})
  229. w, _ := s.Watch("/", true, false, 2)
  230. fc.Advance(700 * time.Millisecond)
  231. s.DeleteExpiredKeys(fc.Now())
  232. eidx = 2
  233. testutil.AssertEqual(t, w.StartIndex(), eidx)
  234. e := nbselect(w.EventChan())
  235. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  236. testutil.AssertEqual(t, e.Action, "update")
  237. testutil.AssertEqual(t, e.Node.Key, "/foo")
  238. testutil.AssertEqual(t, *e.PrevNode.Value, "bar")
  239. }
  240. // Ensure that the store can update the TTL on a value with refresh.
  241. func TestStoreRefresh(t *testing.T) {
  242. s := newStore()
  243. fc := newFakeClock()
  244. s.clock = fc
  245. s.Create("/foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  246. s.Create("/bar", true, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  247. _, err := s.Update("/foo", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  248. testutil.AssertNil(t, err)
  249. _, err = s.Set("/foo", false, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  250. testutil.AssertNil(t, err)
  251. _, err = s.Update("/bar", "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  252. testutil.AssertNil(t, err)
  253. _, err = s.CompareAndSwap("/foo", "bar", 0, "", TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond), Refresh: true})
  254. testutil.AssertNil(t, err)
  255. }
  256. // Ensure that the store can recover from a previously saved state that includes an expiring key.
  257. func TestStoreRecoverWithExpiration(t *testing.T) {
  258. s := newStore()
  259. s.clock = newFakeClock()
  260. fc := newFakeClock()
  261. var eidx uint64 = 4
  262. s.Create("/foo", true, "", false, TTLOptionSet{ExpireTime: Permanent})
  263. s.Create("/foo/x", false, "bar", false, TTLOptionSet{ExpireTime: Permanent})
  264. s.Create("/foo/y", false, "baz", false, TTLOptionSet{ExpireTime: fc.Now().Add(5 * time.Millisecond)})
  265. b, err := s.Save()
  266. testutil.AssertNil(t, err)
  267. time.Sleep(10 * time.Millisecond)
  268. s2 := newStore()
  269. s2.clock = fc
  270. s2.Recovery(b)
  271. fc.Advance(600 * time.Millisecond)
  272. s.DeleteExpiredKeys(fc.Now())
  273. e, err := s.Get("/foo/x", false, false)
  274. testutil.AssertNil(t, err)
  275. testutil.AssertEqual(t, e.EtcdIndex, eidx)
  276. testutil.AssertEqual(t, *e.Node.Value, "bar")
  277. e, err = s.Get("/foo/y", false, false)
  278. testutil.AssertNotNil(t, err)
  279. testutil.AssertNil(t, e)
  280. }
  281. // Ensure that the store doesn't see expirations of hidden keys.
  282. func TestStoreWatchExpireWithHiddenKey(t *testing.T) {
  283. s := newStore()
  284. fc := newFakeClock()
  285. s.clock = fc
  286. s.Create("/_foo", false, "bar", false, TTLOptionSet{ExpireTime: fc.Now().Add(500 * time.Millisecond)})
  287. s.Create("/foofoo", false, "barbarbar", false, TTLOptionSet{ExpireTime: fc.Now().Add(1000 * time.Millisecond)})
  288. w, _ := s.Watch("/", true, false, 0)
  289. c := w.EventChan()
  290. e := nbselect(c)
  291. testutil.AssertNil(t, e)
  292. fc.Advance(600 * time.Millisecond)
  293. s.DeleteExpiredKeys(fc.Now())
  294. e = nbselect(c)
  295. testutil.AssertNil(t, e)
  296. fc.Advance(600 * time.Millisecond)
  297. s.DeleteExpiredKeys(fc.Now())
  298. e = nbselect(c)
  299. testutil.AssertEqual(t, e.Action, "expire")
  300. testutil.AssertEqual(t, e.Node.Key, "/foofoo")
  301. }
  302. // newFakeClock creates a new FakeClock that has been advanced to at least minExpireTime
  303. func newFakeClock() clockwork.FakeClock {
  304. fc := clockwork.NewFakeClock()
  305. for minExpireTime.After(fc.Now()) {
  306. fc.Advance((0x1 << 62) * time.Nanosecond)
  307. }
  308. return fc
  309. }
  310. // Performs a non-blocking select on an event channel.
  311. func nbselect(c <-chan *Event) *Event {
  312. select {
  313. case e := <-c:
  314. return e
  315. default:
  316. return nil
  317. }
  318. }