periodic.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2017 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. "context"
  17. "sync"
  18. "time"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/mvcc"
  21. "github.com/jonboulle/clockwork"
  22. )
  23. // Periodic compacts the log by purging revisions older than
  24. // the configured retention time.
  25. type Periodic struct {
  26. clock clockwork.Clock
  27. period time.Duration
  28. rg RevGetter
  29. c Compactable
  30. revs []int64
  31. ctx context.Context
  32. cancel context.CancelFunc
  33. // mu protects paused
  34. mu sync.RWMutex
  35. paused bool
  36. }
  37. // NewPeriodic creates a new instance of Periodic compactor that purges
  38. // the log older than h Duration.
  39. func NewPeriodic(h time.Duration, rg RevGetter, c Compactable) *Periodic {
  40. return newPeriodic(clockwork.NewRealClock(), h, rg, c)
  41. }
  42. func newPeriodic(clock clockwork.Clock, h time.Duration, rg RevGetter, c Compactable) *Periodic {
  43. t := &Periodic{
  44. clock: clock,
  45. period: h,
  46. rg: rg,
  47. c: c,
  48. revs: make([]int64, 0),
  49. }
  50. t.ctx, t.cancel = context.WithCancel(context.Background())
  51. return t
  52. }
  53. /*
  54. Compaction period 1-hour:
  55. 1. compute compaction period, which is 1-hour
  56. 2. record revisions for every 1/10 of 1-hour (6-minute)
  57. 3. keep recording revisions with no compaction for first 1-hour
  58. 4. do compact with revs[0]
  59. - success? contiue on for-loop and move sliding window; revs = revs[1:]
  60. - failure? update revs, and retry after 1/10 of 1-hour (6-minute)
  61. Compaction period 24-hour:
  62. 1. compute compaction period, which is 1-hour
  63. 2. record revisions for every 1/10 of 1-hour (6-minute)
  64. 3. keep recording revisions with no compaction for first 24-hour
  65. 4. do compact with revs[0]
  66. - success? contiue on for-loop and move sliding window; revs = revs[1:]
  67. - failure? update revs, and retry after 1/10 of 1-hour (6-minute)
  68. Compaction period 59-min:
  69. 1. compute compaction period, which is 59-min
  70. 2. record revisions for every 1/10 of 59-min (5.9-min)
  71. 3. keep recording revisions with no compaction for first 59-min
  72. 4. do compact with revs[0]
  73. - success? contiue on for-loop and move sliding window; revs = revs[1:]
  74. - failure? update revs, and retry after 1/10 of 59-min (5.9-min)
  75. Compaction period 5-sec:
  76. 1. compute compaction period, which is 5-sec
  77. 2. record revisions for every 1/10 of 5-sec (0.5-sec)
  78. 3. keep recording revisions with no compaction for first 5-sec
  79. 4. do compact with revs[0]
  80. - success? contiue on for-loop and move sliding window; revs = revs[1:]
  81. - failure? update revs, and retry after 1/10 of 5-sec (0.5-sec)
  82. */
  83. // Run runs periodic compactor.
  84. func (t *Periodic) Run() {
  85. compactInterval := t.getCompactInterval()
  86. retryInterval := t.getRetryInterval()
  87. retentions := t.getRetentions()
  88. go func() {
  89. lastSuccess := t.clock.Now()
  90. baseInterval := t.period
  91. for {
  92. t.revs = append(t.revs, t.rg.Rev())
  93. if len(t.revs) > retentions {
  94. t.revs = t.revs[1:] // t.revs[0] is always the rev at t.period ago
  95. }
  96. select {
  97. case <-t.ctx.Done():
  98. return
  99. case <-t.clock.After(retryInterval):
  100. t.mu.Lock()
  101. p := t.paused
  102. t.mu.Unlock()
  103. if p {
  104. continue
  105. }
  106. }
  107. if t.clock.Now().Sub(lastSuccess) < baseInterval {
  108. continue
  109. }
  110. // wait up to initial given period
  111. if baseInterval == t.period {
  112. baseInterval = compactInterval
  113. }
  114. rev := t.revs[0]
  115. plog.Noticef("Starting auto-compaction at revision %d (retention: %v)", rev, t.period)
  116. _, err := t.c.Compact(t.ctx, &pb.CompactionRequest{Revision: rev})
  117. if err == nil || err == mvcc.ErrCompacted {
  118. lastSuccess = t.clock.Now()
  119. plog.Noticef("Finished auto-compaction at revision %d", rev)
  120. } else {
  121. plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
  122. plog.Noticef("Retry after %v", retryInterval)
  123. }
  124. }
  125. }()
  126. }
  127. // if given compaction period x is <1-hour, compact every x duration.
  128. // (e.g. --auto-compaction-mode 'periodic' --auto-compaction-retention='10m', then compact every 10-minute)
  129. // if given compaction period x is >1-hour, compact every hour.
  130. // (e.g. --auto-compaction-mode 'periodic' --auto-compaction-retention='2h', then compact every 1-hour)
  131. func (t *Periodic) getCompactInterval() time.Duration {
  132. itv := t.period
  133. if itv > time.Hour {
  134. itv = time.Hour
  135. }
  136. return itv
  137. }
  138. func (t *Periodic) getRetentions() int {
  139. return int(t.period/t.getRetryInterval()) + 1
  140. }
  141. const retryDivisor = 10
  142. func (t *Periodic) getRetryInterval() time.Duration {
  143. itv := t.period
  144. if itv > time.Hour {
  145. itv = time.Hour
  146. }
  147. return itv / retryDivisor
  148. }
  149. // Stop stops periodic compactor.
  150. func (t *Periodic) Stop() {
  151. t.cancel()
  152. }
  153. // Pause pauses periodic compactor.
  154. func (t *Periodic) Pause() {
  155. t.mu.Lock()
  156. defer t.mu.Unlock()
  157. t.paused = true
  158. }
  159. // Resume resumes periodic compactor.
  160. func (t *Periodic) Resume() {
  161. t.mu.Lock()
  162. defer t.mu.Unlock()
  163. t.paused = false
  164. }