kvstore_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package storage
  2. import (
  3. "crypto/rand"
  4. "encoding/binary"
  5. "errors"
  6. "io"
  7. "math"
  8. "os"
  9. "reflect"
  10. "testing"
  11. "time"
  12. "github.com/coreos/etcd/pkg/testutil"
  13. "github.com/coreos/etcd/storage/backend"
  14. "github.com/coreos/etcd/storage/storagepb"
  15. )
  16. func TestStoreRev(t *testing.T) {
  17. s := newStore(tmpPath)
  18. defer os.Remove(tmpPath)
  19. for i := 0; i < 3; i++ {
  20. s.Put([]byte("foo"), []byte("bar"))
  21. if r := s.Rev(); r != int64(i+1) {
  22. t.Errorf("#%d: rev = %d, want %d", i, r, i+1)
  23. }
  24. }
  25. }
  26. func TestStorePut(t *testing.T) {
  27. tests := []struct {
  28. rev revision
  29. r indexGetResp
  30. wrev revision
  31. wev storagepb.Event
  32. wputrev revision
  33. }{
  34. {
  35. revision{1, 0},
  36. indexGetResp{revision{}, revision{}, 0, ErrRevisionNotFound},
  37. revision{1, 1},
  38. storagepb.Event{
  39. Type: storagepb.PUT,
  40. Kv: &storagepb.KeyValue{
  41. Key: []byte("foo"),
  42. Value: []byte("bar"),
  43. CreateRevision: 2,
  44. ModRevision: 2,
  45. Version: 1,
  46. },
  47. },
  48. revision{2, 0},
  49. },
  50. {
  51. revision{1, 1},
  52. indexGetResp{revision{2, 0}, revision{2, 0}, 1, nil},
  53. revision{1, 2},
  54. storagepb.Event{
  55. Type: storagepb.PUT,
  56. Kv: &storagepb.KeyValue{
  57. Key: []byte("foo"),
  58. Value: []byte("bar"),
  59. CreateRevision: 2,
  60. ModRevision: 2,
  61. Version: 2,
  62. },
  63. },
  64. revision{2, 1},
  65. },
  66. {
  67. revision{2, 0},
  68. indexGetResp{revision{2, 1}, revision{2, 0}, 2, nil},
  69. revision{2, 1},
  70. storagepb.Event{
  71. Type: storagepb.PUT,
  72. Kv: &storagepb.KeyValue{
  73. Key: []byte("foo"),
  74. Value: []byte("bar"),
  75. CreateRevision: 2,
  76. ModRevision: 3,
  77. Version: 3,
  78. },
  79. },
  80. revision{3, 0},
  81. },
  82. }
  83. for i, tt := range tests {
  84. s, b, index := newFakeStore()
  85. s.currentRev = tt.rev
  86. index.indexGetRespc <- tt.r
  87. s.put([]byte("foo"), []byte("bar"))
  88. data, err := tt.wev.Marshal()
  89. if err != nil {
  90. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  91. }
  92. wact := []testutil.Action{
  93. {"put", []interface{}{keyBucketName, newTestBytes(tt.wputrev), data}},
  94. }
  95. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  96. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  97. }
  98. wact = []testutil.Action{
  99. {"get", []interface{}{[]byte("foo"), tt.wputrev.main}},
  100. {"put", []interface{}{[]byte("foo"), tt.wputrev}},
  101. }
  102. if g := index.Action(); !reflect.DeepEqual(g, wact) {
  103. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  104. }
  105. if s.currentRev != tt.wrev {
  106. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  107. }
  108. }
  109. }
  110. func TestStoreRange(t *testing.T) {
  111. ev := storagepb.Event{
  112. Type: storagepb.PUT,
  113. Kv: &storagepb.KeyValue{
  114. Key: []byte("foo"),
  115. Value: []byte("bar"),
  116. CreateRevision: 1,
  117. ModRevision: 2,
  118. Version: 1,
  119. },
  120. }
  121. evb, err := ev.Marshal()
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. currev := revision{1, 1}
  126. wrev := int64(2)
  127. tests := []struct {
  128. idxr indexRangeResp
  129. r rangeResp
  130. }{
  131. {
  132. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  133. rangeResp{[][]byte{newTestBytes(revision{2, 0})}, [][]byte{evb}},
  134. },
  135. {
  136. indexRangeResp{[][]byte{[]byte("foo"), []byte("foo1")}, []revision{{2, 0}, {3, 0}}},
  137. rangeResp{[][]byte{newTestBytes(revision{2, 0})}, [][]byte{evb}},
  138. },
  139. }
  140. for i, tt := range tests {
  141. s, b, index := newFakeStore()
  142. s.currentRev = currev
  143. b.tx.rangeRespc <- tt.r
  144. index.indexRangeRespc <- tt.idxr
  145. kvs, rev, err := s.rangeKeys([]byte("foo"), []byte("goo"), 1, 0)
  146. if err != nil {
  147. t.Errorf("#%d: err = %v, want nil", i, err)
  148. }
  149. if w := []storagepb.KeyValue{*ev.Kv}; !reflect.DeepEqual(kvs, w) {
  150. t.Errorf("#%d: kvs = %+v, want %+v", i, kvs, w)
  151. }
  152. if rev != wrev {
  153. t.Errorf("#%d: rev = %d, want %d", i, rev, wrev)
  154. }
  155. wact := []testutil.Action{
  156. {"range", []interface{}{keyBucketName, newTestBytes(tt.idxr.revs[0]), []byte(nil), int64(0)}},
  157. }
  158. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  159. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  160. }
  161. wact = []testutil.Action{
  162. {"range", []interface{}{[]byte("foo"), []byte("goo"), wrev}},
  163. }
  164. if g := index.Action(); !reflect.DeepEqual(g, wact) {
  165. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  166. }
  167. if s.currentRev != currev {
  168. t.Errorf("#%d: current rev = %+v, want %+v", i, s.currentRev, currev)
  169. }
  170. }
  171. }
  172. func TestStoreDeleteRange(t *testing.T) {
  173. tests := []struct {
  174. rev revision
  175. r indexRangeResp
  176. wrev revision
  177. wrrev int64
  178. wdelrev revision
  179. }{
  180. {
  181. revision{2, 0},
  182. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  183. revision{2, 1},
  184. 2,
  185. revision{3, 0},
  186. },
  187. {
  188. revision{2, 1},
  189. indexRangeResp{[][]byte{[]byte("foo")}, []revision{{2, 0}}},
  190. revision{2, 2},
  191. 3,
  192. revision{3, 1},
  193. },
  194. }
  195. for i, tt := range tests {
  196. s, b, index := newFakeStore()
  197. s.currentRev = tt.rev
  198. index.indexRangeRespc <- tt.r
  199. n := s.deleteRange([]byte("foo"), []byte("goo"))
  200. if n != 1 {
  201. t.Errorf("#%d: n = %d, want 1", i, n)
  202. }
  203. data, err := (&storagepb.Event{
  204. Type: storagepb.DELETE,
  205. Kv: &storagepb.KeyValue{
  206. Key: []byte("foo"),
  207. },
  208. }).Marshal()
  209. if err != nil {
  210. t.Errorf("#%d: marshal err = %v, want nil", i, err)
  211. }
  212. wact := []testutil.Action{
  213. {"put", []interface{}{keyBucketName, newTestBytes(tt.wdelrev), data}},
  214. }
  215. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  216. t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact)
  217. }
  218. wact = []testutil.Action{
  219. {"range", []interface{}{[]byte("foo"), []byte("goo"), tt.wrrev}},
  220. {"tombstone", []interface{}{[]byte("foo"), tt.wdelrev}},
  221. }
  222. if g := index.Action(); !reflect.DeepEqual(g, wact) {
  223. t.Errorf("#%d: index action = %+v, want %+v", i, g, wact)
  224. }
  225. if s.currentRev != tt.wrev {
  226. t.Errorf("#%d: rev = %+v, want %+v", i, s.currentRev, tt.wrev)
  227. }
  228. }
  229. }
  230. func TestStoreCompact(t *testing.T) {
  231. s, b, index := newFakeStore()
  232. s.currentRev = revision{3, 0}
  233. index.indexCompactRespc <- map[revision]struct{}{revision{1, 0}: {}}
  234. b.tx.rangeRespc <- rangeResp{[][]byte{newTestBytes(revision{1, 0}), newTestBytes(revision{2, 0})}, nil}
  235. s.Compact(3)
  236. s.wg.Wait()
  237. if s.compactMainRev != 3 {
  238. t.Errorf("compact main rev = %d, want 3", s.compactMainRev)
  239. }
  240. end := make([]byte, 8)
  241. binary.BigEndian.PutUint64(end, uint64(4))
  242. wact := []testutil.Action{
  243. {"put", []interface{}{metaBucketName, scheduledCompactKeyName, newTestBytes(revision{3, 0})}},
  244. {"range", []interface{}{keyBucketName, make([]byte, 17), end, int64(10000)}},
  245. {"delete", []interface{}{keyBucketName, newTestBytes(revision{2, 0})}},
  246. {"put", []interface{}{metaBucketName, finishedCompactKeyName, newTestBytes(revision{3, 0})}},
  247. }
  248. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  249. t.Errorf("tx actions = %+v, want %+v", g, wact)
  250. }
  251. wact = []testutil.Action{
  252. {"compact", []interface{}{int64(3)}},
  253. }
  254. if g := index.Action(); !reflect.DeepEqual(g, wact) {
  255. t.Errorf("index action = %+v, want %+v", g, wact)
  256. }
  257. }
  258. func TestStoreRestore(t *testing.T) {
  259. s, b, index := newFakeStore()
  260. putev := storagepb.Event{
  261. Type: storagepb.PUT,
  262. Kv: &storagepb.KeyValue{
  263. Key: []byte("foo"),
  264. Value: []byte("bar"),
  265. CreateRevision: 3,
  266. ModRevision: 3,
  267. Version: 1,
  268. },
  269. }
  270. putevb, err := putev.Marshal()
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. delev := storagepb.Event{
  275. Type: storagepb.DELETE,
  276. Kv: &storagepb.KeyValue{
  277. Key: []byte("foo"),
  278. },
  279. }
  280. delevb, err := delev.Marshal()
  281. if err != nil {
  282. t.Fatal(err)
  283. }
  284. b.tx.rangeRespc <- rangeResp{[][]byte{finishedCompactKeyName}, [][]byte{newTestBytes(revision{2, 0})}}
  285. b.tx.rangeRespc <- rangeResp{[][]byte{newTestBytes(revision{3, 0}), newTestBytes(revision{4, 0})}, [][]byte{putevb, delevb}}
  286. b.tx.rangeRespc <- rangeResp{[][]byte{scheduledCompactKeyName}, [][]byte{newTestBytes(revision{2, 0})}}
  287. s.Restore()
  288. if s.compactMainRev != 2 {
  289. t.Errorf("compact rev = %d, want 4", s.compactMainRev)
  290. }
  291. wrev := revision{4, 0}
  292. if !reflect.DeepEqual(s.currentRev, wrev) {
  293. t.Errorf("current rev = %v, want %v", s.currentRev, wrev)
  294. }
  295. wact := []testutil.Action{
  296. {"range", []interface{}{metaBucketName, finishedCompactKeyName, []byte(nil), int64(0)}},
  297. {"range", []interface{}{keyBucketName, newTestBytes(revision{}), newTestBytes(revision{math.MaxInt64, math.MaxInt64}), int64(0)}},
  298. {"range", []interface{}{metaBucketName, scheduledCompactKeyName, []byte(nil), int64(0)}},
  299. }
  300. if g := b.tx.Action(); !reflect.DeepEqual(g, wact) {
  301. t.Errorf("tx actions = %+v, want %+v", g, wact)
  302. }
  303. wact = []testutil.Action{
  304. {"restore", []interface{}{[]byte("foo"), revision{3, 0}, revision{3, 0}, int64(1)}},
  305. {"tombstone", []interface{}{[]byte("foo"), revision{4, 0}}},
  306. }
  307. if g := index.Action(); !reflect.DeepEqual(g, wact) {
  308. t.Errorf("index action = %+v, want %+v", g, wact)
  309. }
  310. }
  311. func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
  312. s0 := newStore(tmpPath)
  313. defer os.Remove(tmpPath)
  314. s0.Put([]byte("foo"), []byte("bar"))
  315. s0.Put([]byte("foo"), []byte("bar1"))
  316. s0.Put([]byte("foo"), []byte("bar2"))
  317. // write scheduled compaction, but not do compaction
  318. rbytes := newRevBytes()
  319. revToBytes(revision{main: 2}, rbytes)
  320. tx := s0.b.BatchTx()
  321. tx.Lock()
  322. tx.UnsafePut(metaBucketName, scheduledCompactKeyName, rbytes)
  323. tx.Unlock()
  324. s0.Close()
  325. s1 := newStore(tmpPath)
  326. s1.Restore()
  327. // wait for scheduled compaction to be finished
  328. time.Sleep(100 * time.Millisecond)
  329. if _, _, err := s1.Range([]byte("foo"), nil, 0, 2); err != ErrCompacted {
  330. t.Errorf("range on compacted rev error = %v, want %v", err, ErrCompacted)
  331. }
  332. // check the key in backend is deleted
  333. revbytes := newRevBytes()
  334. // TODO: compact should delete main=2 key too
  335. revToBytes(revision{main: 1}, revbytes)
  336. tx = s1.b.BatchTx()
  337. tx.Lock()
  338. ks, _ := tx.UnsafeRange(keyBucketName, revbytes, nil, 0)
  339. if len(ks) != 0 {
  340. t.Errorf("key for rev %+v still exists, want deleted", bytesToRev(revbytes))
  341. }
  342. tx.Unlock()
  343. }
  344. func BenchmarkStorePut(b *testing.B) {
  345. s := newStore(tmpPath)
  346. defer os.Remove(tmpPath)
  347. // prepare keys
  348. keys := make([][]byte, b.N)
  349. for i := 0; i < b.N; i++ {
  350. keys[i] = make([]byte, 64)
  351. rand.Read(keys[i])
  352. }
  353. b.ResetTimer()
  354. for i := 0; i < b.N; i++ {
  355. s.Put(keys[i], []byte("foo"))
  356. }
  357. }
  358. func newTestBytes(rev revision) []byte {
  359. bytes := newRevBytes()
  360. revToBytes(rev, bytes)
  361. return bytes
  362. }
  363. func newFakeStore() (*store, *fakeBackend, *fakeIndex) {
  364. b := &fakeBackend{&fakeBatchTx{rangeRespc: make(chan rangeResp, 5)}}
  365. index := &fakeIndex{
  366. indexGetRespc: make(chan indexGetResp, 1),
  367. indexRangeRespc: make(chan indexRangeResp, 1),
  368. indexCompactRespc: make(chan map[revision]struct{}, 1),
  369. }
  370. return &store{
  371. b: b,
  372. kvindex: index,
  373. currentRev: revision{},
  374. compactMainRev: -1,
  375. }, b, index
  376. }
  377. type rangeResp struct {
  378. keys [][]byte
  379. vals [][]byte
  380. }
  381. type fakeBatchTx struct {
  382. testutil.Recorder
  383. rangeRespc chan rangeResp
  384. }
  385. func (b *fakeBatchTx) Lock() {}
  386. func (b *fakeBatchTx) Unlock() {}
  387. func (b *fakeBatchTx) UnsafeCreateBucket(name []byte) {}
  388. func (b *fakeBatchTx) UnsafePut(bucketName []byte, key []byte, value []byte) {
  389. b.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{bucketName, key, value}})
  390. }
  391. func (b *fakeBatchTx) UnsafeRange(bucketName []byte, key, endKey []byte, limit int64) (keys [][]byte, vals [][]byte) {
  392. b.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{bucketName, key, endKey, limit}})
  393. r := <-b.rangeRespc
  394. return r.keys, r.vals
  395. }
  396. func (b *fakeBatchTx) UnsafeDelete(bucketName []byte, key []byte) {
  397. b.Recorder.Record(testutil.Action{Name: "delete", Params: []interface{}{bucketName, key}})
  398. }
  399. func (b *fakeBatchTx) Commit() {}
  400. func (b *fakeBatchTx) CommitAndStop() {}
  401. type fakeBackend struct {
  402. tx *fakeBatchTx
  403. }
  404. func (b *fakeBackend) BatchTx() backend.BatchTx { return b.tx }
  405. func (b *fakeBackend) Snapshot(w io.Writer) (n int64, err error) { return 0, errors.New("unsupported") }
  406. func (b *fakeBackend) ForceCommit() {}
  407. func (b *fakeBackend) Close() error { return nil }
  408. type indexGetResp struct {
  409. rev revision
  410. created revision
  411. ver int64
  412. err error
  413. }
  414. type indexRangeResp struct {
  415. keys [][]byte
  416. revs []revision
  417. }
  418. type fakeIndex struct {
  419. testutil.Recorder
  420. indexGetRespc chan indexGetResp
  421. indexRangeRespc chan indexRangeResp
  422. indexCompactRespc chan map[revision]struct{}
  423. }
  424. func (i *fakeIndex) Get(key []byte, atRev int64) (rev, created revision, ver int64, err error) {
  425. i.Recorder.Record(testutil.Action{Name: "get", Params: []interface{}{key, atRev}})
  426. r := <-i.indexGetRespc
  427. return r.rev, r.created, r.ver, r.err
  428. }
  429. func (i *fakeIndex) Range(key, end []byte, atRev int64) ([][]byte, []revision) {
  430. i.Recorder.Record(testutil.Action{Name: "range", Params: []interface{}{key, end, atRev}})
  431. r := <-i.indexRangeRespc
  432. return r.keys, r.revs
  433. }
  434. func (i *fakeIndex) Put(key []byte, rev revision) {
  435. i.Recorder.Record(testutil.Action{Name: "put", Params: []interface{}{key, rev}})
  436. }
  437. func (i *fakeIndex) Restore(key []byte, created, modified revision, ver int64) {
  438. i.Recorder.Record(testutil.Action{Name: "restore", Params: []interface{}{key, created, modified, ver}})
  439. }
  440. func (i *fakeIndex) Tombstone(key []byte, rev revision) error {
  441. i.Recorder.Record(testutil.Action{Name: "tombstone", Params: []interface{}{key, rev}})
  442. return nil
  443. }
  444. func (i *fakeIndex) RangeEvents(key, end []byte, rev int64) []revision {
  445. return nil
  446. }
  447. func (i *fakeIndex) Compact(rev int64) map[revision]struct{} {
  448. i.Recorder.Record(testutil.Action{Name: "compact", Params: []interface{}{rev}})
  449. return <-i.indexCompactRespc
  450. }
  451. func (i *fakeIndex) Equal(b index) bool { return false }