revision.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Revision compacts the log by purging revisions older than
  24. // the configured reivison number. Compaction happens every 5 minutes.
  25. type Revision struct {
  26. clock clockwork.Clock
  27. retention int64
  28. rg RevGetter
  29. c Compactable
  30. ctx context.Context
  31. cancel context.CancelFunc
  32. mu sync.Mutex
  33. paused bool
  34. }
  35. // NewRevision creates a new instance of Revisonal compactor that purges
  36. // the log older than retention revisions from the current revision.
  37. func NewRevision(retention int64, rg RevGetter, c Compactable) *Revision {
  38. return newRevision(clockwork.NewRealClock(), retention, rg, c)
  39. }
  40. func newRevision(clock clockwork.Clock, retention int64, rg RevGetter, c Compactable) *Revision {
  41. t := &Revision{
  42. clock: clock,
  43. retention: retention,
  44. rg: rg,
  45. c: c,
  46. }
  47. t.ctx, t.cancel = context.WithCancel(context.Background())
  48. return t
  49. }
  50. const revInterval = 5 * time.Minute
  51. // Run runs revision-based compactor.
  52. func (t *Revision) Run() {
  53. prev := int64(0)
  54. go func() {
  55. for {
  56. select {
  57. case <-t.ctx.Done():
  58. return
  59. case <-t.clock.After(revInterval):
  60. t.mu.Lock()
  61. p := t.paused
  62. t.mu.Unlock()
  63. if p {
  64. continue
  65. }
  66. }
  67. rev := t.rg.Rev() - t.retention
  68. if rev <= 0 || rev == prev {
  69. continue
  70. }
  71. plog.Noticef("Starting auto-compaction at revision %d (retention: %d revisions)", rev, t.retention)
  72. _, err := t.c.Compact(t.ctx, &pb.CompactionRequest{Revision: rev})
  73. if err == nil || err == mvcc.ErrCompacted {
  74. prev = rev
  75. plog.Noticef("Finished auto-compaction at revision %d", rev)
  76. } else {
  77. plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
  78. plog.Noticef("Retry after %v", revInterval)
  79. }
  80. }
  81. }()
  82. }
  83. // Stop stops revision-based compactor.
  84. func (t *Revision) Stop() {
  85. t.cancel()
  86. }
  87. // Pause pauses revision-based compactor.
  88. func (t *Revision) Pause() {
  89. t.mu.Lock()
  90. defer t.mu.Unlock()
  91. t.paused = true
  92. }
  93. // Resume resumes revision-based compactor.
  94. func (t *Revision) Resume() {
  95. t.mu.Lock()
  96. defer t.mu.Unlock()
  97. t.paused = false
  98. }