kvstore_compaction_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "os"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/lease"
  21. "github.com/coreos/etcd/mvcc/backend"
  22. )
  23. func TestScheduleCompaction(t *testing.T) {
  24. revs := []revision{{1, 0}, {2, 0}, {3, 0}}
  25. tests := []struct {
  26. rev int64
  27. keep map[revision]struct{}
  28. wrevs []revision
  29. }{
  30. // compact at 1 and discard all history
  31. {
  32. 1,
  33. nil,
  34. revs[1:],
  35. },
  36. // compact at 3 and discard all history
  37. {
  38. 3,
  39. nil,
  40. nil,
  41. },
  42. // compact at 1 and keeps history one step earlier
  43. {
  44. 1,
  45. map[revision]struct{}{
  46. revision{main: 1}: {},
  47. },
  48. revs,
  49. },
  50. // compact at 1 and keeps history two steps earlier
  51. {
  52. 3,
  53. map[revision]struct{}{
  54. revision{main: 2}: {},
  55. revision{main: 3}: {},
  56. },
  57. revs[1:],
  58. },
  59. }
  60. for i, tt := range tests {
  61. b, tmpPath := backend.NewDefaultTmpBackend()
  62. s := NewStore(b, &lease.FakeLessor{}, nil)
  63. tx := s.b.BatchTx()
  64. tx.Lock()
  65. ibytes := newRevBytes()
  66. for _, rev := range revs {
  67. revToBytes(rev, ibytes)
  68. tx.UnsafePut(keyBucketName, ibytes, []byte("bar"))
  69. }
  70. tx.Unlock()
  71. s.scheduleCompaction(tt.rev, tt.keep)
  72. tx.Lock()
  73. for _, rev := range tt.wrevs {
  74. revToBytes(rev, ibytes)
  75. keys, _ := tx.UnsafeRange(keyBucketName, ibytes, nil, 0)
  76. if len(keys) != 1 {
  77. t.Errorf("#%d: range on %v = %d, want 1", i, rev, len(keys))
  78. }
  79. }
  80. _, vals := tx.UnsafeRange(metaBucketName, finishedCompactKeyName, nil, 0)
  81. revToBytes(revision{main: tt.rev}, ibytes)
  82. if w := [][]byte{ibytes}; !reflect.DeepEqual(vals, w) {
  83. t.Errorf("#%d: vals on %v = %+v, want %+v", i, finishedCompactKeyName, vals, w)
  84. }
  85. tx.Unlock()
  86. cleanup(s, b, tmpPath)
  87. }
  88. }
  89. func TestCompactAllAndRestore(t *testing.T) {
  90. b, tmpPath := backend.NewDefaultTmpBackend()
  91. s0 := NewStore(b, &lease.FakeLessor{}, nil)
  92. defer os.Remove(tmpPath)
  93. s0.Put([]byte("foo"), []byte("bar"), lease.NoLease)
  94. s0.Put([]byte("foo"), []byte("bar1"), lease.NoLease)
  95. s0.Put([]byte("foo"), []byte("bar2"), lease.NoLease)
  96. s0.DeleteRange([]byte("foo"), nil)
  97. rev := s0.Rev()
  98. // compact all keys
  99. done, err := s0.Compact(rev)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. select {
  104. case <-done:
  105. case <-time.After(10 * time.Second):
  106. t.Fatal("timeout waiting for compaction to finish")
  107. }
  108. err = s0.Close()
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. s1 := NewStore(b, &lease.FakeLessor{}, nil)
  113. if s1.Rev() != rev {
  114. t.Errorf("rev = %v, want %v", s1.Rev(), rev)
  115. }
  116. _, err = s1.Range([]byte("foo"), nil, RangeOptions{})
  117. if err != nil {
  118. t.Errorf("unexpect range error %v", err)
  119. }
  120. }