periodic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/internal/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 &Periodic{
  41. clock: clockwork.NewRealClock(),
  42. period: h,
  43. rg: rg,
  44. c: c,
  45. }
  46. }
  47. // periodDivisor divides Periodic.period in into checkCompactInterval duration
  48. const periodDivisor = 10
  49. func (t *Periodic) Run() {
  50. t.ctx, t.cancel = context.WithCancel(context.Background())
  51. t.revs = make([]int64, 0)
  52. clock := t.clock
  53. checkCompactInterval := t.period / time.Duration(periodDivisor)
  54. go func() {
  55. last := clock.Now()
  56. for {
  57. t.revs = append(t.revs, t.rg.Rev())
  58. select {
  59. case <-t.ctx.Done():
  60. return
  61. case <-clock.After(checkCompactInterval):
  62. t.mu.Lock()
  63. p := t.paused
  64. t.mu.Unlock()
  65. if p {
  66. continue
  67. }
  68. }
  69. if clock.Now().Sub(last) < t.period {
  70. continue
  71. }
  72. rev, remaining := t.getRev()
  73. if rev < 0 {
  74. continue
  75. }
  76. plog.Noticef("Starting auto-compaction at revision %d (retention: %v)", rev, t.period)
  77. _, err := t.c.Compact(t.ctx, &pb.CompactionRequest{Revision: rev})
  78. if err == nil || err == mvcc.ErrCompacted {
  79. t.revs = remaining
  80. plog.Noticef("Finished auto-compaction at revision %d", rev)
  81. } else {
  82. plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
  83. plog.Noticef("Retry after %v", checkCompactInterval)
  84. }
  85. }
  86. }()
  87. }
  88. func (t *Periodic) Stop() {
  89. t.cancel()
  90. }
  91. func (t *Periodic) Pause() {
  92. t.mu.Lock()
  93. defer t.mu.Unlock()
  94. t.paused = true
  95. }
  96. func (t *Periodic) Resume() {
  97. t.mu.Lock()
  98. defer t.mu.Unlock()
  99. t.paused = false
  100. }
  101. func (t *Periodic) getRev() (int64, []int64) {
  102. i := len(t.revs) - periodDivisor
  103. if i < 0 {
  104. return -1, t.revs
  105. }
  106. return t.revs[i], t.revs[i+1:]
  107. }