periodic_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 v3compactor
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "github.com/jonboulle/clockwork"
  22. "go.uber.org/zap"
  23. )
  24. func TestPeriodicHourly(t *testing.T) {
  25. retentionHours := 2
  26. retentionDuration := time.Duration(retentionHours) * time.Hour
  27. fc := clockwork.NewFakeClock()
  28. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  29. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  30. tb := newPeriodic(zap.NewExample(), fc, retentionDuration, rg, compactable)
  31. tb.Run()
  32. defer tb.Stop()
  33. initialIntervals, intervalsPerPeriod := tb.getRetentions(), 10
  34. // compaction doesn't happen til 2 hours elapse
  35. for i := 0; i < initialIntervals; i++ {
  36. rg.Wait(1)
  37. fc.Advance(tb.getRetryInterval())
  38. }
  39. // very first compaction
  40. a, err := compactable.Wait(1)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. expectedRevision := int64(1)
  45. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  46. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  47. }
  48. // simulate 3 hours
  49. // now compactor kicks in, every hour
  50. for i := 0; i < 3; i++ {
  51. // advance one hour, one revision for each interval
  52. for j := 0; j < intervalsPerPeriod; j++ {
  53. rg.Wait(1)
  54. fc.Advance(tb.getRetryInterval())
  55. }
  56. a, err = compactable.Wait(1)
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. expectedRevision = int64((i + 1) * 10)
  61. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  62. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  63. }
  64. }
  65. }
  66. func TestPeriodicMinutes(t *testing.T) {
  67. retentionMinutes := 5
  68. retentionDuration := time.Duration(retentionMinutes) * time.Minute
  69. fc := clockwork.NewFakeClock()
  70. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  71. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  72. tb := newPeriodic(zap.NewExample(), fc, retentionDuration, rg, compactable)
  73. tb.Run()
  74. defer tb.Stop()
  75. initialIntervals, intervalsPerPeriod := tb.getRetentions(), 10
  76. // compaction doesn't happen til 5 minutes elapse
  77. for i := 0; i < initialIntervals; i++ {
  78. rg.Wait(1)
  79. fc.Advance(tb.getRetryInterval())
  80. }
  81. // very first compaction
  82. a, err := compactable.Wait(1)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. expectedRevision := int64(1)
  87. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  88. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  89. }
  90. // compaction happens at every interval
  91. for i := 0; i < 5; i++ {
  92. // advance 5-minute, one revision for each interval
  93. for j := 0; j < intervalsPerPeriod; j++ {
  94. rg.Wait(1)
  95. fc.Advance(tb.getRetryInterval())
  96. }
  97. a, err := compactable.Wait(1)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. expectedRevision = int64((i + 1) * 10)
  102. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  103. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  104. }
  105. }
  106. }
  107. func TestPeriodicPause(t *testing.T) {
  108. fc := clockwork.NewFakeClock()
  109. retentionDuration := time.Hour
  110. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  111. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  112. tb := newPeriodic(zap.NewExample(), fc, retentionDuration, rg, compactable)
  113. tb.Run()
  114. tb.Pause()
  115. n := tb.getRetentions()
  116. // tb will collect 3 hours of revisions but not compact since paused
  117. for i := 0; i < n*3; i++ {
  118. rg.Wait(1)
  119. fc.Advance(tb.getRetryInterval())
  120. }
  121. // t.revs = [21 22 23 24 25 26 27 28 29 30]
  122. select {
  123. case a := <-compactable.Chan():
  124. t.Fatalf("unexpected action %v", a)
  125. case <-time.After(10 * time.Millisecond):
  126. }
  127. // tb resumes to being blocked on the clock
  128. tb.Resume()
  129. rg.Wait(1)
  130. // unblock clock, will kick off a compaction at T=3h6m by retry
  131. fc.Advance(tb.getRetryInterval())
  132. // T=3h6m
  133. a, err := compactable.Wait(1)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. // compact the revision from hour 2:06
  138. wreq := &pb.CompactionRequest{Revision: int64(1 + 2*n + 1)}
  139. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  140. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  141. }
  142. }