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