kv_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 mvcc
  15. import (
  16. "fmt"
  17. "os"
  18. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/coreos/etcd/lease"
  22. "github.com/coreos/etcd/mvcc/backend"
  23. "github.com/coreos/etcd/mvcc/mvccpb"
  24. "github.com/coreos/etcd/pkg/testutil"
  25. )
  26. // Functional tests for features implemented in v3 store. It treats v3 store
  27. // as a black box, and tests it by feeding the input and validating the output.
  28. // TODO: add similar tests on operations in one txn/rev
  29. type (
  30. rangeFunc func(kv KV, key, end []byte, ro RangeOptions) (*RangeResult, error)
  31. putFunc func(kv KV, key, value []byte, lease lease.LeaseID) int64
  32. deleteRangeFunc func(kv KV, key, end []byte) (n, rev int64)
  33. )
  34. var (
  35. normalRangeFunc = func(kv KV, key, end []byte, ro RangeOptions) (*RangeResult, error) {
  36. return kv.Range(key, end, ro)
  37. }
  38. txnRangeFunc = func(kv KV, key, end []byte, ro RangeOptions) (*RangeResult, error) {
  39. id := kv.TxnBegin()
  40. defer kv.TxnEnd(id)
  41. return kv.TxnRange(id, key, end, ro)
  42. }
  43. normalPutFunc = func(kv KV, key, value []byte, lease lease.LeaseID) int64 {
  44. return kv.Put(key, value, lease)
  45. }
  46. txnPutFunc = func(kv KV, key, value []byte, lease lease.LeaseID) int64 {
  47. id := kv.TxnBegin()
  48. defer kv.TxnEnd(id)
  49. rev, err := kv.TxnPut(id, key, value, lease)
  50. if err != nil {
  51. panic("txn put error")
  52. }
  53. return rev
  54. }
  55. normalDeleteRangeFunc = func(kv KV, key, end []byte) (n, rev int64) {
  56. return kv.DeleteRange(key, end)
  57. }
  58. txnDeleteRangeFunc = func(kv KV, key, end []byte) (n, rev int64) {
  59. id := kv.TxnBegin()
  60. defer kv.TxnEnd(id)
  61. n, rev, err := kv.TxnDeleteRange(id, key, end)
  62. if err != nil {
  63. panic("txn delete error")
  64. }
  65. return n, rev
  66. }
  67. )
  68. func TestKVRange(t *testing.T) { testKVRange(t, normalRangeFunc) }
  69. func TestKVTxnRange(t *testing.T) { testKVRange(t, txnRangeFunc) }
  70. func testKVRange(t *testing.T, f rangeFunc) {
  71. b, tmpPath := backend.NewDefaultTmpBackend()
  72. s := NewStore(b, &lease.FakeLessor{}, nil)
  73. defer cleanup(s, b, tmpPath)
  74. kvs := put3TestKVs(s)
  75. wrev := int64(4)
  76. tests := []struct {
  77. key, end []byte
  78. wkvs []mvccpb.KeyValue
  79. }{
  80. // get no keys
  81. {
  82. []byte("doo"), []byte("foo"),
  83. nil,
  84. },
  85. // get no keys when key == end
  86. {
  87. []byte("foo"), []byte("foo"),
  88. nil,
  89. },
  90. // get no keys when ranging single key
  91. {
  92. []byte("doo"), nil,
  93. nil,
  94. },
  95. // get all keys
  96. {
  97. []byte("foo"), []byte("foo3"),
  98. kvs,
  99. },
  100. // get partial keys
  101. {
  102. []byte("foo"), []byte("foo1"),
  103. kvs[:1],
  104. },
  105. // get single key
  106. {
  107. []byte("foo"), nil,
  108. kvs[:1],
  109. },
  110. // get entire keyspace
  111. {
  112. []byte(""), []byte(""),
  113. kvs,
  114. },
  115. }
  116. for i, tt := range tests {
  117. r, err := f(s, tt.key, tt.end, RangeOptions{})
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. if r.Rev != wrev {
  122. t.Errorf("#%d: rev = %d, want %d", i, r.Rev, wrev)
  123. }
  124. if !reflect.DeepEqual(r.KVs, tt.wkvs) {
  125. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, tt.wkvs)
  126. }
  127. }
  128. }
  129. func TestKVRangeRev(t *testing.T) { testKVRangeRev(t, normalRangeFunc) }
  130. func TestKVTxnRangeRev(t *testing.T) { testKVRangeRev(t, normalRangeFunc) }
  131. func testKVRangeRev(t *testing.T, f rangeFunc) {
  132. b, tmpPath := backend.NewDefaultTmpBackend()
  133. s := NewStore(b, &lease.FakeLessor{}, nil)
  134. defer cleanup(s, b, tmpPath)
  135. kvs := put3TestKVs(s)
  136. tests := []struct {
  137. rev int64
  138. wrev int64
  139. wkvs []mvccpb.KeyValue
  140. }{
  141. {-1, 4, kvs},
  142. {0, 4, kvs},
  143. {2, 4, kvs[:1]},
  144. {3, 4, kvs[:2]},
  145. {4, 4, kvs},
  146. }
  147. for i, tt := range tests {
  148. r, err := f(s, []byte("foo"), []byte("foo3"), RangeOptions{Rev: tt.rev})
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. if r.Rev != tt.wrev {
  153. t.Errorf("#%d: rev = %d, want %d", i, r.Rev, tt.wrev)
  154. }
  155. if !reflect.DeepEqual(r.KVs, tt.wkvs) {
  156. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, tt.wkvs)
  157. }
  158. }
  159. }
  160. func TestKVRangeBadRev(t *testing.T) { testKVRangeBadRev(t, normalRangeFunc) }
  161. func TestKVTxnRangeBadRev(t *testing.T) { testKVRangeBadRev(t, normalRangeFunc) }
  162. func testKVRangeBadRev(t *testing.T, f rangeFunc) {
  163. b, tmpPath := backend.NewDefaultTmpBackend()
  164. s := NewStore(b, &lease.FakeLessor{}, nil)
  165. defer cleanup(s, b, tmpPath)
  166. put3TestKVs(s)
  167. if _, err := s.Compact(4); err != nil {
  168. t.Fatalf("compact error (%v)", err)
  169. }
  170. tests := []struct {
  171. rev int64
  172. werr error
  173. }{
  174. {-1, nil}, // <= 0 is most recent store
  175. {0, nil},
  176. {1, ErrCompacted},
  177. {2, ErrCompacted},
  178. {4, nil},
  179. {5, ErrFutureRev},
  180. {100, ErrFutureRev},
  181. }
  182. for i, tt := range tests {
  183. _, err := f(s, []byte("foo"), []byte("foo3"), RangeOptions{Rev: tt.rev})
  184. if err != tt.werr {
  185. t.Errorf("#%d: error = %v, want %v", i, err, tt.werr)
  186. }
  187. }
  188. }
  189. func TestKVRangeLimit(t *testing.T) { testKVRangeLimit(t, normalRangeFunc) }
  190. func TestKVTxnRangeLimit(t *testing.T) { testKVRangeLimit(t, txnRangeFunc) }
  191. func testKVRangeLimit(t *testing.T, f rangeFunc) {
  192. b, tmpPath := backend.NewDefaultTmpBackend()
  193. s := NewStore(b, &lease.FakeLessor{}, nil)
  194. defer cleanup(s, b, tmpPath)
  195. kvs := put3TestKVs(s)
  196. wrev := int64(4)
  197. tests := []struct {
  198. limit int64
  199. wkvs []mvccpb.KeyValue
  200. }{
  201. // no limit
  202. {-1, kvs},
  203. // no limit
  204. {0, kvs},
  205. {1, kvs[:1]},
  206. {2, kvs[:2]},
  207. {3, kvs},
  208. {100, kvs},
  209. }
  210. for i, tt := range tests {
  211. r, err := f(s, []byte("foo"), []byte("foo3"), RangeOptions{Limit: tt.limit})
  212. if err != nil {
  213. t.Fatalf("#%d: range error (%v)", i, err)
  214. }
  215. if !reflect.DeepEqual(r.KVs, tt.wkvs) {
  216. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, tt.wkvs)
  217. }
  218. if r.Rev != wrev {
  219. t.Errorf("#%d: rev = %d, want %d", i, r.Rev, wrev)
  220. }
  221. if r.Count != len(kvs) {
  222. t.Errorf("#%d: count = %d, want %d", i, r.Count, len(kvs))
  223. }
  224. }
  225. }
  226. func TestKVPutMultipleTimes(t *testing.T) { testKVPutMultipleTimes(t, normalPutFunc) }
  227. func TestKVTxnPutMultipleTimes(t *testing.T) { testKVPutMultipleTimes(t, txnPutFunc) }
  228. func testKVPutMultipleTimes(t *testing.T, f putFunc) {
  229. b, tmpPath := backend.NewDefaultTmpBackend()
  230. s := NewStore(b, &lease.FakeLessor{}, nil)
  231. defer cleanup(s, b, tmpPath)
  232. for i := 0; i < 10; i++ {
  233. base := int64(i + 1)
  234. rev := f(s, []byte("foo"), []byte("bar"), lease.LeaseID(base))
  235. if rev != base+1 {
  236. t.Errorf("#%d: rev = %d, want %d", i, rev, base+1)
  237. }
  238. r, err := s.Range([]byte("foo"), nil, RangeOptions{})
  239. if err != nil {
  240. t.Fatal(err)
  241. }
  242. wkvs := []mvccpb.KeyValue{
  243. {Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: base + 1, Version: base, Lease: base},
  244. }
  245. if !reflect.DeepEqual(r.KVs, wkvs) {
  246. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, wkvs)
  247. }
  248. }
  249. }
  250. func TestKVDeleteRange(t *testing.T) { testKVDeleteRange(t, normalDeleteRangeFunc) }
  251. func TestKVTxnDeleteRange(t *testing.T) { testKVDeleteRange(t, txnDeleteRangeFunc) }
  252. func testKVDeleteRange(t *testing.T, f deleteRangeFunc) {
  253. tests := []struct {
  254. key, end []byte
  255. wrev int64
  256. wN int64
  257. }{
  258. {
  259. []byte("foo"), nil,
  260. 5, 1,
  261. },
  262. {
  263. []byte("foo"), []byte("foo1"),
  264. 5, 1,
  265. },
  266. {
  267. []byte("foo"), []byte("foo2"),
  268. 5, 2,
  269. },
  270. {
  271. []byte("foo"), []byte("foo3"),
  272. 5, 3,
  273. },
  274. {
  275. []byte("foo3"), []byte("foo8"),
  276. 4, 0,
  277. },
  278. {
  279. []byte("foo3"), nil,
  280. 4, 0,
  281. },
  282. }
  283. for i, tt := range tests {
  284. b, tmpPath := backend.NewDefaultTmpBackend()
  285. s := NewStore(b, &lease.FakeLessor{}, nil)
  286. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  287. s.Put([]byte("foo1"), []byte("bar1"), lease.NoLease)
  288. s.Put([]byte("foo2"), []byte("bar2"), lease.NoLease)
  289. n, rev := f(s, tt.key, tt.end)
  290. if n != tt.wN || rev != tt.wrev {
  291. t.Errorf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, tt.wN, tt.wrev)
  292. }
  293. cleanup(s, b, tmpPath)
  294. }
  295. }
  296. func TestKVDeleteMultipleTimes(t *testing.T) { testKVDeleteMultipleTimes(t, normalDeleteRangeFunc) }
  297. func TestKVTxnDeleteMultipleTimes(t *testing.T) { testKVDeleteMultipleTimes(t, txnDeleteRangeFunc) }
  298. func testKVDeleteMultipleTimes(t *testing.T, f deleteRangeFunc) {
  299. b, tmpPath := backend.NewDefaultTmpBackend()
  300. s := NewStore(b, &lease.FakeLessor{}, nil)
  301. defer cleanup(s, b, tmpPath)
  302. s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  303. n, rev := f(s, []byte("foo"), nil)
  304. if n != 1 || rev != 3 {
  305. t.Fatalf("n = %d, rev = %d, want (%d, %d)", n, rev, 1, 3)
  306. }
  307. for i := 0; i < 10; i++ {
  308. n, rev := f(s, []byte("foo"), nil)
  309. if n != 0 || rev != 3 {
  310. t.Fatalf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, 0, 3)
  311. }
  312. }
  313. }
  314. // test that range, put, delete on single key in sequence repeatedly works correctly.
  315. func TestKVOperationInSequence(t *testing.T) {
  316. b, tmpPath := backend.NewDefaultTmpBackend()
  317. s := NewStore(b, &lease.FakeLessor{}, nil)
  318. defer cleanup(s, b, tmpPath)
  319. for i := 0; i < 10; i++ {
  320. base := int64(i*2 + 1)
  321. // put foo
  322. rev := s.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  323. if rev != base+1 {
  324. t.Errorf("#%d: put rev = %d, want %d", i, rev, base+1)
  325. }
  326. r, err := s.Range([]byte("foo"), nil, RangeOptions{Rev: base + 1})
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. wkvs := []mvccpb.KeyValue{
  331. {Key: []byte("foo"), Value: []byte("bar"), CreateRevision: base + 1, ModRevision: base + 1, Version: 1, Lease: int64(lease.NoLease)},
  332. }
  333. if !reflect.DeepEqual(r.KVs, wkvs) {
  334. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, wkvs)
  335. }
  336. if r.Rev != base+1 {
  337. t.Errorf("#%d: range rev = %d, want %d", i, rev, base+1)
  338. }
  339. // delete foo
  340. n, rev := s.DeleteRange([]byte("foo"), nil)
  341. if n != 1 || rev != base+2 {
  342. t.Errorf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, 1, base+2)
  343. }
  344. r, err = s.Range([]byte("foo"), nil, RangeOptions{Rev: base + 2})
  345. if err != nil {
  346. t.Fatal(err)
  347. }
  348. if r.KVs != nil {
  349. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, nil)
  350. }
  351. if r.Rev != base+2 {
  352. t.Errorf("#%d: range rev = %d, want %d", i, r.Rev, base+2)
  353. }
  354. }
  355. }
  356. func TestKVTxnBlockNonTxnOperations(t *testing.T) {
  357. b, tmpPath := backend.NewDefaultTmpBackend()
  358. s := NewStore(b, &lease.FakeLessor{}, nil)
  359. tests := []func(){
  360. func() { s.Range([]byte("foo"), nil, RangeOptions{}) },
  361. func() { s.Put([]byte("foo"), nil, lease.NoLease) },
  362. func() { s.DeleteRange([]byte("foo"), nil) },
  363. }
  364. for i, tt := range tests {
  365. id := s.TxnBegin()
  366. done := make(chan struct{}, 1)
  367. go func() {
  368. tt()
  369. done <- struct{}{}
  370. }()
  371. select {
  372. case <-done:
  373. t.Fatalf("#%d: operation failed to be blocked", i)
  374. case <-time.After(10 * time.Millisecond):
  375. }
  376. s.TxnEnd(id)
  377. select {
  378. case <-done:
  379. case <-time.After(10 * time.Second):
  380. testutil.FatalStack(t, fmt.Sprintf("#%d: operation failed to be unblocked", i))
  381. }
  382. }
  383. // only close backend when we know all the tx are finished
  384. cleanup(s, b, tmpPath)
  385. }
  386. func TestKVTxnWrongID(t *testing.T) {
  387. b, tmpPath := backend.NewDefaultTmpBackend()
  388. s := NewStore(b, &lease.FakeLessor{}, nil)
  389. defer cleanup(s, b, tmpPath)
  390. id := s.TxnBegin()
  391. wrongid := id + 1
  392. tests := []func() error{
  393. func() error {
  394. _, err := s.TxnRange(wrongid, []byte("foo"), nil, RangeOptions{})
  395. return err
  396. },
  397. func() error {
  398. _, err := s.TxnPut(wrongid, []byte("foo"), nil, lease.NoLease)
  399. return err
  400. },
  401. func() error {
  402. _, _, err := s.TxnDeleteRange(wrongid, []byte("foo"), nil)
  403. return err
  404. },
  405. func() error { return s.TxnEnd(wrongid) },
  406. }
  407. for i, tt := range tests {
  408. err := tt()
  409. if err != ErrTxnIDMismatch {
  410. t.Fatalf("#%d: err = %+v, want %+v", i, err, ErrTxnIDMismatch)
  411. }
  412. }
  413. err := s.TxnEnd(id)
  414. if err != nil {
  415. t.Fatalf("end err = %+v, want %+v", err, nil)
  416. }
  417. }
  418. // test that txn range, put, delete on single key in sequence repeatedly works correctly.
  419. func TestKVTxnOperationInSequence(t *testing.T) {
  420. b, tmpPath := backend.NewDefaultTmpBackend()
  421. s := NewStore(b, &lease.FakeLessor{}, nil)
  422. defer cleanup(s, b, tmpPath)
  423. for i := 0; i < 10; i++ {
  424. id := s.TxnBegin()
  425. base := int64(i + 1)
  426. // put foo
  427. rev, err := s.TxnPut(id, []byte("foo"), []byte("bar"), lease.NoLease)
  428. if err != nil {
  429. t.Fatal(err)
  430. }
  431. if rev != base+1 {
  432. t.Errorf("#%d: put rev = %d, want %d", i, rev, base+1)
  433. }
  434. r, err := s.TxnRange(id, []byte("foo"), nil, RangeOptions{Rev: base + 1})
  435. if err != nil {
  436. t.Fatal(err)
  437. }
  438. wkvs := []mvccpb.KeyValue{
  439. {Key: []byte("foo"), Value: []byte("bar"), CreateRevision: base + 1, ModRevision: base + 1, Version: 1, Lease: int64(lease.NoLease)},
  440. }
  441. if !reflect.DeepEqual(r.KVs, wkvs) {
  442. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, wkvs)
  443. }
  444. if r.Rev != base+1 {
  445. t.Errorf("#%d: range rev = %d, want %d", i, r.Rev, base+1)
  446. }
  447. // delete foo
  448. n, rev, err := s.TxnDeleteRange(id, []byte("foo"), nil)
  449. if err != nil {
  450. t.Fatal(err)
  451. }
  452. if n != 1 || rev != base+1 {
  453. t.Errorf("#%d: n = %d, rev = %d, want (%d, %d)", i, n, rev, 1, base+1)
  454. }
  455. r, err = s.TxnRange(id, []byte("foo"), nil, RangeOptions{Rev: base + 1})
  456. if err != nil {
  457. t.Errorf("#%d: range error (%v)", i, err)
  458. }
  459. if r.KVs != nil {
  460. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, nil)
  461. }
  462. if r.Rev != base+1 {
  463. t.Errorf("#%d: range rev = %d, want %d", i, r.Rev, base+1)
  464. }
  465. s.TxnEnd(id)
  466. }
  467. }
  468. func TestKVCompactReserveLastValue(t *testing.T) {
  469. b, tmpPath := backend.NewDefaultTmpBackend()
  470. s := NewStore(b, &lease.FakeLessor{}, nil)
  471. defer cleanup(s, b, tmpPath)
  472. s.Put([]byte("foo"), []byte("bar0"), 1)
  473. s.Put([]byte("foo"), []byte("bar1"), 2)
  474. s.DeleteRange([]byte("foo"), nil)
  475. s.Put([]byte("foo"), []byte("bar2"), 3)
  476. // rev in tests will be called in Compact() one by one on the same store
  477. tests := []struct {
  478. rev int64
  479. // wanted kvs right after the compacted rev
  480. wkvs []mvccpb.KeyValue
  481. }{
  482. {
  483. 1,
  484. []mvccpb.KeyValue{
  485. {Key: []byte("foo"), Value: []byte("bar0"), CreateRevision: 2, ModRevision: 2, Version: 1, Lease: 1},
  486. },
  487. },
  488. {
  489. 2,
  490. []mvccpb.KeyValue{
  491. {Key: []byte("foo"), Value: []byte("bar1"), CreateRevision: 2, ModRevision: 3, Version: 2, Lease: 2},
  492. },
  493. },
  494. {
  495. 3,
  496. nil,
  497. },
  498. {
  499. 4,
  500. []mvccpb.KeyValue{
  501. {Key: []byte("foo"), Value: []byte("bar2"), CreateRevision: 5, ModRevision: 5, Version: 1, Lease: 3},
  502. },
  503. },
  504. }
  505. for i, tt := range tests {
  506. _, err := s.Compact(tt.rev)
  507. if err != nil {
  508. t.Errorf("#%d: unexpect compact error %v", i, err)
  509. }
  510. r, err := s.Range([]byte("foo"), nil, RangeOptions{Rev: tt.rev + 1})
  511. if err != nil {
  512. t.Errorf("#%d: unexpect range error %v", i, err)
  513. }
  514. if !reflect.DeepEqual(r.KVs, tt.wkvs) {
  515. t.Errorf("#%d: kvs = %+v, want %+v", i, r.KVs, tt.wkvs)
  516. }
  517. }
  518. }
  519. func TestKVCompactBad(t *testing.T) {
  520. b, tmpPath := backend.NewDefaultTmpBackend()
  521. s := NewStore(b, &lease.FakeLessor{}, nil)
  522. defer cleanup(s, b, tmpPath)
  523. s.Put([]byte("foo"), []byte("bar0"), lease.NoLease)
  524. s.Put([]byte("foo"), []byte("bar1"), lease.NoLease)
  525. s.Put([]byte("foo"), []byte("bar2"), lease.NoLease)
  526. // rev in tests will be called in Compact() one by one on the same store
  527. tests := []struct {
  528. rev int64
  529. werr error
  530. }{
  531. {0, nil},
  532. {1, nil},
  533. {1, ErrCompacted},
  534. {4, nil},
  535. {5, ErrFutureRev},
  536. {100, ErrFutureRev},
  537. }
  538. for i, tt := range tests {
  539. _, err := s.Compact(tt.rev)
  540. if err != tt.werr {
  541. t.Errorf("#%d: compact error = %v, want %v", i, err, tt.werr)
  542. }
  543. }
  544. }
  545. func TestKVHash(t *testing.T) {
  546. hashes := make([]uint32, 3)
  547. for i := 0; i < len(hashes); i++ {
  548. var err error
  549. b, tmpPath := backend.NewDefaultTmpBackend()
  550. kv := NewStore(b, &lease.FakeLessor{}, nil)
  551. kv.Put([]byte("foo0"), []byte("bar0"), lease.NoLease)
  552. kv.Put([]byte("foo1"), []byte("bar0"), lease.NoLease)
  553. hashes[i], _, err = kv.Hash()
  554. if err != nil {
  555. t.Fatalf("failed to get hash: %v", err)
  556. }
  557. cleanup(kv, b, tmpPath)
  558. }
  559. for i := 1; i < len(hashes); i++ {
  560. if hashes[i-1] != hashes[i] {
  561. t.Errorf("hash[%d](%d) != hash[%d](%d)", i-1, hashes[i-1], i, hashes[i])
  562. }
  563. }
  564. }
  565. func TestKVRestore(t *testing.T) {
  566. tests := []func(kv KV){
  567. func(kv KV) {
  568. kv.Put([]byte("foo"), []byte("bar0"), 1)
  569. kv.Put([]byte("foo"), []byte("bar1"), 2)
  570. kv.Put([]byte("foo"), []byte("bar2"), 3)
  571. },
  572. func(kv KV) {
  573. kv.Put([]byte("foo"), []byte("bar0"), 1)
  574. kv.DeleteRange([]byte("foo"), nil)
  575. kv.Put([]byte("foo"), []byte("bar1"), 2)
  576. },
  577. func(kv KV) {
  578. kv.Put([]byte("foo"), []byte("bar0"), 1)
  579. kv.Put([]byte("foo"), []byte("bar1"), 2)
  580. kv.Compact(1)
  581. },
  582. }
  583. for i, tt := range tests {
  584. b, tmpPath := backend.NewDefaultTmpBackend()
  585. s := NewStore(b, &lease.FakeLessor{}, nil)
  586. tt(s)
  587. var kvss [][]mvccpb.KeyValue
  588. for k := int64(0); k < 10; k++ {
  589. r, _ := s.Range([]byte("a"), []byte("z"), RangeOptions{Rev: k})
  590. kvss = append(kvss, r.KVs)
  591. }
  592. s.Close()
  593. // ns should recover the the previous state from backend.
  594. ns := NewStore(b, &lease.FakeLessor{}, nil)
  595. // wait for possible compaction to finish
  596. testutil.WaitSchedule()
  597. var nkvss [][]mvccpb.KeyValue
  598. for k := int64(0); k < 10; k++ {
  599. r, _ := ns.Range([]byte("a"), []byte("z"), RangeOptions{Rev: k})
  600. nkvss = append(nkvss, r.KVs)
  601. }
  602. cleanup(ns, b, tmpPath)
  603. if !reflect.DeepEqual(nkvss, kvss) {
  604. t.Errorf("#%d: kvs history = %+v, want %+v", i, nkvss, kvss)
  605. }
  606. }
  607. }
  608. func TestKVSnapshot(t *testing.T) {
  609. b, tmpPath := backend.NewDefaultTmpBackend()
  610. s := NewStore(b, &lease.FakeLessor{}, nil)
  611. defer cleanup(s, b, tmpPath)
  612. wkvs := put3TestKVs(s)
  613. newPath := "new_test"
  614. f, err := os.Create(newPath)
  615. if err != nil {
  616. t.Fatal(err)
  617. }
  618. defer os.Remove(newPath)
  619. snap := s.b.Snapshot()
  620. defer snap.Close()
  621. _, err = snap.WriteTo(f)
  622. if err != nil {
  623. t.Fatal(err)
  624. }
  625. f.Close()
  626. ns := NewStore(b, &lease.FakeLessor{}, nil)
  627. defer ns.Close()
  628. r, err := ns.Range([]byte("a"), []byte("z"), RangeOptions{})
  629. if err != nil {
  630. t.Errorf("unexpect range error (%v)", err)
  631. }
  632. if !reflect.DeepEqual(r.KVs, wkvs) {
  633. t.Errorf("kvs = %+v, want %+v", r.KVs, wkvs)
  634. }
  635. if r.Rev != 4 {
  636. t.Errorf("rev = %d, want %d", r.Rev, 4)
  637. }
  638. }
  639. func TestWatchableKVWatch(t *testing.T) {
  640. b, tmpPath := backend.NewDefaultTmpBackend()
  641. s := WatchableKV(newWatchableStore(b, &lease.FakeLessor{}, nil))
  642. defer cleanup(s, b, tmpPath)
  643. w := s.NewWatchStream()
  644. defer w.Close()
  645. wid := w.Watch([]byte("foo"), []byte("fop"), 0)
  646. wev := []mvccpb.Event{
  647. {Type: mvccpb.PUT,
  648. Kv: &mvccpb.KeyValue{
  649. Key: []byte("foo"),
  650. Value: []byte("bar"),
  651. CreateRevision: 2,
  652. ModRevision: 2,
  653. Version: 1,
  654. Lease: 1,
  655. },
  656. },
  657. {
  658. Type: mvccpb.PUT,
  659. Kv: &mvccpb.KeyValue{
  660. Key: []byte("foo1"),
  661. Value: []byte("bar1"),
  662. CreateRevision: 3,
  663. ModRevision: 3,
  664. Version: 1,
  665. Lease: 2,
  666. },
  667. },
  668. {
  669. Type: mvccpb.PUT,
  670. Kv: &mvccpb.KeyValue{
  671. Key: []byte("foo1"),
  672. Value: []byte("bar11"),
  673. CreateRevision: 3,
  674. ModRevision: 4,
  675. Version: 2,
  676. Lease: 3,
  677. },
  678. },
  679. }
  680. s.Put([]byte("foo"), []byte("bar"), 1)
  681. select {
  682. case resp := <-w.Chan():
  683. if resp.WatchID != wid {
  684. t.Errorf("resp.WatchID got = %d, want = %d", resp.WatchID, wid)
  685. }
  686. ev := resp.Events[0]
  687. if !reflect.DeepEqual(ev, wev[0]) {
  688. t.Errorf("watched event = %+v, want %+v", ev, wev[0])
  689. }
  690. case <-time.After(5 * time.Second):
  691. // CPU might be too slow, and the routine is not able to switch around
  692. testutil.FatalStack(t, "failed to watch the event")
  693. }
  694. s.Put([]byte("foo1"), []byte("bar1"), 2)
  695. select {
  696. case resp := <-w.Chan():
  697. if resp.WatchID != wid {
  698. t.Errorf("resp.WatchID got = %d, want = %d", resp.WatchID, wid)
  699. }
  700. ev := resp.Events[0]
  701. if !reflect.DeepEqual(ev, wev[1]) {
  702. t.Errorf("watched event = %+v, want %+v", ev, wev[1])
  703. }
  704. case <-time.After(5 * time.Second):
  705. testutil.FatalStack(t, "failed to watch the event")
  706. }
  707. w = s.NewWatchStream()
  708. wid = w.Watch([]byte("foo1"), []byte("foo2"), 3)
  709. select {
  710. case resp := <-w.Chan():
  711. if resp.WatchID != wid {
  712. t.Errorf("resp.WatchID got = %d, want = %d", resp.WatchID, wid)
  713. }
  714. ev := resp.Events[0]
  715. if !reflect.DeepEqual(ev, wev[1]) {
  716. t.Errorf("watched event = %+v, want %+v", ev, wev[1])
  717. }
  718. case <-time.After(5 * time.Second):
  719. testutil.FatalStack(t, "failed to watch the event")
  720. }
  721. s.Put([]byte("foo1"), []byte("bar11"), 3)
  722. select {
  723. case resp := <-w.Chan():
  724. if resp.WatchID != wid {
  725. t.Errorf("resp.WatchID got = %d, want = %d", resp.WatchID, wid)
  726. }
  727. ev := resp.Events[0]
  728. if !reflect.DeepEqual(ev, wev[2]) {
  729. t.Errorf("watched event = %+v, want %+v", ev, wev[2])
  730. }
  731. case <-time.After(5 * time.Second):
  732. testutil.FatalStack(t, "failed to watch the event")
  733. }
  734. }
  735. func cleanup(s KV, b backend.Backend, path string) {
  736. s.Close()
  737. b.Close()
  738. os.Remove(path)
  739. }
  740. func put3TestKVs(s KV) []mvccpb.KeyValue {
  741. s.Put([]byte("foo"), []byte("bar"), 1)
  742. s.Put([]byte("foo1"), []byte("bar1"), 2)
  743. s.Put([]byte("foo2"), []byte("bar2"), 3)
  744. return []mvccpb.KeyValue{
  745. {Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 2, ModRevision: 2, Version: 1, Lease: 1},
  746. {Key: []byte("foo1"), Value: []byte("bar1"), CreateRevision: 3, ModRevision: 3, Version: 1, Lease: 2},
  747. {Key: []byte("foo2"), Value: []byte("bar2"), CreateRevision: 4, ModRevision: 4, Version: 1, Lease: 3},
  748. }
  749. }