revision.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // Revision compacts the log by purging revisions older than
  25. // the configured reivison number. Compaction happens every 5 minutes.
  26. type Revision struct {
  27. lg *zap.Logger
  28. clock clockwork.Clock
  29. retention int64
  30. rg RevGetter
  31. c Compactable
  32. ctx context.Context
  33. cancel context.CancelFunc
  34. mu sync.Mutex
  35. paused bool
  36. }
  37. // newRevision creates a new instance of Revisonal compactor that purges
  38. // the log older than retention revisions from the current revision.
  39. func newRevision(lg *zap.Logger, clock clockwork.Clock, retention int64, rg RevGetter, c Compactable) *Revision {
  40. rc := &Revision{
  41. lg: lg,
  42. clock: clock,
  43. retention: retention,
  44. rg: rg,
  45. c: c,
  46. }
  47. rc.ctx, rc.cancel = context.WithCancel(context.Background())
  48. return rc
  49. }
  50. const revInterval = 5 * time.Minute
  51. // Run runs revision-based compactor.
  52. func (rc *Revision) Run() {
  53. prev := int64(0)
  54. go func() {
  55. for {
  56. select {
  57. case <-rc.ctx.Done():
  58. return
  59. case <-rc.clock.After(revInterval):
  60. rc.mu.Lock()
  61. p := rc.paused
  62. rc.mu.Unlock()
  63. if p {
  64. continue
  65. }
  66. }
  67. rev := rc.rg.Rev() - rc.retention
  68. if rev <= 0 || rev == prev {
  69. continue
  70. }
  71. now := time.Now()
  72. if rc.lg != nil {
  73. rc.lg.Info(
  74. "starting auto revision compaction",
  75. zap.Int64("revision", rev),
  76. zap.Int64("revision-compaction-retention", rc.retention),
  77. )
  78. } else {
  79. plog.Noticef("Starting auto-compaction at revision %d (retention: %d revisions)", rev, rc.retention)
  80. }
  81. _, err := rc.c.Compact(rc.ctx, &pb.CompactionRequest{Revision: rev})
  82. if err == nil || err == mvcc.ErrCompacted {
  83. prev = rev
  84. if rc.lg != nil {
  85. rc.lg.Info(
  86. "completed auto revision compaction",
  87. zap.Int64("revision", rev),
  88. zap.Int64("revision-compaction-retention", rc.retention),
  89. zap.Duration("took", time.Since(now)),
  90. )
  91. } else {
  92. plog.Noticef("Finished auto-compaction at revision %d", rev)
  93. }
  94. } else {
  95. if rc.lg != nil {
  96. rc.lg.Warn(
  97. "failed auto revision compaction",
  98. zap.Int64("revision", rev),
  99. zap.Int64("revision-compaction-retention", rc.retention),
  100. zap.Duration("retry-interval", revInterval),
  101. zap.Error(err),
  102. )
  103. } else {
  104. plog.Noticef("Failed auto-compaction at revision %d (%v)", rev, err)
  105. plog.Noticef("Retry after %v", revInterval)
  106. }
  107. }
  108. }
  109. }()
  110. }
  111. // Stop stops revision-based compactor.
  112. func (rc *Revision) Stop() {
  113. rc.cancel()
  114. }
  115. // Pause pauses revision-based compactor.
  116. func (rc *Revision) Pause() {
  117. rc.mu.Lock()
  118. rc.paused = true
  119. rc.mu.Unlock()
  120. }
  121. // Resume resumes revision-based compactor.
  122. func (rc *Revision) Resume() {
  123. rc.mu.Lock()
  124. rc.paused = false
  125. rc.mu.Unlock()
  126. }