periodic_test.go 4.7 KB

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