periodic.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 v3compactor
  15. import (
  16. "context"
  17. "sync"
  18. "time"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. "go.etcd.io/etcd/mvcc"
  21. "github.com/jonboulle/clockwork"
  22. "go.uber.org/zap"
  23. )
  24. // Periodic compacts the log by purging revisions older than
  25. // the configured retention time.
  26. type Periodic struct {
  27. lg *zap.Logger
  28. clock clockwork.Clock
  29. period time.Duration
  30. rg RevGetter
  31. c Compactable
  32. revs []int64
  33. ctx context.Context
  34. cancel context.CancelFunc
  35. // mu protects paused
  36. mu sync.RWMutex
  37. paused bool
  38. }
  39. // newPeriodic creates a new instance of Periodic compactor that purges
  40. // the log older than h Duration.
  41. func newPeriodic(lg *zap.Logger, clock clockwork.Clock, h time.Duration, rg RevGetter, c Compactable) *Periodic {
  42. pc := &Periodic{
  43. lg: lg,
  44. clock: clock,
  45. period: h,
  46. rg: rg,
  47. c: c,
  48. revs: make([]int64, 0),
  49. }
  50. pc.ctx, pc.cancel = context.WithCancel(context.Background())
  51. return pc
  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 (pc *Periodic) Run() {
  85. compactInterval := pc.getCompactInterval()
  86. retryInterval := pc.getRetryInterval()
  87. retentions := pc.getRetentions()
  88. go func() {
  89. lastSuccess := pc.clock.Now()
  90. baseInterval := pc.period
  91. for {
  92. pc.revs = append(pc.revs, pc.rg.Rev())
  93. if len(pc.revs) > retentions {
  94. pc.revs = pc.revs[1:] // pc.revs[0] is always the rev at pc.period ago
  95. }
  96. select {
  97. case <-pc.ctx.Done():
  98. return
  99. case <-pc.clock.After(retryInterval):
  100. pc.mu.Lock()
  101. p := pc.paused
  102. pc.mu.Unlock()
  103. if p {
  104. continue
  105. }
  106. }
  107. if pc.clock.Now().Sub(lastSuccess) < baseInterval {
  108. continue
  109. }
  110. // wait up to initial given period
  111. if baseInterval == pc.period {
  112. baseInterval = compactInterval
  113. }
  114. rev := pc.revs[0]
  115. if pc.lg != nil {
  116. pc.lg.Info(
  117. "starting auto periodic compaction",
  118. zap.Int64("revision", rev),
  119. zap.Duration("compact-period", pc.period),
  120. )
  121. } else {
  122. plog.Noticef("Starting auto-compaction at revision %d (retention: %v)", rev, pc.period)
  123. }
  124. _, err := pc.c.Compact(pc.ctx, &pb.CompactionRequest{Revision: rev})
  125. if err == nil || err == mvcc.ErrCompacted {
  126. if pc.lg != nil {
  127. pc.lg.Info(
  128. "completed auto periodic compaction",
  129. zap.Int64("revision", rev),
  130. zap.Duration("compact-period", pc.period),
  131. zap.Duration("took", time.Since(lastSuccess)),
  132. )
  133. } else {
  134. plog.Noticef("Finished auto-compaction at revision %d", rev)
  135. }
  136. lastSuccess = pc.clock.Now()
  137. } else {
  138. if pc.lg != nil {
  139. pc.lg.Warn(
  140. "failed auto periodic compaction",
  141. zap.Int64("revision", rev),
  142. zap.Duration("compact-period", pc.period),
  143. zap.Duration("retry-interval", retryInterval),
  144. zap.Error(err),
  145. )
  146. } else {
  147. plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
  148. plog.Noticef("Retry after %v", retryInterval)
  149. }
  150. }
  151. }
  152. }()
  153. }
  154. // if given compaction period x is <1-hour, compact every x duration.
  155. // (e.g. --auto-compaction-mode 'periodic' --auto-compaction-retention='10m', then compact every 10-minute)
  156. // if given compaction period x is >1-hour, compact every hour.
  157. // (e.g. --auto-compaction-mode 'periodic' --auto-compaction-retention='2h', then compact every 1-hour)
  158. func (pc *Periodic) getCompactInterval() time.Duration {
  159. itv := pc.period
  160. if itv > time.Hour {
  161. itv = time.Hour
  162. }
  163. return itv
  164. }
  165. func (pc *Periodic) getRetentions() int {
  166. return int(pc.period/pc.getRetryInterval()) + 1
  167. }
  168. const retryDivisor = 10
  169. func (pc *Periodic) getRetryInterval() time.Duration {
  170. itv := pc.period
  171. if itv > time.Hour {
  172. itv = time.Hour
  173. }
  174. return itv / retryDivisor
  175. }
  176. // Stop stops periodic compactor.
  177. func (pc *Periodic) Stop() {
  178. pc.cancel()
  179. }
  180. // Pause pauses periodic compactor.
  181. func (pc *Periodic) Pause() {
  182. pc.mu.Lock()
  183. pc.paused = true
  184. pc.mu.Unlock()
  185. }
  186. // Resume resumes periodic compactor.
  187. func (pc *Periodic) Resume() {
  188. pc.mu.Lock()
  189. pc.paused = false
  190. pc.mu.Unlock()
  191. }