kvstore_compaction_test.go 3.1 KB

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