kvstore_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "crypto/rand"
  17. "encoding/binary"
  18. "math"
  19. "os"
  20. "reflect"
  21. "testing"
  22. "time"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "github.com/coreos/etcd/storage/backend"
  25. "github.com/coreos/etcd/storage/storagepb"
  26. )
  27. func TestStoreRev(t *testing.T) {
  28. s := newStore(tmpPath)
  29. defer os.Remove(tmpPath)
  30. for i := 0; i < 3; i++ {
  31. s.Put([]byte("foo"), []byte("bar"))
  32. if r := s.Rev(); r != int64(i+1) {
  33. t.Errorf("#%d: rev = %d, want %d", i, r, i+1)
  34. }
  35. }
  36. }
  37. func TestStorePut(t *testing.T) {
  38. tests := []struct {
  39. rev revision
  40. r indexGetResp
  41. wrev revision
  42. wkey []byte
  43. wkv storagepb.KeyValue
  44. wputrev revision
  45. }{
  46. {
  47. revision{1, 0},
  48. indexGetResp{revision{}, revision{}, 0, ErrRevisionNotFound},
  49. revision{1, 1},
  50. newTestKeyBytes(revision{2, 0}, false),
  51. storagepb.KeyValue{
  52. Key: []byte("foo"),
  53. Value: []byte("bar"),
  54. CreateRevision: 2,
  55. ModRevision: 2,
  56. Version: 1,
  57. },
  58. revision{2, 0},
  59. },
  60. {
  61. revision{1, 1},
  62. indexGetResp{revision{2, 0}, revision{2, 0}, 1, nil},
  63. revision{1, 2},
  64. newTestKeyBytes(revision{2, 1}, false),
  65. storagepb.KeyValue{
  66. Key: []byte("foo"),
  67. Value: []byte("bar"),
  68. CreateRevision: 2,
  69. ModRevision: 2,
  70. Version: 2,
  71. },
  72. revision{2, 1},
  73. },
  74. {
  75. revision{2, 0},
  76. indexGetResp{revision{2, 1}, revision{2, 0}, 2, nil},
  77. revision{2, 1},
  78. newTestKeyBytes(revision{3, 0}, false),
  79. storagepb.KeyValue{
  80. Key: []byte("foo"),
  81. Value: []byte("bar"),
  82. CreateRevision: 2,
  83. ModRevision: 3,
  84. Version: 3,
  85. },
  86. revision{3, 0},
  87. },
  88. }
  89. for i, tt := range tests {
  90. s := newFakeStore()
  91. b := s.b.(*fakeBackend)
  92. fi := s.kvindex.(*fakeIndex)
  93. s.currentRev = tt.rev
  94. s.tx = b.BatchTx()
  95. fi.indexGetRespc <- tt.r
  96. s.put([]byte("foo"), []byte("bar"))
  97. data, err := tt.wkv.Marshal()
  98. if err != nil {
  99. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  100. }
  101. wact := []testutil.Action{
  102. {"put", []interface{}{keyBucketName, tt.wkey, data}},
  103. }
  104. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  105. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  106. }
  107. wact = []testutil.Action{
  108. {"get", []interface{}{[]byte("foo"), tt.wputrev.main}},
  109. {"put", []interface{}{[]byte("foo"), tt.wputrev}},
  110. }
  111. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  112. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  113. }
  114. if s.currentRev != tt.wrev {
  115. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  116. }
  117. }
  118. }
  119. func TestStoreRange(t *testing.T) {
  120. key := newTestKeyBytes(revision{2, 0}, false)
  121. kv := storagepb.KeyValue{
  122. Key: []byte("foo"),
  123. Value: []byte("bar"),
  124. CreateRevision: 1,
  125. ModRevision: 2,
  126. Version: 1,
  127. }
  128. kvb, err := kv.Marshal()
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. currev := revision{1, 1}
  133. wrev := int64(2)
  134. tests := []struct {
  135. idxr indexRangeResp
  136. r rangeResp
  137. }{
  138. {
  139. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  140. rangeResp{[][]byte{key}, [][]byte{kvb}},
  141. },
  142. {
  143. indexRangeResp{[][]byte{[]byte("foo"), []byte("foo1")}, []revision{{2, 0}, {3, 0}}},
  144. rangeResp{[][]byte{key}, [][]byte{kvb}},
  145. },
  146. }
  147. for i, tt := range tests {
  148. s := newFakeStore()
  149. b := s.b.(*fakeBackend)
  150. fi := s.kvindex.(*fakeIndex)
  151. s.currentRev = currev
  152. s.tx = b.BatchTx()
  153. b.tx.rangeRespc <- tt.r
  154. fi.indexRangeRespc <- tt.idxr
  155. kvs, rev, err := s.rangeKeys([]byte("foo"), []byte("goo"), 1, 0)
  156. if err != nil {
  157. t.Errorf("#%d: err = %v, want nil", i, err)
  158. }
  159. if w := []storagepb.KeyValue{kv}; !reflect.DeepEqual(kvs, w) {
  160. t.Errorf("#%d: kvs = %+v, want %+v", i, kvs, w)
  161. }
  162. if rev != wrev {
  163. t.Errorf("#%d: rev = %d, want %d", i, rev, wrev)
  164. }
  165. wstart, wend := revBytesRange(tt.idxr.revs[0])
  166. wact := []testutil.Action{
  167. {"range", []interface{}{keyBucketName, wstart, wend, int64(0)}},
  168. }
  169. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  170. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  171. }
  172. wact = []testutil.Action{
  173. {"range", []interface{}{[]byte("foo"), []byte("goo"), wrev}},
  174. }
  175. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  176. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  177. }
  178. if s.currentRev != currev {
  179. t.Errorf("#%d: current rev = %+v, want %+v", i, s.currentRev, currev)
  180. }
  181. }
  182. }
  183. func TestStoreDeleteRange(t *testing.T) {
  184. tests := []struct {
  185. rev revision
  186. r indexRangeResp
  187. wkey []byte
  188. wrev revision
  189. wrrev int64
  190. wdelrev revision
  191. }{
  192. {
  193. revision{2, 0},
  194. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  195. newTestKeyBytes(revision{3, 0}, true),
  196. revision{2, 1},
  197. 2,
  198. revision{3, 0},
  199. },
  200. {
  201. revision{2, 1},
  202. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  203. newTestKeyBytes(revision{3, 1}, true),
  204. revision{2, 2},
  205. 3,
  206. revision{3, 1},
  207. },
  208. }
  209. for i, tt := range tests {
  210. s := newFakeStore()
  211. b := s.b.(*fakeBackend)
  212. fi := s.kvindex.(*fakeIndex)
  213. s.currentRev = tt.rev
  214. s.tx = b.BatchTx()
  215. fi.indexRangeRespc <- tt.r
  216. n := s.deleteRange([]byte("foo"), []byte("goo"))
  217. if n != 1 {
  218. t.Errorf("#%d: n = %d, want 1", i, n)
  219. }
  220. data, err := (&storagepb.KeyValue{
  221. Key: []byte("foo"),
  222. }).Marshal()
  223. if err != nil {
  224. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  225. }
  226. wact := []testutil.Action{
  227. {"put", []interface{}{keyBucketName, tt.wkey, data}},
  228. }
  229. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  230. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  231. }
  232. wact = []testutil.Action{
  233. {"range", []interface{}{[]byte("foo"), []byte("goo"), tt.wrrev}},
  234. {"tombstone", []interface{}{[]byte("foo"), tt.wdelrev}},
  235. }
  236. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  237. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  238. }
  239. if s.currentRev != tt.wrev {
  240. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  241. }
  242. }
  243. }
  244. func TestStoreCompact(t *testing.T) {
  245. s := newFakeStore()
  246. b := s.b.(*fakeBackend)
  247. fi := s.kvindex.(*fakeIndex)
  248. s.currentRev = revision{3, 0}
  249. fi.indexCompactRespc <- map[revision]struct{}{revision{1, 0}: {}}
  250. key1 := newTestKeyBytes(revision{1, 0}, false)
  251. key2 := newTestKeyBytes(revision{2, 0}, false)
  252. b.tx.rangeRespc <- rangeResp{[][]byte{key1, key2}, nil}
  253. s.Compact(3)
  254. s.wg.Wait()
  255. if s.compactMainRev != 3 {
  256. t.Errorf("compact main rev = %d, want 3", s.compactMainRev)
  257. }
  258. end := make([]byte, 8)
  259. binary.BigEndian.PutUint64(end, uint64(4))
  260. wact := []testutil.Action{
  261. {"put", []interface{}{metaBucketName, scheduledCompactKeyName, newTestRevBytes(revision{3, 0})}},
  262. {"range", []interface{}{keyBucketName, make([]byte, 17), end, int64(10000)}},
  263. {"delete", []interface{}{keyBucketName, key2}},
  264. {"put", []interface{}{metaBucketName, finishedCompactKeyName, newTestRevBytes(revision{3, 0})}},
  265. }
  266. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  267. t.Errorf("tx actions = %+v, want %+v", g, wact)
  268. }
  269. wact = []testutil.Action{
  270. {"compact", []interface{}{int64(3)}},
  271. }
  272. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  273. t.Errorf("index action = %+v, want %+v", g, wact)
  274. }
  275. }
  276. func TestStoreRestore(t *testing.T) {
  277. s := newFakeStore()
  278. b := s.b.(*fakeBackend)
  279. fi := s.kvindex.(*fakeIndex)
  280. putkey := newTestKeyBytes(revision{3, 0}, false)
  281. putkv := storagepb.KeyValue{
  282. Key: []byte("foo"),
  283. Value: []byte("bar"),
  284. CreateRevision: 3,
  285. ModRevision: 3,
  286. Version: 1,
  287. }
  288. putkvb, err := putkv.Marshal()
  289. if err != nil {
  290. t.Fatal(err)
  291. }
  292. delkey := newTestKeyBytes(revision{4, 0}, true)
  293. delkv := storagepb.KeyValue{
  294. Key: []byte("foo"),
  295. }
  296. delkvb, err := delkv.Marshal()
  297. if err != nil {
  298. t.Fatal(err)
  299. }
  300. b.tx.rangeRespc <- rangeResp{[][]byte{finishedCompactKeyName}, [][]byte{newTestRevBytes(revision{2, 0})}}
  301. b.tx.rangeRespc <- rangeResp{[][]byte{putkey, delkey}, [][]byte{putkvb, delkvb}}
  302. b.tx.rangeRespc <- rangeResp{[][]byte{scheduledCompactKeyName}, [][]byte{newTestRevBytes(revision{2, 0})}}
  303. s.Restore()
  304. if s.compactMainRev != 2 {
  305. t.Errorf("compact rev = %d, want 4", s.compactMainRev)
  306. }
  307. wrev := revision{4, 0}
  308. if !reflect.DeepEqual(s.currentRev, wrev) {
  309. t.Errorf("current rev = %v, want %v", s.currentRev, wrev)
  310. }
  311. wact := []testutil.Action{
  312. {"range", []interface{}{metaBucketName, finishedCompactKeyName, []byte(nil), int64(0)}},
  313. {"range", []interface{}{keyBucketName, newTestRevBytes(revision{}), newTestRevBytes(revision{math.MaxInt64, math.MaxInt64}), int64(0)}},
  314. {"range", []interface{}{metaBucketName, scheduledCompactKeyName, []byte(nil), int64(0)}},
  315. }
  316. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  317. t.Errorf("tx actions = %+v, want %+v", g, wact)
  318. }
  319. wact = []testutil.Action{
  320. {"restore", []interface{}{[]byte("foo"), revision{3, 0}, revision{3, 0}, int64(1)}},
  321. {"tombstone", []interface{}{[]byte("foo"), revision{4, 0}}},
  322. }
  323. if g := fi.Action(); !reflect.DeepEqual(g, wact) {
  324. t.Errorf("index action = %+v, want %+v", g, wact)
  325. }
  326. }
  327. func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
  328. s0 := newStore(tmpPath)
  329. defer os.Remove(tmpPath)
  330. s0.Put([]byte("foo"), []byte("bar"))
  331. s0.Put([]byte("foo"), []byte("bar1"))
  332. s0.Put([]byte("foo"), []byte("bar2"))
  333. // write scheduled compaction, but not do compaction
  334. rbytes := newRevBytes()
  335. revToBytes(revision{main: 2}, rbytes)
  336. tx := s0.b.BatchTx()
  337. tx.Lock()
  338. tx.UnsafePut(metaBucketName, scheduledCompactKeyName, rbytes)
  339. tx.Unlock()
  340. s0.Close()
  341. s1 := newStore(tmpPath)
  342. s1.Restore()
  343. // wait for scheduled compaction to be finished
  344. time.Sleep(100 * time.Millisecond)
  345. if _, _, err := s1.Range([]byte("foo"), nil, 0, 2); err != ErrCompacted {
  346. t.Errorf("range on compacted rev error = %v, want %v", err, ErrCompacted)
  347. }
  348. // check the key in backend is deleted
  349. revbytes := newRevBytes()
  350. // TODO: compact should delete main=2 key too
  351. revToBytes(revision{main: 1}, revbytes)
  352. // The disk compaction is done asynchronously and requires more time on slow disk.
  353. // try 5 times for CI with slow IO.
  354. for i := 0; i < 5; i++ {
  355. tx = s1.b.BatchTx()
  356. tx.Lock()
  357. ks, _ := tx.UnsafeRange(keyBucketName, revbytes, nil, 0)
  358. tx.Unlock()
  359. if len(ks) != 0 {
  360. time.Sleep(100 * time.Millisecond)
  361. continue
  362. }
  363. return
  364. }
  365. t.Errorf("key for rev %+v still exists, want deleted", bytesToRev(revbytes))
  366. }
  367. func TestTxnPut(t *testing.T) {
  368. // assign arbitrary size
  369. bytesN := 30
  370. sliceN := 100
  371. keys := createBytesSlice(bytesN, sliceN)
  372. vals := createBytesSlice(bytesN, sliceN)
  373. s := newStore(tmpPath)
  374. defer cleanup(s, tmpPath)
  375. for i := 0; i < sliceN; i++ {
  376. id := s.TxnBegin()
  377. base := int64(i + 1)
  378. rev, err := s.TxnPut(id, keys[i], vals[i])
  379. if err != nil {
  380. t.Error("txn put error")
  381. }
  382. if rev != base {
  383. t.Errorf("#%d: rev = %d, want %d", i, rev, base)
  384. }
  385. s.TxnEnd(id)
  386. }
  387. }
  388. func TestTxnBlockBackendForceCommit(t *testing.T) {
  389. s := newStore(tmpPath)
  390. defer os.Remove(tmpPath)
  391. id := s.TxnBegin()
  392. done := make(chan struct{})
  393. go func() {
  394. s.b.ForceCommit()
  395. done <- struct{}{}
  396. }()
  397. select {
  398. case <-done:
  399. t.Fatalf("failed to block ForceCommit")
  400. case <-time.After(100 * time.Millisecond):
  401. }
  402. s.TxnEnd(id)
  403. select {
  404. case <-done:
  405. case <-time.After(time.Second):
  406. t.Fatalf("failed to execute ForceCommit")
  407. }
  408. }
  409. func newTestRevBytes(rev revision) []byte {
  410. bytes := newRevBytes()
  411. revToBytes(rev, bytes)
  412. return bytes
  413. }
  414. func newTestKeyBytes(rev revision, tombstone bool) []byte {
  415. bytes := newRevBytes()
  416. revToBytes(rev, bytes)
  417. if tombstone {
  418. bytes = appendMarkTombstone(bytes)
  419. }
  420. return bytes
  421. }
  422. func newFakeStore() *store {
  423. b := &fakeBackend{&fakeBatchTx{rangeRespc: make(chan rangeResp, 5)}}
  424. fi := &fakeIndex{
  425. indexGetRespc: make(chan indexGetResp, 1),
  426. indexRangeRespc: make(chan indexRangeResp, 1),
  427. indexRangeEventsRespc: make(chan indexRangeEventsResp, 1),
  428. indexCompactRespc: make(chan map[revision]struct{}, 1),
  429. }
  430. return &store{
  431. b: b,
  432. kvindex: fi,
  433. currentRev: revision{},
  434. compactMainRev: -1,
  435. }
  436. }
  437. type rangeResp struct {
  438. keys [][]byte
  439. vals [][]byte
  440. }
  441. type fakeBatchTx struct {
  442. testutil.Recorder
  443. rangeRespc chan rangeResp
  444. }
  445. func (b *fakeBatchTx) Lock() {}
  446. func (b *fakeBatchTx) Unlock() {}
  447. func (b *fakeBatchTx) UnsafeCreateBucket(name []byte) {}
  448. func (b *fakeBatchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  449. b.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{bucketName, key, value}})
  450. }
  451. func (b *fakeBatchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) {
  452. b.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{bucketName, key, endKey, limit}})
  453. r := <-b.rangeRespc
  454. return r.keys, r.vals
  455. }
  456. func (b *fakeBatchTx) UnsafeDelete(bucketName []byte, key []byte) {
  457. b.Recorder.Record(testutil.Action{Name: "delete", Params: []interface{}{bucketName, key}})
  458. }
  459. func (b *fakeBatchTx) Commit() {}
  460. func (b *fakeBatchTx) CommitAndStop() {}
  461. type fakeBackend struct {
  462. tx *fakeBatchTx
  463. }
  464. func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
  465. func (b *fakeBackend) Hash() (uint32, error) { return 0, nil }
  466. func (b *fakeBackend) Size() int64 { return 0 }
  467. func (b *fakeBackend) Snapshot() backend.Snapshot { return nil }
  468. func (b *fakeBackend) ForceCommit() {}
  469. func (b *fakeBackend) Close() error { return nil }
  470. type indexGetResp struct {
  471. rev revision
  472. created revision
  473. ver int64
  474. err error
  475. }
  476. type indexRangeResp struct {
  477. keys [][]byte
  478. revs []revision
  479. }
  480. type indexRangeEventsResp struct {
  481. revs []revision
  482. }
  483. type fakeIndex struct {
  484. testutil.Recorder
  485. indexGetRespc chan indexGetResp
  486. indexRangeRespc chan indexRangeResp
  487. indexRangeEventsRespc chan indexRangeEventsResp
  488. indexCompactRespc chan map[revision]struct{}
  489. }
  490. func (i *fakeIndex) Get(key []byte, atRev int64) (rev, created revision, ver int64, err error) {
  491. i.Recorder.Record(testutil.Action{Name: "get", Params: []interface{}{key, atRev}})
  492. r := <-i.indexGetRespc
  493. return r.rev, r.created, r.ver, r.err
  494. }
  495. func (i *fakeIndex) Range(key, end []byte, atRev int64) ([][]byte, []revision) {
  496. i.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{key, end, atRev}})
  497. r := <-i.indexRangeRespc
  498. return r.keys, r.revs
  499. }
  500. func (i *fakeIndex) Put(key []byte, rev revision) {
  501. i.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{key, rev}})
  502. }
  503. func (i *fakeIndex) Restore(key []byte, created, modified revision, ver int64) {
  504. i.Recorder.Record(testutil.Action{Name: "restore", Params: []interface{}{key, created, modified, ver}})
  505. }
  506. func (i *fakeIndex) Tombstone(key []byte, rev revision) error {
  507. i.Recorder.Record(testutil.Action{Name: "tombstone", Params: []interface{}{key, rev}})
  508. return nil
  509. }
  510. func (i *fakeIndex) RangeSince(key, end []byte, rev int64) []revision {
  511. i.Recorder.Record(testutil.Action{Name: "rangeEvents", Params: []interface{}{key, end, rev}})
  512. r := <-i.indexRangeEventsRespc
  513. return r.revs
  514. }
  515. func (i *fakeIndex) Compact(rev int64) map[revision]struct{} {
  516. i.Recorder.Record(testutil.Action{Name: "compact", Params: []interface{}{rev}})
  517. return <-i.indexCompactRespc
  518. }
  519. func (i *fakeIndex) Equal(b index) bool { return false }
  520. func createBytesSlice(bytesN, sliceN int) [][]byte {
  521. rs := [][]byte{}
  522. for len(rs) != sliceN {
  523. v := make([]byte, bytesN)
  524. if _, err := rand.Read(v); err != nil {
  525. panic(err)
  526. }
  527. rs = append(rs, v)
  528. }
  529. return rs
  530. }