compactor.go 3.0 KB

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