kv_test.go 22 KB

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