kvstore_test.go 13 KB

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