compactor.go 2.8 KB

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