kvstore_compaction_test.go 3.2 KB

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