revision.go 2.5 KB

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