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. "sync"
  17. "time"
  18. "github.com/jonboulle/clockwork"
  19. "golang.org/x/net/context"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/mvcc"
  22. )
  23. // Periodic compacts the log by purging revisions older than
  24. // the configured retention time. Compaction happens hourly.
  25. type Periodic struct {
  26. clock clockwork.Clock
  27. periodInHour int
  28. rg RevGetter
  29. c Compactable
  30. revs []int64
  31. ctx context.Context
  32. cancel context.CancelFunc
  33. mu sync.Mutex
  34. paused bool
  35. }
  36. // NewPeriodic creates a new instance of Periodic compactor that purges
  37. // the log older than h hours.
  38. func NewPeriodic(h int, rg RevGetter, c Compactable) *Periodic {
  39. return &Periodic{
  40. clock: clockwork.NewRealClock(),
  41. periodInHour: h,
  42. rg: rg,
  43. c: c,
  44. }
  45. }
  46. func (t *Periodic) Run() {
  47. t.ctx, t.cancel = context.WithCancel(context.Background())
  48. t.revs = make([]int64, 0)
  49. clock := t.clock
  50. go func() {
  51. last := clock.Now()
  52. for {
  53. t.revs = append(t.revs, t.rg.Rev())
  54. select {
  55. case <-t.ctx.Done():
  56. return
  57. case <-clock.After(checkCompactionInterval):
  58. t.mu.Lock()
  59. p := t.paused
  60. t.mu.Unlock()
  61. if p {
  62. continue
  63. }
  64. }
  65. if clock.Now().Sub(last) < executeCompactionInterval {
  66. continue
  67. }
  68. rev, remaining := t.getRev(t.periodInHour)
  69. if rev < 0 {
  70. continue
  71. }
  72. plog.Noticef("Starting auto-compaction at revision %d (retention: %d hours)", rev, t.periodInHour)
  73. _, err := t.c.Compact(t.ctx, &pb.CompactionRequest{Revision: rev})
  74. if err == nil || err == mvcc.ErrCompacted {
  75. t.revs = remaining
  76. last = clock.Now()
  77. plog.Noticef("Finished auto-compaction at revision %d", rev)
  78. } else {
  79. plog.Noticef("Failed auto-compaction at revision %d (%v)", err, rev)
  80. plog.Noticef("Retry after %v", checkCompactionInterval)
  81. }
  82. }
  83. }()
  84. }
  85. func (t *Periodic) Stop() {
  86. t.cancel()
  87. }
  88. func (t *Periodic) Pause() {
  89. t.mu.Lock()
  90. defer t.mu.Unlock()
  91. t.paused = true
  92. }
  93. func (t *Periodic) Resume() {
  94. t.mu.Lock()
  95. defer t.mu.Unlock()
  96. t.paused = false
  97. }
  98. func (t *Periodic) getRev(h int) (int64, []int64) {
  99. i := len(t.revs) - int(time.Duration(h)*time.Hour/checkCompactionInterval)
  100. if i < 0 {
  101. return -1, t.revs
  102. }
  103. return t.revs[i], t.revs[i+1:]
  104. }