kv_test.go 21 KB

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